forked from rainycape/unidecode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode.go
44 lines (40 loc) · 827 Bytes
/
decode.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
package unidecode
import (
"compress/zlib"
"io"
"strings"
)
const (
dummyLenght = byte(0xff)
)
var (
transliterations [65536][]rune
transCount = rune(len(transliterations))
)
func decodeTransliterations() {
r, err := zlib.NewReader(strings.NewReader(tableData))
if err != nil {
panic(err)
}
defer r.Close()
b := make([]byte, 0, 13) // 13 = longest transliteration, adjust if needed
lenB := b[:1]
chr := uint16(0xffff) // char counter, rely on overflow on first pass
for {
chr++
if _, err := io.ReadFull(r, lenB); err != nil {
if err == io.EOF {
break
}
panic(err)
}
if lenB[0] == dummyLenght {
continue
}
b = b[:lenB[0]] // resize, preserving allocation
if _, err := io.ReadFull(r, b); err != nil {
panic(err)
}
transliterations[int(chr)] = []rune(string(b))
}
}