mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
regex: fix find() when using anchors (start / end) (#18259)
This commit is contained in:
parent
fc6a34355d
commit
dc16e50d55
22
vlib/regex/regex_anchor_test.v
Normal file
22
vlib/regex/regex_anchor_test.v
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import regex
|
||||||
|
|
||||||
|
fn test_anchor_start() {
|
||||||
|
mut re := regex.regex_opt(r'^\w+') or { panic(err) }
|
||||||
|
start, end := re.find('id.')
|
||||||
|
assert start == 0
|
||||||
|
assert end == 2
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_anchor_end() {
|
||||||
|
mut re := regex.regex_opt(r'\w+$') or { panic(err) }
|
||||||
|
start, end := re.find('(id')
|
||||||
|
assert start == 1
|
||||||
|
assert end == 3
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_anchor_both() {
|
||||||
|
mut re := regex.regex_opt(r'^\w+$') or { panic(err) }
|
||||||
|
start, end := re.find('(id)')
|
||||||
|
assert start == -1
|
||||||
|
assert end == -1
|
||||||
|
}
|
@ -202,6 +202,14 @@ pub fn (mut re RE) find(in_txt string) (int, int) {
|
|||||||
re.groups[gi] += i
|
re.groups[gi] += i
|
||||||
gi++
|
gi++
|
||||||
}
|
}
|
||||||
|
// when ^ (f_ms) is used, it must match on beginning of string
|
||||||
|
if (re.flag & f_ms) != 0 && s > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// when $ (f_me) is used, it must match on ending of string
|
||||||
|
if (re.flag & f_me) != 0 && i + e < in_txt.len {
|
||||||
|
break
|
||||||
|
}
|
||||||
return i + s, i + e
|
return i + s, i + e
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
|
Loading…
Reference in New Issue
Block a user