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/module.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
26public:
27 using char_type = std::byte;
28 using int_type = unsigned int;
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 *>(std::memchr(reinterpret_cast<char const *>(s), static_cast<uint8_t>(ch), count));
76 }
77
78 static constexpr std::byte to_char_type(unsigned int c) noexcept
79 {
80 return static_cast<std::byte>(c);
81 }
82
83 static constexpr unsigned int to_int_type(std::byte c) noexcept
84 {
85 return static_cast<unsigned int>(c);
86 }
87
88 static constexpr bool eq_int_type(unsigned int c1, unsigned int c2) noexcept
89 {
90 return c1 == c2;
91 }
92
93 static constexpr unsigned int eof() noexcept
94 {
95 return 256;
96 }
97
98 static constexpr unsigned int not_eof(unsigned int e) noexcept
99 {
100 return eq_int_type(e, eof()) ? 0 : e;
101 }
102};
103
105using bstring_view = std::basic_string_view<std::byte, byte_char_traits>;
106
107[[nodiscard]] inline bstring to_bstring(std::string_view src) noexcept
108{
109 return bstring{reinterpret_cast<std::byte const *>(src.data()), src.size()};
110}
111
112[[nodiscard]] inline bstring to_bstring(std::integral auto... args) noexcept
113{
114 return bstring{{static_cast<std::byte>(args)...}};
115}
116
117} // namespace hi::inline v1
118
119template<>
120struct std::hash<hi::bstring> {
121 [[nodiscard]] size_t operator()(hi::bstring const& rhs) const noexcept
122 {
123 auto r = size_t{0};
124 for (auto c : rhs) {
125 r = hi::hash_mix_two(r, std::hash<uint8_t>{}(hi::char_cast<uint8_t>(c)));
126 }
127 return r;
128 }
129};
130
131template<>
132struct std::hash<hi::bstring_view> {
133 [[nodiscard]] size_t operator()(hi::bstring_view const& rhs) const noexcept
134 {
135 auto r = size_t{0};
136 for (auto c : rhs) {
137 r = hi::hash_mix_two(r, std::hash<uint8_t>{}(hi::char_cast<uint8_t>(c)));
138 }
139 return r;
140 }
141};
142
143hi_warning_pop();
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Definition byte_string.hpp:25
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)