63 lines
2.2 KiB
Go
63 lines
2.2 KiB
Go
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
|
||
FixedPrecision = false // Si vrai le nombre de chiffres après la virgule est fixe.
|
||
)
|
||
|
||
const (
|
||
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`
|
||
rExp10 = `E|e`
|
||
rExpN = `×\d+\^`
|
||
)
|
||
|
||
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+`, 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)
|
||
rDec16 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase16, rNb16, rNb16, rNb16)
|
||
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)
|
||
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))
|
||
regExpN = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponentN))
|
||
|
||
regFrac = regexp.MustCompile(fmt.Sprintf(`^%s/%s$`, rAll, rAll))
|
||
)
|