github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_skip_number.go (about) 1 package jzon 2 3 import ( 4 "io" 5 ) 6 7 func (it *Iterator) skipExponentPart() (err error) { 8 c, err := it.nextByte() 9 if err != nil { 10 return err 11 } 12 it.head++ 13 if c == '+' || c == '-' { 14 c, err = it.nextByte() 15 if err != nil { 16 return err 17 } 18 it.head++ 19 } 20 if intDigits[c] == invalidDigit { 21 return InvalidDigitError{c: c} 22 } 23 for { 24 i := it.head 25 for ; i < it.tail; i++ { 26 c = it.buffer[i] 27 digit := intDigits[c] 28 if digit == invalidDigit { 29 it.head = i 30 return nil 31 } 32 } 33 // i == it.tail 34 it.head = i 35 if err = it.readMore(); err != nil { 36 if err == io.EOF { 37 return nil 38 } 39 return err 40 } 41 } 42 } 43 44 func (it *Iterator) skipFractionPart() (err error) { 45 c, err := it.nextByte() 46 if err != nil { 47 return err 48 } 49 if intDigits[c] == invalidDigit { 50 return InvalidDigitError{c: c} 51 } 52 it.head++ 53 for { 54 i := it.head 55 for ; i < it.tail; i++ { 56 c = it.buffer[i] 57 digit := floatDigits[c] 58 if digit < 0 { 59 switch digit { 60 case expInNumber: 61 it.head = i + 1 62 return it.skipExponentPart() 63 default: 64 it.head = i 65 return nil 66 } 67 } 68 } 69 // i == it.tail 70 it.head = i 71 if err = it.readMore(); err != nil { 72 if err == io.EOF { 73 return nil 74 } 75 return err 76 } 77 } 78 } 79 80 // make sure that c is '-' or '0'~'9' before calling this method 81 // (the call of nextToken before this method return NumberValue) 82 func skipNumber(it *Iterator, c byte) (err error) { 83 if c == '-' { 84 c, err = it.nextByte() 85 if err != nil { 86 return 87 } 88 if intDigits[c] == invalidDigit { 89 return InvalidDigitError{c: c} 90 } 91 it.head++ 92 } 93 // positive 94 // here the c can only be '0'~'9' 95 if c == '0' { 96 if it.head == it.tail { 97 if err = it.readMore(); err != nil { 98 if err == io.EOF { 99 return nil 100 } 101 return err 102 } 103 } 104 switch floatDigits[it.buffer[it.head]] { 105 case dotInNumber: 106 it.head++ 107 return it.skipFractionPart() 108 case expInNumber: 109 it.head++ 110 return it.skipExponentPart() 111 default: 112 return nil 113 } 114 } else { 115 for { 116 i := it.head 117 for ; i < it.tail; i++ { 118 c = it.buffer[i] 119 digit := floatDigits[c] 120 if digit < 0 { 121 switch digit { 122 case dotInNumber: 123 it.head = i + 1 124 return it.skipFractionPart() 125 case expInNumber: 126 it.head = i + 1 127 return it.skipExponentPart() 128 default: 129 it.head = i 130 return nil 131 } 132 } 133 } 134 // i == it.tail 135 it.head = i 136 if err = it.readMore(); err != nil { 137 if err == io.EOF { 138 return nil 139 } 140 return err 141 } 142 } 143 } 144 } 145 146 // SkipNumber skips a number 147 func (it *Iterator) SkipNumber() error { 148 c, err := it.nextToken() 149 if err != nil { 150 return err 151 } 152 if valueTypeMap[c] != NumberValue { 153 return UnexpectedByteError{got: c} 154 } 155 it.head++ 156 return skipNumber(it, c) 157 }