2024-02-17 18:13:24 +00:00
|
|
|
|
package number
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"regexp"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
FloatingPrecision uint64 = 10 // Nombre de chiffres après la virgule pour un nombre décimal ou un nombre scientifique
|
2024-02-21 09:12:59 +00:00
|
|
|
|
FixedPrecision = false // Si vrai le nombre de chiffres après la virgule est fixe.
|
2024-02-17 18:13:24 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2024-02-21 13:23:18 +00:00
|
|
|
|
rSign = `\+|-`
|
2024-02-21 09:12:59 +00:00
|
|
|
|
rBSign = `(0|1)`
|
|
|
|
|
rNb2 = `(0|1)`
|
|
|
|
|
rNb8 = `[0-7]`
|
|
|
|
|
rNb10 = `\d`
|
|
|
|
|
rNb16 = `[0-9a-fA-F]`
|
|
|
|
|
rNbN = `[0-9a-zA-Z]`
|
2024-02-21 13:23:18 +00:00
|
|
|
|
rBase2 = `B|b`
|
|
|
|
|
rBase8 = `O|o`
|
|
|
|
|
rBase16 = `X|x`
|
2024-02-21 09:12:59 +00:00
|
|
|
|
rExp10 = `E|e`
|
|
|
|
|
rExpN = `×\d+\^`
|
2024-02-17 18:13:24 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2024-02-21 09:12:59 +00:00
|
|
|
|
rInt2 = fmt.Sprintf(`(%s)%s+`, rBase2, rNb2)
|
|
|
|
|
rInt8 = fmt.Sprintf(`(%s)%s+`, rBase8, rNb8)
|
2024-02-21 13:23:18 +00:00
|
|
|
|
rInt10 = fmt.Sprintf(`(%s)?%s+`, rSign, rNb10)
|
|
|
|
|
rInt16 = fmt.Sprintf(`(%s)%s+`, rBase16, rNb16)
|
|
|
|
|
rIntN = fmt.Sprintf(`\(%s+\)(%s)?%s+`, rNb10, rSign, rNbN)
|
2024-02-21 09:12:59 +00:00
|
|
|
|
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)
|
2024-02-21 13:23:18 +00:00
|
|
|
|
rDec10 = fmt.Sprintf(`(%s)?(%s+\.%s*|\.%s+)`, rSign, rNb10, rNb10, rNb10)
|
2024-02-21 09:12:59 +00:00
|
|
|
|
rDec16 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase16, rNb16, rNb16, rNb16)
|
2024-02-21 13:23:18 +00:00
|
|
|
|
rDecN = fmt.Sprintf(`\(%s+\)(%s)?(%s+\.%s*|\.%s+)`, rNb10, rSign, rNbN, rNbN, rNbN)
|
2024-02-21 09:12:59 +00:00
|
|
|
|
rDecB = fmt.Sprintf(`%s(%s|%s|%s)`, rBSign, rDec2, rDec8, rDec16)
|
|
|
|
|
rDec = fmt.Sprintf(`(%s|%s|%s)`, rDec10, rDecN, rDecB)
|
2024-02-21 13:23:18 +00:00
|
|
|
|
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)
|
2024-02-21 09:12:59 +00:00
|
|
|
|
rAll = fmt.Sprintf(`(%s|%s)`, rAll10, rAllN)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
regInt10 = regexp.MustCompile(fmt.Sprintf(`^%s$`, rInt10))
|
|
|
|
|
regIntN = regexp.MustCompile(fmt.Sprintf(`^%s$`, rIntN))
|
|
|
|
|
regIntB = regexp.MustCompile(fmt.Sprintf(`^%s$`, rIntB))
|
|
|
|
|
|
|
|
|
|
regDec = regexp.MustCompile(fmt.Sprintf(`^%s$`, rDec))
|
|
|
|
|
|
|
|
|
|
regExp10 = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponent10))
|
2024-02-21 13:23:18 +00:00
|
|
|
|
regExpN = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponentN))
|
2024-02-21 09:12:59 +00:00
|
|
|
|
|
|
|
|
|
regFrac = regexp.MustCompile(fmt.Sprintf(`^%s/%s$`, rAll, rAll))
|
2024-02-17 18:13:24 +00:00
|
|
|
|
)
|