From 2533f06e1be259d4280a12cc2183d374b9b7cdde Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 7 Sep 2021 06:01:23 +0200 Subject: [PATCH] regex: define the predicate matches_string (#11422) --- vlib/regex/regex_test.v | 9 ++++++++- vlib/regex/regex_util.v | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/vlib/regex/regex_test.v b/vlib/regex/regex_test.v index 8fc9fa7096..e9a3baf1ce 100644 --- a/vlib/regex/regex_test.v +++ b/vlib/regex/regex_test.v @@ -556,6 +556,13 @@ fn test_regex(){ continue } + // test the match predicate + if to.s >= 0 { + assert re.matches_string(to.src) + } else { + assert !re.matches_string(to.src) + } + // rerun to test consistency tmp_str1 := to.src.clone() start1, end1 := re.match_string(tmp_str1) @@ -637,4 +644,4 @@ fn test_quantifier_sequences(){ } assert re_err == regex.err_syntax_error } -} \ No newline at end of file +} diff --git a/vlib/regex/regex_util.v b/vlib/regex/regex_util.v index bad5d727fc..060d01dabb 100644 --- a/vlib/regex/regex_util.v +++ b/vlib/regex/regex_util.v @@ -144,6 +144,12 @@ pub fn (mut re RE) match_string(in_txt string) (int, int) { return start, end } +// matches_string Checks if the pattern matches the in_txt string +pub fn (mut re RE) matches_string(in_txt string) bool { + start, _ := re.match_string(in_txt) + return start != no_match_found +} + /****************************************************************************** * * Finders