1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

regex: fix bug in replace_by_fn (#13344)

This commit is contained in:
penguindark 2022-02-02 09:52:18 +01:00 committed by GitHub
parent b10b65e134
commit c3573454d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -596,6 +596,21 @@ fn test_regex_func(){
} }
} }
fn my_repl_1(re regex.RE, in_txt string, start int, end int) string {
s0 := re.get_group_by_id(in_txt,0)
println("[$start, $end] => ${s0}")
return "a" + s0.to_upper()
}
fn test_regex_func_replace1(){
txt := "abbabbbabbbbaabba"
query := r"a(b+)"
mut re := regex.regex_opt(query) or { panic(err) }
result := re.replace_by_fn(txt, my_repl_1)
assert result == "aBBaBBBaBBBBaaBBa"
}
fn my_repl(re regex.RE, in_txt string, start int, end int) string { fn my_repl(re regex.RE, in_txt string, start int, end int) string {
s0 := re.get_group_by_id(in_txt,0)[0..1] + "X" s0 := re.get_group_by_id(in_txt,0)[0..1] + "X"
s1 := re.get_group_by_id(in_txt,1)[0..1] + "X" s1 := re.get_group_by_id(in_txt,1)[0..1] + "X"
@ -603,7 +618,6 @@ fn my_repl(re regex.RE, in_txt string, start int, end int) string {
return "${s0}${s1}${s2}" return "${s0}${s1}${s2}"
} }
// test regex replace function // test regex replace function
fn test_regex_func_replace(){ fn test_regex_func_replace(){
filler := "E il primo dei tre regni dell'Oltretomba cristiano visitato da Dante nel corso del viaggio, con la guida di Virgilio." filler := "E il primo dei tre regni dell'Oltretomba cristiano visitato da Dante nel corso del viaggio, con la guida di Virgilio."

View File

@ -212,7 +212,7 @@ pub fn (mut re RE) find(in_txt string) (int, int) {
[direct_array_access] [direct_array_access]
pub fn (mut re RE) find_from(in_txt string, start int) (int, int) { pub fn (mut re RE) find_from(in_txt string, start int) (int, int) {
old_flag := re.flag old_flag := re.flag
re.flag |= f_src // enable search mode // re.flag |= f_src // enable search mode
mut i := start mut i := start
if i < 0 { if i < 0 {