feature_iter_string.go 4.4 KB

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