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
#include "Util/Utf8.h"
std::size_t utf8::Utf8nLen(const char *source, std::size_t byteNum) {
const char *t = source;
std::size_t length = 0;
while (static_cast<std::size_t>(source - t) < byteNum && '\0' != *source) {
if (0xf0 == (0xf8 & *source)) {
// 4-byte utf8 code point (began with 0b11110xxx)
source += 4;
} else if (0xe0 == (0xf0 & *source)) {
// 3-byte utf8 code point (began with 0b1110xxxx)
source += 3;
} else if (0xc0 == (0xe0 & *source)) {
// 2-byte utf8 code point (began with 0b110xxxxx)
source += 2;
} else {
// if (0x00 == (0x80 & *s)) {
// 1-byte ascii (began with 0b0xxxxxxx)
source += 1;
}
// no matter the bytes we marched s forward by, it was
// only 1 utf8 codepoint
length++;
}
if (static_cast<size_t>(source - t) > byteNum) {
length--;
}
return length;
}
std::size_t utf8::Utf8nByteNum(const char *source, std::size_t maxByteNum, std::size_t utf8Position) {
const char *t = source;
std::size_t length = 0;
std::size_t byteNum = 0;
while (byteNum < maxByteNum && '\0' != *source) {
if (0xf0 == (0xf8 & *source)) {
// 4-byte utf8 code point (began with 0b11110xxx)
source += 4;
} else if (0xe0 == (0xf0 & *source)) {
// 3-byte utf8 code point (began with 0b1110xxxx)
source += 3;
} else if (0xc0 == (0xe0 & *source)) {
// 2-byte utf8 code point (began with 0b110xxxxx)
source += 2;
} else {
// if (0x00 == (0x80 & *s)) {
// 1-byte ascii (began with 0b0xxxxxxx)
source += 1;
}
// no matter the bytes we marched s forward by, it was
// only 1 utf8 codepoint
length++;
if (length > utf8Position) {
return byteNum;
}
byteNum = static_cast<std::size_t>(source - t);
}
if (byteNum > maxByteNum) {
return maxByteNum;
}
return byteNum;
}
std::size_t utf8::Utf8nLenAtFirstLine(const char *source, std::size_t byteNum) {
const char *t = source;
std::size_t length = 0;
while (static_cast<std::size_t>(source - t) < byteNum && (*source != '\0' && *source != '\r' && *source != '\n')) {
if (0xf0 == (0xf8 & *source)) {
// 4-byte utf8 code point (began with 0b11110xxx)
source += 4;
} else if (0xe0 == (0xf0 & *source)) {
// 3-byte utf8 code point (began with 0b1110xxxx)
source += 3;
} else if (0xc0 == (0xe0 & *source)) {
// 2-byte utf8 code point (began with 0b110xxxxx)
source += 2;
} else {
// if (0x00 == (0x80 & *s)) {
// 1-byte ascii (began with 0b0xxxxxxx)
source += 1;
}
// no matter the bytes we marched s forward by, it was
// only 1 utf8 codepoint
length++;
}
if (static_cast<size_t>(source - t) > byteNum) {
length--;
}
return length;
}
std::size_t utf8::Utf8OneCharLen(const char *source) {
if (0xf0 == (0xf8 & *source)) {
// 4-byte utf8 code point (began with 0b11110xxx)
return 4;
} else if (0xe0 == (0xf0 & *source)) {
// 3-byte utf8 code point (began with 0b1110xxxx)
return 3;
} else if (0xc0 == (0xe0 & *source)) {
// 2-byte utf8 code point (began with 0b110xxxxx)
return 2;
} else {
// if (0x00 == (0x80 & *s)) {
// 1-byte ascii (began with 0b0xxxxxxx)
return 1;
}
}
uint32_t utf8::Utf8ToUnicode(const char *source, std::size_t maxNum, std::size_t &byteNum) {
if (maxNum == 0) {
byteNum = 1;
return 0;
}
if (0xf0 == (0xf8 & *source) && maxNum >= 4) {
byteNum = 4;
// 4-byte utf8 code point (began with 0b11110xxx)
return ((source[0] & 0x07) << 18) | ((source[1] & 0x3f) << 12) | ((source[2] & 0x3f) << 6) | (source[3] & 0x3f);
} else if (0xe0 == (0xf0 & *source) && maxNum >= 3) {
byteNum = 3;
// 3-byte utf8 code point (began with 0b1110xxxx)
return ((source[0] & 0x0f) << 12) | ((source[1] & 0x3f) << 6) | (source[2] & 0x3f);
} else if (0xc0 == (0xe0 & *source) && maxNum >= 2) {
byteNum = 2;
// 2-byte utf8 code point (began with 0b110xxxxx)
return ((source[0] & 0x1f) << 6) | (source[1] & 0x3f);
} else {
byteNum = 1;
// if (0x00 == (0x80 & *s)) {
// 1-byte ascii (began with 0b0xxxxxxx)
return source[0];
}
}