This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gutf.go
113 lines (88 loc) · 1.94 KB
/
gutf.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
"unicode"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
eunicode "golang.org/x/text/encoding/unicode"
"golang.org/x/text/encoding/unicode/utf32"
)
// StripSpaces removes the spaces from a string
func StripSpaces(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, s)
}
func main() {
if len(os.Args) != 5 {
fmt.Fprintf(os.Stderr, "usage:\n\t%s [input] [encoding] [output] [encoding]\n", os.Args[0])
os.Exit(1)
}
count := 0
m := make(map[string]encoding.Encoding)
for _, v := range charmap.All {
s := StripSpaces(fmt.Sprint(v))
count++
m[s] = v
}
for _, v := range japanese.All {
s := StripSpaces(fmt.Sprint(v))
count++
m[s] = v
}
for _, v := range korean.All {
s := StripSpaces(fmt.Sprint(v))
count++
m[s] = v
}
for _, v := range simplifiedchinese.All {
s := StripSpaces(fmt.Sprint(v))
count++
m[s] = v
}
for _, v := range traditionalchinese.All {
s := StripSpaces(fmt.Sprint(v))
count++
m[s] = v
}
for _, v := range eunicode.All {
s := StripSpaces(fmt.Sprint(v))
count++
m[s] = v
}
for _, v := range utf32.All {
s := StripSpaces(fmt.Sprint(v))
count++
m[s] = v
}
input, iencoding, output, oencoding := os.Args[1], os.Args[2], os.Args[3], os.Args[4]
f, err := os.Open(input)
if err != nil {
log.Fatal(err)
}
outFile, err := os.Create(output)
if err != nil {
log.Fatal(err)
}
in := bufio.NewReaderSize(f, 1<<20)
out := bufio.NewWriterSize(outFile, 1<<20)
decode := m[iencoding].NewDecoder().Reader(in)
encode := m[oencoding].NewEncoder().Writer(out)
buf := make([]byte, 1<<20)
io.CopyBuffer(encode, decode, buf)
out.Flush()
outFile.Close()
f.Close()
}