2026-11-16 14:11:02 +00:00
|
|
|
// Package jsonnumber converts exact integral JSON tokens without float rounding.
|
|
|
|
|
package jsonnumber
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-17 04:22:20 +00:00
|
|
|
"encoding/json"
|
2026-11-16 14:11:02 +00:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-17 04:22:20 +00:00
|
|
|
// Integer accepts exact JSON integer values within the native integer range.
|
|
|
|
|
type Integer int
|
|
|
|
|
|
|
|
|
|
func (n *Integer) UnmarshalJSON(raw []byte) error {
|
|
|
|
|
if value, ok := Int(string(raw)); ok {
|
|
|
|
|
*n = Integer(value)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return json.Unmarshal(raw, (*int)(n))
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 14:11:02 +00:00
|
|
|
// Int accepts lexical integers and equivalent decimal and exponent spellings
|
|
|
|
|
// in the native integer range. value must be a valid JSON number token.
|
|
|
|
|
func Int(value string) (int, bool) {
|
|
|
|
|
if integer, err := strconv.Atoi(value); err == nil {
|
|
|
|
|
return integer, true
|
|
|
|
|
}
|
|
|
|
|
digits, ok := integralDecimal(value)
|
|
|
|
|
if !ok {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
integer, err := strconv.Atoi(digits)
|
|
|
|
|
return integer, err == nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PositiveIndex accepts only references in [1, maximum]. Comparison with the
|
|
|
|
|
// requested range precedes conversion to the local integer index.
|
|
|
|
|
func PositiveIndex(value string, maximum int) (int, bool) {
|
|
|
|
|
if strings.ContainsAny(value, ".eE") {
|
|
|
|
|
var ok bool
|
|
|
|
|
if value, ok = integralDecimal(value); !ok {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if maximum < 1 || value == "" || value[0] < '1' || value[0] > '9' {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
limit := strconv.Itoa(maximum)
|
|
|
|
|
if len(value) > len(limit) || (len(value) == len(limit) && value > limit) {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
index, err := strconv.Atoi(value)
|
|
|
|
|
return index, err == nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func integralDecimal(value string) (string, bool) {
|
|
|
|
|
if value == "" || (value[0] != '-' && (value[0] < '0' || value[0] > '9')) || !strings.ContainsAny(value, ".eE") {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
digits, negative := strings.CutPrefix(value, "-")
|
|
|
|
|
var exponentText string
|
|
|
|
|
if i := strings.IndexAny(digits, "eE"); i >= 0 {
|
|
|
|
|
exponentText, digits = digits[i+1:], digits[:i]
|
|
|
|
|
}
|
|
|
|
|
whole, fraction, _ := strings.Cut(digits, ".")
|
|
|
|
|
digits = strings.TrimLeft(whole+fraction, "0")
|
|
|
|
|
if digits == "" {
|
|
|
|
|
return "0", true
|
|
|
|
|
}
|
|
|
|
|
var exponent int
|
|
|
|
|
if exponentText != "" {
|
|
|
|
|
var err error
|
|
|
|
|
exponent, err = strconv.Atoi(exponentText)
|
|
|
|
|
if err != nil || exponent < -len(digits) || exponent > len(fraction)+19 {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
shift := exponent - len(fraction)
|
|
|
|
|
if shift < 0 {
|
|
|
|
|
// A negative shift is integral only if every discarded digit is zero.
|
|
|
|
|
if -shift > len(digits)-len(strings.TrimRight(digits, "0")) {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
digits = digits[:len(digits)+shift]
|
|
|
|
|
} else {
|
|
|
|
|
if len(digits)+shift > 19 {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
digits += strings.Repeat("0", shift)
|
|
|
|
|
}
|
|
|
|
|
if negative {
|
|
|
|
|
digits = "-" + digits
|
|
|
|
|
}
|
|
|
|
|
return digits, true
|
|
|
|
|
}
|