1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#include "cru/base/String.h"
#include "cru/base/StringUtil.h"
#include <catch2/catch_test_macros.hpp>
using cru::Index;
using cru::k_invalid_code_point;
TEST_CASE("StringUtil Split", "[string]") {
using cru::string::Split;
REQUIRE(Split("abc", "b") == std::vector<std::string>{"a", "c"});
REQUIRE(Split("abcd", "bc") == std::vector<std::string>{"a", "d"});
REQUIRE(Split("abcdbcd", "bc") == std::vector<std::string>{"a", "d", "d"});
REQUIRE(Split("aaa", "a") == std::vector<std::string>{"", "", "", ""});
}
TEST_CASE("StringUtil Utf8NextCodePoint", "[string]") {
using cru::Utf8NextCodePoint;
std::string_view text = "aπ你🤣!";
Index current = 0;
REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x0061);
REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x03C0);
REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x4F60);
REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x1F923);
REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x0021);
REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) ==
k_invalid_code_point);
REQUIRE(current == static_cast<Index>(text.size()));
}
TEST_CASE("StringUtil Utf16NextCodePoint", "[string]") {
using cru::Utf16NextCodePoint;
std::u16string_view text = u"aπ你🤣!";
Index current = 0;
REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x0061);
REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x03C0);
REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x4F60);
REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x1F923);
REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) ==
0x0021);
REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) ==
k_invalid_code_point);
REQUIRE(current == static_cast<Index>(text.size()));
}
TEST_CASE("StringUtil Utf16PreviousCodePoint", "[string]") {
using cru::Utf16PreviousCodePoint;
std::u16string_view text = u"aπ你🤣!";
Index current = text.size();
REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) ==
0x0021);
REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) ==
0x1F923);
REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) ==
0x4F60);
REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) ==
0x03C0);
REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) ==
0x0061);
REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) ==
k_invalid_code_point);
REQUIRE(current == 0);
}
TEST_CASE("StringUtil Utf8CodePointIterator", "[string]") {
using cru::Utf8CodePointIterator;
std::string_view text = "aπ你🤣!";
std::vector<cru::CodePoint> code_points;
for (auto cp : Utf8CodePointIterator(text.data(), text.size())) {
code_points.push_back(cp);
}
std::vector<cru::CodePoint> expected_code_points{0x0061, 0x03C0, 0x4F60,
0x1F923, 0x0021};
REQUIRE(code_points == expected_code_points);
}
TEST_CASE("StringUtil Utf16CodePointIterator", "[string]") {
using cru::Utf16CodePointIterator;
std::u16string_view text = u"aπ你🤣!";
std::vector<cru::CodePoint> code_points;
for (auto cp : Utf16CodePointIterator(text.data(), text.size())) {
code_points.push_back(cp);
}
std::vector<cru::CodePoint> expected_code_points{0x0061, 0x03C0, 0x4F60,
0x1F923, 0x0021};
REQUIRE(code_points == expected_code_points);
}
TEST_CASE("ParseToNumber Work", "[string]") {
using namespace cru::string;
auto r1 = ParseToNumber<int>("123");
REQUIRE(r1.valid);
REQUIRE(r1.value == 123);
REQUIRE(r1.processed_char_count == 3);
auto r2 = ParseToNumber<int>("123.123");
REQUIRE(!r2.valid);
auto r3 = ParseToNumber<float>("123.123");
REQUIRE(r3.valid);
REQUIRE(r3.value == 123.123f);
REQUIRE(r3.processed_char_count == 7);
auto r4 = ParseToNumber<float>("a123");
REQUIRE(!r4.valid);
}
TEST_CASE("ParseToNumber AllowLeadingZeroFlag", "[string]") {
using namespace cru::string;
auto r1 = ParseToNumber<int>(" 123");
REQUIRE(!r1.valid);
auto r2 = ParseToNumber<int>(" 123", ParseToNumberFlags::AllowLeadingSpaces);
REQUIRE(r2.valid);
REQUIRE(r2.value == 123);
REQUIRE(r2.processed_char_count == 5);
auto r3 = ParseToNumber<float>(" 123.123");
REQUIRE(!r3.valid);
auto r4 =
ParseToNumber<float>(" 123.123", ParseToNumberFlags::AllowLeadingSpaces);
REQUIRE(r4.valid);
REQUIRE(r4.value == 123.123f);
REQUIRE(r4.processed_char_count == 9);
}
TEST_CASE("StringToIntegerConverterImpl AllowTrailingSpacesFlag", "[string]") {
using namespace cru::string;
auto r1 = ParseToNumber<int>("123 ");
REQUIRE(!r1.valid);
auto r2 =
ParseToNumber<int>("123 ", ParseToNumberFlags::AllowTrailingSpaces);
REQUIRE(r2.valid);
REQUIRE(r2.value == 123);
REQUIRE(r2.processed_char_count == 3);
auto r3 = ParseToNumber<float>("123.123 ");
REQUIRE(!r3.valid);
auto r4 = ParseToNumber<float>("123.123 ",
ParseToNumberFlags::AllowTrailingSpaces);
REQUIRE(r4.valid);
REQUIRE(r4.value == 123.123f);
REQUIRE(r4.processed_char_count == 7);
}
TEST_CASE("StringToIntegerConverterImpl AllowTrailingJunk", "[string]") {
using namespace cru::string;
auto r1 = ParseToNumber<int>("123ab");
REQUIRE(!r1.valid);
auto r2 = ParseToNumber<int>("123ab", ParseToNumberFlags::AllowTrailingJunk);
REQUIRE(r2.valid);
REQUIRE(r2.value == 123);
REQUIRE(r2.processed_char_count == 3);
auto r3 = ParseToNumber<float>("123.123ab");
REQUIRE(!r3.valid);
auto r4 =
ParseToNumber<float>("123.123ab", ParseToNumberFlags::AllowTrailingJunk);
REQUIRE(r4.valid);
REQUIRE(r4.value == 123.123f);
REQUIRE(r4.processed_char_count == 7);
}
TEST_CASE("StringToIntegerConverterImpl CompositeFlags", "[string]") {
using namespace cru::string;
auto r1 =
ParseToNumber<int>(" 123ab", ParseToNumberFlags::AllowLeadingSpaces |
ParseToNumberFlags::AllowTrailingJunk);
REQUIRE(r1.valid);
REQUIRE(r1.value == 123);
REQUIRE(r1.processed_char_count == 5);
auto r2 = ParseToNumber<float>(" 123.123ab",
ParseToNumberFlags::AllowLeadingSpaces |
ParseToNumberFlags::AllowTrailingJunk);
REQUIRE(r2.valid);
REQUIRE(r2.value == 123.123f);
REQUIRE(r2.processed_char_count == 9);
}
TEST_CASE("String ParseToNumberList", "[string]") {
using namespace cru::string;
auto r1 = ParseToNumberList<int>("123 456 789");
REQUIRE(r1 == std::vector<int>{123, 456, 789});
auto r2 = ParseToNumberList<float>("1.1 2.2 3.3");
REQUIRE(r2 == std::vector<float>{1.1f, 2.2f, 3.3f});
}
// TEST(WinString, IndexUtf8ToUtf16) {
// using cru::platform::win::IndexUtf8ToUtf16;
// std::string_view utf8_string = "aπ你🤣!";
// std::wstring_view utf16_string = L"aπ你🤣!";
// REQUIRE(IndexUtf8ToUtf16(utf8_string, 0, utf16_string), 0);
// REQUIRE(IndexUtf8ToUtf16(utf8_string, 1, utf16_string), 1);
// REQUIRE(IndexUtf8ToUtf16(utf8_string, 3, utf16_string), 2);
// REQUIRE(IndexUtf8ToUtf16(utf8_string, 6, utf16_string), 3);
// REQUIRE(IndexUtf8ToUtf16(utf8_string, 10, utf16_string), 5);
// REQUIRE(IndexUtf8ToUtf16(utf8_string, 11, utf16_string), 6);
// }
// TEST(WinString, IndexUtf16ToUtf8) {
// using cru::platform::win::IndexUtf16ToUtf8;
// std::string_view utf8_string = "aπ你🤣!";
// std::wstring_view utf16_string = L"aπ你🤣!";
// REQUIRE(IndexUtf16ToUtf8(utf16_string, 0, utf8_string), 0);
// REQUIRE(IndexUtf16ToUtf8(utf16_string, 1, utf8_string), 1);
// REQUIRE(IndexUtf16ToUtf8(utf16_string, 2, utf8_string), 3);
// REQUIRE(IndexUtf16ToUtf8(utf16_string, 3, utf8_string), 6);
// REQUIRE(IndexUtf16ToUtf8(utf16_string, 5, utf8_string), 10);
// REQUIRE(IndexUtf16ToUtf8(utf16_string, 6, utf8_string), 11);
// }
|