big5.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 traditionalchinese
  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. // All is a list of all defined encodings in this package.
  14. var All = []encoding.Encoding{Big5}
  15. // Big5 is the Big5 encoding, also known as Code Page 950.
  16. var Big5 encoding.Encoding = &big5
  17. var big5 = internal.Encoding{
  18. &internal.SimpleEncoding{big5Decoder{}, big5Encoder{}},
  19. "Big5",
  20. identifier.Big5,
  21. }
  22. var errInvalidBig5 = errors.New("traditionalchinese: invalid Big5 encoding")
  23. type big5Decoder struct{ transform.NopResetter }
  24. func (big5Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  25. r, size, s := rune(0), 0, ""
  26. loop:
  27. for ; nSrc < len(src); nSrc += size {
  28. switch c0 := src[nSrc]; {
  29. case c0 < utf8.RuneSelf:
  30. r, size = rune(c0), 1
  31. case 0x81 <= c0 && c0 < 0xff:
  32. if nSrc+1 >= len(src) {
  33. err = transform.ErrShortSrc
  34. break loop
  35. }
  36. c1 := src[nSrc+1]
  37. switch {
  38. case 0x40 <= c1 && c1 < 0x7f:
  39. c1 -= 0x40
  40. case 0xa1 <= c1 && c1 < 0xff:
  41. c1 -= 0x62
  42. default:
  43. err = errInvalidBig5
  44. break loop
  45. }
  46. r, size = '\ufffd', 2
  47. if i := int(c0-0x81)*157 + int(c1); i < len(decode) {
  48. if 1133 <= i && i < 1167 {
  49. // The two-rune special cases for LATIN CAPITAL / SMALL E WITH CIRCUMFLEX
  50. // AND MACRON / CARON are from http://encoding.spec.whatwg.org/#big5
  51. switch i {
  52. case 1133:
  53. s = "\u00CA\u0304"
  54. goto writeStr
  55. case 1135:
  56. s = "\u00CA\u030C"
  57. goto writeStr
  58. case 1164:
  59. s = "\u00EA\u0304"
  60. goto writeStr
  61. case 1166:
  62. s = "\u00EA\u030C"
  63. goto writeStr
  64. }
  65. }
  66. r = rune(decode[i])
  67. if r == 0 {
  68. r = '\ufffd'
  69. }
  70. }
  71. default:
  72. err = errInvalidBig5
  73. break loop
  74. }
  75. if nDst+utf8.RuneLen(r) > len(dst) {
  76. err = transform.ErrShortDst
  77. break loop
  78. }
  79. nDst += utf8.EncodeRune(dst[nDst:], r)
  80. continue loop
  81. writeStr:
  82. if nDst+len(s) > len(dst) {
  83. err = transform.ErrShortDst
  84. break loop
  85. }
  86. nDst += copy(dst[nDst:], s)
  87. continue loop
  88. }
  89. if atEOF && err == transform.ErrShortSrc {
  90. err = errInvalidBig5
  91. }
  92. return nDst, nSrc, err
  93. }
  94. type big5Encoder struct{ transform.NopResetter }
  95. func (big5Encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  96. r, size := rune(0), 0
  97. for ; nSrc < len(src); nSrc += size {
  98. r = rune(src[nSrc])
  99. // Decode a 1-byte rune.
  100. if r < utf8.RuneSelf {
  101. size = 1
  102. if nDst >= len(dst) {
  103. err = transform.ErrShortDst
  104. break
  105. }
  106. dst[nDst] = uint8(r)
  107. nDst++
  108. continue
  109. } else {
  110. // Decode a multi-byte rune.
  111. r, size = utf8.DecodeRune(src[nSrc:])
  112. if size == 1 {
  113. // All valid runes of size 1 (those below utf8.RuneSelf) were
  114. // handled above. We have invalid UTF-8 or we haven't seen the
  115. // full character yet.
  116. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  117. err = transform.ErrShortSrc
  118. break
  119. }
  120. }
  121. }
  122. if r >= utf8.RuneSelf {
  123. // func init checks that the switch covers all tables.
  124. switch {
  125. case encode0Low <= r && r < encode0High:
  126. if r = rune(encode0[r-encode0Low]); r != 0 {
  127. goto write2
  128. }
  129. case encode1Low <= r && r < encode1High:
  130. if r = rune(encode1[r-encode1Low]); r != 0 {
  131. goto write2
  132. }
  133. case encode2Low <= r && r < encode2High:
  134. if r = rune(encode2[r-encode2Low]); r != 0 {
  135. goto write2
  136. }
  137. case encode3Low <= r && r < encode3High:
  138. if r = rune(encode3[r-encode3Low]); r != 0 {
  139. goto write2
  140. }
  141. case encode4Low <= r && r < encode4High:
  142. if r = rune(encode4[r-encode4Low]); r != 0 {
  143. goto write2
  144. }
  145. case encode5Low <= r && r < encode5High:
  146. if r = rune(encode5[r-encode5Low]); r != 0 {
  147. goto write2
  148. }
  149. case encode6Low <= r && r < encode6High:
  150. if r = rune(encode6[r-encode6Low]); r != 0 {
  151. goto write2
  152. }
  153. case encode7Low <= r && r < encode7High:
  154. if r = rune(encode7[r-encode7Low]); r != 0 {
  155. goto write2
  156. }
  157. }
  158. err = internal.ErrASCIIReplacement
  159. break
  160. }
  161. write2:
  162. if nDst+2 > len(dst) {
  163. err = transform.ErrShortDst
  164. break
  165. }
  166. dst[nDst+0] = uint8(r >> 8)
  167. dst[nDst+1] = uint8(r)
  168. nDst += 2
  169. continue
  170. }
  171. return nDst, nSrc, err
  172. }
  173. func init() {
  174. // Check that the hard-coded encode switch covers all tables.
  175. if numEncodeTables != 8 {
  176. panic("bad numEncodeTables")
  177. }
  178. }