-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_unix.go
121 lines (109 loc) · 2.6 KB
/
main_unix.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
114
115
116
117
118
119
120
121
//go:build !windows
// +build !windows
package mbcs
import (
"os"
"strings"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/transform"
)
const cpUTF8 = 65001
var cpToEncoding = map[uintptr]encoding.Encoding{
932: japanese.ShiftJIS,
936: simplifiedchinese.GBK,
949: korean.EUCKR, // Unified Hangul Code
950: traditionalchinese.Big5,
951: traditionalchinese.Big5, // Big5-HKSCS
50222: japanese.ISO2022JP,
51932: japanese.EUCJP,
51949: korean.EUCKR,
52936: simplifiedchinese.HZGB2312,
}
var currentOsEncoding encoding.Encoding
func getCurrentOsEncoding() (e encoding.Encoding) {
if currentOsEncoding != nil {
return currentOsEncoding
}
defer func() {
currentOsEncoding = e
}()
envLang, ok := os.LookupEnv("LC_ALL")
if !ok || envLang == "" {
envLang, ok = os.LookupEnv("LANG")
if !ok || envLang == "" {
return encoding.Nop
}
}
periodPos := strings.IndexByte(envLang, '.')
if periodPos >= 0 {
envLang = envLang[periodPos+1:]
}
enc, err := ianaindex.IANA.Encoding(envLang)
if err != nil {
return encoding.Nop
}
return enc
}
// AtoU Converts Ansi-bytes to UTF8-String
func ansiToUtf8(mbcs []byte, codepage uintptr) (string, error) {
var enc encoding.Encoding
switch codepage {
case ACP, THREAD_ACP:
enc = getCurrentOsEncoding()
case cpUTF8:
return string(mbcs), nil
default:
var ok bool
enc, ok = cpToEncoding[codepage]
if !ok {
return string(mbcs), ErrUnsupportedOs
}
}
b, err := enc.NewDecoder().Bytes(mbcs)
return string(b), err
}
// UtoA Converts UTF8-String to Ansi-bytes
func utf8ToAnsi(utf8 string, codepage uintptr) ([]byte, error) {
var enc encoding.Encoding
switch codepage {
case ACP, THREAD_ACP:
enc = getCurrentOsEncoding()
case cpUTF8:
return []byte(utf8), nil
default:
var ok bool
enc, ok = cpToEncoding[codepage]
if !ok {
return []byte(utf8), ErrUnsupportedOs
}
}
return enc.NewEncoder().Bytes([]byte(utf8))
}
func consoleCP() uintptr {
return ACP
}
func newEncoder(cp uintptr) transform.Transformer {
if cp == ACP || cp == THREAD_ACP {
return getCurrentOsEncoding().NewEncoder()
}
enc, ok := cpToEncoding[cp]
if ok {
return enc.NewEncoder()
}
return encoding.Nop.NewEncoder()
}
func newDecoder(cp uintptr) transform.Transformer {
if cp == ACP || cp == THREAD_ACP {
return getCurrentOsEncoding().NewDecoder()
}
enc, ok := cpToEncoding[cp]
if ok {
return enc.NewDecoder()
}
return encoding.Nop.NewDecoder()
}