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