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 <cstddef>
8#include <string>
9#include <string_view>
10#include <cstring>
11#include <concepts>
12#include <type_traits>
13
14namespace hi::inline v1 {
15
16using byteptr = std::byte *;
17using cbyteptr = std::byte const *;
18
20public:
21 using char_type = std::byte;
22 using int_type = unsigned int;
26
27 static constexpr void assign(std::byte &r, std::byte const &a) noexcept
28 {
29 r = a;
30 }
31
32 static constexpr bool eq(std::byte a, std::byte b) noexcept
33 {
34 return static_cast<uint8_t>(a) == static_cast<uint8_t>(b);
35 }
36
37 static constexpr bool lt(std::byte a, std::byte b) noexcept
38 {
39 return static_cast<uint8_t>(a) < static_cast<uint8_t>(b);
40 }
41
42 static std::byte *assign(std::byte *p, std::size_t count, char_type a)
43 {
44 return reinterpret_cast<std::byte *>(std::memset(p, static_cast<uint8_t>(a), count));
45 }
46
47 static std::byte *move(std::byte *dest, std::byte const *src, std::size_t count)
48 {
49 return reinterpret_cast<std::byte *>(std::memmove(dest, src, count));
50 }
51
52 static std::byte *copy(std::byte *dest, std::byte const *src, std::size_t count)
53 {
54 return reinterpret_cast<std::byte *>(std::memcpy(dest, src, count));
55 }
56
57 static int compare(std::byte const *a, std::byte const *b, std::size_t count)
58 {
59 return std::memcmp(a, b, count);
60 }
61
62 static std::size_t length(std::byte const *s)
63 {
64 return std::strlen(reinterpret_cast<char const *>(s));
65 }
66
67 static std::byte const *find(std::byte const *s, std::size_t count, std::byte const &ch)
68 {
69 return reinterpret_cast<std::byte const *>(
70 std::memchr(reinterpret_cast<char const *>(s), static_cast<uint8_t>(ch), count));
71 }
72
73 static constexpr std::byte to_char_type(unsigned int c) noexcept
74 {
75 return static_cast<std::byte>(c);
76 }
77
78 static constexpr unsigned int to_int_type(std::byte c) noexcept
79 {
80 return static_cast<unsigned int>(c);
81 }
82
83 static constexpr bool eq_int_type(unsigned int c1, unsigned int c2) noexcept
84 {
85 return c1 == c2;
86 }
87
88 static constexpr unsigned int eof() noexcept
89 {
90 return 256;
91 }
92
93 static constexpr unsigned int not_eof(unsigned int e) noexcept
94 {
95 return eq_int_type(e, eof()) ? 0 : e;
96 }
97};
98
100using bstring_view = std::basic_string_view<std::byte, byte_char_traits>;
101
102[[nodiscard]] inline bstring to_bstring(std::string_view src) noexcept
103{
104 return bstring{reinterpret_cast<std::byte const *>(src.data()), src.size()};
105}
106
107[[nodiscard]] inline bstring to_bstring(std::integral auto... args) noexcept
108{
109 return bstring{{static_cast<std::byte>(args)...}};
110}
111
112} // namespace hi::inline v1
Definition byte_string.hpp:19
T memchr(T... args)
T memcmp(T... args)
T memcpy(T... args)
T memmove(T... args)
T memset(T... args)
T size(T... args)
T strlen(T... args)