Преглед изворни кода

chore: use NewReplacer to avoid placeholders

Signed-off-by: Chasing1020 <chasing1020@gmail.com>
Chasing1020 пре 1 година
родитељ
комит
a3eca2fc9e
2 измењених фајлова са 54 додато и 3 уклоњено
  1. 6 3
      pkg/ast/expr.go
  2. 48 0
      pkg/ast/expr_test.go

+ 6 - 3
pkg/ast/expr.go

@@ -211,9 +211,12 @@ func (l *LikePattern) expr() {}
 func (l *LikePattern) node() {}
 func (l *LikePattern) node() {}
 
 
 func (l *LikePattern) Compile(likestr string) (*regexp.Regexp, error) {
 func (l *LikePattern) Compile(likestr string) (*regexp.Regexp, error) {
-	likestr = strings.ReplaceAll(strings.ReplaceAll(likestr, `\%`, `!@#`), `\_`, `!@$`)
-	regstr := strings.ReplaceAll(strings.ReplaceAll(likestr, "%", ".*"), "_", ".")
-	regstr = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(regstr, `!@$`, `\_`), `!@#`, `\%`), `\`, `\\`)
+	regstr := strings.ReplaceAll(strings.NewReplacer(
+		`\%`, `\%`,
+		`\_`, `\_`,
+		`%`, `.*`,
+		`_`, `.`,
+	).Replace(likestr), `\`, `\\`)
 	re, err := regexp.Compile("^" + regstr + "$")
 	re, err := regexp.Compile("^" + regstr + "$")
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err

+ 48 - 0
pkg/ast/expr_test.go

@@ -0,0 +1,48 @@
+// Copyright 2021-2023 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package ast
+
+import (
+	"regexp"
+	"strings"
+	"testing"
+)
+
+func compileByReplaceAll(likestr string) (*regexp.Regexp, error) {
+	likestr = strings.ReplaceAll(strings.ReplaceAll(likestr, `\%`, `!@#`), `\_`, `!@$`)
+	regstr := strings.ReplaceAll(strings.ReplaceAll(likestr, "%", ".*"), "_", ".")
+	regstr = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(regstr, `!@$`, `\_`), `!@#`, `\%`), `\`, `\\`)
+	return regexp.Compile("^" + regstr + "$")
+}
+
+func TestLikePatternCompile(t *testing.T) {
+	l := LikePattern{}
+	tests := []string{
+		`%`, `_`, `\`, `\\`, `\%`, `\_`, `\\%`, `\\_`, `%\\%`, `%\\_`, `%\\%\\%`, `%\\%\\_`, `%\\%\\%`, `%\\%\\_`,
+	}
+	for i, test := range tests {
+		expect, err := compileByReplaceAll(test)
+		if err != nil {
+			panic(err)
+		}
+		actual, err := l.Compile(test)
+		if err != nil {
+			panic(err)
+		}
+		if expect.String() != actual.String() {
+			t.Errorf("%d. \nexpect: %s, actual: %s\n", i, expect, actual)
+		}
+	}
+}