iter_skip_strict.go 2.0 KB

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