-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTF-8-Validation.cpp
64 lines (59 loc) · 1.58 KB
/
UTF-8-Validation.cpp
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
class Solution
{
public:
bool validUtf8(vector<int> &data)
{
const int n = data.size();
for (int i = 0; i < n; i++)
{
int tmp = (data[i] & 0xff);
tmp >>= 3;
if (tmp == 0x1e)
{
if (i + 3 >= n)
return false;
for (int j = i + 1; j <= i + 3; j++)
{
int tmp1 = ((data[j] & 0xff) >> 6) & 0x3;
if (tmp1 != 0x2)
return false;
};
i = i + 3;
continue;
};
tmp >>= 1;
if (tmp == 0xe)
{
if (i + 2 >= n)
return false;
for (int j = i + 1; j <= i + 2; j++)
{
int tmp1 = ((data[j] & 0xff) >> 6) & 0x3;
if (tmp1 != 0x2)
return false;
};
i = i + 2;
continue;
};
tmp >>= 1;
if (tmp == 0x6)
{
if (i + 1 >= n)
return false;
for (int j = i + 1; j <= i + 1; j++)
{
int tmp1 = ((data[j] & 0xff) >> 6) & 0x3;
if (tmp1 != 0x2)
return false;
};
i = i + 1;
continue;
};
tmp >>= 2;
if (tmp == 0x0)
continue;
return false;
};
return true;
}
};