aboutsummaryrefslogtreecommitdiffstats
path: root/util/charset/wide.cpp
blob: ddf898c1a0a2400fa9e79625f0df856844f870f7 (plain) (blame)
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
#include "wide.h"

#include <util/generic/mem_copy.h> 
#include <util/string/strip.h>

namespace {
    //! the constants are not zero-terminated
    const wchar16 LT[] = {'&', 'l', 't', ';'};
    const wchar16 GT[] = {'&', 'g', 't', ';'};
    const wchar16 AMP[] = {'&', 'a', 'm', 'p', ';'};
    const wchar16 BR[] = {'<', 'B', 'R', '>'};
    const wchar16 QUOT[] = {'&', 'q', 'u', 'o', 't', ';'};

    template <bool insertBr>
    inline size_t EscapedLen(wchar16 c) {
        switch (c) {
            case '<':
                return Y_ARRAY_SIZE(LT);
            case '>':
                return Y_ARRAY_SIZE(GT);
            case '&':
                return Y_ARRAY_SIZE(AMP);
            case '\"':
                return Y_ARRAY_SIZE(QUOT);
            default:
                if (insertBr && (c == '\r' || c == '\n'))
                    return Y_ARRAY_SIZE(BR);
                else
                    return 1;
        }
    }
}

void Collapse(TUtf16String& w) {
    CollapseImpl(w, w, 0, IsWhitespace);
}

size_t Collapse(wchar16* s, size_t n) {
    return CollapseImpl(s, n, IsWhitespace);
}

TWtringBuf StripLeft(const TWtringBuf text) noexcept {
    const auto* p = text.data();
    const auto* const pe = text.data() + text.size();

    for (; p != pe && IsWhitespace(*p); ++p) {
    }

    return {p, pe};
}

void StripLeft(TUtf16String& text) {
    const auto stripped = StripLeft(TWtringBuf(text));
    if (stripped.size() == text.size()) {
        return;
    }

    text = stripped;
}

TWtringBuf StripRight(const TWtringBuf text) noexcept {
    if (!text) {
        return {};
    }

    const auto* const pe = text.data() - 1;
    const auto* p = text.data() + text.size() - 1;

    for (; p != pe && IsWhitespace(*p); --p) {
    }

    return {pe + 1, p + 1};
}

void StripRight(TUtf16String& text) {
    const auto stripped = StripRight(TWtringBuf(text));
    if (stripped.size() == text.size()) {
        return;
    }

    text.resize(stripped.size());
}

TWtringBuf Strip(const TWtringBuf text) noexcept {
    return StripRight(StripLeft(text));
}

void Strip(TUtf16String& text) {
    StripLeft(text);
    StripRight(text);
}

template <typename T>
static bool IsReductionOnSymbolsTrue(const TWtringBuf text, T&& f) {
    const auto* p = text.data();
    const auto* const pe = text.data() + text.length();
    while (p != pe) {
        const auto symbol = ReadSymbolAndAdvance(p, pe);
        if (!f(symbol)) {
            return false;
        }
    }

    return true;
}

bool IsLowerWord(const TWtringBuf text) noexcept {
    return IsReductionOnSymbolsTrue(text, [](const wchar32 s) { return IsLower(s); });
}

bool IsUpperWord(const TWtringBuf text) noexcept {
    return IsReductionOnSymbolsTrue(text, [](const wchar32 s) { return IsUpper(s); });
}

bool IsLower(const TWtringBuf text) noexcept {
    return IsReductionOnSymbolsTrue(text, [](const wchar32 s) {
        if (IsAlpha(s)) {
            return IsLower(s);
        }
        return true;
    });
}

bool IsUpper(const TWtringBuf text) noexcept {
    return IsReductionOnSymbolsTrue(text, [](const wchar32 s) {
        if (IsAlpha(s)) {
            return IsUpper(s);
        }
        return true;
    });
}

bool IsTitleWord(const TWtringBuf text) noexcept {
    if (!text) {
        return false;
    }

    const auto* p = text.data();
    const auto* pe = text.data() + text.size();

    const auto firstSymbol = ReadSymbolAndAdvance(p, pe);
    if (firstSymbol != ToTitle(firstSymbol)) {
        return false;
    }

    return IsLowerWord({p, pe});
}

