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
12namespace tt {
13
14using byteptr = std::byte *;
15using cbyteptr = std::byte const *;
16
18public:
19 using char_type = std::byte;
20 using int_type = unsigned int;
24
25 static constexpr void assign(std::byte &r, std::byte const &a) noexcept {
26 r = a;
27 }
28
29 static constexpr bool eq(std::byte a, std::byte b) noexcept {
30 return static_cast<uint8_t>(a) == static_cast<uint8_t>(b);
31 }
32
33 static constexpr bool lt(std::byte a, std::byte b) noexcept {
34 return static_cast<uint8_t>(a) < static_cast<uint8_t>(b);
35 }
36
37 static std::byte *assign(std::byte *p, std::size_t count, char_type a) {
38 return reinterpret_cast<std::byte *>(std::memset(p, static_cast<uint8_t>(a), count));
39 }
40
41 static std::byte *move(std::byte *dest, std::byte const *src, size_t count) {
42 return reinterpret_cast<std::byte *>(std::memmove(dest, src, count));
43 }
44
45 static std::byte *copy(std::byte *dest, std::byte const *src, size_t count) {
46 return reinterpret_cast<std::byte *>(std::memcpy(dest, src, count));
47 }
48
49 static int compare(std::byte const *a, std::byte const *b, size_t count) {
50 return std::memcmp(a, b, count);
51 }
52
53 static size_t length(std::byte const *s) {
54 return std::strlen(reinterpret_cast<char const *>(s));
55 }
56
57 static std::byte const *find(std::byte const *s, size_t count, std::byte const &ch) {
58 return reinterpret_cast<std::byte const *>(std::memchr(reinterpret_cast<char const *>(s), static_cast<uint8_t>(ch), count));
59 }
60
61 static constexpr std::byte to_char_type(unsigned int c) noexcept {
62 return static_cast<std::byte>(c);
63 }
64
65 static constexpr unsigned int to_int_type(std::byte c) noexcept {
66 return static_cast<unsigned int>(c);
67 }
68
69 static constexpr bool eq_int_type(unsigned int c1, unsigned int c2) noexcept {
70 return c1 == c2;
71 }
72
73 static constexpr unsigned int eof() noexcept {
74 return 256;
75 }
76
77 static constexpr unsigned int not_eof(unsigned int e) noexcept {
78 return eq_int_type(e, eof()) ? 0 : e;
79 }
80};
81
83using bstring_view = std::basic_string_view<std::byte, byte_char_traits>;
84
85
86[[nodiscard]] inline bstring to_bstring(std::string src) noexcept {
87 return bstring{reinterpret_cast<std::byte *>(src.data()), src.size()};
88}
89
90}
Definition byte_string.hpp:17
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)