From 302b3649ef893419326c1946aa5c3bc6fa632692 Mon Sep 17 00:00:00 2001 From: Sergey Lanzman Date: Mon, 4 Sep 2017 22:56:06 +0300 Subject: [PATCH] Move regexp.MustCompile to global variable Kubernetes-commit: 34747474659b26456c3615d9f79d014deacf505a --- util/jsonpath/parser.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/util/jsonpath/parser.go b/util/jsonpath/parser.go index 2f02de1a3..ef0f9213a 100644 --- a/util/jsonpath/parser.go +++ b/util/jsonpath/parser.go @@ -43,7 +43,11 @@ type Parser struct { width int } -var ErrSyntax = errors.New("invalid syntax") +var ( + ErrSyntax = errors.New("invalid syntax") + dictKeyRex = regexp.MustCompile(`^'([^']*)'$`) + sliceOperatorRex = regexp.MustCompile(`^(-?[\d]*)(:-?[\d]*)?(:[\d]*)?$`) +) // Parse parsed the given text and return a node Parser. // If an error is encountered, parsing stops and an empty @@ -283,8 +287,7 @@ Loop: } // dict key - reg := regexp.MustCompile(`^'([^']*)'$`) - value := reg.FindStringSubmatch(text) + value := dictKeyRex.FindStringSubmatch(text) if value != nil { parser, err := parseAction("arraydict", fmt.Sprintf(".%s", value[1])) if err != nil { @@ -297,8 +300,7 @@ Loop: } //slice operator - reg = regexp.MustCompile(`^(-?[\d]*)(:-?[\d]*)?(:[\d]*)?$`) - value = reg.FindStringSubmatch(text) + value = sliceOperatorRex.FindStringSubmatch(text) if value == nil { return fmt.Errorf("invalid array index %s", text) }