template <bool stopOnFirstModification, typename TCharType, typename F>
static bool ModifySequence(TCharType*& p, const TCharType* const pe, F&& f) {
    while (p != pe) {
        const auto symbol = ReadSymbol(p, pe);
        const auto modified = f(symbol);
        if (symbol != modified) {
            if (stopOnFirstModification) {
                return true;
            }

            WriteSymbol(modified, p); // also moves `p` forward
        } else {
            p = SkipSymbol(p, pe);
        }
    }

    return false;
}

template <bool stopOnFirstModification, typename TCharType, typename F>
static bool ModifySequence(const TCharType*& p, const TCharType* const pe, TCharType*& out, F&& f) {
    while (p != pe) {
        const auto symbol = stopOnFirstModification ? ReadSymbol(p, pe) : ReadSymbolAndAdvance(p, pe);
        const auto modified = f(symbol);

        if (stopOnFirstModification) {
            if (symbol != modified) {
                return true;
            }

            p = SkipSymbol(p, pe);
        }

        WriteSymbol(modified, out);
    }

    return false;
}

template <class TStringType>
static void DetachAndFixPointers(TStringType& text, typename TStringType::value_type*& p, const typename TStringType::value_type*& pe) {
    const auto pos = p - text.data();
    const auto count = pe - p;
    p = text.Detach() + pos;
    pe = p + count;
}

template <class TStringType, typename F>
static bool ModifyStringSymbolwise(TStringType& text, size_t pos, size_t count, F&& f) {
    // TODO(yazevnul): this is done for consistency with `TUtf16String::to_lower` and friends
    // at r2914050, maybe worth replacing them with asserts. Also see the same code in `ToTitle`.
    pos = pos < text.size() ? pos : text.size();
    count = count < text.size() - pos ? count : text.size() - pos;

    // TUtf16String is refcounted and it's `data` method return pointer to the constant memory.
    // To simplify the code we do a `const_cast`, though first write to the memory will be done only
    // after we call `Detach()` and get pointer to a writable piece of memory.
    auto* p = const_cast<typename TStringType::value_type*>(text.data() + pos);
    const auto* pe = text.data() + pos + count;

    if (ModifySequence<true>(p, pe, f)) {
        DetachAndFixPointers(text, p, pe);
        ModifySequence<false>(p, pe, f);
        return true;
    }

    return false;
}

bool ToLower(TUtf16String& text, size_t pos, size_t count) {
    const auto f = [](const wchar32 s) { return ToLower(s); };
    return ModifyStringSymbolwise(text, pos, count, f);
}

bool ToUpper(TUtf16String& text, size_t pos, size_t count) {
    const auto f = [](const wchar32 s) { return ToUpper(s); };
    return ModifyStringSymbolwise(text, pos, count, f);
}

bool ToLower(TUtf32String& text, size_t pos, size_t count) {
    const auto f = [](const wchar32 s) { return ToLower(s); };
    return ModifyStringSymbolwise(text, pos, count, f);
}

bool ToUpper(TUtf32String& text, size_t pos, size_t count) {
    const auto f = [](const wchar32 s) { return ToUpper(s); };
    return ModifyStringSymbolwise(text, pos, count, f);
}

bool ToTitle(TUtf16String& text, size_t pos, size_t count) {
    if (!text) {
        return false;
    }

    pos = pos < text.size() ? pos : text.size();
    count = count < text.size() - pos ? count : text.size() - pos;

    const auto toLower = [](const wchar32 s) { return ToLower(s); };

    auto* p = const_cast<wchar16*>(text.data() + pos);
    const auto* pe = text.data() + pos + count;

    const auto firstSymbol = ReadSymbol(p, pe);
    if (firstSymbol == ToTitle(firstSymbol)) {
        p = SkipSymbol(p, pe);
        if (ModifySequence<true>(p, pe, toLower)) {
            DetachAndFixPointers(text, p, pe);
            ModifySequence<false>(p, pe, toLower);
            return true;
        }
    } else {
        DetachAndFixPointers(text, p, pe);
        WriteSymbol(ToTitle(ReadSymbol(p, pe)), p); // also moves `p` forward
        ModifySequence<false>(p, pe, toLower);
        return true;
    }

    return false;
}

