-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso2022_jp.cpp
58 lines (52 loc) · 1.48 KB
/
iso2022_jp.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
#include "iso2022_jp.h"
#include <map>
#include <string.h>
const map<string, ISO2022_jp_pair> ISO2022_Detect = {
{("\x1b(B"), {1, [](const unsigned char *) { return true; }}},
{("\x1b(J"), {1, [](const unsigned char *) { return true; }}},
{("\x1b$@"), {2, [](const unsigned char *) { return true; }}},
{("\x1b$B"), {2, [](const unsigned char *) { return true; }}} };
ISO2022_JPChecker::ISO2022_JPChecker()
: CheckerBase("iso2022_jp")
{
}
bool ISO2022_JPChecker::detect(const string &str) const
{
const char *buffer = str.c_str();
ISO2022_jp_pair func = {1, [](const unsigned char *s){ return *s <= 0x7F; }};
while(strlen(buffer))
{
auto result = checkFlag(buffer);
if(get<0>(result))
{
func = get<1>(result);
}
else
{
if(func.second((const unsigned char *)buffer))
{
buffer += func.first;
}
else
{
break;
}
}
}
return (strlen(buffer) == 0);
}
tuple<bool, ISO2022_jp_pair> ISO2022_JPChecker::checkFlag(const char *&str) const
{
tuple<bool, ISO2022_jp_pair> ret = make_tuple(false, ISO2022_jp_pair());
for(auto &func : ISO2022_Detect)
{
string flag = func.first;
if(!strncmp(flag.c_str(), (const char *)str, flag.size()))
{
str += flag.size();
ret = make_tuple(true, func.second);
break;
}
}
return ret;
}