Skip to content

Commit e27150c

Browse files
committed
perf: only compile regex once
1 parent 08f979a commit e27150c

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

internal/interpreter/interpreter.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,17 @@ func (st *programState) evaluateSentAmt(sentValue parser.SentValue) (*string, *b
858858
}
859859
}
860860

861+
var percentRegex = regexp.MustCompile(`^([0-9]+)(?:[.]([0-9]+))?[%]$`)
862+
var fractionRegex = regexp.MustCompile(`^([0-9]+)\s?[/]\s?([0-9]+)$`)
863+
861864
// slightly edited copy-paste from:
862865
// https://github.com/formancehq/ledger/blob/b188d0c80eadaab5024d74edc967c7005e155f7c/internal/machine/portion.go#L57
863866

864867
func ParsePortionSpecific(input string) (*big.Rat, InterpreterError) {
865868
var res *big.Rat
866869
var ok bool
867870

868-
re := regexp.MustCompile(`^([0-9]+)(?:[.]([0-9]+))?[%]$`)
869-
percentMatch := re.FindStringSubmatch(input)
871+
percentMatch := percentRegex.FindStringSubmatch(input)
870872
if len(percentMatch) != 0 {
871873
integral := percentMatch[1]
872874
fractional := percentMatch[2]
@@ -876,8 +878,7 @@ func ParsePortionSpecific(input string) (*big.Rat, InterpreterError) {
876878
}
877879
res.Mul(res, big.NewRat(1, 100))
878880
} else {
879-
re = regexp.MustCompile(`^([0-9]+)\s?[/]\s?([0-9]+)$`)
880-
fractionMatch := re.FindStringSubmatch(input)
881+
fractionMatch := fractionRegex.FindStringSubmatch(input)
881882
if len(fractionMatch) != 0 {
882883
numerator := fractionMatch[1]
883884
denominator := fractionMatch[2]

0 commit comments

Comments
 (0)