bool ToTitle(TUtf32String& text, size_t pos, size_t count) {
    if (!text) {
        return false;
    }

    pos = pos < text.size() ? pos : text.size();
    count = count < text.size() - pos ? count : text.size() - pos;

    const auto toLower = [](const wchar32 s) { return ToLower(s); };

    auto* p = const_cast<wchar32*>(text.data() + pos);
    const auto* pe = text.data() + pos + count;

    const auto firstSymbol = *p;
    if (firstSymbol == ToTitle(firstSymbol)) {
        p += 1;
        if (ModifySequence<true>(p, pe, toLower)) {
            DetachAndFixPointers(text, p, pe);
            ModifySequence<false>(p, pe, toLower);
            return true;
        }
    } else {
        DetachAndFixPointers(text, p, pe);
        WriteSymbol(ToTitle(ReadSymbol(p, pe)), p); // also moves `p` forward
        ModifySequence<false>(p, pe, toLower);
        return true;
    }

    return false;
}

TUtf16String ToLowerRet(TUtf16String text, size_t pos, size_t count) {
    ToLower(text, pos, count);
    return text;
}

TUtf16String ToUpperRet(TUtf16String text, size_t pos, size_t count) {
    ToUpper(text, pos, count);
    return text;
}

TUtf16String ToTitleRet(TUtf16String text, size_t pos, size_t count) {
    ToTitle(text, pos, count);
    return text;
}

TUtf32String ToLowerRet(TUtf32String text, size_t pos, size_t count) {
    ToLower(text, pos, count);
    return text;
}

TUtf32String ToUpperRet(TUtf32String text, size_t pos, size_t count) {
    ToUpper(text, pos, count);
    return text;
}

TUtf32String ToTitleRet(TUtf32String text, size_t pos, size_t count) {
    ToTitle(text, pos, count);
    return text;
}

bool ToLower(const wchar16* text, size_t length, wchar16* out) noexcept {
    // TODO(yazevnul): get rid of `text == out` case (it is probably used only in lemmer) and then
    // we can declare text and out as `__restrict__`
    Y_ASSERT(text == out || !(out >= text && out < text + length));
    const auto f = [](const wchar32 s) { return ToLower(s); };
    const auto* p = text;
    const auto* const pe = text + length;
    if (ModifySequence<true>(p, pe, out, f)) {
        ModifySequence<false>(p, pe, out, f);
        return true;
    }
    return false;
}

bool ToUpper(const wchar16* text, size_t length, wchar16* out) noexcept {
    Y_ASSERT(text == out || !(out >= text && out < text + length));
    const auto f = [](const wchar32 s) { return ToUpper(s); };
    const auto* p = text;
    const auto* const pe = text + length;
    if (ModifySequence<true>(p, pe, out, f)) {
        ModifySequence<false>(p, pe, out, f);
        return true;
    }
    return false;
}

bool ToTitle(const wchar16* text, size_t length, wchar16* out) noexcept {
    if (!length) {
        return false;
    }

    Y_ASSERT(text == out || !(out >= text && out < text + length));

    const auto* const textEnd = text + length;
    const auto firstSymbol = ReadSymbolAndAdvance(text, textEnd);
    const auto firstSymbolTitle = ToTitle(firstSymbol);

    WriteSymbol(firstSymbolTitle, out);

    return ToLower(text, textEnd - text, out) || firstSymbol != firstSymbolTitle;
}

bool ToLower(wchar16* text, size_t length) noexcept {
    const auto f = [](const wchar32 s) { return ToLower(s); };
    const auto* const textEnd = text + length;
    if (ModifySequence<true>(text, textEnd, f)) {
        ModifySequence<false>(text, textEnd, f);
        return true;
    }
    return false;
}

