HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
byte_string.hpp
1// Copyright Take Vos 2019.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "required.hpp"
8#include <cstddef>
9#include <string>
10#include <string_view>
11#include <cstring>
12#include <concepts>
13#include <type_traits>
14
15hi_warning_push()
16// C26490: Don't use reinterpret_cast (type.1).
17// Need to call strlen() and friends with a `char *`.
18hi_warning_ignore_msvc(26490)
19
20namespace hi::inline v1 {
21
22using byteptr = std::byte *;
23using cbyteptr = std::byte const *;
24
25class byte_char_traits {
26public:
27 using char_type = std::byte;
28 using int_type = unsigned int;
29 using off_type = std::streamoff;
30 using pos_type = std::fpos<std::mbstate_t>;
31 using state_type = std::mbstate_t;
32
33 static constexpr void assign(std::byte &r, std::byte const &a) noexcept
34 {
35 r = a;
36 }
37
38 static constexpr bool eq(std::byte a, std::byte b) noexcept
39 {
40 return static_cast<uint8_t>(a) == static_cast<uint8_t>(b);
41 }
42
43 static constexpr bool lt(std::byte a, std::byte b) noexcept
44 {
45 return static_cast<uint8_t>(a) < static_cast<uint8_t>(b);
46 }
47
48 static std::byte *assign(std::byte *p, std::size_t count, char_type a) noexcept
49 {
50 return static_cast<std::byte *>(std::memset(p, static_cast<uint8_t>(a), count));
51 }
52
53 static std::byte *move(std::byte *dest, std::byte const *src, std::size_t count) noexcept
54 {
55 return static_cast<std::byte *>(std::memmove(dest, src, count));
56 }
57
58 static std::byte *copy(std::byte *dest, std::byte const *src, std::size_t count) noexcept
59 {
60 return static_cast<std::byte *>(std::memcpy(dest, src, count));
61 }
62
63 static int compare(std::byte const *a, std::byte const *b, std::size_t count) noexcept
64 {
65 return std::memcmp(a, b, count);
66 }
67
68 static std::size_t length(std::byte const *s) noexcept
69 {
70 return std::strlen(reinterpret_cast<char const *>(s));
71 }
72
73 static std::byte const *find(std::byte const *s, std::size_t count, std::byte const &ch) noexcept
74 {
75 return static_cast<std::byte const *>(
76 std::memchr(reinterpret_cast<char const *>(s), static_cast<uint8_t>(ch), count));
77 }
78
79 static constexpr std::byte to_char_type(unsigned int c) noexcept
80 {
81 return static_cast<std::byte>(c);
82 }
83
84 static constexpr unsigned int to_int_type(std::byte c) noexcept
85 {
86 return static_cast<unsigned int>(c);
87 }
88
89 static constexpr bool eq_int_type(unsigned int c1, unsigned int c2) noexcept
90 {
91 return c1 == c2;
92 }
93
94 static constexpr unsigned int eof() noexcept
95 {
96 return 256;
97 }
98
99 static constexpr unsigned int not_eof(unsigned int e) noexcept
100 {
101 return eq_int_type(e, eof()) ? 0 : e;
102 }
103};
104
106using bstring_view = std::basic_string_view<std::byte, byte_char_traits>;
107
108[[nodiscard]] inline bstring to_bstring(std::string_view src) noexcept
109{
110 return bstring{reinterpret_cast<std::byte const *>(src.data()), src.size()};
111}
112
113[[nodiscard]] inline bstring to_bstring(std::integral auto... args) noexcept
114{
115 return bstring{{static_cast<std::byte>(args)...}};
116}
117
118} // namespace hi::inline v1
119
120hi_warning_pop();
This file includes required definitions.
T copy(T... args)
T find(T... args)
T memchr(T... args)
T memcmp(T... args)
T memcpy(T... args)
T memmove(T... args)
T memset(T... args)
T move(T... args)
T size(T... args)
T strlen(T... args)