feature_iter_string.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package jsoniter
  2. import (
  3. "unicode/utf16"
  4. "fmt"
  5. )
  6. // ReadString read string from iterator
  7. func (iter *Iterator) ReadString() (ret string) {
  8. c := iter.nextToken()
  9. if c == '"' {
  10. for i := iter.head; i < iter.tail; i++ {
  11. c := iter.buf[i]
  12. if c == '"' {
  13. ret = string(iter.buf[iter.head:i])
  14. iter.head = i + 1
  15. return ret
  16. } else if c == '\\' {
  17. break
  18. } else if c < ' ' {
  19. iter.ReportError("ReadString",
  20. fmt.Sprintf(`invalid control character found: %d`, c))
  21. return
  22. }
  23. }
  24. return iter.readStringSlowPath()
  25. } else if c == 'n' {
  26. iter.skipThreeBytes('u', 'l', 'l')
  27. return ""
  28. }
  29. iter.ReportError("ReadString", `expects " or n`)
  30. return
  31. }
  32. func (iter *Iterator) readStringSlowPath() (ret string) {
  33. var str []byte
  34. var c byte
  35. for iter.Error == nil {
  36. c = iter.readByte()
  37. if c == '"' {
  38. return string(str)
  39. }
  40. if c == '\\' {
  41. c = iter.readByte()
  42. switch c {
  43. case 'u', 'U':
  44. r := iter.readU4()
  45. if utf16.IsSurrogate(r) {
  46. c = iter.readByte()
  47. if iter.Error != nil {
  48. return
  49. }
  50. if c != '\\' {
  51. iter.ReportError("ReadString",
  52. `expects \u after utf16 surrogate, but \ not found`)
  53. return
  54. }
  55. c = iter.readByte()
  56. if iter.Error != nil {
  57. return
  58. }
  59. if c != 'u' && c != 'U' {
  60. iter.ReportError("ReadString",
  61. `expects \u after utf16 surrogate, but \u not found`)
  62. return
  63. }
  64. r2 := iter.readU4()
  65. if iter.Error != nil {
  66. return
  67. }
  68. combined := utf16.DecodeRune(r, r2)
  69. str = appendRune(str, combined)
  70. } else {
  71. str = appendRune(str, r)
  72. }
  73. case '"':
  74. str = append(str, '"')
  75. case '\\':
  76. str = append(str, '\\')
  77. case '/':
  78. str = append(str, '/')
  79. case 'b':
  80. str = append(str, '\b')
  81. case 'f':
  82. str = append(str, '\f')
  83. case 'n':
  84. str = append(str, '\n')
  85. case 'r':
  86. str = append(str, '\r')
  87. case 't':
  88. str = append(str, '\t')
  89. default:
  90. iter.ReportError("ReadString",
  91. `invalid escape char after \`)
  92. return
  93. }
  94. } else {
  95. str = append(str, c)
  96. }
  97. }
  98. iter.ReportError("ReadString", "unexpected end of input")
  99. return
  100. }
  101. // ReadStringAsSlice read string from iterator without copying into string form.
  102. // The []byte can not be kept, as it will change after next iterator call.
  103. func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
  104. c := iter.nextToken()
  105. if c == '"' {
  106. for i := iter.head; i < iter.tail; i++ {
  107. // require ascii string and no escape
  108. // for: field name, base64, number
  109. if iter.buf[i] == '"' {
  110. // fast path: reuse the underlying buffer
  111. ret = iter.buf[iter.head:i]
  112. iter.head = i + 1
  113. return ret
  114. }
  115. }
  116. readLen := iter.tail - iter.head
  117. copied := make([]byte, readLen, readLen*2)
  118. copy(copied, iter.buf[iter.head:iter.tail])
  119. iter.head = iter.tail
  120. for iter.Error == nil {
  121. c := iter.readByte()
  122. if c == '"' {
  123. return copied
  124. }
  125. copied = append(copied, c)
  126. }
  127. return copied
  128. }
  129. iter.ReportError("ReadString", `expects " or n`)
  130. return
  131. }
  132. func (iter *Iterator) readU4() (ret rune) {
  133. for i := 0; i < 4; i++ {
  134. c := iter.readByte()
  135. if iter.Error != nil {
  136. return
  137. }
  138. if c >= '0' && c <= '9' {
  139. ret = ret*16 + rune(c-'0')
  140. } else if c >= 'a' && c <= 'f' {
  141. ret = ret*16 + rune(c-'a'+10)
  142. } else if c >= 'A' && c <= 'F' {
  143. ret = ret*16 + rune(c-'A'+10)
  144. } else {
  145. iter.ReportError("readU4", "expects 0~9 or a~f")
  146. return
  147. }
  148. }
  149. return ret
  150. }
  151. const (
  152. t1 = 0x00 // 0000 0000
  153. tx = 0x80 // 1000 0000
  154. t2 = 0xC0 // 1100 0000
  155. t3 = 0xE0 // 1110 0000
  156. t4 = 0xF0 // 1111 0000
  157. t5 = 0xF8 // 1111 1000
  158. maskx = 0x3F // 0011 1111
  159. mask2 = 0x1F // 0001 1111
  160. mask3 = 0x0F // 0000 1111
  161. mask4 = 0x07 // 0000 0111
  162. rune1Max = 1<<7 - 1
  163. rune2Max = 1<<11 - 1
  164. rune3Max = 1<<16 - 1
  165. surrogateMin = 0xD800
  166. surrogateMax = 0xDFFF
  167. maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
  168. runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
  169. )
  170. func appendRune(p []byte, r rune) []byte {
  171. // Negative values are erroneous. Making it unsigned addresses the problem.
  172. switch i := uint32(r); {
  173. case i <= rune1Max:
  174. p = append(p, byte(r))
  175. return p
  176. case i <= rune2Max:
  177. p = append(p, t2|byte(r>>6))
  178. p = append(p, tx|byte(r)&maskx)
  179. return p
  180. case i > maxRune, surrogateMin <= i && i <= surrogateMax:
  181. r = runeError
  182. fallthrough
  183. case i <= rune3Max:
  184. p = append(p, t3|byte(r>>12))
  185. p = append(p, tx|byte(r>>6)&maskx)
  186. p = append(p, tx|byte(r)&maskx)
  187. return p
  188. default:
  189. p = append(p, t4|byte(r>>18))
  190. p = append(p, tx|byte(r>>12)&maskx)
  191. p = append(p, tx|byte(r>>6)&maskx)
  192. p = append(p, tx|byte(r)&maskx)
  193. return p
  194. }
  195. }