summaryrefslogtreecommitdiffstats
path: root/modules/mahonia/cp51932.go
blob: a8c3d00eee202d04b8ee13d796b1a53675a845c1 (plain)
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
package mahonia

import (
	"unicode/utf8"
)

// Converters for Microsoft's version of the EUC-JP encoding

func init() {
	RegisterCharset(&Charset{
		Name:    "cp51932",
		Aliases: []string{"windows-51932"},
		NewDecoder: func() Decoder {
			return decodeCP51932
		},
		NewEncoder: func() Encoder {
			msJISTable.Reverse()
			return encodeCP51932
		},
	})
}

func decodeCP51932(p []byte) (c rune, size int, status Status) {
	if len(p) == 0 {
		return 0, 0, NO_ROOM
	}

	b := p[0]
	switch {
	case b < 0x80:
		return rune(b), 1, SUCCESS

	case b == 0x8e:
		if len(p) < 2 {
			return 0, 0, NO_ROOM
		}
		b2 := p[1]
		if b2 < 0xa1 || b2 > 0xdf {
			return utf8.RuneError, 1, INVALID_CHAR
		}
		return rune(b2) + (0xff61 - 0xa1), 2, SUCCESS

	case 0xa1 <= b && b <= 0xfe:
		return msJISTable.DecodeHigh(p)
	}

	return utf8.RuneError, 1, INVALID_CHAR
}

func encodeCP51932(p []byte, c rune) (size int, status Status) {
	if len(p) == 0 {
		return 0, NO_ROOM
	}

	if c < 0x80 {
		p[0] = byte(c)
		return 1, SUCCESS
	}

	if len(p) < 2 {
		return 0, NO_ROOM
	}

	if c > 0xffff {
		p[0] = '?'
		return 1, INVALID_CHAR
	}

	if 0xff61 <= c && c <= 0xff9f {
		p[0] = 0x8e
		p[1] = byte(c - (0xff61 - 0xa1))
		return 2, SUCCESS
	}

	return msJISTable.EncodeHigh(p, c)
}