-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathUtf8Iterator.cpp
More file actions
60 lines (54 loc) · 1.26 KB
/
Copy pathUtf8Iterator.cpp
File metadata and controls
60 lines (54 loc) · 1.26 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
#include "Utf8Iterator.h"
Utf8Iterator::Utf8Iterator(String str)
{
_str = str;
_pos = 0;
}
uint16_t Utf8Iterator::next()
{
if (_pos >= _str.length()) {
return 0;
}
uint8_t len = sequenceLength(_str[_pos]);
uint16_t cp = _str[_pos];
switch (len) {
case 1:
++_pos;
return cp;
case 2:
cp = ((cp << 6) & 0x7FF) + (_str[++_pos] & 0x3F);
++_pos;
return cp;
case 3:
cp = ((cp << 12) & 0xFFFF) + ((_str[++_pos] << 6) & 0xFFF);
cp += _str[++_pos] & 0x3F;
++_pos;
return cp;
case 4:
// Code points over U+FFFF are not supported
_pos += 4;
return 0xFFFD;
default:
return 0;
}
}
uint8_t Utf8Iterator::sequenceLength(uint8_t lead)
{
if (lead < 0x80) {
return 1;
} else if ((lead >> 5) == 0x6) {
return 2;
} else if ((lead >> 4) == 0xE) {
return 3;
} else if ((lead >> 3) == 0x1E) {
return 4;
} else {
return 0;
}
}
bool Utf8Iterator::isSpaceCodePoint(uint16_t cp) {
return cp == 0x9 || cp == 0x20 || cp == 0xA0;
};
bool Utf8Iterator::isNewlineCodePoint(uint16_t cp) {
return cp == 0xA;
};