diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-09-07 19:58:01 -0400 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-09-07 19:58:01 -0400 |
commit | 25d6ae69d1cb392922b9d9dc0da1c17aef9a9db2 (patch) | |
tree | 4bc49d5520c52bf1ea6367cc0b80bf0065b920bc /modules/mahonia/cp51932.go | |
parent | f8977f4847b8df9feec1bb5913f75401d79db876 (diff) | |
download | gitea-25d6ae69d1cb392922b9d9dc0da1c17aef9a9db2.tar.gz gitea-25d6ae69d1cb392922b9d9dc0da1c17aef9a9db2.zip |
Remove hg dep
Diffstat (limited to 'modules/mahonia/cp51932.go')
-rw-r--r-- | modules/mahonia/cp51932.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/modules/mahonia/cp51932.go b/modules/mahonia/cp51932.go new file mode 100644 index 0000000000..a8c3d00eee --- /dev/null +++ b/modules/mahonia/cp51932.go @@ -0,0 +1,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) +} |