Validate internal decisions and reconcile exact model references
This commit is contained in:
82
internal/core/jsonnumber/integer.go
Normal file
82
internal/core/jsonnumber/integer.go
Normal file
@@ -0,0 +1,82 @@
|
||||
// Package jsonnumber converts exact integral JSON tokens without float rounding.
|
||||
package jsonnumber
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user