feature_iter_object.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "unicode"
  5. )
  6. func (iter *Iterator) ReadObject() (ret string) {
  7. c := iter.nextToken()
  8. switch c {
  9. case 'n':
  10. iter.skipFixedBytes(3)
  11. return "" // null
  12. case '{':
  13. c = iter.nextToken()
  14. if c == '"' {
  15. iter.unreadByte()
  16. return string(iter.readObjectFieldAsBytes())
  17. }
  18. if c == '}' {
  19. return "" // end of object
  20. }
  21. iter.reportError("ReadObject", `expect " after {`)
  22. return
  23. case ',':
  24. return string(iter.readObjectFieldAsBytes())
  25. case '}':
  26. return "" // end of object
  27. default:
  28. iter.reportError("ReadObject", fmt.Sprintf(`expect { or , or } or n, but found %s`, string([]byte{c})))
  29. return
  30. }
  31. }
  32. func (iter *Iterator) readFieldHash() int32 {
  33. hash := int64(0x811c9dc5)
  34. c := iter.nextToken()
  35. if c == '"' {
  36. for {
  37. for i := iter.head; i < iter.tail; i++ {
  38. // require ascii string and no escape
  39. b := iter.buf[i]
  40. if 'A' <= b && b <= 'Z' {
  41. b += 'a' - 'A'
  42. }
  43. if b == '"' {
  44. iter.head = i + 1
  45. c = iter.nextToken()
  46. if c != ':' {
  47. iter.reportError("readFieldHash", `expect :, but found `+string([]byte{c}))
  48. }
  49. return int32(hash)
  50. }
  51. hash ^= int64(b)
  52. hash *= 0x1000193
  53. }
  54. if !iter.loadMore() {
  55. iter.reportError("readFieldHash", `incomplete field name`)
  56. return 0
  57. }
  58. }
  59. }
  60. iter.reportError("readFieldHash", `expect ", but found `+string([]byte{c}))
  61. return 0
  62. }
  63. func calcHash(str string) int32 {
  64. hash := int64(0x811c9dc5)
  65. for _, b := range str {
  66. hash ^= int64(unicode.ToLower(b))
  67. hash *= 0x1000193
  68. }
  69. return int32(hash)
  70. }
  71. func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
  72. c := iter.nextToken()
  73. if c == '{' {
  74. c = iter.nextToken()
  75. if c == '"' {
  76. iter.unreadByte()
  77. field := string(iter.readObjectFieldAsBytes())
  78. if !callback(iter, field) {
  79. return false
  80. }
  81. for iter.nextToken() == ',' {
  82. field = string(iter.readObjectFieldAsBytes())
  83. if !callback(iter, field) {
  84. return false
  85. }
  86. }
  87. return true
  88. }
  89. if c == '}' {
  90. return true
  91. }
  92. iter.reportError("ReadObjectCB", `expect " after }`)
  93. return false
  94. }
  95. if c == 'n' {
  96. iter.skipFixedBytes(3)
  97. return true // null
  98. }
  99. iter.reportError("ReadObjectCB", `expect { or n`)
  100. return false
  101. }
  102. func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
  103. c := iter.nextToken()
  104. if c == '{' {
  105. c = iter.nextToken()
  106. if c == '"' {
  107. iter.unreadByte()
  108. field := iter.ReadString()
  109. if iter.nextToken() != ':' {
  110. iter.reportError("ReadMapCB", "expect : after object field")
  111. return false
  112. }
  113. if !callback(iter, field) {
  114. return false
  115. }
  116. for iter.nextToken() == ',' {
  117. field = iter.ReadString()
  118. if iter.nextToken() != ':' {
  119. iter.reportError("ReadMapCB", "expect : after object field")
  120. return false
  121. }
  122. if !callback(iter, field) {
  123. return false
  124. }
  125. }
  126. return true
  127. }
  128. if c == '}' {
  129. return true
  130. }
  131. iter.reportError("ReadMapCB", `expect " after }`)
  132. return false
  133. }
  134. if c == 'n' {
  135. iter.skipFixedBytes(3)
  136. return true // null
  137. }
  138. iter.reportError("ReadMapCB", `expect { or n`)
  139. return false
  140. }
  141. func (iter *Iterator) readObjectStart() bool {
  142. c := iter.nextToken()
  143. if c == '{' {
  144. c = iter.nextToken()
  145. if c == '}' {
  146. return false
  147. }
  148. iter.unreadByte()
  149. return true
  150. } else if c == 'n' {
  151. iter.skipFixedBytes(3)
  152. return false
  153. }
  154. iter.reportError("readObjectStart", "expect { or n")
  155. return false
  156. }
  157. func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
  158. str := iter.ReadStringAsSlice()
  159. if iter.skipWhitespacesWithoutLoadMore() {
  160. if ret == nil {
  161. ret = make([]byte, len(str))
  162. copy(ret, str)
  163. }
  164. if !iter.loadMore() {
  165. return
  166. }
  167. }
  168. if iter.buf[iter.head] != ':' {
  169. iter.reportError("readObjectFieldAsBytes", "expect : after object field")
  170. return
  171. }
  172. iter.head++
  173. if iter.skipWhitespacesWithoutLoadMore() {
  174. if ret == nil {
  175. ret = make([]byte, len(str))
  176. copy(ret, str)
  177. }
  178. if !iter.loadMore() {
  179. return
  180. }
  181. }
  182. if ret == nil {
  183. return str
  184. }
  185. return ret
  186. }