Skip to content

Commit dd8c35a

Browse files
committed
Precompile validation regexp
1 parent 3e13de9 commit dd8c35a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Diff for: pkg/fqbn/fqbn.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func MustParse(fqbnIn string) *FQBN {
4242
return res
4343
}
4444

45+
var fqbnValidationRegex = regexp.MustCompile(`^[a-zA-Z0-9_.-]*$`)
46+
var valueValidationRegex = regexp.MustCompile(`^[a-zA-Z0-9=_.-]*$`)
47+
4548
// Parse parses an FQBN string from the input string
4649
func Parse(fqbnIn string) (*FQBN, error) {
4750
// Split fqbn parts
@@ -60,7 +63,6 @@ func Parse(fqbnIn string) (*FQBN, error) {
6063
return nil, errors.New(i18n.Tr("empty board identifier"))
6164
}
6265
// Check if the fqbn contains invalid characters
63-
fqbnValidationRegex := regexp.MustCompile(`^[a-zA-Z0-9_.-]*$`)
6466
for i := 0; i < 3; i++ {
6567
if !fqbnValidationRegex.MatchString(fqbnParts[i]) {
6668
return nil, errors.New(i18n.Tr("fqbn's field %s contains an invalid character", fqbnParts[i]))
@@ -81,7 +83,6 @@ func Parse(fqbnIn string) (*FQBN, error) {
8183
return nil, errors.New(i18n.Tr("config key %s contains an invalid character", k))
8284
}
8385
// The config value can also contain the = symbol
84-
valueValidationRegex := regexp.MustCompile(`^[a-zA-Z0-9=_.-]*$`)
8586
if !valueValidationRegex.MatchString(v) {
8687
return nil, errors.New(i18n.Tr("config value %s contains an invalid character", v))
8788
}

0 commit comments

Comments
 (0)