github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/mahonia/big5.go (about) 1 package mahonia 2 3 // Converters for Big 5 encoding. 4 5 import ( 6 "sync" 7 ) 8 9 func init() { 10 RegisterCharset(&Charset{ 11 Name: "Big5", 12 Aliases: []string{"csBig5"}, 13 NewDecoder: func() Decoder { 14 return decodeBig5Rune 15 }, 16 NewEncoder: func() Encoder { 17 big5Once.Do(reverseBig5Table) 18 return encodeBig5Rune 19 }, 20 }) 21 } 22 23 func decodeBig5Rune(p []byte) (r rune, size int, status Status) { 24 if len(p) == 0 { 25 status = NO_ROOM 26 return 27 } 28 29 b := p[0] 30 if b < 128 { 31 return rune(b), 1, SUCCESS 32 } 33 34 if len(p) < 2 { 35 status = NO_ROOM 36 return 37 } 38 39 c := int(p[0])<<8 + int(p[1]) 40 c = int(big5ToUnicode[c]) 41 if c > 0 { 42 return rune(c), 2, SUCCESS 43 } 44 45 return 0xfffd, 1, INVALID_CHAR 46 } 47 48 func encodeBig5Rune(p []byte, r rune) (size int, status Status) { 49 if len(p) == 0 { 50 status = NO_ROOM 51 return 52 } 53 54 if r < 128 { 55 p[0] = byte(r) 56 return 1, SUCCESS 57 } 58 59 if len(p) < 2 { 60 status = NO_ROOM 61 return 62 } 63 64 if r < 0x10000 { 65 c := unicodeToBig5[r] 66 if c > 0 { 67 p[0] = byte(c >> 8) 68 p[1] = byte(c) 69 return 2, SUCCESS 70 } 71 } 72 73 p[0] = '?' 74 return 1, INVALID_CHAR 75 } 76 77 var big5Once sync.Once 78 79 var unicodeToBig5 []uint16 80 81 func reverseBig5Table() { 82 unicodeToBig5 = make([]uint16, 65536) 83 84 for big5, unicode := range big5ToUnicode { 85 if unicode > 0 { 86 unicodeToBig5[unicode] = uint16(big5) 87 } 88 } 89 }