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
|
/*
* libfud
* Copyright 2024 Dominick Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FUD_UTF8_HPP
#define FUD_UTF8_HPP
#include "fud_array.hpp"
#include "fud_c_string.hpp"
#include "fud_option.hpp"
#include "fud_unique_array.hpp"
#include <cstdint>
#include <type_traits>
namespace fud {
using utf8 = unsigned char;
class String;
struct StringView;
constexpr utf8 ASCII_MASK = 0x7F;
constexpr utf8 UTF8_MB_PATTERN_MASK = 0xC0;
constexpr utf8 UTF8_MB_PATTERN = 0x80;
constexpr utf8 UTF8_MB_MASK = static_cast<utf8>(~UTF8_MB_PATTERN_MASK);
constexpr utf8 UTF8_2B_PATTERN_MASK = 0xE0;
constexpr utf8 UTF8_2B_PATTERN = 0xC0;
constexpr utf8 UTF8_2B_MASK = static_cast<utf8>(~UTF8_2B_PATTERN_MASK);
constexpr utf8 UTF8_3B_PATTERN_MASK = 0xF0;
constexpr utf8 UTF8_3B_PATTERN = 0xE0;
constexpr utf8 UTF8_3B_MASK = static_cast<utf8>(~UTF8_3B_PATTERN_MASK);
constexpr utf8 UTF8_4B_PATTERN_MASK = 0xF8;
constexpr utf8 UTF8_4B_PATTERN = 0xF0;
constexpr utf8 UTF8_4B_MASK = static_cast<utf8>(~UTF8_4B_PATTERN_MASK);
namespace privateImpl {
constexpr bool validUtf8MB(utf8 code) noexcept
{
return (code & UTF8_MB_PATTERN_MASK) == UTF8_MB_PATTERN;
}
} // namespace privateImpl
struct Ascii {
Array<utf8, 1> characters;
constexpr Ascii() noexcept = default; // cppcheck-suppress uninitMemberVar
constexpr explicit Ascii(utf8 chr) noexcept : characters{{chr}}
{
}
[[nodiscard]] constexpr utf8 character() const noexcept
{
return characters[0];
}
[[nodiscard]] constexpr char asChar() const noexcept
{
return static_cast<char>(characters[0]);
}
static constexpr size_t size() noexcept
{
return 1;
}
[[nodiscard]] constexpr bool valid() const noexcept
{
return valid(characters[0]);
}
static constexpr bool valid(utf8 character) noexcept
{
return static_cast<utf8>(character & ~ASCII_MASK) == 0;
}
auto operator<=>(const Ascii& other) const noexcept = default;
};
static_assert(std::is_trivial_v<Ascii>);
static_assert(std::is_standard_layout_v<Ascii>);
/*
| B | E | Byte 1 | Byte 2 | Byte 3 | Byte 4
| U+0000 | U+007F | 0xxxxxxx | | |
| U+0080 | U+07FF | 110xxxxx | 10xxxxxx | |
| U+0800 | U+FFFF | 1110xxxx | 10xxxxxx | 10xxxxxx |
| U+10000 | U+10FFFF | 11110xxx | 10xxxxxx | 10xxxxxx | 10xxxxxx
*/
struct Utf82Byte {
constexpr Utf82Byte(utf8 first, utf8 second) noexcept : characters{{first, second}}
{
}
__attribute__((nonnull)) constexpr Utf82Byte(const char* letterStr) noexcept : characters{}
{
auto length = cStringLength(letterStr, 2);
if (length < 2) {
return;
}
characters[0] = static_cast<utf8>(letterStr[0]);
characters[1] = static_cast<utf8>(letterStr[1]);
}
Array<utf8, 2> characters;
static constexpr size_t size() noexcept
{
return 2;
}
[[nodiscard]] constexpr bool valid() const noexcept
{
return valid(first(), second());
}
static constexpr bool valid(utf8 first, utf8 second) noexcept
{
using privateImpl::validUtf8MB;
return ((first & UTF8_2B_PATTERN_MASK) == UTF8_2B_PATTERN) && validUtf8MB(second);
}
[[nodiscard]] constexpr utf8 first() const noexcept
{
return characters[0];
}
[[nodiscard]] constexpr utf8 second() const noexcept
{
return characters[1];
}
auto operator<=>(const Utf82Byte& other) const noexcept = default;
};
struct Utf83Byte {
constexpr Utf83Byte(utf8 first, utf8 second, utf8 third) noexcept : characters{{first, second, third}}
{
}
__attribute__((nonnull)) constexpr Utf83Byte(const char* letterStr) noexcept : characters{}
{
auto length = cStringLength(letterStr, 3);
if (length < 3) {
return;
}
characters[0] = static_cast<utf8>(letterStr[0]);
characters[1] = static_cast<utf8>(letterStr[1]);
characters[2] = static_cast<utf8>(letterStr[2]);
}
Array<utf8, 3> characters;
static constexpr size_t size() noexcept
{
return 3;
}
[[nodiscard]] constexpr bool valid() const noexcept
{
return valid(first(), second(), third());
}
static constexpr bool valid(utf8 first, utf8 second, utf8 third) noexcept
{
using privateImpl::validUtf8MB;
return ((first & UTF8_3B_PATTERN_MASK) == UTF8_3B_PATTERN) && validUtf8MB(second) && validUtf8MB(third);
}
[[nodiscard]] constexpr utf8 first() const noexcept
{
return characters[0];
}
[[nodiscard]] constexpr utf8 second() const noexcept
{
return characters[1];
}
[[nodiscard]] constexpr utf8 third() const noexcept
{
return characters[2];
}
auto operator<=>(const Utf83Byte& other) const noexcept = default;
};
struct Utf84Byte {
constexpr Utf84Byte(utf8 first, utf8 second, utf8 third, utf8 fourth) noexcept :
characters{{first, second, third, fourth}}
{
}
__attribute__((nonnull)) constexpr Utf84Byte(const char* letterStr) noexcept : characters{}
{
auto length = cStringLength(letterStr, 4);
if (length < 4) {
return;
}
characters[0] = static_cast<utf8>(letterStr[0]);
characters[1] = static_cast<utf8>(letterStr[1]);
characters[2] = static_cast<utf8>(letterStr[2]);
characters[3] = static_cast<utf8>(letterStr[3]);
}
Array<utf8, 4> characters;
static constexpr size_t size() noexcept
{
return 4;
}
[[nodiscard]] constexpr bool valid() const noexcept
{
return valid(first(), second(), third(), fourth());
}
static constexpr bool valid(utf8 first, utf8 second, utf8 third, utf8 fourth) noexcept
{
using privateImpl::validUtf8MB;
if ((first & UTF8_4B_PATTERN_MASK) != UTF8_4B_PATTERN) {
return false;
}
return validUtf8MB(second) && validUtf8MB(third) && validUtf8MB(fourth);
}
[[nodiscard]] constexpr utf8 first() const noexcept
{
return characters[0];
}
[[nodiscard]] constexpr utf8 second() const noexcept
{
return characters[1];
}
[[nodiscard]] constexpr utf8 third() const noexcept
{
return characters[2];
}
[[nodiscard]] constexpr utf8 fourth() const noexcept
{
return characters[3];
}
auto operator<=>(const Utf84Byte& other) const noexcept = default;
};
using Utf8Variant = std::variant<Ascii, Utf82Byte, Utf83Byte, Utf84Byte>;
constexpr auto Utf8TypeSet{UniqueArray<size_t, 0, 1, 2, 3>{}};
enum class Utf8Type : uint8_t
{
Ascii,
Utf82Byte,
Utf83Byte,
Utf84Byte,
};
static_assert(Utf8TypeSet.m_values[0] == static_cast<uint8_t>(Utf8Type::Ascii));
static_assert(Utf8TypeSet.m_values[1] == static_cast<uint8_t>(Utf8Type::Utf82Byte));
static_assert(Utf8TypeSet.m_values[2] == static_cast<uint8_t>(Utf8Type::Utf83Byte));
static_assert(Utf8TypeSet.m_values[3] == static_cast<uint8_t>(Utf8Type::Utf84Byte));
struct FudUtf8 {
Utf8Variant m_variant{Utf8Variant{Ascii{}}};
static constexpr Ascii invalidAsciiCode{Ascii{0xFF}};
static FudUtf8 from(const String& fudString, size_t index) noexcept;
static FudUtf8 from(StringView view, size_t index) noexcept;
static constexpr FudUtf8 make(const Array<utf8, 4>& data)
{
FudUtf8 unicode{};
if (Ascii::valid(data[0])) {
unicode.m_variant = Ascii{data[0]};
} else if (Utf82Byte::valid(data[0], data[1])) {
unicode.m_variant = Utf82Byte{data[0], data[1]};
} else if (Utf83Byte::valid(data[0], data[1], data[2])) {
unicode.m_variant = Utf83Byte{data[0], data[1], data[2]};
} else if (Utf84Byte::valid(data[0], data[1], data[2], data[3])) {
unicode.m_variant = Utf84Byte{data[0], data[1], data[2], data[3]};
} else {
unicode.m_variant = invalidAsciiCode;
}
return unicode;
}
static constexpr FudUtf8 make(utf8 utf8Char)
{
return make(Ascii{utf8Char});
}
static constexpr FudUtf8 make(Ascii utf8Char)
{
FudUtf8 unicode{{Utf8Variant{Ascii{}}}};
if (utf8Char.valid()) {
unicode.m_variant = utf8Char;
} else {
unicode.m_variant = invalidAsciiCode;
}
return unicode;
}
static constexpr FudUtf8 make(Utf8Variant utf8Variant)
{
FudUtf8 unicode{};
unicode.m_variant = utf8Variant;
if (!std::visit([](auto arg) { return arg.valid(); }, utf8Variant)) {
unicode.m_variant = invalidAsciiCode;
}
return unicode;
}
static constexpr FudUtf8 invalidAscii()
{
FudUtf8 character{};
character.m_variant = Ascii{invalidAsciiCode};
return character;
}
[[nodiscard]] constexpr Utf8Type getType() const
{
return static_cast<Utf8Type>(m_variant.index());
}
[[nodiscard]] constexpr bool isAscii() const
{
return getType() == Utf8Type::Ascii;
}
[[nodiscard]] constexpr bool valid() const noexcept
{
switch (m_variant.index()) {
case static_cast<size_t>(Utf8Type::Ascii):
return std::get<Ascii>(m_variant).valid();
case static_cast<size_t>(Utf8Type::Utf82Byte):
return std::get<Utf82Byte>(m_variant).valid();
case static_cast<size_t>(Utf8Type::Utf83Byte):
return std::get<Utf83Byte>(m_variant).valid();
case static_cast<size_t>(Utf8Type::Utf84Byte):
return std::get<Utf84Byte>(m_variant).valid();
default: // unlikely
return false;
}
}
[[nodiscard]] constexpr size_t size() const noexcept
{
if (!valid()) {
return 0;
}
switch (m_variant.index()) {
case static_cast<size_t>(Utf8Type::Ascii):
return Ascii::size();
case static_cast<size_t>(Utf8Type::Utf82Byte):
return Utf82Byte::size();
case static_cast<size_t>(Utf8Type::Utf83Byte):
return Utf83Byte::size();
case static_cast<size_t>(Utf8Type::Utf84Byte):
return Utf84Byte::size();
default: // unlikely
return 0;
}
}
[[nodiscard]] constexpr const uint8_t* data() const noexcept
{
if (!valid()) {
return nullptr;
}
switch (m_variant.index()) {
case static_cast<size_t>(Utf8Type::Ascii):
return std::get<Ascii>(m_variant).characters.data();
case static_cast<size_t>(Utf8Type::Utf82Byte):
return std::get<Utf82Byte>(m_variant).characters.data();
case static_cast<size_t>(Utf8Type::Utf83Byte):
return std::get<Utf83Byte>(m_variant).characters.data();
case static_cast<size_t>(Utf8Type::Utf84Byte):
return std::get<Utf84Byte>(m_variant).characters.data();
default: // unlikely
return nullptr;
}
}
template <typename Func>
[[nodiscard]] bool transformAscii(Func&& transform)
{
if (isAscii()) {
std::forward<Func>(transform)(std::get<Ascii>(m_variant));
return true;
}
return false;
}
[[nodiscard]] constexpr int64_t hash() const noexcept
{
using fud::Utf82Byte;
using fud::Utf83Byte;
using fud::Utf84Byte;
using fud::Utf8Type;
if (!valid()) {
return -1;
}
constexpr uint8_t OneByteShift = 8;
constexpr uint8_t TwoByteShift = 2 * OneByteShift;
constexpr uint8_t ThreeByteShift = 3 * OneByteShift;
switch (static_cast<Utf8Type>(m_variant.index())) {
case Utf8Type::Ascii:
return std::get<Ascii>(m_variant).characters[0];
case Utf8Type::Utf82Byte:
return static_cast<int64_t>(std::get<Utf82Byte>(m_variant).characters[0]) << OneByteShift |
static_cast<int64_t>(std::get<Utf82Byte>(m_variant).characters[1]);
case Utf8Type::Utf83Byte:
return static_cast<int64_t>(std::get<Utf83Byte>(m_variant).characters[0]) << TwoByteShift |
static_cast<int64_t>(std::get<Utf83Byte>(m_variant).characters[1]) << OneByteShift |
static_cast<int64_t>(std::get<Utf83Byte>(m_variant).characters[2]);
case Utf8Type::Utf84Byte:
return static_cast<int64_t>(std::get<Utf84Byte>(m_variant).characters[0]) << ThreeByteShift |
static_cast<int64_t>(std::get<Utf84Byte>(m_variant).characters[1]) << TwoByteShift |
static_cast<int64_t>(std::get<Utf84Byte>(m_variant).characters[2]) << OneByteShift |
static_cast<int64_t>(std::get<Utf84Byte>(m_variant).characters[3]);
default: // unlikely
return -1;
}
}
constexpr bool operator==(const FudUtf8& other) const noexcept = default;
constexpr auto operator<=>(const FudUtf8& other) const noexcept
{
auto hasSameAlternative = []<typename T>(const FudUtf8& lhs, const FudUtf8& rhs) noexcept {
return std::holds_alternative<T>(lhs.m_variant) && std::holds_alternative<T>(rhs.m_variant);
};
auto getSameAlternative = []<typename T>(const FudUtf8& lhs, const FudUtf8& rhs) noexcept {
return std::get<T>(lhs.m_variant).operator<=>(std::get<T>(rhs.m_variant));
};
if (hasSameAlternative.template operator()<Ascii>(*this, other)) {
return getSameAlternative.template operator()<Ascii>(*this, other);
}
if (hasSameAlternative.template operator()<Utf82Byte>(*this, other)) {
return getSameAlternative.template operator()<Utf82Byte>(*this, other);
}
if (hasSameAlternative.template operator()<Utf83Byte>(*this, other)) {
return getSameAlternative.template operator()<Utf83Byte>(*this, other);
}
if (hasSameAlternative.template operator()<Utf84Byte>(*this, other)) {
return getSameAlternative.template operator()<Utf84Byte>(*this, other);
}
if (std::holds_alternative<Ascii>(m_variant)) {
return std::strong_ordering::less;
}
if (std::holds_alternative<Ascii>(other.m_variant)) {
return std::strong_ordering::greater;
}
if (std::holds_alternative<Utf82Byte>(m_variant)) {
return std::strong_ordering::less;
}
if (std::holds_alternative<Utf82Byte>(other.m_variant)) {
return std::strong_ordering::greater;
}
if (std::holds_alternative<Utf83Byte>(m_variant)) {
return std::strong_ordering::less;
}
return std::strong_ordering::greater;
}
Option<Ascii> getAscii() const
{
if (m_variant.index() == static_cast<size_t>(Utf8Type::Ascii)) {
return std::get<Ascii>(m_variant);
}
return NullOpt;
}
};
namespace classify {
using CharPredicate = bool (*)(char);
using Utf8Predicate = bool (*)(utf8);
using FudUtf8Predicate = bool (*)(FudUtf8);
/** \brief Checks if a character is ascii. */
[[nodiscard]] bool isAscii(char character);
[[nodiscard]] bool isAscii(utf8 character);
[[nodiscard]] bool isAscii(FudUtf8 character);
/** \brief Checks if a character is alphanumeric. */
[[nodiscard]] bool isAlphanumeric(char character);
/** \brief Checks if a character is alphanumeric. */
[[nodiscard]] bool isAlphanumeric(utf8 character);
/** \brief Checks if a character is alphanumeric. */
[[nodiscard]] bool isAlphanumeric(FudUtf8 character);
/** \brief Checks if a character is alphabetic. */
[[nodiscard]] bool isAlpha(char character);
/** \brief Checks if a character is alphabetic. */
[[nodiscard]] bool isAlpha(utf8 character);
/** \brief Checks if a character is alphabetic. */
[[nodiscard]] bool isAlpha(FudUtf8 character);
/** \brief Checks if a character is lowercase. */
[[nodiscard]] bool isLowercase(char character);
/** \brief Checks if a character is lowercase. */
[[nodiscard]] bool isLowercase(utf8 character);
/** \brief Checks if a character is lowercase. */
[[nodiscard]] bool isLowercase(FudUtf8 character);
/** \brief Checks if a character is uppercase. */
[[nodiscard]] bool isUppercase(char character);
/** \brief Checks if a character is uppercase. */
[[nodiscard]] bool isUppercase(utf8 character);
/** \brief Checks if a character is uppercase. */
[[nodiscard]] bool isUppercase(FudUtf8 character);
/** \brief Checks if a character is a digit. */
[[nodiscard]] bool isDigit(char character);
/** \brief Checks if a character is a digit. */
[[nodiscard]] bool isDigit(utf8 character);
/** \brief Checks if a character is a digit. */
[[nodiscard]] bool isDigit(FudUtf8 character);
/** \brief Checks if a character is a hexadecimal character. */
[[nodiscard]] bool isHexDigit(char character);
/** \brief Checks if a character is a hexadecimal character. */
[[nodiscard]] bool isHexDigit(utf8 character);
/** \brief Checks if a character is a hexadecimal digit. */
[[nodiscard]] bool isHexDigit(FudUtf8 character);
/** \brief Checks if a character is a control character. */
[[nodiscard]] bool isControl(char character);
/** \brief Checks if a character is a control character. */
[[nodiscard]] bool isControl(utf8 character);
/** \brief Checks if a character is a control character. */
[[nodiscard]] bool isControl(FudUtf8 character);
/** \brief Checks if a character is a graphical character. */
[[nodiscard]] bool isGraphical(char character);
/** \brief Checks if a character is a graphical character. */
[[nodiscard]] bool isGraphical(utf8 character);
/** \brief Checks if a character is a graphical character. */
[[nodiscard]] bool isGraphical(FudUtf8 character);
/** \brief Checks if a character is a space character. */
[[nodiscard]] bool isSpace(char character);
/** \brief Checks if a character is a space character. */
[[nodiscard]] bool isSpace(utf8 character);
/** \brief Checks if a character is a space character. */
[[nodiscard]] bool isSpace(FudUtf8 character);
/** \brief Checks if a character is a blank character. */
[[nodiscard]] bool isBlank(char character);
/** \brief Checks if a character is a blank character. */
[[nodiscard]] bool isBlank(utf8 character);
/** \brief Checks if a character is a blank character. */
[[nodiscard]] bool isBlank(FudUtf8 character);
/** \brief Checks if a character is a printable character. */
[[nodiscard]] bool isPrintable(char character);
/** \brief Checks if a character is a printable character. */
[[nodiscard]] bool isPrintable(utf8 character);
/** \brief Checks if a character is a printable character. */
[[nodiscard]] bool isPrintable(FudUtf8 character);
/** \brief Checks if a character is a punctuation character. */
[[nodiscard]] bool isPunctuation(char character);
/** \brief Checks if a character is a punctuation character. */
[[nodiscard]] bool isPunctuation(utf8 character);
/** \brief Checks if a character is a punctuation character. */
[[nodiscard]] bool isPunctuation(FudUtf8 character);
} // namespace classify
/** \brief Converts character to lowercase if valid. */
uint8_t charToLower(uint8_t character);
/** \brief Converts character to lowercase if valid. */
FudUtf8 utf8ToLower(FudUtf8 character);
/** \brief Converts character to uppercase if valid. */
uint8_t charToUpper(uint8_t character);
/** \brief Converts character to uppercase if valid. */
FudUtf8 utf8ToUpper(FudUtf8 character);
} // namespace fud
#endif
|