From cc565b22a9d6820fa55cc2de626ebbef1fc63be7 Mon Sep 17 00:00:00 2001 From: penguindark <57967770+penguindark@users.noreply.github.com> Date: Sat, 20 Feb 2021 20:39:08 +0100 Subject: [PATCH] regex: remove [deprecated] functions/methods, code clean, add test for regex_base (#8862) --- vlib/regex/regex.v | 37 ------------------------------------- vlib/regex/regex_test.v | 16 +++++++++++++++- vlib/regex/regex_util.v | 37 +++++++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/vlib/regex/regex.v b/vlib/regex/regex.v index dfaca4a9af..8259731cd7 100644 --- a/vlib/regex/regex.v +++ b/vlib/regex/regex.v @@ -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 -} diff --git a/vlib/regex/regex_test.v b/vlib/regex/regex_test.v index 972351838f..c5909e8d47 100644 --- a/vlib/regex/regex_test.v +++ b/vlib/regex/regex_test.v @@ -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 + } +} diff --git a/vlib/regex/regex_util.v b/vlib/regex/regex_util.v index 9e9323ec9a..f74e0bca01 100644 --- a/vlib/regex/regex_util.v +++ b/vlib/regex/regex_util.v @@ -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