bool ToUpper(wchar16* text, size_t length) noexcept {
    const auto f = [](const wchar32 s) { return ToUpper(s); };
    const auto* const textEnd = text + length;
    if (ModifySequence<true>(text, textEnd, f)) {
        ModifySequence<false>(text, textEnd, f);
        return true;
    }
    return false;
}

bool ToTitle(wchar16* text, size_t length) noexcept {
    if (!length) {
        return false;
    }

    const auto* textEnd = text + length;
    const auto firstSymbol = ReadSymbol(text, textEnd);
    const auto firstSymbolTitle = ToTitle(firstSymbol);

    // avoid unnacessary writes to the memory
    if (firstSymbol != firstSymbolTitle) {
        WriteSymbol(firstSymbolTitle, text);
    } else {
        text = SkipSymbol(text, textEnd);
    }

    return ToLower(text, textEnd - text) || firstSymbol != firstSymbolTitle;
}

bool ToLower(const wchar32* text, size_t length, wchar32* out) noexcept {
    // TODO(yazevnul): get rid of `text == out` case (it is probably used only in lemmer) and then
    // we can declare text and out as `__restrict__`
    Y_ASSERT(text == out || !(out >= text && out < text + length));
    const auto f = [](const wchar32 s) { return ToLower(s); };
    const auto* p = text;
    const auto* const pe = text + length;
    if (ModifySequence<true>(p, pe, out, f)) {
        ModifySequence<false>(p, pe, out, f);
        return true;
    }
    return false;
}

bool ToUpper(const wchar32* text, size_t length, wchar32* out) noexcept {
    Y_ASSERT(text == out || !(out >= text && out < text + length));
    const auto f = [](const wchar32 s) { return ToUpper(s); };
    const auto* p = text;
    const auto* const pe = text + length;
    if (ModifySequence<true>(p, pe, out, f)) {
        ModifySequence<false>(p, pe, out, f);
        return true;
    }
    return false;
}

bool ToTitle(const wchar32* text, size_t length, wchar32* out) noexcept {
    if (!length) {
        return false;
    }

    Y_ASSERT(text == out || !(out >= text && out < text + length));

    const auto* const textEnd = text + length;
    const auto firstSymbol = ReadSymbolAndAdvance(text, textEnd);
    const auto firstSymbolTitle = ToTitle(firstSymbol);

    WriteSymbol(firstSymbolTitle, out);

    return ToLower(text, textEnd - text, out) || firstSymbol != firstSymbolTitle;
}

bool ToLower(wchar32* text, size_t length) noexcept {
    const auto f = [](const wchar32 s) { return ToLower(s); };
    const auto* const textEnd = text + length;
    if (ModifySequence<true>(text, textEnd, f)) {
        ModifySequence<false>(text, textEnd, f);
        return true;
    }
    return false;
}

bool ToUpper(wchar32* text, size_t length) noexcept {
    const auto f = [](const wchar32 s) { return ToUpper(s); };
    const auto* const textEnd = text + length;
    if (ModifySequence<true>(text, textEnd, f)) {
        ModifySequence<false>(text, textEnd, f);
        return true;
    }
    return false;
}

bool ToTitle(wchar32* text, size_t length) noexcept {
    if (!length) {
        return false;
    }

    const auto* textEnd = text + length;
    const auto firstSymbol = ReadSymbol(text, textEnd);
    const auto firstSymbolTitle = ToTitle(firstSymbol);

    // avoid unnacessary writes to the memory
    if (firstSymbol != firstSymbolTitle) {
        WriteSymbol(firstSymbolTitle, text);
    } else {
        text = SkipSymbol(text, textEnd);
    }

    return ToLower(text, textEnd - text) || firstSymbol != firstSymbolTitle;
}

template <typename F>
static TUtf16String ToSmthRet(const TWtringBuf text, size_t pos, size_t count, F&& f) {
    pos = pos < text.size() ? pos : text.size();
    count = count < text.size() - pos ? count : text.size() - pos;

    auto res = TUtf16String::Uninitialized(text.size());
    auto* const resBegin = res.Detach();

    if (pos) {
        MemCopy(resBegin, text.data(), pos);
    }

    f(text.data() + pos, count, resBegin + pos);

    if (count - pos != text.size()) {
        MemCopy(resBegin + pos + count, text.data() + pos + count, text.size() - pos - count);
    }

    return res;
}

