Skip to content

Commit 519ca90

Browse files
authored
regex: added default support for long queries (#12635)
1 parent f86710d commit 519ca90

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

vlib/regex/regex_opt.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn regex_opt(pattern string) ?RE {
4040
re.prog = []Token{len: pattern.len + 1} // max program length, can not be longer then the pattern
4141
re.cc = []CharClass{len: pattern.len} // can not be more char class the the length of the pattern
4242
re.group_csave_flag = false // enable continuos group saving
43-
re.group_max_nested = 128 // set max 128 group nested
43+
re.group_max_nested = pattern.len >> 1 // set max 128 group nested
4444
re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth
4545

4646
re.group_stack = []int{len: re.group_max, init: -1}

vlib/regex/regex_test.v

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import regex
22
import rand
3+
import strings
34

45
/******************************************************************************
56
*
@@ -720,3 +721,42 @@ fn test_errors(){
720721
}
721722
assert count == err_query_list.len
722723
}
724+
725+
726+
fn test_long_query() {
727+
test_len := 32768
728+
mut buf := strings.new_builder(test_len * 3)
729+
base_string := rand.string(test_len)
730+
731+
for c in base_string {
732+
buf.write_b(`(`)
733+
buf.write_b(c)
734+
buf.write_b(`)`)
735+
}
736+
737+
mut query := buf.str()
738+
739+
//println(base_string)
740+
//println(buf.str())
741+
742+
// test 1
743+
mut re := regex.regex_opt(query) or { panic(err) }
744+
mut start, mut end := re.match_string(base_string)
745+
//println("$start, $end")
746+
assert start >= 0 && end == base_string.len
747+
748+
// test 2
749+
buf.clear()
750+
for c in base_string {
751+
buf.write_b(`(`)
752+
buf.write_b(c)
753+
}
754+
for _ in 0..base_string.len {
755+
buf.write_b(`)`)
756+
}
757+
query = buf.str()
758+
re = regex.regex_opt(query) or { panic(err) }
759+
start, end = re.match_string(base_string)
760+
//println("$start, $end")
761+
assert start >= 0 && end == base_string.len
762+
}

vlib/regex/regex_util.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn regex_base(pattern string) (RE, int, int) {
2121
re.prog = []Token{len: pattern.len + 1} // max program length, can not be longer then the pattern
2222
re.cc = []CharClass{len: pattern.len} // can not be more char class the the length of the pattern
2323
re.group_csave_flag = false // enable continuos group saving
24-
re.group_max_nested = 128 // set max 128 group nested
24+
re.group_max_nested = pattern.len >> 1 // set max 128 group nested
2525
re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth
2626

2727
re.group_stack = []int{len: re.group_max, init: -1}
@@ -63,7 +63,7 @@ pub fn (re RE) get_group_by_name(in_txt string, group_name string) string {
6363
// get_group_by_id get a group string by its id
6464
pub fn (re RE) get_group_by_id(in_txt string, group_id int) string {
6565
if group_id < (re.groups.len >> 1) {
66-
index := group_id << 1
66+
index := group_id * 2
6767
start := re.groups[index]
6868
end := re.groups[index + 1]
6969
if start >= 0 && end > start {
@@ -76,7 +76,7 @@ pub fn (re RE) get_group_by_id(in_txt string, group_id int) string {
7676
// get_group_by_id get a group boundaries by its id
7777
pub fn (re RE) get_group_bounds_by_id(group_id int) (int, int) {
7878
if group_id < re.group_count {
79-
index := group_id << 1
79+
index := group_id * 2
8080
return re.groups[index], re.groups[index + 1]
8181
}
8282
return -1, -1
@@ -366,8 +366,8 @@ pub fn (mut re RE) replace_by_fn(in_txt string, repl_fn FnReplace) string {
366366
}
367367
/*
368368
for g_i in 0 .. re.group_count {
369-
re.groups[g_i << 1] += i
370-
re.groups[(g_i << 1) + 1] += i
369+
re.groups[g_i * 2] += i
370+
re.groups[(g_i * 2) + 1] += i
371371
}
372372
*/
373373
repl := repl_fn(re, in_txt, s, e)
@@ -428,8 +428,8 @@ pub fn (mut re RE) replace(in_txt string, repl_str string) string {
428428
}
429429
/*
430430
for g_i in 0 .. re.group_count {
431-
re.groups[g_i << 1] += i
432-
re.groups[(g_i << 1) + 1] += i
431+
re.groups[g_i * 2] += i
432+
re.groups[(g_i * 2) + 1] += i
433433
}
434434
*/
435435
// repl := repl_fn(re, in_txt, s, e)

0 commit comments

Comments
 (0)