diff --git a/number/const.go b/number/const.go index 3a1ee97..808c1c9 100644 --- a/number/const.go +++ b/number/const.go @@ -11,16 +11,16 @@ var ( ) const ( - rSign = `(\+|-)` + rSign = `\+|-` rBSign = `(0|1)` rNb2 = `(0|1)` rNb8 = `[0-7]` rNb10 = `\d` rNb16 = `[0-9a-fA-F]` rNbN = `[0-9a-zA-Z]` - rBase2 = `(B|b)` - rBase8 = `(O|o)` - rBase16 = `(X|x)` + rBase2 = `B|b` + rBase8 = `O|o` + rBase16 = `X|x` rExp10 = `E|e` rExpN = `×\d+\^` ) @@ -28,23 +28,23 @@ const ( var ( rInt2 = fmt.Sprintf(`(%s)%s+`, rBase2, rNb2) rInt8 = fmt.Sprintf(`(%s)%s+`, rBase8, rNb8) - rInt10 = fmt.Sprintf(`(%s)%s+`, rSign, rNb10) - rInt16 = fmt.Sprintf(`(%s)%s+`, rBase8, rNb8) - rIntN = fmt.Sprintf(`\(%s+\)%s?%s+`, rNb10, rSign, rNbN) + rInt10 = fmt.Sprintf(`(%s)?%s+`, rSign, rNb10) + rInt16 = fmt.Sprintf(`(%s)%s+`, rBase16, rNb16) + rIntN = fmt.Sprintf(`\(%s+\)(%s)?%s+`, rNb10, rSign, rNbN) rIntB = fmt.Sprintf(`%s(%s|%s|%s)`, rBSign, rInt2, rInt8, rInt16) rInt = fmt.Sprintf(`(%s|%s|%s)`, rInt10, rIntN, rIntB) rDec2 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase2, rNb2, rNb2, rNb2) rDec8 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase8, rNb8, rNb8, rNb8) - rDec10 = fmt.Sprintf(`%s?(%s+\.%s*|\.%s+)`, rSign, rNb10, rNb10, rNb10) + rDec10 = fmt.Sprintf(`(%s)?(%s+\.%s*|\.%s+)`, rSign, rNb10, rNb10, rNb10) rDec16 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase16, rNb16, rNb16, rNb16) - rDecN = fmt.Sprintf(`\(%s+\)%s?(%s+\.%s*|\.%s+)`, rNb10, rSign, rNbN, rNbN, rNbN) + rDecN = fmt.Sprintf(`\(%s+\)(%s)?(%s+\.%s*|\.%s+)`, rNb10, rSign, rNbN, rNbN, rNbN) rDecB = fmt.Sprintf(`%s(%s|%s|%s)`, rBSign, rDec2, rDec8, rDec16) rDec = fmt.Sprintf(`(%s|%s|%s)`, rDec10, rDecN, rDecB) - rExponent10 = fmt.Sprintf(`(%s|%s)(%s)%s?%s+`, rInt10, rDec10, rExp10, rSign, rNb10) - rExponentN = fmt.Sprintf(`(%s|%s)%s%s?%s+`, rInt, rDec, rExpN, rSign, rNb10) - rAll10 = fmt.Sprintf(`(%s|%s)((%s)%s?%s+)?`, rInt10, rDec10, regExp10, rSign, rNb10) - rAllN = fmt.Sprintf(`(%s|%s)(%s%s?%s+)?`, rInt, rDec, rExpN, rSign, rNb10) + rExponent10 = fmt.Sprintf(`(%s|%s)(%s)(%s)?%s+`, rInt10, rDec10, rExp10, rSign, rNb10) + rExponentN = fmt.Sprintf(`(%s|%s)%s(%s)?%s+`, rInt, rDec, rExpN, rSign, rNb10) + rAll10 = fmt.Sprintf(`(%s|%s)((%s)(%s)?%s+)?`, rInt10, rDec10, regExp10, rSign, rNb10) + rAllN = fmt.Sprintf(`(%s|%s)(%s(%s)?%s+)?`, rInt, rDec, rExpN, rSign, rNb10) rAll = fmt.Sprintf(`(%s|%s)`, rAll10, rAllN) ) @@ -56,7 +56,7 @@ var ( regDec = regexp.MustCompile(fmt.Sprintf(`^%s$`, rDec)) regExp10 = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponent10)) - regExpN = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExpN)) + regExpN = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponentN)) regFrac = regexp.MustCompile(fmt.Sprintf(`^%s/%s$`, rAll, rAll)) ) diff --git a/number/convert.go b/number/convert.go index 39bcfa7..bdf6cec 100644 --- a/number/convert.go +++ b/number/convert.go @@ -233,7 +233,7 @@ func Parse(str string) (out option.Option[Number]) { } case regExpN.MatchString(str): i, j := strings.Index(str, "×"), strings.Index(str, "^") - if expBase, err := strconv.ParseUint(str[i+1:j], 10, 64); err == nil && isBaseValid(expBase) { + if expBase, err := strconv.ParseUint(str[i+2:j], 10, 64); err == nil && isBaseValid(expBase) { if exponent, err := strconv.Atoi(str[j+1:]); err == nil { if n, ok := Parse(str[:i]).Get(); ok { out = option.Some(n.Mul(pow(expBase, exponent)).ToType(Scientific)) @@ -242,7 +242,7 @@ func Parse(str string) (out option.Option[Number]) { } case regFrac.MatchString(str): i := strings.Index(str, "/") - num, denom := Parse(str[:i]), Parse(str[i:]) + num, denom := Parse(str[:i]), Parse(str[i+1:]) if n, ok := num.Get(); ok { if d, ok := denom.Get(); ok { out = option.Some(n.Div(d).ToType(Fraction)) diff --git a/number/number.go b/number/number.go index 320b2bc..fc36be5 100644 --- a/number/number.go +++ b/number/number.go @@ -31,10 +31,10 @@ func (n *Number) get() (r *big.Rat, ok bool) { } func (n *Number) rat() (r *big.Rat, ok bool) { - if r, ok := n.get(); ok { + if r, ok = n.get(); ok { r = new(big.Rat).Set(r) if n.Sign() < 0 { - r = r.Neg(r) + r.Neg(r) } }