| 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 |
#pragma once |
| 31 |
|
| 32 |
#include <cctype> |
| 33 |
#include <cwchar> |
| 34 |
#include <stdexcept> |
| 35 |
|
| 36 |
namespace tools |
| 37 |
{ |
| 38 |
template<typename T, typename Transform> |
| 39 |
inline T utf8canonical(const T &s, Transform t = [](wint_t c)->wint_t { return c; }) |
| 40 |
{ |
| 41 |
T sc = ""; |
| 42 |
size_t avail = s.size(); |
| 43 |
const char *ptr = s.data(); |
| 44 |
wint_t cp = 0; |
| 45 |
int bytes = 1; |
| 46 |
char wbuf[8], *wptr; |
| 47 |
while (avail--) |
| 48 |
{ |
| 49 |
if ((*ptr & 0x80) == 0) |
| 50 |
{ |
| 51 |
cp = *ptr++; |
| 52 |
bytes = 1; |
| 53 |
} |
| 54 |
else if ((*ptr & 0xe0) == 0xc0) |
| 55 |
{ |
| 56 |
if (avail < 1) |
| 57 |
throw std::runtime_error("Invalid UTF-8"); |
| 58 |
cp = (*ptr++ & 0x1f) << 6; |
| 59 |
cp |= *ptr++ & 0x3f; |
| 60 |
--avail; |
| 61 |
bytes = 2; |
| 62 |
} |
| 63 |
else if ((*ptr & 0xf0) == 0xe0) |
| 64 |
{ |
| 65 |
if (avail < 2) |
| 66 |
throw std::runtime_error("Invalid UTF-8"); |
| 67 |
cp = (*ptr++ & 0xf) << 12; |
| 68 |
cp |= (*ptr++ & 0x3f) << 6; |
| 69 |
cp |= *ptr++ & 0x3f; |
| 70 |
avail -= 2; |
| 71 |
bytes = 3; |
| 72 |
} |
| 73 |
else if ((*ptr & 0xf8) == 0xf0) |
| 74 |
{ |
| 75 |
if (avail < 3) |
| 76 |
throw std::runtime_error("Invalid UTF-8"); |
| 77 |
cp = (*ptr++ & 0x7) << 18; |
| 78 |
cp |= (*ptr++ & 0x3f) << 12; |
| 79 |
cp |= (*ptr++ & 0x3f) << 6; |
| 80 |
cp |= *ptr++ & 0x3f; |
| 81 |
avail -= 3; |
| 82 |
bytes = 4; |
| 83 |
} |
| 84 |
else |
| 85 |
throw std::runtime_error("Invalid UTF-8"); |
| 86 |
|
| 87 |
cp = t(cp); |
| 88 |
if (cp <= 0x7f) |
| 89 |
bytes = 1; |
| 90 |
else if (cp <= 0x7ff) |
| 91 |
bytes = 2; |
| 92 |
else if (cp <= 0xffff) |
| 93 |
bytes = 3; |
| 94 |
else if (cp <= 0x10ffff) |
| 95 |
bytes = 4; |
| 96 |
else |
| 97 |
throw std::runtime_error("Invalid code point UTF-8 transformation"); |
| 98 |
|
| 99 |
wptr = wbuf; |
| 100 |
switch (bytes) |
| 101 |
{ |
| 102 |
case 1: *wptr++ = cp; break; |
| 103 |
case 2: *wptr++ = 0xc0 | (cp >> 6); *wptr++ = 0x80 | (cp & 0x3f); break; |
| 104 |
case 3: *wptr++ = 0xe0 | (cp >> 12); *wptr++ = 0x80 | ((cp >> 6) & 0x3f); *wptr++ = 0x80 | (cp & 0x3f); break; |
| 105 |
case 4: *wptr++ = 0xf0 | (cp >> 18); *wptr++ = 0x80 | ((cp >> 12) & 0x3f); *wptr++ = 0x80 | ((cp >> 6) & 0x3f); *wptr++ = 0x80 | (cp & 0x3f); break; |
| 106 |
default: throw std::runtime_error("Invalid UTF-8"); |
| 107 |
} |
| 108 |
*wptr = 0; |
| 109 |
sc.append(wbuf, bytes); |
| 110 |
cp = 0; |
| 111 |
bytes = 1; |
| 112 |
} |
| 113 |
return sc; |
| 114 |
} |
| 115 |
} |