num

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package num provides XSD numeric parsing and comparison helpers. Returned values may share backing storage with input slices.

Index

Constants

This section is empty.

Variables

View Source
var (
	// IntZero is the zero value reused by numeric parsers.
	IntZero = Int{Sign: 0, Digits: zeroDigits}

	// Signed integer bounds reused during range checks.
	MinInt8  = Int{Sign: -1, Digits: []byte("128")}
	MaxInt8  = Int{Sign: 1, Digits: []byte("127")}
	MinInt16 = Int{Sign: -1, Digits: []byte("32768")}
	MaxInt16 = Int{Sign: 1, Digits: []byte("32767")}
	MinInt32 = Int{Sign: -1, Digits: []byte("2147483648")}
	MaxInt32 = Int{Sign: 1, Digits: []byte("2147483647")}
	MinInt64 = Int{Sign: -1, Digits: []byte("9223372036854775808")}
	MaxInt64 = Int{Sign: 1, Digits: []byte("9223372036854775807")}

	// Unsigned integer bounds reused during range checks.
	MaxUint8  = Int{Sign: 1, Digits: []byte("255")}
	MaxUint16 = Int{Sign: 1, Digits: []byte("65535")}
	MaxUint32 = Int{Sign: 1, Digits: []byte("4294967295")}
	MaxUint64 = Int{Sign: 1, Digits: []byte("18446744073709551615")}
)

Functions

func CompareFloat added in v0.0.24

func CompareFloat(a float64, ac FloatClass, b float64, bc FloatClass) (int, bool)

CompareFloat compares two parsed float/double values. The boolean result is false when either side is NaN (unordered).

func EncodeDecKey

func EncodeDecKey(dst []byte, v Dec) []byte

EncodeDecKey appends the canonical decimal key encoding to dst.

func ParseDec

func ParseDec(b []byte) (Dec, *ParseError)

ParseDec parses a decimal lexical value into a Dec. The returned Dec may share backing storage with b.

func ParseDecInto

func ParseDecInto(b, dst []byte) (Dec, []byte, *ParseError)

ParseDecInto parses a decimal lexical value into a Dec using dst as scratch. The returned Dec may share backing storage with b or dst.

func ParseFloat added in v0.0.24

func ParseFloat(b []byte, bits int) (float64, FloatClass, *ParseError)

ParseFloat parses an XSD float/double lexical value for the requested bit size.

func ParseFloat32

func ParseFloat32(b []byte) (float32, FloatClass, *ParseError)

ParseFloat32 parses an XSD float lexical value.

func ParseInt

func ParseInt(b []byte) (Int, *ParseError)

ParseInt parses an integer lexical value into an Int. The returned Int may share backing storage with b.

Types

type Dec

type Dec struct {
	Coef  []byte
	Scale uint32
	Sign  int8
}

Dec represents an arbitrary-precision decimal.

func AddDecInt added in v0.0.22

func AddDecInt(dec Dec, delta Int) Dec

AddDecInt adds an integer delta to a decimal value without narrowing.

func DecFromScaledInt added in v0.0.19

func DecFromScaledInt(val Int, scale uint32) Dec

DecFromScaledInt converts an Int scaled by 10^scale into a Dec.

func (Dec) Compare

func (a Dec) Compare(b Dec) int

Compare compares two Dec values.

func (Dec) RenderCanonical

func (a Dec) RenderCanonical(dst []byte) []byte

RenderCanonical appends the canonical lexical form to dst.

type FloatClass

type FloatClass uint8

FloatClass identifies the ordering class of a float value.

const (
	FloatFinite FloatClass = iota
	FloatPosInf
	FloatNegInf
	FloatNaN
)

type Int

type Int struct {
	Digits []byte
	Sign   int8
}

Int represents an arbitrary-precision integer.

func Add

func Add(a, b Int) Int

Add returns the sum of two Int values.

func DecToScaledInt added in v0.0.19

func DecToScaledInt(dec Dec, scale uint32) Int

DecToScaledInt converts a Dec into an Int scaled by 10^scale. It truncates fractional digits when scale < dec.Scale.

func DecToScaledIntExact added in v0.0.19

func DecToScaledIntExact(dec Dec, scale uint32) (Int, error)

DecToScaledIntExact converts a Dec into an Int scaled by 10^scale. It returns an error if non-zero fractional digits would be lost.

func FromInt64

func FromInt64(v int64) Int

FromInt64 returns an Int representation of an int64.

func FromUint64

func FromUint64(v uint64) Int

FromUint64 returns an Int representation of a non-negative uint64.

func Mul

func Mul(a, b Int) Int

Mul returns the product of two Int values.

func Pow10Int added in v0.0.22

func Pow10Int(scale uint32) Int

Pow10Int returns 10^scale as an Int.

func (Int) AsDec

func (a Int) AsDec() Dec

AsDec converts an Int to a Dec.

func (Int) Compare

func (a Int) Compare(b Int) int

Compare compares two Int values.

func (Int) CompareDec

func (a Int) CompareDec(b Dec) int

CompareDec compares an Int to a Dec.

func (Int) RenderCanonical

func (a Int) RenderCanonical(dst []byte) []byte

RenderCanonical appends the canonical lexical form to dst.

type ParseErrKind

type ParseErrKind uint8

ParseErrKind identifies a parse failure category.

const (
	ParseInvalid ParseErrKind = iota
	ParseEmpty
	ParseBadChar
	ParseMultipleSigns
	ParseMultipleDots
	ParseNoDigits
)

func (ParseErrKind) String

func (k ParseErrKind) String() string

String returns a stable label for the parse error kind.

type ParseError

type ParseError struct {
	Kind ParseErrKind
}

ParseError represents a numeric parse failure.

func ValidateFloatLexical

func ValidateFloatLexical(b []byte) *ParseError

ValidateFloatLexical checks whether a float/double lexical form is valid.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns the formatted error message.

Jump to

Keyboard shortcuts

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