HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
byte_string.hpp
1// Copyright Take Vos 2019, 2021-2022.
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 "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <cstddef>
10#include <string>
11#include <string_view>
12#include <cstring>
13#include <concepts>
14#include <type_traits>
15
16hi_export_module(hikogui.container.byte_string);
17
18hi_warning_push();
19// C26490: Don't use reinterpret_cast (type.1).
20// Need to call strlen() and friends with a `char *`.
21hi_warning_ignore_msvc(26490);
22
23hi_export namespace hi::inline v1 {
24
25using byteptr = std::byte *;
26using cbyteptr = std::byte const *;
27
29public:
30 using char_type = std::byte;
31 using int_type = unsigned int;
35
36 constexpr static void assign(std::byte& r, std::byte const& a) noexcept
37 {
38 r = a;
39 }
40
41 constexpr static bool eq(std::byte a, std::byte b) noexcept
42 {
43 return static_cast<uint8_t>(a) == static_cast<uint8_t>(b);
44 }
45
46 constexpr static bool lt(std::byte a, std::byte b) noexcept
47 {
48 return static_cast<uint8_t>(a) < static_cast<uint8_t>(b);
49 }
50
51 static std::byte *assign(std::byte *p, std::size_t count, char_type a) noexcept
52 {
53 return static_cast<std::byte *>(std::memset(p, static_cast<uint8_t>(a), count));
54 }
55
56 static std::byte *move(std::byte *dest, std::byte const *src, std::size_t count) noexcept
57 {
58 return static_cast<std::byte *>(std::memmove(dest, src, count));
59 }
60
61 static std::byte *copy(std::byte *dest, std::byte const *src, std::size_t count) noexcept
62 {
63 return static_cast<std::byte *>(std::memcpy(dest, src, count));
64 }
65
66 static int compare(std::byte const *a, std::byte const *b, std::size_t count) noexcept
67 {
68 return std::memcmp(a, b, count);
69 }
70
71 static std::size_t length(std::byte const *s) noexcept
72 {
73 return std::strlen(reinterpret_cast<char const *>(s));
74 }
75
76 static std::byte const *find(std::byte const *s, std::size_t count, std::byte const& ch) noexcept
77 {
78 return static_cast<std::byte const *>(std::memchr(reinterpret_cast<char const *>(s), static_cast<uint8_t>(ch), count));
79 }
80
81 constexpr static std::byte to_char_type(unsigned int c) noexcept
82 {
83 return static_cast<std::byte>(c);
84 }
85
86 constexpr static unsigned int to_int_type(std::byte c) noexcept
87 {
88 return static_cast<unsigned int>(c);
89 }
90
91 constexpr static bool eq_int_type(unsigned int c1, unsigned int c2) noexcept
92 {
93 return c1 == c2;
94 }
95
96 constexpr static unsigned int eof() noexcept
97 {
98 return 256;
99 }
100
101 constexpr static unsigned int not_eof(unsigned int e) noexcept
102 {
103 return eq_int_type(e, eof()) ? 0 : e;
104 }
105};
106
108using bstring_view = std::basic_string_view<std::byte, byte_char_traits>;
109
110[[nodiscard]] inline bstring to_bstring(std::string_view src) noexcept
111{
112 return bstring{reinterpret_cast<std::byte const *>(src.data()), src.size()};
113}
114
115[[nodiscard]] inline bstring to_bstring(std::integral auto... args) noexcept
116{
117 return bstring{{static_cast<std::byte>(args)...}};
118}
119
120} // namespace hi::inline v1
121
122template<>
123struct std::hash<hi::bstring> {
124 [[nodiscard]] size_t operator()(hi::bstring const& rhs) const noexcept
125 {
126 auto r = size_t{0};
127 for (auto c : rhs) {
128 r = hi::hash_mix_two(r, std::hash<uint8_t>{}(hi::char_cast<uint8_t>(c)));
129 }
130 return r;
131 }
132};
133
134template<>
135struct std::hash<hi::bstring_view> {
136 [[nodiscard]] size_t operator()(hi::bstring_view const& rhs) const noexcept
137 {
138 auto r = size_t{0};
139 for (auto c : rhs) {
140 r = hi::hash_mix_two(r, std::hash<uint8_t>{}(hi::char_cast<uint8_t>(c)));
141 }
142 return r;
143 }
144};
145
146hi_warning_pop();
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition byte_string.hpp:28
T memchr(T... args)
T memcmp(T... args)
T memcpy(T... args)
T memmove(T... args)
T memset(T... args)
T operator()(T... args)
T size(T... args)
T strlen(T... args)