/
package.d
74 lines (65 loc) · 1.36 KB
/
package.d
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
65
66
67
68
69
70
71
72
73
74
// Written by meatatt.
// Thanks to Jimmy Chen for bug fixing.
module exCode;
import database;
import std.array: appender;
import std.ascii: isASCII;
@safe:
version(NO_FILTER)
enum UseCodecFilter=false;
else
enum UseCodecFilter=true;
private union UNIT{
wchar uni;
char[2] gbk;
struct{
char gbkH;
char gbkT;
}
}
// Convert string from Unicode To GBK wstring
string UNI2GBK(in wchar[] source){
auto res=appender!string;
UNIT unit;
foreach(ref wc;source){
static if (UseCodecFilter) if (isASCII(wc)){
res.put(wc);
continue;
}
if (wc>0x00A3&&wc<0xFFE6){
unit.uni=uni_gbk[wc-0x00A4];
if (unit.uni!=0){
res.put(unit.gbk.idup);
continue;
}
}
static if (UseCodecFilter) res.put(U2G_E);
else res.put(wc);
}
return res.data;
}
// Convert wstring from GBK To Unicode string
wstring GBK2UNI(in char[] source){
auto res=appender!wstring;
UNIT unit;
for (uint i=1;i<source.length;++i){
unit.gbk=source[i-1..i+1];
static if (UseCodecFilter) if (isASCII(unit.gbkH)){
res.put(unit.gbkH);
continue;
}
if (unit.uni>0x4080&&unit.uni<0xFEF8)
if (wchar wc=gbk_uni[unit.uni-0x4081]){
res.put(wc);
unit.gbkT=0;
++i;
continue;
}
static if (UseCodecFilter) res.put(G2U_E);
else res.put(unit.gbk.idup);
}
if(unit.gbkT!=0) res.put(unit.gbkT);
return res.data;
}
enum char U2G_E='?';
enum wchar G2U_E='�';