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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
#include "cru/common/String.hpp"
#include "cru/common/Exception.hpp"
#include "cru/common/StringUtil.hpp"
#include <gsl/gsl>
#include <algorithm>
#include <cstring>
#include <functional>
#include <string_view>
namespace cru {
template <typename C>
Index GetStrSize(const C* str) {
Index i = 0;
while (str[i]) {
i++;
}
return i;
}
String String::FromUtf8(const char* str) {
return FromUtf16(cru::ToUtf16(str, GetStrSize(str)));
}
String String::FromUtf8(const char* str, Index size) {
return FromUtf16(cru::ToUtf16(str, size));
}
char16_t String::kEmptyBuffer[1] = {0};
String::String(const_pointer str) : String(str, GetStrSize(str)) {}
String::String(const_pointer str, Index size) {
this->buffer_ = new value_type[size + 1];
std::memcpy(this->buffer_, str, size * sizeof(char16_t));
this->buffer_[size] = 0;
this->size_ = size;
this->capacity_ = size;
}
String::String(std::initializer_list<char16_t> l)
: String(l.begin(), l.size()) {}
#ifdef CRU_PLATFORM_WINDOWS
String::String(const wchar_t* str) : String(str, GetStrSize(str)) {}
String::String(const wchar_t* str, Index size)
: String(reinterpret_cast<const std::uint16_t*>(str), size) {}
#endif
String::String(const String& other) {
if (other.size_ == 0) return;
this->buffer_ = new value_type[other.size_ + 1];
std::memcpy(this->buffer_, other.buffer_, other.size_ * sizeof(value_type));
this->buffer_[other.size_] = 0;
this->size_ = other.size_;
this->capacity_ = other.size_;
}
String::String(String&& other) noexcept {
this->buffer_ = other.buffer_;
this->size_ = other.size_;
this->capacity_ = other.capacity_;
other.buffer_ = kEmptyBuffer;
other.size_ = 0;
other.capacity_ = 0;
}
String& String::operator=(const String& other) {
if (this != &other) {
if (this->buffer_ != kEmptyBuffer) {
delete[] this->buffer_;
}
if (other.buffer_ == kEmptyBuffer) {
this->buffer_ = kEmptyBuffer;
this->size_ = 0;
this->capacity_ = 0;
} else {
this->buffer_ = new value_type[other.size_ + 1];
std::memcpy(this->buffer_, other.buffer_,
other.size_ * sizeof(value_type));
this->buffer_[other.size_] = 0;
this->size_ = other.size_;
this->capacity_ = other.size_;
}
}
return *this;
}
String& String::operator=(String&& other) noexcept {
if (this != &other) {
if (this->buffer_ != kEmptyBuffer) {
delete[] this->buffer_;
}
this->buffer_ = other.buffer_;
this->size_ = other.size_;
this->capacity_ = other.capacity_;
other.buffer_ = kEmptyBuffer;
other.size_ = 0;
other.capacity_ = 0;
}
return *this;
}
String::~String() {
if (this->buffer_ != kEmptyBuffer) {
delete[] this->buffer_;
}
}
String::String(from_buffer_tag, pointer buffer, Index size, Index capacity)
: buffer_(buffer), size_(size), capacity_(capacity) {}
void String::clear() { resize(0); }
void String::resize(Index new_size) {
Expects(new_size >= 0);
if (new_size == size_) return;
if (new_size < size_) {
size_ = new_size;
buffer_[size_] = 0;
} else {
reserve(new_size);
std::memset(buffer_ + size_, 0, sizeof(value_type) * (new_size - size_));
buffer_[new_size] = 0;
size_ = new_size;
}
}
void String::shrink_to_fit() {
if (capacity_ == size_) return;
if (size_ == 0) {
delete[] buffer_;
buffer_ = kEmptyBuffer;
size_ = 0;
capacity_ = 0;
} else {
auto new_buffer = new value_type[size_ + 1];
std::memcpy(new_buffer, buffer_, sizeof(value_type) * size_);
delete[] buffer_;
buffer_ = new_buffer;
capacity_ = size_;
}
}
void String::reserve(Index new_capacity) {
Expects(new_capacity >= 0);
if (new_capacity <= this->capacity_) return;
if (new_capacity > 0) {
pointer new_buffer = new value_type[new_capacity + 1];
if (this->buffer_ != kEmptyBuffer) {
memcpy(new_buffer, this->buffer_, this->size_ * sizeof(value_type));
delete[] this->buffer_;
}
new_buffer[this->size_] = 0;
this->buffer_ = new_buffer;
this->capacity_ = new_capacity;
}
}
String::iterator String::insert(const_iterator pos, const_iterator str,
Index size) {
Expects(pos >= cbegin() && pos <= cend());
std::vector<value_type> backup_buffer;
if (str >= buffer_ && str < buffer_ + size_) {
backup_buffer.resize(size);
std::copy(str, str + size, backup_buffer.begin());
str = backup_buffer.data();
}
Index index = pos - cbegin();
Index new_size = size_ + size;
if (new_size > capacity_) {
auto new_capacity = capacity_;
if (new_capacity == 0) {
new_capacity = new_size;
} else {
while (new_capacity < new_size) {
new_capacity *= 2;
}
}
this->reserve(new_capacity);
}
std::memmove(begin() + index + size, begin() + index,
(size_ - index) * sizeof(value_type));
std::memcpy(begin() + index, str, size * sizeof(value_type));
buffer_[new_size] = 0;
size_ = new_size;
return begin() + new_size;
}
String::iterator String::erase(const_iterator start, const_iterator end) {
Expects(buffer_ <= start && start <= end && end <= buffer_ + size_);
Index new_size = size_ - (end - start);
auto s = const_cast<iterator>(start);
auto e = const_cast<iterator>(end);
std::memmove(s, e, (cend() - end) * sizeof(value_type));
this->size_ = new_size;
this->buffer_[new_size] = 0;
return s;
}
Index String::Find(value_type value, Index start) const {
Expects(start >= 0 && start <= size_);
for (Index i = start; i < size_; ++i) {
if (buffer_[i] == value) return i;
}
return -1;
}
String& String::TrimStart() {
if (size_ == 0) return *this;
auto start = begin();
while (start != end() && IsWhitespace(*start)) {
++start;
}
if (start == end()) {
clear();
} else {
erase(begin(), start);
}
return *this;
}
String& String::TrimEnd() {
if (size_ == 0) return *this;
while (size_ > 0 && IsWhitespace(buffer_[size_ - 1])) {
size_--;
}
return *this;
}
String& String::Trim() {
TrimStart();
TrimEnd();
return *this;
}
std::vector<String> String::SplitToLines(bool remove_space_line) const {
std::vector<String> result;
if (size_ == 0) return result;
Index line_start = 0;
Index line_end = 0;
while (line_end < size_) {
if (buffer_[line_end] == '\n') {
if (remove_space_line) {
bool add = false;
for (Index i = line_start; i < line_end; i++) {
if (!IsWhitespace(buffer_[i])) {
add = true;
break;
}
}
if (add) result.emplace_back(begin() + line_start, begin() + line_end);
} else {
result.emplace_back(begin() + line_start, begin() + line_end);
}
line_start = line_end + 1;
line_end = line_start;
} else {
line_end++;
}
}
if (remove_space_line) {
bool add = false;
for (Index i = line_start; i < size_; i++) {
if (!IsWhitespace(buffer_[i])) {
add = true;
break;
}
}
if (add) result.emplace_back(begin() + line_start, begin() + size_);
} else {
result.emplace_back(begin() + line_start, begin() + size_);
}
return result;
}
bool String::StartWith(StringView str) const {
if (str.size() > size_) return false;
return std::memcmp(str.data(), buffer_, str.size()) == 0;
}
bool String::EndWith(StringView str) const {
if (str.size() > size_) return false;
return std::memcmp(str.data(), buffer_ + size_ - str.size(), str.size()) == 0;
}
std::string String::ToUtf8() const { return cru::ToUtf8(buffer_, size_); }
void String::AppendCodePoint(CodePoint code_point) {
if (!Utf16EncodeCodePointAppendWithFunc(
code_point, [this](char16_t c) { this->push_back(c); })) {
throw TextEncodeException(u"Code point out of range.");
}
}
Index String::IndexFromCodeUnitToCodePoint(Index code_unit_index) const {
auto iter = CodePointIterator();
Index result = 0;
while (iter.GetPosition() < code_unit_index && !iter.IsPastEnd()) {
++iter;
++result;
}
return result;
}
Index String::IndexFromCodePointToCodeUnit(Index code_point_index) const {
auto iter = CodePointIterator();
Index cpi = 0;
while (cpi < code_point_index && !iter.IsPastEnd()) {
++iter;
++cpi;
}
return iter.GetPosition();
}
Range String::RangeFromCodeUnitToCodePoint(Range code_unit_range) const {
return Range::FromTwoSides(
IndexFromCodeUnitToCodePoint(code_unit_range.GetStart()),
IndexFromCodeUnitToCodePoint(code_unit_range.GetEnd()));
}
Range String::RangeFromCodePointToCodeUnit(Range code_point_range) const {
return Range::FromTwoSides(
IndexFromCodePointToCodeUnit(code_point_range.GetStart()),
IndexFromCodePointToCodeUnit(code_point_range.GetEnd()));
}
String& String::operator+=(StringView other) {
append(other);
return *this;
}
namespace {
inline int Compare(char16_t left, char16_t right) {
if (left < right) return -1;
if (left > right) return 1;
return 0;
}
} // namespace
int String::Compare(const String& other) const {
const_iterator i1 = cbegin();
const_iterator i2 = other.cbegin();
const_iterator end1 = cend();
const_iterator end2 = other.cend();
while (i1 != end1 && i2 != end2) {
int r = cru::Compare(*i1, *i2);
if (r != 0) return r;
i1++;
i2++;
}
if (i1 == end1) {
if (i2 == end2) {
return 0;
} else {
return -1;
}
} else {
return 1;
}
}
namespace details {
std::vector<FormatToken> ParseToFormatTokenList(const String& str) {
std::vector<FormatToken> result;
auto push_char = [&result](char16_t c) {
if (result.empty() || result.back().type == FormatTokenType::PlaceHolder) {
result.push_back(FormatToken{FormatTokenType::Text, String{}});
}
result.back().data.append(c);
};
bool last_is_left_bracket = false;
for (auto c : str) {
if (c == u'{') {
if (last_is_left_bracket) {
push_char(u'{');
last_is_left_bracket = false;
} else {
last_is_left_bracket = true;
}
} else if (c == u'}') {
if (last_is_left_bracket) {
result.push_back(FormatToken{FormatTokenType::PlaceHolder, String{}});
}
last_is_left_bracket = false;
} else {
if (last_is_left_bracket) {
push_char(u'{');
}
push_char(c);
last_is_left_bracket = false;
}
}
return result;
}
void FormatAppendFromFormatTokenList(
String& current, const std::vector<FormatToken>& format_token_list,
Index index) {
for (Index i = index; i < format_token_list.size(); i++) {
const auto& token = format_token_list[i];
if (token.type == FormatTokenType::PlaceHolder) {
current += u"{}";
} else {
current += token.data;
}
}
}
} // namespace details
int StringView::Compare(const StringView& other) const {
const_iterator i1 = cbegin();
const_iterator i2 = other.cbegin();
const_iterator end1 = cend();
const_iterator end2 = other.cend();
while (i1 != end1 && i2 != end2) {
int r = cru::Compare(*i1, *i2);
if (r != 0) return r;
i1++;
i2++;
}
if (i1 == end1) {
if (i2 == end2) {
return 0;
} else {
return -1;
}
} else {
return 1;
}
}
StringView StringView::substr(Index pos) {
Expects(pos >= 0 && pos < size_);
return StringView(ptr_ + pos, size_ - pos);
}
StringView StringView::substr(Index pos, Index size) {
Expects(pos >= 0 && pos < size_);
return StringView(ptr_ + pos, std::min(size, size_ - pos));
}
std::string StringView::ToUtf8() const { return cru::ToUtf8(ptr_, size_); }
String ToLower(StringView s) {
String result;
for (auto c : s) result.push_back(ToLower(c));
return result;
}
String ToUpper(StringView s) {
String result;
for (auto c : s) result.push_back(ToUpper(c));
return result;
}
} // namespace cru
|