iter_skip_strict.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //+build !jsoniter_sloppy
  2. package jsoniter
  3. import "fmt"
  4. func (iter *Iterator) skipNumber() {
  5. if !iter.trySkipNumber() {
  6. iter.unreadByte()
  7. iter.ReadFloat32()
  8. }
  9. }
  10. func (iter *Iterator) trySkipNumber() bool {
  11. dotFound := false
  12. for i := iter.head; i < iter.tail; i++ {
  13. c := iter.buf[i]
  14. switch c {
  15. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  16. case '.':
  17. if dotFound {
  18. iter.ReportError("validateNumber", `more than one dot found in number`)
  19. return true // already failed
  20. }
  21. if i+1 == iter.tail {
  22. return false
  23. }
  24. c = iter.buf[i+1]
  25. switch c {
  26. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  27. default:
  28. iter.ReportError("validateNumber", `missing digit after dot`)
  29. return true // already failed
  30. }
  31. dotFound = true
  32. default:
  33. switch c {
  34. case ',', ']', '}', ' ', '\t', '\n', '\r':
  35. if iter.head == i {
  36. return false // if - without following digits
  37. }
  38. iter.head = i
  39. return true // must be valid
  40. }
  41. return false // may be invalid
  42. }
  43. }
  44. return false
  45. }
  46. func (iter *Iterator) skipString() {
  47. if !iter.trySkipString() {
  48. iter.unreadByte()
  49. iter.ReadString()
  50. }
  51. }
  52. func (iter *Iterator) trySkipString() bool {
  53. for i := iter.head; i < iter.tail; i++ {
  54. c := iter.buf[i]
  55. if c == '"' {
  56. iter.head = i + 1
  57. return true // valid
  58. } else if c == '\\' {
  59. return false
  60. } else if c < ' ' {
  61. iter.ReportError("trySkipString",
  62. fmt.Sprintf(`invalid control character found: %d`, c))
  63. return true // already failed
  64. }
  65. }
  66. return false
  67. }
  68. func (iter *Iterator) skipObject() {
  69. iter.unreadByte()
  70. iter.ReadObjectCB(func(iter *Iterator, field string) bool {
  71. iter.Skip()
  72. return true
  73. })
  74. }
  75. func (iter *Iterator) skipArray() {
  76. iter.unreadByte()
  77. iter.ReadArrayCB(func(iter *Iterator) bool {
  78. iter.Skip()
  79. return true
  80. })
  81. }