-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutf8.cpp
More file actions
58 lines (50 loc) · 1.6 KB
/
Copy pathutf8.cpp
File metadata and controls
58 lines (50 loc) · 1.6 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
#include "utf8.h"
bool bytes_extra_check(const unsigned char* str,int length) // check every byte in str
{
// check utf8 string exclude first byte
for(int i=0;i<length;++i)
{
if((str[i]&0xC0) !=0x80)
{
return false;
}
}
return true;
}
#define multibyte_utf8(a,b,num) [](const unsigned char* s,int len) \
{return (((*s)&a)==b && len>=num && \
bytes_extra_check(s+1,num-1));}
map<int,function<bool(const unsigned char*,int)> >utf8_detect_function={ \
{1,multibyte_utf8(0x80,0x00,1)}, // ascii
{2,multibyte_utf8(0xE0,0xC0,2)},
{3,multibyte_utf8(0xF0,0xE0,3)},
{4,multibyte_utf8(0xF8,0xF0,4)},
{5,multibyte_utf8(0xFC,0xF8,5)},
{6,multibyte_utf8(0xFE,0xFC,6)}};
UTF8Checker::UTF8Checker()
:CheckerBase("utf-8")
{
}
bool UTF8Checker::detect(string str) const
{
int current_index = -1 ;
int length = str.length();
const unsigned char* buffer = (const unsigned char*)str.c_str();
while(current_index+1 < length)
{
bool flag = false;
for(auto& it:utf8_detect_function)
{
if(it.second(buffer,length-current_index-1))
{
buffer += it.first;
current_index += it.first;
flag = true;
break;
}
}
if(flag == false) break;
}
return (current_index+1==length);
}
UTF8Checker _utf8checker = UTF8Checker();