template <typename F>
static TUtf32String ToSmthRet(const TUtf32StringBuf text, size_t pos, size_t count, F&& f) {
    pos = pos < text.size() ? pos : text.size();
    count = count < text.size() - pos ? count : text.size() - pos;

    auto res = TUtf32String::Uninitialized(text.size());
    auto* const resBegin = res.Detach();

    if (pos) {
        MemCopy(resBegin, text.data(), pos);
    }

    f(text.data() + pos, count, resBegin + pos);

    if (count - pos != text.size()) {
        MemCopy(resBegin + pos + count, text.data() + pos + count, text.size() - pos - count);
    }

    return res;
}

TUtf16String ToLowerRet(const TWtringBuf text, size_t pos, size_t count) {
    return ToSmthRet(text, pos, count, [](const wchar16* theText, size_t length, wchar16* out) {
        ToLower(theText, length, out);
    });
}

TUtf16String ToUpperRet(const TWtringBuf text, size_t pos, size_t count) {
    return ToSmthRet(text, pos, count, [](const wchar16* theText, size_t length, wchar16* out) {
        ToUpper(theText, length, out);
    });
}

TUtf16String ToTitleRet(const TWtringBuf text, size_t pos, size_t count) {
    return ToSmthRet(text, pos, count, [](const wchar16* theText, size_t length, wchar16* out) {
        ToTitle(theText, length, out);
    });
}

TUtf32String ToLowerRet(const TUtf32StringBuf text, size_t pos, size_t count) {
    return ToSmthRet(text, pos, count, [](const wchar32* theText, size_t length, wchar32* out) {
        ToLower(theText, length, out);
    });
}

TUtf32String ToUpperRet(const TUtf32StringBuf text, size_t pos, size_t count) {
    return ToSmthRet(text, pos, count, [](const wchar32* theText, size_t length, wchar32* out) {
        ToUpper(theText, length, out);
    });
}

TUtf32String ToTitleRet(const TUtf32StringBuf text, size_t pos, size_t count) {
    return ToSmthRet(text, pos, count, [](const wchar32* theText, size_t length, wchar32* out) {
        ToTitle(theText, length, out);
    });
}

template <bool insertBr>
void EscapeHtmlChars(TUtf16String& str) {
    static const TUtf16String lt(LT, Y_ARRAY_SIZE(LT));
    static const TUtf16String gt(GT, Y_ARRAY_SIZE(GT));
    static const TUtf16String amp(AMP, Y_ARRAY_SIZE(AMP));
    static const TUtf16String br(BR, Y_ARRAY_SIZE(BR));
    static const TUtf16String quot(QUOT, Y_ARRAY_SIZE(QUOT));

    size_t escapedLen = 0;

    const TUtf16String& cs = str;

    for (size_t i = 0; i < cs.size(); ++i)
        escapedLen += EscapedLen<insertBr>(cs[i]);

    if (escapedLen == cs.size())
        return;

    TUtf16String res;
    res.reserve(escapedLen);

    size_t start = 0;

    for (size_t i = 0; i < cs.size(); ++i) {
        const TUtf16String* ent = nullptr;
        switch (cs[i]) {
            case '<':
                ent = &lt;
                break;
            case '>':
                ent = &gt;
                break;
            case '&':
                ent = &amp;
                break;
            case '\"':
                ent = &quot;
                break;
            default:
                if (insertBr && (cs[i] == '\r' || cs[i] == '\n')) {
                    ent = &br;
                    break;
                } else
                    continue;
        }

        res.append(cs.begin() + start, cs.begin() + i);
        res.append(ent->begin(), ent->end());
        start = i + 1;
    }

    res.append(cs.begin() + start, cs.end());
    res.swap(str);
}

template void EscapeHtmlChars<false>(TUtf16String& str);
template void EscapeHtmlChars<true>(TUtf16String& str);