-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathunidecode.go
More file actions
41 lines (35 loc) · 775 Bytes
/
unidecode.go
File metadata and controls
41 lines (35 loc) · 775 Bytes
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
package unidecode
import (
"strings"
"unicode"
"github.com/mozillazg/go-unidecode/table"
)
// Version return version
func Version() string {
return "0.2.0"
}
// Unidecode implements transliterate Unicode text into plain 7-bit ASCII.
// e.g. Unidecode("kožušček") => "kozuscek"
func Unidecode(s string) string {
return unidecode(s)
}
func unidecode(s string) string {
var ret strings.Builder
for _, r := range s {
if r < unicode.MaxASCII {
ret.WriteRune(r)
continue
}
if r > 0xeffff {
continue
}
section := r >> 8 // Chop off the last two hex digits
position := r % 256 // Last two hex digits
if tb, ok := table.Tables[section]; ok {
if len(tb) > int(position) {
ret.WriteString(tb[position])
}
}
}
return ret.String()
}