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