forked from jhillyerd/enmime
/
unicode.go
45 lines (40 loc) · 937 Bytes
/
unicode.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
package stringutil
import (
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var latinSpecialMap = map[rune]rune{
'Đ': 'D',
'đ': 'd',
'Ħ': 'H',
'ħ': 'h',
'ĸ': 'K',
'Ŀ': 'L',
'Ł': 'L',
'ʼn': 'n',
'Ŋ': 'N',
'ŋ': 'n',
'Ŧ': 'T',
'ŧ': 't',
}
// mapLatinSpecial attempts to map non-accented latin extended runes to ASCII
func mapLatinSpecial(r rune) rune {
if v, ok := latinSpecialMap[r]; ok {
return v
}
if r > 0x7e {
return '_'
}
return r
}
// ToASCII converts unicode to ASCII by stripping accents and converting some special characters
// into their ASCII approximations. Anything else will be replaced with an underscore.
func ToASCII(s string) string {
// unicode.Mn: nonspacing marks
tr := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), runes.Map(mapLatinSpecial),
norm.NFC)
r, _, _ := transform.String(tr, s)
return r
}