另客网go项目公用的代码库

iter_object.go 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // ReadObject read one field from object.
  7. // If object ended, returns empty string.
  8. // Otherwise, returns the field name.
  9. func (iter *Iterator) ReadObject() (ret string) {
  10. c := iter.nextToken()
  11. switch c {
  12. case 'n':
  13. iter.skipThreeBytes('u', 'l', 'l')
  14. return "" // null
  15. case '{':
  16. c = iter.nextToken()
  17. if c == '"' {
  18. iter.unreadByte()
  19. field := iter.ReadString()
  20. c = iter.nextToken()
  21. if c != ':' {
  22. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  23. }
  24. return field
  25. }
  26. if c == '}' {
  27. return "" // end of object
  28. }
  29. iter.ReportError("ReadObject", `expect " after {, but found `+string([]byte{c}))
  30. return
  31. case ',':
  32. field := iter.ReadString()
  33. c = iter.nextToken()
  34. if c != ':' {
  35. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  36. }
  37. return field
  38. case '}':
  39. return "" // end of object
  40. default:
  41. iter.ReportError("ReadObject", fmt.Sprintf(`expect { or , or } or n, but found %s`, string([]byte{c})))
  42. return
  43. }
  44. }
  45. // CaseInsensitive
  46. func (iter *Iterator) readFieldHash() int64 {
  47. hash := int64(0x811c9dc5)
  48. c := iter.nextToken()
  49. if c != '"' {
  50. iter.ReportError("readFieldHash", `expect ", but found `+string([]byte{c}))
  51. return 0
  52. }
  53. for {
  54. for i := iter.head; i < iter.tail; i++ {
  55. // require ascii string and no escape
  56. b := iter.buf[i]
  57. if b == '\\' {
  58. iter.head = i
  59. for _, b := range iter.readStringSlowPath() {
  60. if 'A' <= b && b <= 'Z' && !iter.cfg.caseSensitive {
  61. b += 'a' - 'A'
  62. }
  63. hash ^= int64(b)
  64. hash *= 0x1000193
  65. }
  66. c = iter.nextToken()
  67. if c != ':' {
  68. iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
  69. return 0
  70. }
  71. return hash
  72. }
  73. if b == '"' {
  74. iter.head = i + 1
  75. c = iter.nextToken()
  76. if c != ':' {
  77. iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
  78. return 0
  79. }
  80. return hash
  81. }
  82. if 'A' <= b && b <= 'Z' && !iter.cfg.caseSensitive {
  83. b += 'a' - 'A'
  84. }
  85. hash ^= int64(b)
  86. hash *= 0x1000193
  87. }
  88. if !iter.loadMore() {
  89. iter.ReportError("readFieldHash", `incomplete field name`)
  90. return 0
  91. }
  92. }
  93. }
  94. func calcHash(str string, caseSensitive bool) int64 {
  95. if !caseSensitive {
  96. str = strings.ToLower(str)
  97. }
  98. hash := int64(0x811c9dc5)
  99. for _, b := range []byte(str) {
  100. hash ^= int64(b)
  101. hash *= 0x1000193
  102. }
  103. return int64(hash)
  104. }
  105. // ReadObjectCB read object with callback, the key is ascii only and field name not copied
  106. func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
  107. c := iter.nextToken()
  108. var field string
  109. if c == '{' {
  110. c = iter.nextToken()
  111. if c == '"' {
  112. iter.unreadByte()
  113. field = iter.ReadString()
  114. c = iter.nextToken()
  115. if c != ':' {
  116. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  117. }
  118. if !callback(iter, field) {
  119. return false
  120. }
  121. c = iter.nextToken()
  122. for c == ',' {
  123. field = iter.ReadString()
  124. c = iter.nextToken()
  125. if c != ':' {
  126. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  127. }
  128. if !callback(iter, field) {
  129. return false
  130. }
  131. c = iter.nextToken()
  132. }
  133. if c != '}' {
  134. iter.ReportError("ReadObjectCB", `object not ended with }`)
  135. return false
  136. }
  137. return true
  138. }
  139. if c == '}' {
  140. return true
  141. }
  142. iter.ReportError("ReadObjectCB", `expect " after }, but found `+string([]byte{c}))
  143. return false
  144. }
  145. if c == 'n' {
  146. iter.skipThreeBytes('u', 'l', 'l')
  147. return true // null
  148. }
  149. iter.ReportError("ReadObjectCB", `expect { or n, but found `+string([]byte{c}))
  150. return false
  151. }
  152. // ReadMapCB read map with callback, the key can be any string
  153. func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
  154. c := iter.nextToken()
  155. if c == '{' {
  156. c = iter.nextToken()
  157. if c == '"' {
  158. iter.unreadByte()
  159. field := iter.ReadString()
  160. if iter.nextToken() != ':' {
  161. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  162. return false
  163. }
  164. if !callback(iter, field) {
  165. return false
  166. }
  167. c = iter.nextToken()
  168. for c == ',' {
  169. field = iter.ReadString()
  170. if iter.nextToken() != ':' {
  171. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  172. return false
  173. }
  174. if !callback(iter, field) {
  175. return false
  176. }
  177. c = iter.nextToken()
  178. }
  179. if c != '}' {
  180. iter.ReportError("ReadMapCB", `object not ended with }`)
  181. return false
  182. }
  183. return true
  184. }
  185. if c == '}' {
  186. return true
  187. }
  188. iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c}))
  189. return false
  190. }
  191. if c == 'n' {
  192. iter.skipThreeBytes('u', 'l', 'l')
  193. return true // null
  194. }
  195. iter.ReportError("ReadMapCB", `expect { or n, but found `+string([]byte{c}))
  196. return false
  197. }
  198. func (iter *Iterator) readObjectStart() bool {
  199. c := iter.nextToken()
  200. if c == '{' {
  201. c = iter.nextToken()
  202. if c == '}' {
  203. return false
  204. }
  205. iter.unreadByte()
  206. return true
  207. } else if c == 'n' {
  208. iter.skipThreeBytes('u', 'l', 'l')
  209. return false
  210. }
  211. iter.ReportError("readObjectStart", "expect { or n, but found "+string([]byte{c}))
  212. return false
  213. }
  214. func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
  215. str := iter.ReadStringAsSlice()
  216. if iter.skipWhitespacesWithoutLoadMore() {
  217. if ret == nil {
  218. ret = make([]byte, len(str))
  219. copy(ret, str)
  220. }
  221. if !iter.loadMore() {
  222. return
  223. }
  224. }
  225. if iter.buf[iter.head] != ':' {
  226. iter.ReportError("readObjectFieldAsBytes", "expect : after object field, but found "+string([]byte{iter.buf[iter.head]}))
  227. return
  228. }
  229. iter.head++
  230. if iter.skipWhitespacesWithoutLoadMore() {
  231. if ret == nil {
  232. ret = make([]byte, len(str))
  233. copy(ret, str)
  234. }
  235. if !iter.loadMore() {
  236. return
  237. }
  238. }
  239. if ret == nil {
  240. return str
  241. }
  242. return ret
  243. }