euckr.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 korean
  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. // All is a list of all defined encodings in this package.
  13. var All = []encoding.Encoding{EUCKR}
  14. // EUCKR is the EUC-KR encoding, also known as Code Page 949.
  15. var EUCKR encoding.Encoding = &eucKR
  16. var eucKR = internal.Encoding{
  17. &internal.SimpleEncoding{eucKRDecoder{}, eucKREncoder{}},
  18. "EUC-KR",
  19. identifier.EUCKR,
  20. }
  21. type eucKRDecoder struct{ transform.NopResetter }
  22. func (eucKRDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  23. r, size := rune(0), 0
  24. loop:
  25. for ; nSrc < len(src); nSrc += size {
  26. switch c0 := src[nSrc]; {
  27. case c0 < utf8.RuneSelf:
  28. r, size = rune(c0), 1
  29. case 0x81 <= c0 && c0 < 0xff:
  30. if nSrc+1 >= len(src) {
  31. if !atEOF {
  32. err = transform.ErrShortSrc
  33. break loop
  34. }
  35. r, size = utf8.RuneError, 1
  36. break
  37. }
  38. c1 := src[nSrc+1]
  39. size = 2
  40. if c0 < 0xc7 {
  41. r = 178 * rune(c0-0x81)
  42. switch {
  43. case 0x41 <= c1 && c1 < 0x5b:
  44. r += rune(c1) - (0x41 - 0*26)
  45. case 0x61 <= c1 && c1 < 0x7b:
  46. r += rune(c1) - (0x61 - 1*26)
  47. case 0x81 <= c1 && c1 < 0xff:
  48. r += rune(c1) - (0x81 - 2*26)
  49. default:
  50. goto decError
  51. }
  52. } else if 0xa1 <= c1 && c1 < 0xff {
  53. r = 178*(0xc7-0x81) + rune(c0-0xc7)*94 + rune(c1-0xa1)
  54. } else {
  55. goto decError
  56. }
  57. if int(r) < len(decode) {
  58. r = rune(decode[r])
  59. if r != 0 {
  60. break
  61. }
  62. }
  63. decError:
  64. r = utf8.RuneError
  65. if c1 < utf8.RuneSelf {
  66. size = 1
  67. }
  68. default:
  69. r, size = utf8.RuneError, 1
  70. break
  71. }
  72. if nDst+utf8.RuneLen(r) > len(dst) {
  73. err = transform.ErrShortDst
  74. break
  75. }
  76. nDst += utf8.EncodeRune(dst[nDst:], r)
  77. }
  78. return nDst, nSrc, err
  79. }
  80. type eucKREncoder struct{ transform.NopResetter }
  81. func (eucKREncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  82. r, size := rune(0), 0
  83. for ; nSrc < len(src); nSrc += size {
  84. r = rune(src[nSrc])
  85. // Decode a 1-byte rune.
  86. if r < utf8.RuneSelf {
  87. size = 1
  88. if nDst >= len(dst) {
  89. err = transform.ErrShortDst
  90. break
  91. }
  92. dst[nDst] = uint8(r)
  93. nDst++
  94. continue
  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
  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 != 0 {
  111. goto write2
  112. }
  113. case encode1Low <= r && r < encode1High:
  114. if r = rune(encode1[r-encode1Low]); r != 0 {
  115. goto write2
  116. }
  117. case encode2Low <= r && r < encode2High:
  118. if r = rune(encode2[r-encode2Low]); r != 0 {
  119. goto write2
  120. }
  121. case encode3Low <= r && r < encode3High:
  122. if r = rune(encode3[r-encode3Low]); r != 0 {
  123. goto write2
  124. }
  125. case encode4Low <= r && r < encode4High:
  126. if r = rune(encode4[r-encode4Low]); r != 0 {
  127. goto write2
  128. }
  129. case encode5Low <= r && r < encode5High:
  130. if r = rune(encode5[r-encode5Low]); r != 0 {
  131. goto write2
  132. }
  133. case encode6Low <= r && r < encode6High:
  134. if r = rune(encode6[r-encode6Low]); r != 0 {
  135. goto write2
  136. }
  137. }
  138. err = internal.ErrASCIIReplacement
  139. break
  140. }
  141. write2:
  142. if nDst+2 > len(dst) {
  143. err = transform.ErrShortDst
  144. break
  145. }
  146. dst[nDst+0] = uint8(r >> 8)
  147. dst[nDst+1] = uint8(r)
  148. nDst += 2
  149. continue
  150. }
  151. return nDst, nSrc, err
  152. }
  153. func init() {
  154. // Check that the hard-coded encode switch covers all tables.
  155. if numEncodeTables != 7 {
  156. panic("bad numEncodeTables")
  157. }
  158. }