-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwin1251.go
141 lines (129 loc) · 5.54 KB
/
win1251.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cpd
//unit for windows1251
var consonans1251 = [256]byte{
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F*/
/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 2 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 3 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 4 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 5 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 6 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 7 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* B */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* C */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01,
/* D */ 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
/* E */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01,
/* F */ 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
}
var vowels1251 = [256]byte{
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F*/
/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 2 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 3 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 4 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 5 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 6 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 7 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* B */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* C */ 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
/* D */ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,
/* E */ 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
/* F */ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,
}
// cvPairs1251 - counts the number of pairs consonans+vowels
func cvPairs1251(d []byte) (cvPairsCount int) {
for i := 0; i < len(d)-1; i++ {
if consonans1251[d[i]] > 0 {
i++
if vowels1251[d[i]] > 0 {
cvPairsCount++
}
}
}
return cvPairsCount
}
// matchRunes1251 - counts the number of characters that are the most popular letters of the Russian alphabet
func matchRune1251(d []byte, tbl *cpTable) int {
for i := 0; i < len(d); i++ {
if is1251(d[i]) {
upper := lu1251(d[i])
j := tbl.index(rune(d[i]))
(*tbl)[j].count++
for i++; (i < len(d)) && is1251(d[i]); i++ {
if upper >= lu1251(d[i]) {
j = tbl.index(rune(d[i]))
(*tbl)[j].count++
}
}
}
}
return tbl.founded()
}
func match1251(d []byte, tbl *cpTable) MatchRes {
return MatchRes{matchRune1251(d, tbl), cvPairs1251(d)}
}
const (
cp1251BeginUpperChar = 0xC0
cp1251StopUpperChar = 0xDF
cp1251BeginLowerChar = 0xE0
cp1251StopLowerChar = 0xFF
)
func isUpper1251(r byte) bool {
return (r >= cp1251BeginUpperChar) && (r <= cp1251StopUpperChar)
}
// lu1251 - return 1 if r is upper, return 0 if r is lower
func lu1251(r byte) (res int) {
if isUpper1251(r) {
res = 1
}
return
}
func isLower1251(r byte) bool {
return (r >= cp1251BeginLowerChar) && (r <= cp1251StopLowerChar)
}
func is1251(r byte) bool {
return isUpper1251(r) || isLower1251(r)
}
/*var = [66]byte{
0xE0, 0xC0, // 'а'
0xE1, 0xC1, // 'б'
0xE2, 0xC2, // 'в'
0xE3, 0xC3, // 'г'
0xE4, 0xC4, // 'д'
0xE5, 0xC5, // 'е'
0xB8, 0xA8, // 'ё'
0xE6, 0xC6, // 'ж'
0xE7, 0xC7, // 'з'
0xE8, 0xC8, // 'и'
0xE9, 0xC9, // 'й'
0xEA, 0xCA, // 'к'
0xEB, 0xCB, // 'л'
0xEC, 0xCC, // 'м'
0xED, 0xCD, // 'н'
0xEE, 0xCE, // 'о'
0xEF, 0xCF, // 'п'
0xF0, 0xD0, // 'р'
0xF1, 0xD1, // 'с'
0xF2, 0xD2, // 'т'
0xF3, 0xD3, // 'у'
0xF4, 0xD4, // 'ф'
0xF5, 0xD5, // 'х'
0xF6, 0xD6, // 'ц'
0xF7, 0xD7, // 'ч'
0xF8, 0xD8, // 'ш'
0xF9, 0xD9, // 'щ'
0xFA, 0xDA, // 'ъ'
0xFB, 0xDB, // 'ы'
0xFC, 0xDC, // 'ь'
0xFD, 0xDD, // 'э'
0xFE, 0xDE, // 'ю'
0xFF, 0xDF, // 'я'
}*/