File: euc-jp.go

package info (click to toggle)
golang-github-axgle-mahonia 0.0~git20180208.3358181-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,860 kB
  • sloc: makefile: 2
file content (75 lines) | stat: -rw-r--r-- 1,389 bytes parent folder | download | duplicates (2)
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
package mahonia

// Converters for the EUC-JP encoding

import (
	"sync"
)

func init() {
	RegisterCharset(&Charset{
		Name:    "EUC-JP",
		Aliases: []string{"extended_unix_code_packed_format_for_japanese", "cseucpkdfmtjapanese"},
		NewDecoder: func() Decoder {
			eucJPOnce.Do(makeEUCJPTable)
			return eucJPTable.Decoder()
		},
		NewEncoder: func() Encoder {
			eucJPOnce.Do(makeEUCJPTable)
			return eucJPTable.Encoder()
		},
	})
}

var eucJPOnce sync.Once
var eucJPTable MBCSTable

func makeEUCJPTable() {
	var b [3]byte

	b[0] = 0x8f
	for jis0212, unicode := range jis0212ToUnicode {
		if unicode == 0 {
			continue
		}

		b[1] = byte(jis0212>>8) | 128
		b[2] = byte(jis0212) | 128
		eucJPTable.AddCharacter(rune(unicode), string(b[:3]))
	}

	for jis0208, unicode := range jis0208ToUnicode {
		if unicode == 0 {
			continue
		}

		b[0] = byte(jis0208>>8) | 128
		b[1] = byte(jis0208) | 128
		eucJPTable.AddCharacter(rune(unicode), string(b[:2]))
	}

	b[0] = 0x8e
	for i := 128; i < 256; i++ {
		unicode := jis0201ToUnicode[i]
		if unicode == 0 {
			continue
		}

		b[1] = byte(i)
		eucJPTable.AddCharacter(rune(unicode), string(b[:2]))
	}

	for i := '\x00'; i < 128; i++ {
		var unicode rune
		if i < 32 || i == 127 {
			unicode = i
		} else {
			unicode = rune(jis0201ToUnicode[i])
			if unicode == 0 {
				continue
			}
		}

		eucJPTable.AddCharacter(unicode, string(byte(i)))
	}
}