-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiftjis.go
70 lines (55 loc) · 1.11 KB
/
shiftjis.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
package mahonia
// Converters for the Shift-JIS encoding.
import (
"sync"
)
func init() {
RegisterCharset(&Charset{
Name: "Shift_JIS",
Aliases: []string{"MS_Kanji", "csShiftJIS", "SJIS"},
NewDecoder: func() Decoder {
sjisOnce.Do(makeSjisTable)
return sjisTable.Decoder()
},
NewEncoder: func() Encoder {
sjisOnce.Do(makeSjisTable)
return sjisTable.Encoder()
},
})
}
var sjisOnce sync.Once
var sjisTable MBCSTable
func makeSjisTable() {
var b [2]byte
for jis0208, unicode := range jis0208ToUnicode {
if unicode == 0 {
continue
}
j1 := byte(jis0208 >> 8)
j2 := byte(jis0208)
if j1 < 95 {
b[0] = (j1+1)/2 + 112
} else {
b[0] = (j1+1)/2 + 176
}
if j1&1 == 1 {
b[1] = j2 + 31
if j2 >= 96 {
b[1]++
}
} else {
b[1] = j2 + 126
}
sjisTable.AddCharacter(rune(unicode), string(b[:]))
}
for jis0201, unicode := range jis0201ToUnicode {
if unicode == 0 {
continue
}
sjisTable.AddCharacter(rune(unicode), string(byte(jis0201)))
}
for i := '\x00'; i < 32; i++ {
sjisTable.AddCharacter(i, string(byte(i)))
}
sjisTable.AddCharacter(0x7f, "\x7f")
}