-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbig5.cpp
More file actions
53 lines (47 loc) · 1.17 KB
/
Copy pathbig5.cpp
File metadata and controls
53 lines (47 loc) · 1.17 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
#include "big5.h"
#define Big5_two_byte_func(min,max) \
[](const unsigned char* s){unsigned short value=(*s<<8)+s[1];return value>=min && value<= max;}
vector<function<bool(const unsigned char*)>> Big5_two_byte_detect = {
Big5_two_byte_func(0xa140,0xa3bf),
Big5_two_byte_func(0xa440,0xc67e),
Big5_two_byte_func(0xc940,0xf9d5)};
Big5Checker::Big5Checker():
CheckerBase("big5")
{
}
bool Big5Checker::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)
{
if(*buffer <= 0x7F)
{
current_index += 1;
buffer += 1;
}
if(check_two_bytes(buffer))
{
current_index += 2;
buffer += 2;
}
else
{
break;
}
}
return (current_index+1 == length);
}
bool Big5Checker::check_two_bytes(const unsigned char *buffer) const
{
for(auto f:Big5_two_byte_detect)
{
if(f(buffer))
{
return true;
}
}
return false;
}
Big5Checker _big5checker = Big5Checker();