package jximport ()// Num represents number, which can be raw json number or number string.//// Same as Raw, but with number invariants.//// Examples://// 123.45 // Str: false, IsInt: false// "123.45" // Str: true, IsInt: false// "12345" // Str: true, IsInt: true// 12345 // Str: false, IsInt: truetypeNum []bytefunc ( Num) () Decoder { := 0 := len()if .Str() { = 1 -- }returnDecoder{buf: ,head: ,tail: , }}// Str reports whether Num is string number.func ( Num) () bool {returnlen() > 0 && [0] == '"'}func ( Num) () ( int, error) {// Allow decoding floats with zero fractional, like 1.0 as 1. = -1for , := range {if == '.' { = continue }if == -1 {continue }switch {case'0', '"': // okdefault:return , errors.Errorf("non-zero fractional part %q at %d", , ) } }return , nil}// Int64 decodes number as a signed 64-bit integer.// Works on floats with zero fractional part.func ( Num) () (int64, error) { , := .floatAsInt()if != nil {return0, errors.Wrap(, "float as int") } := .dec()if != -1 { .tail = }return .Int64()}// IsInt reports whether number is integer.func ( Num) () bool {iflen() == 0 {returnfalse } := if [0] == '"' { = [1 : len()-1] }if [0] == '-' { = [1:] }for , := range {switch {case'0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // okdefault:returnfalse } }returntrue}// Uint64 decodes number as an unsigned 64-bit integer.// Works on floats with zero fractional part.func ( Num) () (uint64, error) { , := .floatAsInt()if != nil {return0, errors.Wrap(, "float as int") } := .dec()if != -1 { .tail = }return .UInt64()}// Float64 decodes number as 64-bit floating point.func ( Num) () (float64, error) { := .dec()return .Float64()}// Equal reports whether numbers are strictly equal, including their formats.func ( Num) ( Num) bool {returnbytes.Equal(, )}func ( Num) () string {iflen() == 0 {return"<invalid>" }returnstring()}// Format implements fmt.Formatter.func ( Num) ( fmt.State, rune) {switch {case's', 'v': _, _ = .Write()case'd': , := .Int64()if != nil {fmt.Fprintf(, "%%!invalid(Num=%s)", .String())return } := big.NewInt() .Format(, )case'f': , := .Float64()if != nil {fmt.Fprintf(, "%%!invalid(Num=%s)", .String())return } := big.NewFloat() .Format(, ) }}// Sign reports sign of number.//// 0 is zero, 1 is positive, -1 is negative.func ( Num) () int {iflen() == 0 {return0 } := [0]if == '"' {iflen() < 2 {return0 } = [1] }switch {case'-':return -1case'0':return0default:return1 }}// Positive reports whether number is positive.func ( Num) () bool { return .Sign() > 0 }// Negative reports whether number is negative.func ( Num) () bool { return .Sign() < 0 }// Zero reports whether number is zero.func ( Num) () bool {iflen() == 0 {returnfalse }iflen() == 1 {return [0] == '0' }for , := range {switch {case'.', '0', '-':continuedefault:returnfalse } }returntrue}
The pages are generated with Goldsv0.6.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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds.