Skip to content

Commit 1404667

Browse files
committed
Merge PR #107
2 parents f7d323f + 8513c1e commit 1404667

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

func.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,17 @@ func replaceFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
564564
str := asString(t, functionArgs(arg1).Evaluate(t))
565565
src := asString(t, functionArgs(arg2).Evaluate(t))
566566
dst := asString(t, functionArgs(arg3).Evaluate(t))
567+
e, err := getRegexp(src)
568+
if err != nil {
569+
panic(fmt.Errorf("replace() function second argument is not a valid regexp pattern, err: %s", err.Error()))
570+
}
571+
572+
// replace all $i to ${i} for golang regexp.Expand
573+
for idx := e.NumSubexp(); idx > 0; idx-- {
574+
dst = strings.ReplaceAll(dst, fmt.Sprintf("$%d", idx), fmt.Sprintf("${%d}", idx))
575+
}
567576

568-
return strings.Replace(str, src, dst, -1)
577+
return e.ReplaceAllString(str, dst)
569578
}
570579
}
571580

xpath_function_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,16 @@ func Test_func_replace(t *testing.T) {
195195
test_xpath_eval(t, empty_example, `replace("abracadabra", "a", "")`, "brcdbr")
196196
// The below xpath expressions is not supported yet
197197
//
198-
//test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*a", "*")`, "*")
199-
//test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*?a", "*")`, "*c*bra")
200-
//test_xpath_eval(t, empty_example, `replace("abracadabra", ".*?", "$1")`, "*c*bra") // error, because the pattern matches the zero-length string
201-
//test_xpath_eval(t, empty_example, `replace("AAAA", "A+", "b")`, "b")
202-
//test_xpath_eval(t, empty_example, `replace("AAAA", "A+?", "b")`, "bbb")
203-
//test_xpath_eval(t, empty_example, `replace("darted", "^(.*?)d(.*)$", "$1c$2")`, "carted")
204-
//test_xpath_eval(t, empty_example, `replace("abracadabra", "a(.)", "a$1$1")`, "abbraccaddabbra")
198+
test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*a", "*")`, "*")
199+
test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*?a", "*")`, "*c*bra")
200+
// test_xpath_eval(t, empty_example, `replace("abracadabra", ".*?", "$1")`, "*c*bra") // error, because the pattern matches the zero-length string
201+
test_xpath_eval(t, empty_example, `replace("AAAA", "A+", "b")`, "b")
202+
test_xpath_eval(t, empty_example, `replace("AAAA", "A+?", "b")`, "bbbb")
203+
test_xpath_eval(t, empty_example, `replace("darted", "^(.*?)d(.*)$", "$1c$2")`, "carted")
204+
test_xpath_eval(t, empty_example, `replace("abracadabra", "a(.)", "a$1$1")`, "abbraccaddabbra")
205+
test_xpath_eval(t, empty_example, `replace("abcd", "(ab)|(a)", "[1=$1][2=$2]")`, "[1=ab][2=]cd")
206+
test_xpath_eval(t, empty_example, `replace("1/1/c11/1", "(.*)/[^/]+$", "$1")`, "1/1/c11")
207+
test_xpath_eval(t, empty_example, `replace("A/B/C/D/E/F/G/H/I/J/K/L", "([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/(.*)", "$1-$2-$3-$4-$5-$6-$7-$8-$9-$10")`, "A-B-C-D-E-F-G-H-I-J/K/L")
205208
}
206209

207210
func Test_func_reverse(t *testing.T) {

0 commit comments

Comments
 (0)