-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathutils.cpp
More file actions
723 lines (653 loc) · 17.7 KB
/
utils.cpp
File metadata and controls
723 lines (653 loc) · 17.7 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// Headers
#include "utils.h"
#include "compiler.h"
#include "game_message.h"
#include "player.h"
#include <cassert>
#include <cstdint>
#include <cinttypes>
#include <cstdio>
#include <algorithm>
#include <cctype>
#include <istream>
#include <zlib.h>
namespace {
char Lower(char c) {
if (c >= 'A' && c <= 'Z') {
return c + 'a' - 'A';
} else {
return c;
}
}
char Upper(char c) {
if (c >= 'a' && c <= 'z') {
return c + 'A' - 'a';
} else {
return c;
}
};
}
std::string Utils::LowerCase(std::string_view str) {
auto result = std::string(str);
LowerCaseInPlace(result);
return result;
}
std::string& Utils::LowerCaseInPlace(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), Lower);
return str;
}
std::string Utils::UpperCase(std::string_view str) {
auto result = std::string(str);
UpperCaseInPlace(result);
return result;
}
std::string& Utils::UpperCaseInPlace(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), Upper);
return str;
}
int Utils::StrICmp(const char* l, const char* r) {
assert(l != nullptr);
assert(r != nullptr);
while (*l != '\0' && *r != '\0') {
auto d = Lower(*l) - Lower(*r);
if (d != 0) {
return d;
}
++l;
++r;
}
return *l - *r;
}
int Utils::StrICmp(std::string_view l, std::string_view r) {
for (size_t i = 0; i < std::min(l.size(), r.size()); ++i) {
auto d = Lower(l[i]) - Lower(r[i]);
if (d != 0) {
return d;
}
}
return l.size() - r.size();
}
std::u16string Utils::DecodeUTF16(std::string_view str) {
std::u16string result;
for (auto it = str.begin(), str_end = str.end(); it < str_end; ++it) {
uint8_t c1 = static_cast<uint8_t>(*it);
if (c1 < 0x80) {
result.push_back(static_cast<uint16_t>(c1));
}
else if (c1 < 0xC2) {
continue;
}
else if (c1 < 0xE0) {
if (str_end-it < 2)
break;
uint8_t c2 = static_cast<uint8_t>(*(++it));
if ((c2 & 0xC0) != 0x80)
continue;
result.push_back(static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F)));
}
else if (c1 < 0xF0) {
if (str_end-it < 3)
break;
uint8_t c2 = static_cast<uint8_t>(*(++it));
uint8_t c3 = static_cast<uint8_t>(*(++it));
if (c1 == 0xE0) {
if ((c2 & 0xE0) != 0xA0)
continue;
} else if (c1 == 0xED) {
if ((c2 & 0xE0) != 0x80)
continue;
} else {
if ((c2 & 0xC0) != 0x80)
continue;
}
if ((c3 & 0xC0) != 0x80)
continue;
result.push_back(static_cast<uint16_t>(((c1 & 0x0F) << 12)
| ((c2 & 0x3F) << 6)
| (c3 & 0x3F)));
}
else if (c1 < 0xF5) {
if (str_end-it < 4)
break;
uint8_t c2 = static_cast<uint8_t>(*(++it));
uint8_t c3 = static_cast<uint8_t>(*(++it));
uint8_t c4 = static_cast<uint8_t>(*(++it));
if (c1 == 0xF0) {
if (!(0x90 <= c2 && c2 <= 0xBF))
continue;
} else if (c1 == 0xF4) {
if ((c2 & 0xF0) != 0x80)
continue;
} else {
if ((c2 & 0xC0) != 0x80)
continue;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
continue;
if ((((c1 & 7UL) << 18) +
((c2 & 0x3FUL) << 12) +
((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > 0x10FFFF)
continue;
result.push_back(static_cast<uint16_t>(
0xD800
| (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
| ((c2 & 0x0F) << 2)
| ((c3 & 0x30) >> 4)));
result.push_back(static_cast<uint16_t>(
0xDC00
| ((c3 & 0x0F) << 6)
| (c4 & 0x3F)));
}
}
return result;
}
std::u32string Utils::DecodeUTF32(std::string_view str) {
std::u32string result;
for (auto it = str.begin(), str_end = str.end(); it < str_end; ++it) {
uint8_t c1 = static_cast<uint8_t>(*it);
if (c1 < 0x80) {
result.push_back(static_cast<uint32_t>(c1));
}
else if (c1 < 0xC2) {
continue;
}
else if (c1 < 0xE0) {
if (str_end-it < 2)
break;
uint8_t c2 = it[1];
if ((c2 & 0xC0) != 0x80)
continue;
result.push_back(static_cast<uint32_t>(((c1 & 0x1F) << 6)
| (c2 & 0x3F)));
}
else if (c1 < 0xF0) {
if (str_end-it < 3)
break;
uint8_t c2 = static_cast<uint8_t>(*(++it));
uint8_t c3 = static_cast<uint8_t>(*(++it));
if (c1 == 0xE0) {
if ((c2 & 0xE0) != 0xA0)
continue;
} else if (c1 == 0xED) {
if ((c2 & 0xE0) != 0x80)
continue;
} else {
if ((c2 & 0xC0) != 0x80)
continue;
}
if ((c3 & 0xC0) != 0x80)
continue;
result.push_back(static_cast<uint32_t>(((c1 & 0x0F) << 12)
| ((c2 & 0x3F) << 6)
| (c3 & 0x3F)));
}
else if (c1 < 0xF5) {
if (str_end-it < 4)
break;
uint8_t c2 = static_cast<uint8_t>(*(++it));
uint8_t c3 = static_cast<uint8_t>(*(++it));
uint8_t c4 = static_cast<uint8_t>(*(++it));
if (c1 == 0xF0) {
if (!(0x90 <= c2 && c2 <= 0xBF))
continue;
} else if (c1 == 0xF4) {
if ((c2 & 0xF0) != 0x80)
continue;
} else {
if ((c2 & 0xC0) != 0x80)
continue;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
continue;
result.push_back(static_cast<uint32_t>(((c1 & 0x07) << 18)
| ((c2 & 0x3F) << 12)
| ((c3 & 0x3F) << 6)
| (c4 & 0x3F)));
}
}
return result;
}
std::string Utils::EncodeUTF(const std::u16string& str) {
std::string result;
for (auto it = str.begin(), str_end = str.end(); it < str_end; ++it) {
uint16_t wc1 = *it;
if (wc1 < 0x0080) {
result.push_back(static_cast<char>(wc1));
}
else if (wc1 < 0x0800) {
result.push_back(static_cast<char>(0xC0 | (wc1 >> 6)));
result.push_back(static_cast<char>(0x80 | (wc1 & 0x03F)));
}
else if (wc1 < 0xD800) {
result.push_back(static_cast<char>(0xE0 | (wc1 >> 12)));
result.push_back(static_cast<char>(0x80 | ((wc1 & 0x0FC0) >> 6)));
result.push_back(static_cast<char>(0x80 | (wc1 & 0x003F)));
}
else if (wc1 < 0xDC00) {
if (str_end-it < 2)
break;
uint16_t wc2 = *(++it);
if ((wc2 & 0xFC00) != 0xDC00)
continue;
if (((((wc1 & 0x03C0UL) >> 6) + 1) << 16) +
((wc1 & 0x003FUL) << 10) + (wc2 & 0x03FF) > 0x10FFFF)
continue;
uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
result.push_back(static_cast<char>(0xF0 | (z >> 2)));
result.push_back(static_cast<char>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2)));
result.push_back(static_cast<char>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6)));
result.push_back(static_cast<char>(0x80 | (wc2 & 0x003F)));
}
else if (wc1 < 0xE000) {
continue;
}
else {
result.push_back(static_cast<char>(0xE0 | (wc1 >> 12)));
result.push_back(static_cast<char>(0x80 | ((wc1 & 0x0FC0) >> 6)));
result.push_back(static_cast<char>(0x80 | (wc1 & 0x003F)));
}
}
return result;
}
std::string Utils::EncodeUTF(const std::u32string& str) {
std::string result;
for (const char32_t& wc : str) {
if ((wc & 0xFFFFF800) == 0x00D800 || wc > 0x10FFFF)
break;
if (wc < 0x000080) {
result.push_back(static_cast<char>(wc));
}
else if (wc < 0x000800) {
result.push_back(static_cast<char>(0xC0 | (wc >> 6)));
result.push_back(static_cast<char>(0x80 | (wc & 0x03F)));
}
else if (wc < 0x010000) {
result.push_back(static_cast<char>(0xE0 | (wc >> 12)));
result.push_back(static_cast<char>(0x80 | ((wc & 0x0FC0) >> 6)));
result.push_back(static_cast<char>(0x80 | (wc & 0x003F)));
}
else {
result.push_back(static_cast<char>(0xF0 | (wc >> 18)));
result.push_back(static_cast<char>(0x80 | ((wc & 0x03F000) >> 12)));
result.push_back(static_cast<char>(0x80 | ((wc & 0x000FC0) >> 6)));
result.push_back(static_cast<char>(0x80 | (wc & 0x00003F)));
}
}
return result;
}
Utils::UtfNextResult Utils::UTF8Next(const char* iter, const char* const end) {
while (iter != end) {
uint8_t c1 = static_cast<uint8_t>(*iter);
++iter;
if (c1 < 0x80) {
return { iter, static_cast<uint32_t>(c1) };
}
if (c1 < 0xC2) {
continue;
}
if (iter == end) {
break;
}
uint8_t c2 = static_cast<uint8_t>(*iter);
++iter;
if (c1 < 0xE0) {
if ((c2 & 0xC0) != 0x80)
continue;
auto ch = (static_cast<uint32_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F)));
return { iter, ch };
}
if (iter == end) {
break;
}
uint8_t c3 = static_cast<uint8_t>(*iter);
++iter;
if (c1 < 0xF0) {
if (c1 == 0xE0) {
if ((c2 & 0xE0) != 0xA0)
continue;
} else if (c1 == 0xED) {
if ((c2 & 0xE0) != 0x80)
continue;
} else {
if ((c2 & 0xC0) != 0x80)
continue;
}
if ((c3 & 0xC0) != 0x80)
continue;
auto ch = (static_cast<uint32_t>(((c1 & 0x0F) << 12)
| ((c2 & 0x3F) << 6)
| (c3 & 0x3F)));
return { iter, ch };
}
if (iter == end) {
break;
}
uint8_t c4 = static_cast<uint8_t>(*iter);
++iter;
if (c1 < 0xF5) {
if (c1 == 0xF0) {
if (!(0x90 <= c2 && c2 <= 0xBF))
continue;
} else if (c1 == 0xF4) {
if ((c2 & 0xF0) != 0x80)
continue;
} else {
if ((c2 & 0xC0) != 0x80)
continue;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
continue;
auto ch = static_cast<uint32_t>(((c1 & 0x07) << 18)
| ((c2 & 0x3F) << 12)
| ((c3 & 0x3F) << 6)
| (c4 & 0x3F));
return { iter, ch };
}
}
return { iter, 0 };
}
Utils::UtfNextResult Utils::UTF8Skip(const char* iter, const char* end, int skip) {
UtfNextResult ret;
if (skip == 0) {
ret = UTF8Next(iter, end);
return { iter, ret.ch };
}
for (; iter < end && skip > 0; --skip) {
ret = UTF8Next(iter, end);
iter = ret.next;
}
return ret;
}
int Utils::UTF8Length(std::string_view str) {
size_t len = 0;
const char* iter = str.data();
const char* const e = str.data() + str.size();
while (iter < e) {
auto ret = Utils::UTF8Next(iter, e);
iter = ret.next;
++len;
}
return len;
}
Utils::ExFontRet Utils::ExFontNext(const char* iter, const char* end) {
ExFontRet ret;
if (end - iter < 2 || *iter != '$') {
return ret; // Not an ExFont command.
}
// The (0xFFu << 16) is to avoid detection as a control character by the
// font rendering code
// Maniacs Patch Extended Syntax $[x,y] Handling
if (Player::IsPatchManiac() && *(iter + 1) == '[') {
auto pres = Game_Message::ParseParam('$', '$', ++iter, end, Player::escape_char, true, Game_Message::default_max_recursion, true);
if (pres.values.empty()) {
// parse error
return ret;
}
ret.next = pres.next;
if (pres.is_array()) {
// XY Mode: $[x,y]
uint32_t x = pres.values[0];
uint32_t y = pres.values[1];
ret.value = (0xFFu << 16) | (y << 8) | x;
ret.is_valid = true;
return ret;
}
else if (pres.values.size() == 1) {
// Index Mode: $[n] or $[\V[n]]
int icon_index = pres.values[0];
int x = icon_index % 13;
int y = icon_index / 13;
ret.value = (0xFFu << 16) | (static_cast<uint32_t>(y) << 8) | static_cast<uint32_t>(x);
ret.is_valid = true;
return ret;
}
}
// Standard $A-Z Syntax
auto next_ch = *(iter + 1);
bool is_lower = (next_ch >= 'a' && next_ch <= 'z');
bool is_upper = (next_ch >= 'A' && next_ch <= 'Z');
if (is_lower || is_upper) {
ret.next = iter + 2;
char32_t adjusted_glyph = is_lower ? (next_ch - 'a' + 26) : (next_ch - 'A');
int x = adjusted_glyph % 13;
int y = adjusted_glyph / 13;
ret.value = (0xFFu << 16) | (static_cast<uint32_t>(y) << 8) | static_cast<uint32_t>(x);
ret.is_valid = true;
}
return ret;
}
Utils::TextRet Utils::TextNext(const char* iter, const char* end, char32_t escape) {
TextRet ret;
if (EP_UNLIKELY(iter == end)) {
ret.next = iter;
return ret;
}
auto ex_ret = ExFontNext(iter, end);
if (ex_ret) {
ret.next = ex_ret.next;
ret.ch = ex_ret.value;
ret.is_exfont = true;
return ret;
}
auto utf8_ret = UTF8Next(iter, end);
ret.next = utf8_ret.next;
ret.ch = utf8_ret.ch;
if (escape != 0 && ret.ch == escape && ret.next != end) {
auto eret = UTF8Next(ret.next, end);
ret.next = eret.next;
ret.ch = eret.ch;
ret.is_escape = true;
}
ret.is_exfont = false;
return ret;
}
// Please report an issue when you get a compile error here because your toolchain is broken and lacks wchar_t
template<size_t WideSize>
static std::wstring ToWideStringImpl(std::string_view);
#if __SIZEOF_WCHAR_T__ == 4 || __WCHAR_MAX__ > 0x10000
template<> // utf32
std::wstring ToWideStringImpl<4>(std::string_view str) {
const auto tmp = Utils::DecodeUTF32(str);
return std::wstring(tmp.begin(), tmp.end());
}
#else
template<> // utf16
std::wstring ToWideStringImpl<2>(std::string_view str) {
const auto tmp = Utils::DecodeUTF16(str);
return std::wstring(tmp.begin(), tmp.end());
}
#endif
std::wstring Utils::ToWideString(std::string_view str) {
return ToWideStringImpl<sizeof(wchar_t)>(str);
}
template<size_t WideSize>
static std::string FromWideStringImpl(const std::wstring&);
#if __SIZEOF_WCHAR_T__ == 4 || __WCHAR_MAX__ > 0x10000
template<> // utf32
std::string FromWideStringImpl<4>(const std::wstring& str) {
return Utils::EncodeUTF(std::u32string(str.begin(), str.end()));
}
#else
template<> // utf16
std::string FromWideStringImpl<2>(const std::wstring& str) {
return Utils::EncodeUTF(std::u16string(str.begin(), str.end()));
}
#endif
std::string Utils::FromWideString(const std::wstring& str) {
return FromWideStringImpl<sizeof(wchar_t)>(str);
}
int Utils::PositiveModulo(int i, int m) {
return (i % m + m) % m;
}
void Utils::SwapByteOrder(uint16_t& us) {
#ifdef WORDS_BIGENDIAN
us = (us >> 8) |
(us << 8);
#else
(void)us;
#endif
}
void Utils::SwapByteOrder(uint32_t& ui) {
#ifdef WORDS_BIGENDIAN
ui = (ui >> 24) |
((ui<<8) & 0x00FF0000) |
((ui>>8) & 0x0000FF00) |
(ui << 24);
#else
(void)ui;
#endif
}
void Utils::SwapByteOrder(double& d) {
#ifdef WORDS_BIGENDIAN
uint32_t *p = reinterpret_cast<uint32_t *>(&d);
SwapByteOrder(p[0]);
SwapByteOrder(p[1]);
uint32_t tmp = p[0];
p[0] = p[1];
p[1] = tmp;
#else
(void)d;
#endif
}
// based on https://stackoverflow.com/questions/6089231/
bool Utils::ReadLine(std::istream& is, std::string& line_out) {
std::istream::sentry se(is, true);
std::streambuf* sb = is.rdbuf();
if (!is) {
return false;
}
line_out.clear();
for(;;) {
int c = sb->sbumpc();
switch (c) {
case '\n':
return true;
case '\r':
if (sb->sgetc() == '\n') {
sb->sbumpc();
}
return true;
case EOF:
// Also handle the case when the last line has no line ending
if (line_out.empty()) {
is.setstate(std::ios::eofbit);
return false;
}
return true;
default:
line_out += (char)c;
}
}
}
std::vector<std::string> Utils::Tokenize(std::string_view str_to_tokenize, const std::function<bool(char32_t)> predicate) {
std::u32string text = DecodeUTF32(str_to_tokenize);
std::vector<std::string> tokens;
std::u32string cur_token;
for (char32_t& c : text) {
if (predicate(c)) {
tokens.push_back(EncodeUTF(cur_token));
cur_token.clear();
continue;
}
cur_token.push_back(c);
}
tokens.push_back(EncodeUTF(cur_token));
return tokens;
}
std::vector<uint8_t> Utils::ReadStream(std::istream& stream) {
constexpr int buffer_incr = 8192;
std::vector<uint8_t> outbuf;
do {
outbuf.resize(outbuf.size() + buffer_incr);
stream.read(reinterpret_cast<char*>(outbuf.data() + outbuf.size() - buffer_incr), buffer_incr);
} while (stream.gcount() == buffer_incr);
outbuf.resize(outbuf.size() - buffer_incr + stream.gcount());
return outbuf;
}
uint32_t Utils::CRC32(std::istream& stream) {
uLong crc = crc32(0L, Z_NULL, 0);
std::array<uint8_t, 8192> buffer = {};
do {
stream.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
crc = crc32(crc, buffer.data(), stream.gcount());
} while (stream.gcount() == static_cast<std::streamsize>(buffer.size()));
return crc;
}
// via https://stackoverflow.com/q/3418231/
std::string Utils::ReplaceAll(std::string str, const std::string& search, const std::string& replace) {
if (search.empty()) {
return str;
}
size_t start_pos = 0;
while((start_pos = str.find(search, start_pos)) != std::string::npos) {
str.replace(start_pos, search.length(), replace);
start_pos += replace.length(); // Handles case where 'replace' is a substring of 'search'
}
return str;
}
std::string Utils::ReplacePlaceholders(std::string_view text_template, Span<const char> types, Span<const std::string_view> values) {
auto str = std::string(text_template);
size_t index = str.find("%");
while (index != std::string::npos) {
if (index + 1 < str.length()) {
char type = str[index + 1];
if (type != '%') {
auto v_it = values.begin();
for (auto t_it = types.begin();
t_it != types.end() && v_it != values.end();
++t_it, ++v_it) {
if (std::toupper(type) == *t_it) {
str.replace(index, 2, v_it->data(), v_it->size());
index += (*v_it).length() - 2;
break;
}
}
}
}
index = str.find("%", index + 1);
}
return str;
}
std::string_view Utils::TrimWhitespace(std::string_view s) {
size_t left = 0;
for (auto& c: s) {
if (std::isspace(static_cast<int>(c))) {
++left;
} else {
break;
}
}
s.remove_prefix(left);
size_t right = 0;
for (auto it = s.crbegin(); it != s.crend(); ++it) {
if (std::isspace(static_cast<int>(*it))) {
++right;
} else {
break;
}
}
s.remove_suffix(right);
return s;
}
std::string Utils::FormatDate(const std::tm *tm, std::string_view format) {
constexpr int buf_size = 128;
char buffer[buf_size];
auto res = strftime(buffer, buf_size, ToString(format).c_str(), tm);
return std::string(buffer, res);
}