feature_iter_skip.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package jsoniter
  2. import "fmt"
  3. // ReadNil reads a json object as nil and
  4. // returns whether it's a nil or not
  5. func (iter *Iterator) ReadNil() (ret bool) {
  6. c := iter.nextToken()
  7. if c == 'n' {
  8. iter.skipThreeBytes('u', 'l', 'l') // null
  9. return true
  10. }
  11. iter.unreadByte()
  12. return false
  13. }
  14. // ReadBool reads a json object as BoolValue
  15. func (iter *Iterator) ReadBool() (ret bool) {
  16. c := iter.nextToken()
  17. if c == 't' {
  18. iter.skipThreeBytes('r', 'u', 'e')
  19. return true
  20. }
  21. if c == 'f' {
  22. iter.skipFourBytes('a', 'l', 's', 'e')
  23. return false
  24. }
  25. iter.ReportError("ReadBool", "expect t or f")
  26. return
  27. }
  28. // SkipAndReturnBytes skip next JSON element, and return its content as []byte.
  29. // The []byte can be kept, it is a copy of data.
  30. func (iter *Iterator) SkipAndReturnBytes() []byte {
  31. iter.startCapture(iter.head)
  32. iter.Skip()
  33. return iter.stopCapture()
  34. }
  35. type captureBuffer struct {
  36. startedAt int
  37. captured []byte
  38. }
  39. func (iter *Iterator) startCapture(captureStartedAt int) {
  40. if iter.captured != nil {
  41. panic("already in capture mode")
  42. }
  43. iter.captureStartedAt = captureStartedAt
  44. iter.captured = make([]byte, 0, 32)
  45. }
  46. func (iter *Iterator) stopCapture() []byte {
  47. if iter.captured == nil {
  48. panic("not in capture mode")
  49. }
  50. captured := iter.captured
  51. remaining := iter.buf[iter.captureStartedAt:iter.head]
  52. iter.captureStartedAt = -1
  53. iter.captured = nil
  54. if len(captured) == 0 {
  55. return remaining
  56. }
  57. captured = append(captured, remaining...)
  58. return captured
  59. }
  60. // Skip skips a json object and positions to relatively the next json object
  61. func (iter *Iterator) Skip() {
  62. c := iter.nextToken()
  63. switch c {
  64. case '"':
  65. iter.skipString()
  66. case 'n':
  67. iter.skipThreeBytes('u', 'l', 'l') // null
  68. case 't':
  69. iter.skipThreeBytes('r', 'u', 'e') // true
  70. case 'f':
  71. iter.skipFourBytes('a', 'l', 's', 'e') // false
  72. case '0':
  73. iter.unreadByte()
  74. iter.ReadFloat32()
  75. case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  76. iter.skipNumber()
  77. case '[':
  78. iter.skipArray()
  79. case '{':
  80. iter.skipObject()
  81. default:
  82. iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
  83. return
  84. }
  85. }
  86. func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
  87. if iter.readByte() != b1 {
  88. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  89. return
  90. }
  91. if iter.readByte() != b2 {
  92. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  93. return
  94. }
  95. if iter.readByte() != b3 {
  96. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  97. return
  98. }
  99. if iter.readByte() != b4 {
  100. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  101. return
  102. }
  103. }
  104. func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
  105. if iter.readByte() != b1 {
  106. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  107. return
  108. }
  109. if iter.readByte() != b2 {
  110. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  111. return
  112. }
  113. if iter.readByte() != b3 {
  114. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  115. return
  116. }
  117. }