-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgbk.cpp
42 lines (38 loc) · 960 Bytes
/
gbk.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
#include "gbk.h"
GBKChecker::GBKChecker()
:CheckerBase("gbk")
{
}
bool GBKChecker::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) // ascii
{
current_index += 1;
buffer += 1;
}
if(check_two_byte(buffer))
{
current_index += 2;
buffer += 2;
}
else
{
break; // error occured!
}
}
return (current_index+1==length);
}
bool GBKChecker::check_two_byte(const unsigned char *str) const
{
bool first_byte_valid = *str>=0x81 && *str<=0xFE;
++ str; // let check second byte
bool second_byte_valid = *str>=40 && *str<=0xFE && \
*str!=0x7F;
return first_byte_valid&&second_byte_valid;
}
GBKChecker _gbkchecker = GBKChecker();