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 tt {
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 r = a;
29 }
30
31 static constexpr bool eq(std::byte a, std::byte b) noexcept {
32 return static_cast<uint8_t>(a) == static_cast<uint8_t>(b);
33 }
34
35 static constexpr bool lt(std::byte a, std::byte b) noexcept {
36 return static_cast<uint8_t>(a) < static_cast<uint8_t>(b);
37 }
38
39 static std::byte *assign(std::byte *p, std::size_t count, char_type a) {
40 return reinterpret_cast<std::byte *>(std::memset(p, static_cast<uint8_t>(a), count));
41 }
42
43 static std::byte *move(std::byte *dest, std::byte const *src, size_t count) {
44 return reinterpret_cast<std::byte *>(std::memmove(dest, src, count));
45 }
46
47 static std::byte *copy(std::byte *dest, std::byte const *src, size_t count) {
48 return reinterpret_cast<std::byte *>(std::memcpy(dest, src, count));
49 }
50
51 static int compare(std::byte const *a, std::byte const *b, size_t count) {
52 return std::memcmp(a, b, count);
53 }
54
55 static size_t length(std::byte const *s) {
56 return std::strlen(reinterpret_cast<char const *>(s));
57 }
58
59 static std::byte const *find(std::byte const *s, size_t count, std::byte const &ch) {
60 return reinterpret_cast<std::byte const *>(std::memchr(reinterpret_cast<char const *>(s), static_cast<uint8_t>(ch), count));
61 }
62
63 static constexpr std::byte to_char_type(unsigned int c) noexcept {
64 return static_cast<std::byte>(c);
65 }
66
67 static constexpr unsigned int to_int_type(std::byte c) noexcept {
68 return static_cast<unsigned int>(c);
69 }
70
71 static constexpr bool eq_int_type(unsigned int c1, unsigned int c2) noexcept {
72 return c1 == c2;
73 }
74
75 static constexpr unsigned int eof() noexcept {
76 return 256;
77 }
78
79 static constexpr unsigned int not_eof(unsigned int e) noexcept {
80 return eq_int_type(e, eof()) ? 0 : e;
81 }
82};
83
85using bstring_view = std::basic_string_view<std::byte, byte_char_traits>;
86
87
88[[nodiscard]] inline bstring to_bstring(std::string_view src) noexcept {
89 return bstring{reinterpret_cast<std::byte const *>(src.data()), src.size()};
90}
91
92[[nodiscard]] inline bstring to_bstring(std::integral auto... args) noexcept
93{
94 return bstring{{static_cast<std::byte>(args)...}};
95}
96
97}
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)