package jsonnum// The numbers are parsed in one pass: the digits are accumulated as they are read. The parsers read a buffer// which a byte that can't be in a number ends, as the nul byte which the buffer of the decoder of encoding/json// ends with, without checks of the end of the buffer.// MaxUint64Digits is the number of the digits which a uint64 holds whatever they are: 19 nines are less than// 1<<64.constMaxUint64Digits = 19// ParseDigits reads the digits at cursor, which starts a number after its sign, in a terminated buffer, and// returns their value, the position after them and their number. A number which starts with 0 has no other// digit: the digit after it, if any, is not read. The value is exact for up to MaxUint64Digits digits; the value of// more may wrap around, and be any value, 0 too, which only their number tells.func ( []byte, int64) (uint64, int64, int) {if [] == '0' {return0, + 1, 1 } := varuint64for { := [] - '0'if > 9 {break } = *10 + uint64() ++ }return , , int( - )}// TwentyDigits is the value of the 20 digits at start, which ParseDigits doesn't take exactly, and true if it// fits a uint64: the value of the first 19 is exact, and the last one fits if the whole is less than 1<<64.func ( []byte, int64) (uint64, bool) {varuint64for := int64(0); < MaxUint64Digits; ++ { = *10 + uint64([+]-'0') } := uint64([+MaxUint64Digits] - '0')if > (1<<64-1)/10 || ( == (1<<64-1)/10 && > (1<<64-1)%10) {return0, false }return *10 + , true}// ParseUint is the value of b, which is the digits of an integer without a sign and nothing else, and true; or// false if b is not one, as an integer which starts with 0 and has more digits, or its value overflows a uint64.func ( []byte) (uint64, bool) {iflen() == 0 || len() > MaxUint64Digits+1 {return0, false }var [MaxUint64Digits + 2]byte// the byte after b is 0copy([:], ) , , := ParseDigits([:], 0)switch {caseint() != len():return0, falsecase == MaxUint64Digits+1:returnTwentyDigits([:], 0) }return , true}// IsFloatContinuation reports whether c continues an integer as a number which is not an integer.func ( byte) bool {return == '.' || == 'e' || == 'E'}// float64pow10 are the powers of ten which a float64 holds exactly.var float64pow10 = [...]float64{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22,}// ParseFloatTerminated parses the number at cursor of a terminated buffer, as the JSON grammar has it, when its// mantissa has at most 19 digits: the float64 of a mantissa of at most 53 bits times or divided by a power of// ten which a float64 holds exactly is correctly rounded as it is ( Clinger's fast path ), and any other is// multiplied by the power of ten from a table ( mulPow10 ). The result is the one of strconv.ParseFloat, and the// position after the number. It returns false for a number which is not decided so, or anything which is not a// number by the grammar, which the caller parses as it did before.func ( []byte, int64) (float64, int64, bool) {// the start of the number, which significantDigits reads again in the guard after the exponent; a call before // it, in the branch of the fraction, which can derive the start, made the parsing of numbers of a few digits // slower := := [] == '-'if { ++ }if []-'0' > 9 {return0, 0, false }// The digits are counted by their positions, not by the value of the mantissa, which may wrap around past // MaxUint64Digits digits and be 0 after any digit past them. Their number counts the 0 of an integer part 0 and // the zeros of its fraction before the first digit which is not zero, which are not digits of the mantissa: a // number of more than MaxUint64Digits digits counts its digits again without them ( significantDigits ). , , := ParseDigits(, ) := 0if [] == '.' { ++ := for { := [] - '0'if > 9 {break } = *10 + uint64() ++ } := int( - )if == 0 {return0, 0, false } = - += }if := []; == 'e' || == 'E' { ++ := falseswitch [] {case'-': = true ++case'+': ++ } := := 0for { := [] - '0'if > 9 {break }if < 10000 { = *10 + int() } ++ }if == {return0, 0, false }if { = - } += }if > MaxUint64Digits && significantDigits(, ) > MaxUint64Digits {return0, 0, false }varfloat64if <= 1<<53 && >= -22 && <= 22 { = float64()if < 0 { /= float64pow10[-] } else { *= float64pow10[] } } else {varboolif , = mulPow10(, ); ! {return0, 0, false } }if { = - }return , , true}// significantDigits is the number of the digits of the mantissa of the number at start, which ParseFloatTerminated// read: the digits of its integer part and its fraction from the first one which is not zero. It is a function of// its own, which is not inlined, so that the numbers of a few digits, which don't need it, are parsed without a jump// over it.////go:noinlinefunc significantDigits( []byte, int64) int {if [] == '-' { ++ } := 0if [] == '0' {// the integer part starts with 0 only if it is 0, which is not a digit of the mantissa, nor are the zeros of // the fraction before its first digit which is not zero ++if [] == '.' { ++ }for [] == '0' { ++ } } else {for []-'0' <= 9 { ++ ++ }if [] == '.' { ++ } }for []-'0' <= 9 { ++ ++ }return}// maxParsedLen is the longest number which ParseFloat parses: a longer one has more digits than a mantissa of// ParseFloatTerminated holds, unless it has many zeros, which strconv parses.const maxParsedLen = 31// ParseFloat is ParseFloatTerminated of b, which is a JSON number and nothing else: it is copied to a terminated// buffer of its own. It returns false where strconv parses the number.func ( []byte) (float64, bool) {iflen() > maxParsedLen {return0, false }var [maxParsedLen + 1]byte// the byte after b is 0copy([:], ) , , := ParseFloatTerminated([:], 0)return , && int() == len()}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.