-
Notifications
You must be signed in to change notification settings - Fork 2
/
halfwidth.go
35 lines (28 loc) · 1.29 KB
/
halfwidth.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
package autocorrect
import (
"regexp"
"strings"
)
var (
charWidthMap = map[rune]rune{
'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd', 'e': 'e', 'f': 'f', 'g': 'g', 'h': 'h', 'i': 'i', 'j': 'j', 'k': 'k', 'l': 'l', 'm': 'm', 'n': 'n', 'o': 'o', 'p': 'p', 'q': 'q', 'r': 'r', 's': 's', 't': 't', 'u': 'u', 'v': 'v', 'w': 'w', 'x': 'x', 'y': 'y', 'z': 'z', 'A': 'A', 'B': 'B', 'C': 'C', 'D': 'D', 'E': 'E', 'F': 'F', 'G': 'G', 'H': 'H', 'I': 'I', 'J': 'J', 'K': 'K', 'L': 'L', 'M': 'M', 'N': 'N', 'O': 'O', 'P': 'P', 'Q': 'Q', 'R': 'R', 'S': 'S', 'T': 'T', 'U': 'U', 'V': 'V', 'W': 'W', 'X': 'X', 'Y': 'Y', 'Z': 'Z', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9', '0': '0', ' ': ' ',
}
halfTimeRe = regexp.MustCompile(`(\d)(:)(\d)`)
)
func halfwidth(text string) string {
runes := []rune{}
for _, char := range text {
newChar := charWidthMap[char]
if newChar != 0 {
runes = append(runes, charWidthMap[char])
} else {
runes = append(runes, char)
}
}
out := string(runes)
// Fix 12:00 -> 12:00
out = halfTimeRe.ReplaceAllStringFunc(out, func(part string) string {
return strings.Replace(part, ":", ":", 1)
})
return out
}