cpd/win1251.go

142 строки
5.5 KiB
Go

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, // 'я'
}*/