shiftjis.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 japanese
  5. import (
  6. "unicode/utf8"
  7. "golang.org/x/text/encoding"
  8. "golang.org/x/text/encoding/internal"
  9. "golang.org/x/text/encoding/internal/identifier"
  10. "golang.org/x/text/transform"
  11. )
  12. // ShiftJIS is the Shift JIS encoding, also known as Code Page 932 and
  13. // Windows-31J.
  14. var ShiftJIS encoding.Encoding = &shiftJIS
  15. var shiftJIS = internal.Encoding{
  16. &internal.SimpleEncoding{shiftJISDecoder{}, shiftJISEncoder{}},
  17. "Shift JIS",
  18. identifier.ShiftJIS,
  19. }
  20. type shiftJISDecoder struct{ transform.NopResetter }
  21. func (shiftJISDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  22. r, size := rune(0), 0
  23. loop:
  24. for ; nSrc < len(src); nSrc += size {
  25. switch c0 := src[nSrc]; {
  26. case c0 < utf8.RuneSelf:
  27. r, size = rune(c0), 1
  28. case 0xa1 <= c0 && c0 < 0xe0:
  29. r, size = rune(c0)+(0xff61-0xa1), 1
  30. case (0x81 <= c0 && c0 < 0xa0) || (0xe0 <= c0 && c0 < 0xfd):
  31. if c0 <= 0x9f {
  32. c0 -= 0x70
  33. } else {
  34. c0 -= 0xb0
  35. }
  36. c0 = 2*c0 - 0x21
  37. if nSrc+1 >= len(src) {
  38. if !atEOF {
  39. err = transform.ErrShortSrc
  40. break loop
  41. }
  42. r, size = '\ufffd', 1
  43. goto write
  44. }
  45. c1 := src[nSrc+1]
  46. switch {
  47. case c1 < 0x40:
  48. r, size = '\ufffd', 1 // c1 is ASCII so output on next round
  49. goto write
  50. case c1 < 0x7f:
  51. c0--
  52. c1 -= 0x40
  53. case c1 == 0x7f:
  54. r, size = '\ufffd', 1 // c1 is ASCII so output on next round
  55. goto write
  56. case c1 < 0x9f:
  57. c0--
  58. c1 -= 0x41
  59. case c1 < 0xfd:
  60. c1 -= 0x9f
  61. default:
  62. r, size = '\ufffd', 2
  63. goto write
  64. }
  65. r, size = '\ufffd', 2
  66. if i := int(c0)*94 + int(c1); i < len(jis0208Decode) {
  67. r = rune(jis0208Decode[i])
  68. if r == 0 {
  69. r = '\ufffd'
  70. }
  71. }
  72. case c0 == 0x80:
  73. r, size = 0x80, 1
  74. default:
  75. r, size = '\ufffd', 1
  76. }
  77. write:
  78. if nDst+utf8.RuneLen(r) > len(dst) {
  79. err = transform.ErrShortDst
  80. break loop
  81. }
  82. nDst += utf8.EncodeRune(dst[nDst:], r)
  83. }
  84. return nDst, nSrc, err
  85. }
  86. type shiftJISEncoder struct{ transform.NopResetter }
  87. func (shiftJISEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  88. r, size := rune(0), 0
  89. loop:
  90. for ; nSrc < len(src); nSrc += size {
  91. r = rune(src[nSrc])
  92. // Decode a 1-byte rune.
  93. if r < utf8.RuneSelf {
  94. size = 1
  95. } else {
  96. // Decode a multi-byte rune.
  97. r, size = utf8.DecodeRune(src[nSrc:])
  98. if size == 1 {
  99. // All valid runes of size 1 (those below utf8.RuneSelf) were
  100. // handled above. We have invalid UTF-8 or we haven't seen the
  101. // full character yet.
  102. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  103. err = transform.ErrShortSrc
  104. break loop
  105. }
  106. }
  107. // func init checks that the switch covers all tables.
  108. switch {
  109. case encode0Low <= r && r < encode0High:
  110. if r = rune(encode0[r-encode0Low]); r>>tableShift == jis0208 {
  111. goto write2
  112. }
  113. case encode1Low <= r && r < encode1High:
  114. if r = rune(encode1[r-encode1Low]); r>>tableShift == jis0208 {
  115. goto write2
  116. }
  117. case encode2Low <= r && r < encode2High:
  118. if r = rune(encode2[r-encode2Low]); r>>tableShift == jis0208 {
  119. goto write2
  120. }
  121. case encode3Low <= r && r < encode3High:
  122. if r = rune(encode3[r-encode3Low]); r>>tableShift == jis0208 {
  123. goto write2
  124. }
  125. case encode4Low <= r && r < encode4High:
  126. if r = rune(encode4[r-encode4Low]); r>>tableShift == jis0208 {
  127. goto write2
  128. }
  129. case encode5Low <= r && r < encode5High:
  130. if 0xff61 <= r && r < 0xffa0 {
  131. r -= 0xff61 - 0xa1
  132. goto write1
  133. }
  134. if r = rune(encode5[r-encode5Low]); r>>tableShift == jis0208 {
  135. goto write2
  136. }
  137. }
  138. err = internal.ErrASCIIReplacement
  139. break
  140. }
  141. write1:
  142. if nDst >= len(dst) {
  143. err = transform.ErrShortDst
  144. break
  145. }
  146. dst[nDst] = uint8(r)
  147. nDst++
  148. continue
  149. write2:
  150. j1 := uint8(r>>codeShift) & codeMask
  151. j2 := uint8(r) & codeMask
  152. if nDst+2 > len(dst) {
  153. err = transform.ErrShortDst
  154. break loop
  155. }
  156. if j1 <= 61 {
  157. dst[nDst+0] = 129 + j1/2
  158. } else {
  159. dst[nDst+0] = 193 + j1/2
  160. }
  161. if j1&1 == 0 {
  162. dst[nDst+1] = j2 + j2/63 + 64
  163. } else {
  164. dst[nDst+1] = j2 + 159
  165. }
  166. nDst += 2
  167. continue
  168. }
  169. return nDst, nSrc, err
  170. }