jsonnum

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 3 Imported by: 0

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

View Source
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

func End(b []byte) int

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

func GatherTops(m uint64) uint8

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

func IsFloatContinuation(c byte) bool

IsFloatContinuation reports whether c continues an integer as a number which is not an integer.

func IsValid

func IsValid(b []byte) bool

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

func NonDigitTops(w uint64) uint64

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

func ParseDigits(buf []byte, cursor int64) (uint64, int64, int)

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

func ParseFloat(b []byte) (float64, bool)

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

func ParseFloatTerminated(buf []byte, cursor int64) (float64, int64, bool)

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

func ParseUint(b []byte) (uint64, bool)

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

func ScanDigits(b []byte, i int) int

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 TwentyDigits

func TwentyDigits(buf []byte, start int64) (uint64, bool)

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.

Types

type Place

type Place uint8

Place is where Scan found a number invalid.

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
)

func Scan

func Scan(b []byte) (int, Place)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址