regex: bug fix on find groups indexes (#12152)
parent
3c8be0db72
commit
0fafefc078
|
@ -645,3 +645,64 @@ fn test_quantifier_sequences(){
|
||||||
assert re_err == regex.err_syntax_error
|
assert re_err == regex.err_syntax_error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test group index in find
|
||||||
|
struct Test_find_groups {
|
||||||
|
src string
|
||||||
|
q string
|
||||||
|
s int // start index
|
||||||
|
e int // end index
|
||||||
|
res []int // groups indexes
|
||||||
|
}
|
||||||
|
const (
|
||||||
|
find_groups_test_suite = [
|
||||||
|
Test_find_groups{
|
||||||
|
"aabbbccccdd",
|
||||||
|
r"(b+)(c+)",
|
||||||
|
2,
|
||||||
|
9,
|
||||||
|
[2, 5, 5, 9],
|
||||||
|
},
|
||||||
|
Test_find_groups{
|
||||||
|
"aabbbccccdd",
|
||||||
|
r"(a+).*(c+)",
|
||||||
|
0,
|
||||||
|
9,
|
||||||
|
[0, 2, 5, 9],
|
||||||
|
},
|
||||||
|
Test_find_groups{
|
||||||
|
"aabbbccccdd",
|
||||||
|
r"((b+).*)(d+)",
|
||||||
|
2,
|
||||||
|
11,
|
||||||
|
[2, 9, 2, 5, 9, 11],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
fn test_groups_in_find(){
|
||||||
|
for test_obj in find_groups_test_suite {
|
||||||
|
src_text := test_obj.src
|
||||||
|
query := test_obj.q
|
||||||
|
mut re := regex.regex_opt(query) or { panic(err) }
|
||||||
|
start, end := re.find(src_text)
|
||||||
|
// Debug print do not remove!!
|
||||||
|
/*
|
||||||
|
println("---------")
|
||||||
|
println("src_text:[${src_text}]")
|
||||||
|
println("query :[${query}]")
|
||||||
|
println("[${start}, ${end}]")
|
||||||
|
println(re.groups)
|
||||||
|
mut gi := 0
|
||||||
|
for gi < re.groups.len {
|
||||||
|
if re.groups[gi] >= 0 {
|
||||||
|
println('${gi / 2} :[${src_text[re.groups[gi]..re.groups[gi + 1]]}]')
|
||||||
|
}
|
||||||
|
gi += 2
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
// check
|
||||||
|
assert start == test_obj.s
|
||||||
|
assert end == test_obj.e
|
||||||
|
assert re.groups == test_obj.res
|
||||||
|
}
|
||||||
|
}
|
|
@ -194,6 +194,11 @@ pub fn (mut re RE) find(in_txt string) (int, int) {
|
||||||
if s >= 0 && e > s {
|
if s >= 0 && e > s {
|
||||||
// println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]")
|
// println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]")
|
||||||
// re.flag = old_flag
|
// re.flag = old_flag
|
||||||
|
mut gi := 0
|
||||||
|
for gi < re.groups.len {
|
||||||
|
re.groups[gi] += i
|
||||||
|
gi++
|
||||||
|
}
|
||||||
return i + s, i + e
|
return i + s, i + e
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
|
@ -229,6 +234,11 @@ pub fn (mut re RE) find_from(in_txt string, start int) (int, int) {
|
||||||
if s >= 0 && e > s {
|
if s >= 0 && e > s {
|
||||||
// println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]")
|
// println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]")
|
||||||
re.flag = old_flag
|
re.flag = old_flag
|
||||||
|
mut gi := 0
|
||||||
|
for gi < re.groups.len {
|
||||||
|
re.groups[gi] += i
|
||||||
|
gi++
|
||||||
|
}
|
||||||
return i + s, i + e
|
return i + s, i + e
|
||||||
} else {
|
} else {
|
||||||
i++
|
i++
|
||||||
|
@ -354,12 +364,12 @@ pub fn (mut re RE) replace_by_fn(in_txt string, repl_fn FnReplace) string {
|
||||||
if last_end < s {
|
if last_end < s {
|
||||||
res.write_string(in_txt[last_end..s])
|
res.write_string(in_txt[last_end..s])
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
for g_i in 0 .. re.group_count {
|
for g_i in 0 .. re.group_count {
|
||||||
re.groups[g_i << 1] += i
|
re.groups[g_i << 1] += i
|
||||||
re.groups[(g_i << 1) + 1] += i
|
re.groups[(g_i << 1) + 1] += i
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
repl := repl_fn(re, in_txt, s, e)
|
repl := repl_fn(re, in_txt, s, e)
|
||||||
// println("repl res: $repl")
|
// println("repl res: $repl")
|
||||||
res.write_string(repl)
|
res.write_string(repl)
|
||||||
|
@ -416,12 +426,12 @@ pub fn (mut re RE) replace(in_txt string, repl_str string) string {
|
||||||
if last_end < s {
|
if last_end < s {
|
||||||
res.write_string(in_txt[last_end..s])
|
res.write_string(in_txt[last_end..s])
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
for g_i in 0 .. re.group_count {
|
for g_i in 0 .. re.group_count {
|
||||||
re.groups[g_i << 1] += i
|
re.groups[g_i << 1] += i
|
||||||
re.groups[(g_i << 1) + 1] += i
|
re.groups[(g_i << 1) + 1] += i
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
// repl := repl_fn(re, in_txt, s, e)
|
// repl := repl_fn(re, in_txt, s, e)
|
||||||
repl := re.parsed_replace_string(in_txt, repl_str)
|
repl := re.parsed_replace_string(in_txt, repl_str)
|
||||||
// println("repl res: $repl")
|
// println("repl res: $repl")
|
||||||
|
|
Loading…
Reference in New Issue