HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
agstring.hpp
1// Copyright Take Vos 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 "agrapheme.hpp"
8#include "../unicode/gstring.hpp"
9#include "../utility/module.hpp"
10#include "../strings.hpp"
11#include <vector>
12#include <string>
13
14template<>
15struct std::char_traits<hi::agrapheme> {
16 using char_type = hi::agrapheme;
17 using int_type = hi::agrapheme::value_type;
21 using comparison_category = std::strong_ordering;
22
23 static constexpr void assign(char_type& r, char_type const& a) noexcept
24 {
25 r = a;
26 }
27
28 static constexpr char_type *assign(char_type *p, std::size_t count, char_type a) noexcept
29 {
31
32 for (std::size_t i = 0; i != count; ++i) {
33 p[i] = a;
34 }
35 return p;
36 }
37
38 [[nodiscard]] static constexpr bool eq(char_type a, char_type b) noexcept
39 {
40 return a == b;
41 }
42
43 [[nodiscard]] static constexpr bool lt(char_type a, char_type b) noexcept
44 {
45 return a < b;
46 }
47
48 static constexpr char_type *move(char_type *dst, char_type const *src, std::size_t count) noexcept
49 {
52
53 if (src >= dst) {
54 for (std::size_t i = 0; i != count; ++i) {
55 dst[i] = src[i];
56 }
57 } else {
58 for (std::size_t i = count; i != 0; --i) {
59 dst[i - 1] = src[i - 1];
60 }
61 }
62 return dst;
63 }
64
65 static constexpr char_type *copy(char_type *dst, char_type const *src, std::size_t count) noexcept
66 {
69
70 for (std::size_t i = 0; i != count; ++i) {
71 dst[i] = src[i];
72 }
73 return dst;
74 }
75
76 static constexpr int compare(char_type const *s1, char_type const *s2, std::size_t count) noexcept
77 {
80
81 for (std::size_t i = 0; i != count; ++i) {
82 if (s1[i] != s2[i]) {
83 return s1[i] < s2[i] ? -1 : 1;
84 }
85 }
86 return 0;
87 }
88
89 static constexpr std::size_t length(char_type const *s) noexcept
90 {
92
93 std::size_t i = 0;
94 while (not s[i].empty()) {
95 ++i;
96 }
97 return i;
98 }
99
100 static constexpr char_type const *find(const char_type *p, std::size_t count, const char_type& ch) noexcept
101 {
103
104 for (std::size_t i = 0; i != count; ++i, ++p) {
105 if (*p == ch) {
106 return p;
107 }
108 }
109 return nullptr;
110 }
111
112 static constexpr char_type to_char_type(int_type c) noexcept
113 {
114 char_type r;
115 r._value = c;
116 return r;
117 }
118
119 static constexpr int_type to_int_type(char_type c) noexcept
120 {
121 return c._value;
122 }
123
124 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept
125 {
126 return c1 == c2;
127 }
128
129 static constexpr int_type eof() noexcept
130 {
131 // The empty grapheme has all 21 bit '1'.
132 return 0x1f'ffffULL << 43;
133 }
134
135 static constexpr int_type not_eof(int_type e) noexcept
136 {
137 // Return the replacement-char if e is eof.
138 return e == eof() ? (0xfffdULL << 43) : e;
139 }
140};
141
142namespace hi::inline v1 {
143
144using agstring = std::basic_string<agrapheme>;
145using agstring_view = std::basic_string_view<agrapheme>;
146
147namespace pmr {
148using agstring = std::pmr::basic_string<agrapheme>;
149}
150
151} // namespace hi::inline v1
152
153template<>
154struct std::hash<hi::agstring> {
155 [[nodiscard]] std::size_t operator()(hi::agstring const& rhs) noexcept
156 {
157 auto r = std::hash<std::size_t>{}(rhs.size());
158 for (hilet c : rhs) {
159 r = hi::hash_mix_two(r, std::hash<hi::agrapheme>{}(c));
160 }
161 return r;
162 }
163};
164
165template<>
166struct std::hash<hi::pmr::agstring> {
167 [[nodiscard]] std::size_t operator()(hi::pmr::agstring const& rhs) noexcept
168 {
169 auto r = std::hash<std::size_t>{}(rhs.size());
170 for (hilet c : rhs) {
171 r = hi::hash_mix_two(r, std::hash<hi::agrapheme>{}(c));
172 }
173 return r;
174 }
175};
#define hi_axiom_not_null(expression,...)
Assert if an expression is not nullptr.
Definition assert.hpp:257
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
T assign(T... args)
T eq(T... args)
T compare(T... args)
T copy(T... args)
T count(T... args)
T eof(T... args)
T eq_int_type(T... args)
T find(T... args)
T length(T... args)
T move(T... args)
T not_eof(T... args)
T operator()(T... args)
T to_char_type(T... args)
T to_int_type(T... args)