hzgb2312.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package simplifiedchinese
  5. import (
  6. "errors"
  7. "unicode/utf8"
  8. "golang.org/x/text/encoding"
  9. "golang.org/x/text/encoding/internal"
  10. "golang.org/x/text/encoding/internal/identifier"
  11. "golang.org/x/text/transform"
  12. )
  13. // HZGB2312 is the HZ-GB2312 encoding.
  14. var HZGB2312 encoding.Encoding = &hzGB2312
  15. var hzGB2312 = internal.Encoding{
  16. internal.FuncEncoding{hzGB2312NewDecoder, hzGB2312NewEncoder},
  17. "HZ-GB2312",
  18. identifier.HZGB2312,
  19. }
  20. func hzGB2312NewDecoder() transform.Transformer {
  21. return new(hzGB2312Decoder)
  22. }
  23. func hzGB2312NewEncoder() transform.Transformer {
  24. return new(hzGB2312Encoder)
  25. }
  26. var errInvalidHZGB2312 = errors.New("simplifiedchinese: invalid HZ-GB2312 encoding")
  27. const (
  28. asciiState = iota
  29. gbState
  30. )
  31. type hzGB2312Decoder int
  32. func (d *hzGB2312Decoder) Reset() {
  33. *d = asciiState
  34. }
  35. func (d *hzGB2312Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  36. r, size := rune(0), 0
  37. loop:
  38. for ; nSrc < len(src); nSrc += size {
  39. c0 := src[nSrc]
  40. if c0 >= utf8.RuneSelf {
  41. err = errInvalidHZGB2312
  42. break loop
  43. }
  44. if c0 == '~' {
  45. if nSrc+1 >= len(src) {
  46. err = transform.ErrShortSrc
  47. break loop
  48. }
  49. size = 2
  50. switch src[nSrc+1] {
  51. case '{':
  52. *d = gbState
  53. continue
  54. case '}':
  55. *d = asciiState
  56. continue
  57. case '~':
  58. if nDst >= len(dst) {
  59. err = transform.ErrShortDst
  60. break loop
  61. }
  62. dst[nDst] = '~'
  63. nDst++
  64. continue
  65. case '\n':
  66. continue
  67. default:
  68. err = errInvalidHZGB2312
  69. break loop
  70. }
  71. }
  72. if *d == asciiState {
  73. r, size = rune(c0), 1
  74. } else {
  75. if nSrc+1 >= len(src) {
  76. err = transform.ErrShortSrc
  77. break loop
  78. }
  79. c1 := src[nSrc+1]
  80. if c0 < 0x21 || 0x7e <= c0 || c1 < 0x21 || 0x7f <= c1 {
  81. err = errInvalidHZGB2312
  82. break loop
  83. }
  84. r, size = '\ufffd', 2
  85. if i := int(c0-0x01)*190 + int(c1+0x3f); i < len(decode) {
  86. r = rune(decode[i])
  87. if r == 0 {
  88. r = '\ufffd'
  89. }
  90. }
  91. }
  92. if nDst+utf8.RuneLen(r) > len(dst) {
  93. err = transform.ErrShortDst
  94. break loop
  95. }
  96. nDst += utf8.EncodeRune(dst[nDst:], r)
  97. }
  98. if atEOF && err == transform.ErrShortSrc {
  99. err = errInvalidHZGB2312
  100. }
  101. return nDst, nSrc, err
  102. }
  103. type hzGB2312Encoder int
  104. func (d *hzGB2312Encoder) Reset() {
  105. *d = asciiState
  106. }
  107. func (e *hzGB2312Encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  108. r, size := rune(0), 0
  109. for ; nSrc < len(src); nSrc += size {
  110. r = rune(src[nSrc])
  111. // Decode a 1-byte rune.
  112. if r < utf8.RuneSelf {
  113. size = 1
  114. if r == '~' {
  115. if nDst+2 > len(dst) {
  116. err = transform.ErrShortDst
  117. break
  118. }
  119. dst[nDst+0] = '~'
  120. dst[nDst+1] = '~'
  121. nDst += 2
  122. continue
  123. } else if *e != asciiState {
  124. if nDst+3 > len(dst) {
  125. err = transform.ErrShortDst
  126. break
  127. }
  128. *e = asciiState
  129. dst[nDst+0] = '~'
  130. dst[nDst+1] = '}'
  131. nDst += 2
  132. } else if nDst >= len(dst) {
  133. err = transform.ErrShortDst
  134. break
  135. }
  136. dst[nDst] = uint8(r)
  137. nDst += 1
  138. continue
  139. }
  140. // Decode a multi-byte rune.
  141. r, size = utf8.DecodeRune(src[nSrc:])
  142. if size == 1 {
  143. // All valid runes of size 1 (those below utf8.RuneSelf) were
  144. // handled above. We have invalid UTF-8 or we haven't seen the
  145. // full character yet.
  146. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  147. err = transform.ErrShortSrc
  148. break
  149. }
  150. }
  151. // func init checks that the switch covers all tables.
  152. switch {
  153. case encode0Low <= r && r < encode0High:
  154. if r = rune(encode0[r-encode0Low]); r != 0 {
  155. goto writeGB
  156. }
  157. case encode1Low <= r && r < encode1High:
  158. if r = rune(encode1[r-encode1Low]); r != 0 {
  159. goto writeGB
  160. }
  161. case encode2Low <= r && r < encode2High:
  162. if r = rune(encode2[r-encode2Low]); r != 0 {
  163. goto writeGB
  164. }
  165. case encode3Low <= r && r < encode3High:
  166. if r = rune(encode3[r-encode3Low]); r != 0 {
  167. goto writeGB
  168. }
  169. case encode4Low <= r && r < encode4High:
  170. if r = rune(encode4[r-encode4Low]); r != 0 {
  171. goto writeGB
  172. }
  173. }
  174. terminateInASCIIState:
  175. // Switch back to ASCII state in case of error so that an ASCII
  176. // replacement character can be written in the correct state.
  177. if *e != asciiState {
  178. if nDst+2 > len(dst) {
  179. err = transform.ErrShortDst
  180. break
  181. }
  182. dst[nDst+0] = '~'
  183. dst[nDst+1] = '}'
  184. nDst += 2
  185. }
  186. err = internal.ErrASCIIReplacement
  187. break
  188. writeGB:
  189. c0 := uint8(r>>8) - 0x80
  190. c1 := uint8(r) - 0x80
  191. if c0 < 0x21 || 0x7e <= c0 || c1 < 0x21 || 0x7f <= c1 {
  192. goto terminateInASCIIState
  193. }
  194. if *e == asciiState {
  195. if nDst+4 > len(dst) {
  196. err = transform.ErrShortDst
  197. break
  198. }
  199. *e = gbState
  200. dst[nDst+0] = '~'
  201. dst[nDst+1] = '{'
  202. nDst += 2
  203. } else if nDst+2 > len(dst) {
  204. err = transform.ErrShortDst
  205. break
  206. }
  207. dst[nDst+0] = c0
  208. dst[nDst+1] = c1
  209. nDst += 2
  210. continue
  211. }
  212. // TODO: should one always terminate in ASCII state to make it safe to
  213. // concatenate two HZ-GB2312-encoded strings?
  214. return nDst, nSrc, err
  215. }