Documentation ¶
Index ¶
- Variables
- func As[T xmath.Numeric](value Int) T
- func NewEvaluator(resolver eval.VariableResolver) *eval.Evaluator
- func ResetIfOutOfRange[T xmath.Numeric | Int](value, minValue, maxValue, defValue T) T
- type DP
- type Fraction
- type Int
- func ApplyRounding(value Int, roundDown bool) Int
- func EvaluateToNumber(expression string, resolver eval.VariableResolver) Int
- func Extract(in string) (value Int, remainder string)
- func From[T xmath.Numeric](value T) Int
- func FromString(value string) (Int, error)
- func FromStringForced(value string) Int
- type Length
- type LengthUnit
- func (enum LengthUnit) EnsureValid() LengthUnit
- func (enum LengthUnit) Format(length Length) string
- func (enum LengthUnit) Key() string
- func (enum LengthUnit) MarshalText() (text []byte, err error)
- func (enum LengthUnit) String() string
- func (enum LengthUnit) ToInches(length Int) Int
- func (enum *LengthUnit) UnmarshalText(text []byte) error
- type Weight
- type WeightUnit
- func (enum WeightUnit) EnsureValid() WeightUnit
- func (enum WeightUnit) Format(weight Weight) string
- func (enum WeightUnit) Key() string
- func (enum WeightUnit) MarshalText() (text []byte, err error)
- func (enum WeightUnit) String() string
- func (enum WeightUnit) ToPounds(weight Int) Int
- func (enum *WeightUnit) UnmarshalText(text []byte) error
Constants ¶
This section is empty.
Variables ¶
var ( EvalOperators = eval.FixedOperators[DP](true) EvalFuncs = eval.FixedFunctions[DP]() )
The evaluator operators and functions that will be used when calling NewEvaluator().
var ( Min = Int(f64.Min) NegPointEight = FromStringForced("-0.8") Twentieth = FromStringForced("0.05") PointZeroSix = FromStringForced("0.06") PointZeroSeven = FromStringForced("0.07") PointZeroEight = FromStringForced("0.08") PointZeroNine = FromStringForced("0.09") Tenth = FromStringForced("0.1") PointOneTwo = FromStringForced("0.12") Eighth = FromStringForced("0.125") PointOneFive = FromStringForced("0.15") Fifth = FromStringForced("0.2") Quarter = FromStringForced("0.25") ThreeTenths = FromStringForced("0.3") TwoFifths = FromStringForced("0.4") Half = FromStringForced("0.5") ThreeFifths = FromStringForced("0.6") SevenTenths = FromStringForced("0.7") ThreeQuarters = FromStringForced("0.75") FourFifths = FromStringForced("0.8") One = From(1) OnePointOne = FromStringForced("1.1") OnePointTwo = FromStringForced("1.2") OneAndAQuarter = FromStringForced("1.25") OneAndAHalf = FromStringForced("1.5") Two = From(2) TwoAndAHalf = FromStringForced("2.5") Three = From(3) ThreeAndAHalf = FromStringForced("3.5") Four = From(4) Five = From(5) Six = From(6) Seven = From(7) Eight = From(8) Nine = From(9) Ten = From(10) Twelve = From(12) Fifteen = From(15) Nineteen = From(19) Twenty = From(20) TwentyFour = From(24) TwentyFive = From(25) ThirtySix = From(36) Thirty = From(30) Forty = From(40) Fifty = From(50) Seventy = From(70) Eighty = From(80) NinetyNine = From(99) Hundred = From(100) Thousand = From(1000) MillionMinusOne = From(999999) BillionMinusOne = From(999999999) MaxBasePoints = MillionMinusOne Max = Int(f64.Max) )
Common values that can be reused.
var DebugVariableResolver = false
DebugVariableResolver produces debug output for the variable resolver when enabled.
var LengthUnits = []LengthUnit{ FeetAndInches, Inch, Feet, Yard, Mile, Centimeter, Kilometer, Meter, }
LengthUnits holds all possible values.
var WeightUnits = []WeightUnit{ Pound, PoundAlt, Ounce, Ton, TonAlt, Kilogram, Gram, }
WeightUnits holds all possible values.
Functions ¶
func NewEvaluator ¶
func NewEvaluator(resolver eval.VariableResolver) *eval.Evaluator
NewEvaluator creates a new evaluator whose number type is an Int.
func ResetIfOutOfRange ¶
ResetIfOutOfRange checks the value and if it is lower than minValue or greater than maxValue, returns defValue, otherwise returns value.
Types ¶
type Fraction ¶
Fraction is an alias for the fixed-point fractional type we are using.
func NewFraction ¶
NewFraction creates a new fractional value from a string.
type Int ¶
Int is an alias for the fixed-point type we are using.
func ApplyRounding ¶
ApplyRounding rounds in the positive direction of roundDown is false, or in the negative direction if roundDown is true.
func EvaluateToNumber ¶
func EvaluateToNumber(expression string, resolver eval.VariableResolver) Int
EvaluateToNumber evaluates the provided expression and returns a number.
func Extract ¶
Extract a leading value from a string. If a value is found, it is returned along with the portion of the string that was unused. If a value is not found, then 0 is returned along with the original string.
func FromString ¶
FromString creates an Int from a string.
func FromStringForced ¶
FromStringForced creates an Int from a string, ignoring any conversion inaccuracies.
type Length ¶ added in v5.16.2
type Length Int
Length contains a fixed-point value in inches. Conversions to/from metric are done using the simplified Length metric conversion of 1 yd = 1 meter. For consistency, all metric lengths are converted to meters, then to yards, rather than the variations at different lengths that the Length rules suggest.
func LengthFromInteger ¶ added in v5.16.2
func LengthFromInteger[T constraints.Integer](value T, unit LengthUnit) Length
LengthFromInteger creates a new Length.
func LengthFromString ¶ added in v5.16.2
func LengthFromString(text string, defaultUnits LengthUnit) (Length, error)
LengthFromString creates a new Length. May have any of the known Units suffixes, a feet and inches format (e.g. 6'2"), or no notation at all, in which case defaultUnits is used.
func LengthFromStringForced ¶ added in v5.16.2
func LengthFromStringForced(text string, defaultUnits LengthUnit) Length
LengthFromStringForced creates a new Length. May have any of the known Units suffixes, a feet and inches format (e.g. 6'2"), or no notation at all, in which case defaultUnits is used.
func (Length) MarshalJSON ¶ added in v5.16.2
MarshalJSON implements json.Marshaler.
func (*Length) UnmarshalJSON ¶ added in v5.16.2
UnmarshalJSON implements json.Unmarshaler.
type LengthUnit ¶ added in v5.19.0
type LengthUnit byte
LengthUnit holds the length unit type. Note that conversions to/from metric are done using the simplified GURPS metric conversion of 1 yd = 1 meter. For consistency, all metric lengths are converted to meters, then to yards, rather than the variations at different lengths that the GURPS rules suggest.
const ( FeetAndInches LengthUnit = iota Inch Feet Yard Mile Centimeter Kilometer Meter )
Possible values.
const LastLengthUnit LengthUnit = Meter
LastLengthUnit is the last valid value.
func ExtractLengthUnit ¶ added in v5.19.0
func ExtractLengthUnit(str string) LengthUnit
ExtractLengthUnit extracts the value from a string.
func (LengthUnit) EnsureValid ¶ added in v5.19.0
func (enum LengthUnit) EnsureValid() LengthUnit
EnsureValid ensures this is of a known value.
func (LengthUnit) Format ¶ added in v5.19.0
func (enum LengthUnit) Format(length Length) string
Format the length for this LengthUnit.
func (LengthUnit) Key ¶ added in v5.19.0
func (enum LengthUnit) Key() string
Key returns the key used in serialization.
func (LengthUnit) MarshalText ¶ added in v5.19.0
func (enum LengthUnit) MarshalText() (text []byte, err error)
MarshalText implements the encoding.TextMarshaler interface.
func (LengthUnit) String ¶ added in v5.19.0
func (enum LengthUnit) String() string
String implements fmt.Stringer.
func (LengthUnit) ToInches ¶ added in v5.19.0
func (enum LengthUnit) ToInches(length Int) Int
ToInches converts the length in this LengthUnit to inches.
func (*LengthUnit) UnmarshalText ¶ added in v5.19.0
func (enum *LengthUnit) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.
type Weight ¶ added in v5.16.2
type Weight Int
Weight contains a fixed-point value in pounds.
func WeightFromInteger ¶ added in v5.16.2
func WeightFromInteger[T constraints.Integer](value T, unit WeightUnit) Weight
WeightFromInteger creates a new Weight.
func WeightFromString ¶ added in v5.16.2
func WeightFromString(text string, defaultUnits WeightUnit) (Weight, error)
WeightFromString creates a new Weight. May have any of the known Weight suffixes or no notation at all, in which case defaultUnits is used.
func WeightFromStringForced ¶ added in v5.16.2
func WeightFromStringForced(text string, defaultUnits WeightUnit) Weight
WeightFromStringForced creates a new Weight. May have any of the known Weight suffixes or no notation at all, in which case defaultUnits is used.
func (Weight) MarshalJSON ¶ added in v5.16.2
MarshalJSON implements json.Marshaler.
func (*Weight) UnmarshalJSON ¶ added in v5.16.2
UnmarshalJSON implements json.Unmarshaler.
type WeightUnit ¶ added in v5.19.0
type WeightUnit byte
WeightUnit holds the weight unit type. Note that conversions to/from metric are done using the simplified GURPS metric conversion of 1 lb = 0.5kg. For consistency, all metric weights are converted to kilograms, then to pounds, rather than the variations at different weights that the GURPS rules suggest.
const ( Pound WeightUnit = iota PoundAlt Ounce Ton TonAlt Kilogram Gram )
Possible values.
const LastWeightUnit WeightUnit = Gram
LastWeightUnit is the last valid value.
func ExtractWeightUnit ¶ added in v5.19.0
func ExtractWeightUnit(str string) WeightUnit
ExtractWeightUnit extracts the value from a string.
func TrailingWeightUnitFromString ¶ added in v5.19.0
func TrailingWeightUnitFromString(s string, defUnits WeightUnit) WeightUnit
TrailingWeightUnitFromString extracts a trailing WeightUnit from a string.
func (WeightUnit) EnsureValid ¶ added in v5.19.0
func (enum WeightUnit) EnsureValid() WeightUnit
EnsureValid ensures this is of a known value.
func (WeightUnit) Format ¶ added in v5.19.0
func (enum WeightUnit) Format(weight Weight) string
Format the weight for this WeightUnit.
func (WeightUnit) Key ¶ added in v5.19.0
func (enum WeightUnit) Key() string
Key returns the key used in serialization.
func (WeightUnit) MarshalText ¶ added in v5.19.0
func (enum WeightUnit) MarshalText() (text []byte, err error)
MarshalText implements the encoding.TextMarshaler interface.
func (WeightUnit) String ¶ added in v5.19.0
func (enum WeightUnit) String() string
String implements fmt.Stringer.
func (WeightUnit) ToPounds ¶ added in v5.19.0
func (enum WeightUnit) ToPounds(weight Int) Int
ToPounds the weight for this WeightUnit.
func (*WeightUnit) UnmarshalText ¶ added in v5.19.0
func (enum *WeightUnit) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.