charmap.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. //go:generate go run maketables.go
  5. // Package charmap provides simple character encodings such as IBM Code Page 437
  6. // and Windows 1252.
  7. package charmap // import "golang.org/x/text/encoding/charmap"
  8. import (
  9. "unicode/utf8"
  10. "golang.org/x/text/encoding"
  11. "golang.org/x/text/encoding/internal"
  12. "golang.org/x/text/encoding/internal/identifier"
  13. "golang.org/x/text/transform"
  14. )
  15. // These encodings vary only in the way clients should interpret them. Their
  16. // coded character set is identical and a single implementation can be shared.
  17. var (
  18. // ISO8859_6E is the ISO 8859-6E encoding.
  19. ISO8859_6E encoding.Encoding = &iso8859_6E
  20. // ISO8859_6I is the ISO 8859-6I encoding.
  21. ISO8859_6I encoding.Encoding = &iso8859_6I
  22. // ISO8859_8E is the ISO 8859-8E encoding.
  23. ISO8859_8E encoding.Encoding = &iso8859_8E
  24. // ISO8859_8I is the ISO 8859-8I encoding.
  25. ISO8859_8I encoding.Encoding = &iso8859_8I
  26. iso8859_6E = internal.Encoding{
  27. ISO8859_6,
  28. "ISO-8859-6E",
  29. identifier.ISO88596E,
  30. }
  31. iso8859_6I = internal.Encoding{
  32. ISO8859_6,
  33. "ISO-8859-6I",
  34. identifier.ISO88596I,
  35. }
  36. iso8859_8E = internal.Encoding{
  37. ISO8859_8,
  38. "ISO-8859-8E",
  39. identifier.ISO88598E,
  40. }
  41. iso8859_8I = internal.Encoding{
  42. ISO8859_8,
  43. "ISO-8859-8I",
  44. identifier.ISO88598I,
  45. }
  46. )
  47. // All is a list of all defined encodings in this package.
  48. var All = listAll
  49. // TODO: implement these encodings, in order of importance.
  50. // ASCII, ISO8859_1: Rather common. Close to Windows 1252.
  51. // ISO8859_9: Close to Windows 1254.
  52. // utf8Enc holds a rune's UTF-8 encoding in data[:len].
  53. type utf8Enc struct {
  54. len uint8
  55. data [3]byte
  56. }
  57. // charmap describes an 8-bit character set encoding.
  58. type charmap struct {
  59. // name is the encoding's name.
  60. name string
  61. // mib is the encoding type of this encoder.
  62. mib identifier.MIB
  63. // asciiSuperset states whether the encoding is a superset of ASCII.
  64. asciiSuperset bool
  65. // low is the lower bound of the encoded byte for a non-ASCII rune. If
  66. // charmap.asciiSuperset is true then this will be 0x80, otherwise 0x00.
  67. low uint8
  68. // replacement is the encoded replacement character.
  69. replacement byte
  70. // decode is the map from encoded byte to UTF-8.
  71. decode [256]utf8Enc
  72. // encoding is the map from runes to encoded bytes. Each entry is a
  73. // uint32: the high 8 bits are the encoded byte and the low 24 bits are
  74. // the rune. The table entries are sorted by ascending rune.
  75. encode [256]uint32
  76. }
  77. func (m *charmap) NewDecoder() *encoding.Decoder {
  78. return &encoding.Decoder{Transformer: charmapDecoder{charmap: m}}
  79. }
  80. func (m *charmap) NewEncoder() *encoding.Encoder {
  81. return &encoding.Encoder{Transformer: charmapEncoder{charmap: m}}
  82. }
  83. func (m *charmap) String() string {
  84. return m.name
  85. }
  86. func (m *charmap) ID() (mib identifier.MIB, other string) {
  87. return m.mib, ""
  88. }
  89. // charmapDecoder implements transform.Transformer by decoding to UTF-8.
  90. type charmapDecoder struct {
  91. transform.NopResetter
  92. charmap *charmap
  93. }
  94. func (m charmapDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  95. for i, c := range src {
  96. if m.charmap.asciiSuperset && c < utf8.RuneSelf {
  97. if nDst >= len(dst) {
  98. err = transform.ErrShortDst
  99. break
  100. }
  101. dst[nDst] = c
  102. nDst++
  103. nSrc = i + 1
  104. continue
  105. }
  106. decode := &m.charmap.decode[c]
  107. n := int(decode.len)
  108. if nDst+n > len(dst) {
  109. err = transform.ErrShortDst
  110. break
  111. }
  112. // It's 15% faster to avoid calling copy for these tiny slices.
  113. for j := 0; j < n; j++ {
  114. dst[nDst] = decode.data[j]
  115. nDst++
  116. }
  117. nSrc = i + 1
  118. }
  119. return nDst, nSrc, err
  120. }
  121. // charmapEncoder implements transform.Transformer by encoding from UTF-8.
  122. type charmapEncoder struct {
  123. transform.NopResetter
  124. charmap *charmap
  125. }
  126. func (m charmapEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  127. r, size := rune(0), 0
  128. loop:
  129. for nSrc < len(src) {
  130. if nDst >= len(dst) {
  131. err = transform.ErrShortDst
  132. break
  133. }
  134. r = rune(src[nSrc])
  135. // Decode a 1-byte rune.
  136. if r < utf8.RuneSelf {
  137. if m.charmap.asciiSuperset {
  138. nSrc++
  139. dst[nDst] = uint8(r)
  140. nDst++
  141. continue
  142. }
  143. size = 1
  144. } else {
  145. // Decode a multi-byte rune.
  146. r, size = utf8.DecodeRune(src[nSrc:])
  147. if size == 1 {
  148. // All valid runes of size 1 (those below utf8.RuneSelf) were
  149. // handled above. We have invalid UTF-8 or we haven't seen the
  150. // full character yet.
  151. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  152. err = transform.ErrShortSrc
  153. } else {
  154. err = internal.RepertoireError(m.charmap.replacement)
  155. }
  156. break
  157. }
  158. }
  159. // Binary search in [low, high) for that rune in the m.charmap.encode table.
  160. for low, high := int(m.charmap.low), 0x100; ; {
  161. if low >= high {
  162. err = internal.RepertoireError(m.charmap.replacement)
  163. break loop
  164. }
  165. mid := (low + high) / 2
  166. got := m.charmap.encode[mid]
  167. gotRune := rune(got & (1<<24 - 1))
  168. if gotRune < r {
  169. low = mid + 1
  170. } else if gotRune > r {
  171. high = mid
  172. } else {
  173. dst[nDst] = byte(got >> 24)
  174. nDst++
  175. break
  176. }
  177. }
  178. nSrc += size
  179. }
  180. return nDst, nSrc, err
  181. }