feature_iter_object.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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) readObjectStart() bool {
  103. c := iter.nextToken()
  104. if c == '{' {
  105. c = iter.nextToken()
  106. if c == '}' {
  107. return false
  108. }
  109. iter.unreadByte()
  110. return true
  111. } else if c == 'n' {
  112. iter.skipFixedBytes(3)
  113. return false
  114. }
  115. iter.reportError("readObjectStart", "expect { or n")
  116. return false
  117. }
  118. func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
  119. str := iter.ReadStringAsSlice()
  120. if iter.skipWhitespacesWithoutLoadMore() {
  121. if ret == nil {
  122. ret = make([]byte, len(str))
  123. copy(ret, str)
  124. }
  125. if !iter.loadMore() {
  126. return
  127. }
  128. }
  129. if iter.buf[iter.head] != ':' {
  130. iter.reportError("readObjectFieldAsBytes", "expect : after object field")
  131. return
  132. }
  133. iter.head++
  134. if iter.skipWhitespacesWithoutLoadMore() {
  135. if ret == nil {
  136. ret = make([]byte, len(str))
  137. copy(ret, str)
  138. }
  139. if !iter.loadMore() {
  140. return
  141. }
  142. }
  143. if ret == nil {
  144. return str
  145. }
  146. return ret
  147. }