github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/mahonia/cp51932.go (about) 1 package mahonia 2 3 import ( 4 "unicode/utf8" 5 ) 6 7 // Converters for Microsoft's version of the EUC-JP encoding 8 9 func init() { 10 RegisterCharset(&Charset{ 11 Name: "cp51932", 12 Aliases: []string{"windows-51932"}, 13 NewDecoder: func() Decoder { 14 return decodeCP51932 15 }, 16 NewEncoder: func() Encoder { 17 msJISTable.Reverse() 18 return encodeCP51932 19 }, 20 }) 21 } 22 23 func decodeCP51932(p []byte) (c rune, size int, status Status) { 24 if len(p) == 0 { 25 return 0, 0, NO_ROOM 26 } 27 28 b := p[0] 29 switch { 30 case b < 0x80: 31 return rune(b), 1, SUCCESS 32 33 case b == 0x8e: 34 if len(p) < 2 { 35 return 0, 0, NO_ROOM 36 } 37 b2 := p[1] 38 if b2 < 0xa1 || b2 > 0xdf { 39 return utf8.RuneError, 1, INVALID_CHAR 40 } 41 return rune(b2) + (0xff61 - 0xa1), 2, SUCCESS 42 43 case 0xa1 <= b && b <= 0xfe: 44 return msJISTable.DecodeHigh(p) 45 } 46 47 return utf8.RuneError, 1, INVALID_CHAR 48 } 49 50 func encodeCP51932(p []byte, c rune) (size int, status Status) { 51 if len(p) == 0 { 52 return 0, NO_ROOM 53 } 54 55 if c < 0x80 { 56 p[0] = byte(c) 57 return 1, SUCCESS 58 } 59 60 if len(p) < 2 { 61 return 0, NO_ROOM 62 } 63 64 if c > 0xffff { 65 p[0] = '?' 66 return 1, INVALID_CHAR 67 } 68 69 if 0xff61 <= c && c <= 0xff9f { 70 p[0] = 0x8e 71 p[1] = byte(c - (0xff61 - 0xa1)) 72 return 2, SUCCESS 73 } 74 75 return msJISTable.EncodeHigh(p, c) 76 }