Documentation
¶
Overview ¶
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.
Index ¶
- Constants
- func End(b []byte) int
- func GatherTops(m uint64) uint8
- func IsFloatContinuation(c byte) bool
- func IsValid(b []byte) bool
- func NonDigitTops(w uint64) uint64
- func ParseDigits(buf []byte, cursor int64) (uint64, int64, int)
- func ParseFloat(b []byte) (float64, bool)
- func ParseFloatTerminated(buf []byte, cursor int64) (float64, int64, bool)
- func ParseUint(b []byte) (uint64, bool)
- func ScanDigits(b []byte, i int) int
- func TwentyDigits(buf []byte, start int64) (uint64, bool)
- type Place
Constants ¶
const MaxUint64Digits = 19
MaxUint64Digits is the number of the digits which a uint64 holds whatever they are: 19 nines are less than 1<<64.
Variables ¶
This section is empty.
Functions ¶
func End ¶
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 GatherTops ¶
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 IsFloatContinuation ¶
IsFloatContinuation reports whether c continues an integer as a number which is not an integer.
func IsValid ¶
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 NonDigitTops ¶
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 ParseDigits ¶
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 ParseFloat ¶
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 ParseFloatTerminated ¶
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 ParseUint ¶
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 ScanDigits ¶
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.
Types ¶
type Place ¶
type Place uint8
Place is where Scan found a number invalid.
func Scan ¶
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.