// Package jsonnum reads JSON numbers by the grammar of RFC 8259, section 6, for the encoder and the decoder of // encoding/json and for jsontext. The callers write their own errors from where a number is invalid.
package jsonnum import ( ) // Place is where Scan found a number invalid. type Place uint8 const ( Valid Place = iota // the number is valid Start // the first digit, after the sign if any Fraction // the first digit after the decimal point Exponent // the first digit of the exponent, after its sign if any ) // Scan returns the length of the longest JSON number which b starts with, and Valid; or, if the number is // invalid, the position of the byte where it is, which is len(b) if b ends there, and the place of that byte. // The byte after a valid number is not looked at: it may be anything, which the caller checks. func ( []byte) (int, Place) { := 0 if < len() && [] == '-' { ++ } switch { case == len(): return , Start case [] == '0': ++ case '1' <= [] && [] <= '9': = ScanDigits(, +1) default: return , Start } if < len() && [] == '.' { if ++; == len() || [] < '0' || '9' < [] { return , Fraction } = ScanDigits(, +1) } if < len() && ([] == 'e' || [] == 'E') { if ++; < len() && ([] == '+' || [] == '-') { ++ } if == len() || [] < '0' || '9' < [] { return , Exponent } = ScanDigits(, +1) } return , Valid } // IsValid reports whether b is one JSON number, without anything before or after it. It takes a byte at a time // by one lookup in a table of the states of the grammar, nearly as cheap as a check of the bytes alone, for the // short numbers which a json.Number has: the state is the index of its row, which the byte is added to. func ( []byte) bool { := uint16(stateStart * 256) for , := range { = transitions[uint()+uint()] } return accepting[>>8] } // NonDigitTops has the top bit of each byte of w which is not a digit. It is small enough to be inlined, as // GatherTops is. func ( uint64) uint64 { const , = 0x0101010101010101, 0x8080808080808080 // the top bit of each byte which is 0x3A or more, or less than 0x30, of its low 7 bits, or which has its own: // adding to the low 7 bits, and subtracting from the bytes with the top bit, carries between no bytes. return (((&^)+*(0x80-0x3a))|)& | &^((|)-*0x30) } // GatherTops gathers the top bits of the bytes of m, which has no other bits, into a byte: of the byte i in the // bit i. func ( uint64) uint8 { return uint8(( >> 7) * 0x0102040810204080 >> 56) } // The states of IsValid: where a number is, after the bytes before. const ( stateStart = iota // before the sign stateSign // after the sign stateZero // after the integer 0 stateInt // in the digits of an integer which starts with 1 to 9 stateDot // after the decimal point stateFrac // in the digits of the fraction stateE // after e or E stateExpSign // after the sign of the exponent stateExp // in the digits of the exponent stateInvalid // not a number, whatever follows numStates ) // accepting are the states of IsValid at the end of a number. var accepting = [numStates]bool{stateZero: true, stateInt: true, stateFrac: true, stateExp: true} // transitions are the states of IsValid after each byte from each state, times 256, at the state times 256 plus // the byte; the bytes which are not in the grammar from a state lead to stateInvalid. var transitions = func() [numStates * 256]uint16 { var [numStates * 256]uint16 var [numStates][256]uint8 for := range { for := range [] { [][] = stateInvalid } } := func(, uint8) { for := '0'; <= '9'; ++ { [][] = } } [stateStart]['-'] = stateSign for , := range []uint8{stateStart, stateSign} { []['0'] = stateZero for := '1'; <= '9'; ++ { [][] = stateInt } } (stateInt, stateInt) for , := range []uint8{stateZero, stateInt} { []['.'] = stateDot []['e'], []['E'] = stateE, stateE } (stateDot, stateFrac) (stateFrac, stateFrac) [stateFrac]['e'], [stateFrac]['E'] = stateE, stateE [stateE]['+'], [stateE]['-'] = stateExpSign, stateExpSign (stateE, stateExp) (stateExpSign, stateExp) (stateExp, stateExp) for := range { for := range [] { [*256+] = uint16([][]) * 256 } } return }() // End returns the end of the valid JSON number which starts b, if a byte which ends it follows it in b, or -1: // at an error, and at a number which may continue after b. It is the fast check of a scanner which reads its // input by parts. func ( []byte) int { := 0 if < len() && [] == '-' { ++ } switch { case == len(): return -1 case [] == '0': ++ case '1' <= [] && [] <= '9': = ScanDigits(, +1) default: return -1 } if < len() && [] == '.' { if ++; == len() || [] < '0' || '9' < [] { return -1 } = ScanDigits(, +1) } if < len() && ([] == 'e' || [] == 'E') { if ++; < len() && ([] == '+' || [] == '-') { ++ } if == len() || [] < '0' || '9' < [] { return -1 } = ScanDigits(, +1) } if == len() { return -1 } return } // ScanDigits returns the position of the first byte of b from i which is not a digit, or len(b). Long runs of // digits, as the fractions of floats have, are looked at 8 bytes at a time. func ( []byte, int) int { for +8 <= len() { // a digit XOR '0' is below 10: adding 0x76 to each byte of the low 7 bits carries into the top bit from // 10 on, and any byte of 0x80 or more has its top bit already. := binary.LittleEndian.Uint64([:]) ^ 0x3030303030303030 if := (( &^ 0x8080808080808080) + 0x7676767676767676 | ) & 0x8080808080808080; != 0 { return + bits.TrailingZeros64()/8 } += 8 } for < len() && '0' <= [] && [] <= '9' { ++ } return }