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

regex: remove [deprecated] functions/methods, code clean, add test for regex_base (#8862)

This commit is contained in:
penguindark 2021-02-20 20:39:08 +01:00 committed by GitHub
parent 8f486cb8cf
commit cc565b22a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 42 deletions

View File

@ -903,11 +903,6 @@ fn (re RE) parse_groups(in_txt string, in_i int) (int, bool, string, int) {
// main compiler
//
// compile return (return code, index) where index is the index of the error in the query string if return code is an error code
[deprecated]
pub fn (mut re RE) compile(in_txt string) (int, int) {
return re.impl_compile(in_txt)
}
fn (mut re RE) impl_compile(in_txt string) (int,int) {
mut i := 0 // input string index
mut pc := 0 // program counter
@ -2381,35 +2376,3 @@ pub fn (mut re RE) match_base(in_txt byteptr, in_txt_len int ) (int,int) {
//println("no_match_found, natural end")
return no_match_found, 0
}
/******************************************************************************
*
* Public functions
*
******************************************************************************/
//
// Matchers
//
[direct_array_access]
pub fn (mut re RE) match_string(in_txt string) (int,int) {
start, mut end := re.match_base(in_txt.str, in_txt.len + 1)
if end > in_txt.len {
end = in_txt.len
}
if start >= 0 && end > start {
if (re.flag & f_ms) != 0 && start > 0 {
return no_match_found, 0
}
if (re.flag & f_me) != 0 && end < in_txt.len {
if in_txt[end] in new_line_list {
return start, end
}
return no_match_found, 0
}
return start, end
}
return start, end
}

View File

@ -479,7 +479,21 @@ fn test_regex(){
}
}
if debug { println("DONE!") }
if debug { println("DONE!") }
}
// test regex_base function
fn test_regex_func(){
query := r"\d\dabcd"
test_str := "78abcd"
mut re, re_err, err_pos := regex.regex_base(query)
if re_err == regex.compile_ok {
start, end := re.match_string(test_str)
assert (start == 0) && (end == 6)
} else {
eprintln("Error in query string in pos ${err_pos}")
eprintln("Error: ${re.get_parse_error_string(re_err)}")
assert false
}
}

View File

@ -14,9 +14,8 @@ module regex
* Inits
*
******************************************************************************/
// regex create a regex object from the query string
[deprecated]
pub fn regex(pattern string) (RE,int,int){
// regex create a regex object from the query string, retunr RE object and errors as re_err, err_pos
pub fn regex_base(pattern string) (RE,int,int){
// init regex
mut re := regex.RE{}
re.prog = []Token {len: pattern.len + 1} // max program length, can not be longer then the pattern
@ -28,7 +27,7 @@ pub fn regex(pattern string) (RE,int,int){
re.group_stack = []int{len: re.group_max, init: -1}
re.group_data = []int{len: re.group_max, init: -1}
re_err,err_pos := re.compile(pattern)
re_err,err_pos := re.impl_compile(pattern)
return re, re_err, err_pos
}
@ -111,6 +110,36 @@ pub fn (re RE) get_group_list() []Re_group {
return res
}
/******************************************************************************
*
* Matchers
*
******************************************************************************/
// match_string Match the pattern with the in_txt string
[direct_array_access]
pub fn (mut re RE) match_string(in_txt string) (int,int) {
start, mut end := re.match_base(in_txt.str, in_txt.len + 1)
if end > in_txt.len {
end = in_txt.len
}
if start >= 0 && end > start {
if (re.flag & f_ms) != 0 && start > 0 {
return no_match_found, 0
}
if (re.flag & f_me) != 0 && end < in_txt.len {
if in_txt[end] in new_line_list {
return start, end
}
return no_match_found, 0
}
return start, end
}
return start, end
}
/******************************************************************************
*
* Finders