2020-12-20 06:52:02 +03:00
|
|
|
/*
|
|
|
|
regex 1.0 alpha
|
|
|
|
|
2021-01-18 15:20:06 +03:00
|
|
|
Copyright (c) 2019-2021 Dario Deledda. All rights reserved.
|
2020-12-20 06:52:02 +03:00
|
|
|
Use of this source code is governed by an MIT license
|
|
|
|
that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
module regex
|
2021-05-08 13:32:29 +03:00
|
|
|
|
2021-03-20 02:54:12 +03:00
|
|
|
import strings
|
2020-12-20 06:52:02 +03:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Inits
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2021-02-20 22:39:08 +03:00
|
|
|
// regex create a regex object from the query string, retunr RE object and errors as re_err, err_pos
|
2021-05-08 13:32:29 +03:00
|
|
|
pub fn regex_base(pattern string) (RE, int, int) {
|
2020-12-22 23:34:46 +03:00
|
|
|
// init regex
|
2021-05-08 13:32:29 +03:00
|
|
|
mut re := RE{}
|
|
|
|
re.prog = []Token{len: pattern.len + 1} // max program length, can not be longer then the pattern
|
|
|
|
re.cc = []CharClass{len: pattern.len} // can not be more char class the the length of the pattern
|
|
|
|
re.group_csave_flag = false // enable continuos group saving
|
2021-12-01 09:38:50 +03:00
|
|
|
re.group_max_nested = pattern.len >> 1 // set max 128 group nested
|
2021-05-08 13:32:29 +03:00
|
|
|
re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth
|
2020-12-22 23:34:46 +03:00
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
re.group_stack = []int{len: re.group_max, init: -1}
|
|
|
|
re.group_data = []int{len: re.group_max, init: -1}
|
2020-12-22 23:34:46 +03:00
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
re_err, err_pos := re.impl_compile(pattern)
|
2020-12-20 06:52:02 +03:00
|
|
|
return re, re_err, err_pos
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Utilities
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
// get_group_bounds_by_name get a group boundaries by its name
|
|
|
|
pub fn (re RE) get_group_bounds_by_name(group_name string) (int, int) {
|
|
|
|
if group_name in re.group_map {
|
2021-05-08 13:32:29 +03:00
|
|
|
tmp_index := re.group_map[group_name] - 1
|
|
|
|
start := re.groups[tmp_index * 2]
|
|
|
|
end := re.groups[tmp_index * 2 + 1]
|
|
|
|
return start, end
|
2020-12-20 06:52:02 +03:00
|
|
|
}
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_group_by_name get a group boundaries by its name
|
|
|
|
pub fn (re RE) get_group_by_name(in_txt string, group_name string) string {
|
|
|
|
if group_name in re.group_map {
|
2021-05-08 13:32:29 +03:00
|
|
|
tmp_index := re.group_map[group_name] - 1
|
|
|
|
start := re.groups[tmp_index * 2]
|
|
|
|
end := re.groups[tmp_index * 2 + 1]
|
2021-04-03 23:16:56 +03:00
|
|
|
if start >= 0 && end > start {
|
|
|
|
return in_txt[start..end]
|
|
|
|
}
|
2020-12-20 06:52:02 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
return ''
|
2020-12-20 06:52:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get_group_by_id get a group string by its id
|
|
|
|
pub fn (re RE) get_group_by_id(in_txt string, group_id int) string {
|
|
|
|
if group_id < (re.groups.len >> 1) {
|
2021-12-01 09:38:50 +03:00
|
|
|
index := group_id * 2
|
2020-12-20 06:52:02 +03:00
|
|
|
start := re.groups[index]
|
2021-05-08 13:32:29 +03:00
|
|
|
end := re.groups[index + 1]
|
2021-04-03 23:16:56 +03:00
|
|
|
if start >= 0 && end > start {
|
|
|
|
return in_txt[start..end]
|
|
|
|
}
|
2020-12-20 06:52:02 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
return ''
|
2020-12-20 06:52:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get_group_by_id get a group boundaries by its id
|
2021-05-08 13:32:29 +03:00
|
|
|
pub fn (re RE) get_group_bounds_by_id(group_id int) (int, int) {
|
2021-03-20 02:54:12 +03:00
|
|
|
if group_id < re.group_count {
|
2021-12-01 09:38:50 +03:00
|
|
|
index := group_id * 2
|
2020-12-20 06:52:02 +03:00
|
|
|
return re.groups[index], re.groups[index + 1]
|
|
|
|
}
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
pub struct Re_group {
|
2020-12-20 06:52:02 +03:00
|
|
|
pub:
|
|
|
|
start int = -1
|
|
|
|
end int = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_group_list return a list of Re_group for the found groups
|
|
|
|
pub fn (re RE) get_group_list() []Re_group {
|
|
|
|
mut res := []Re_group{len: re.groups.len >> 1}
|
|
|
|
mut gi := 0
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("len: ${re.groups.len} groups: ${re.groups}")
|
2020-12-26 23:08:56 +03:00
|
|
|
|
2020-12-20 06:52:02 +03:00
|
|
|
for gi < re.groups.len {
|
|
|
|
if re.groups[gi] >= 0 {
|
|
|
|
txt_st := re.groups[gi]
|
2021-05-08 13:32:29 +03:00
|
|
|
txt_en := re.groups[gi + 1]
|
|
|
|
|
|
|
|
// println("#${gi/2} start: ${re.groups[gi]} end: ${re.groups[gi + 1]} ")
|
|
|
|
if txt_st >= 0 && txt_en > txt_st {
|
|
|
|
tmp := Re_group{
|
|
|
|
start: re.groups[gi]
|
|
|
|
end: re.groups[gi + 1]
|
|
|
|
}
|
|
|
|
// println(tmp)
|
2020-12-20 06:52:02 +03:00
|
|
|
res[gi >> 1] = tmp
|
|
|
|
} else {
|
|
|
|
res[gi >> 1] = Re_group{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gi += 2
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-02-20 22:39:08 +03:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Matchers
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
// match_string Match the pattern with the in_txt string
|
|
|
|
[direct_array_access]
|
2021-05-08 13:32:29 +03:00
|
|
|
pub fn (mut re RE) match_string(in_txt string) (int, int) {
|
2021-02-20 22:39:08 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-07 07:01:23 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-24 08:27:46 +03:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Finders
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2021-01-03 18:59:00 +03:00
|
|
|
/*
|
2021-03-20 02:54:12 +03:00
|
|
|
// find internal implementation HERE for reference do not remove!!
|
2020-12-24 08:27:46 +03:00
|
|
|
[direct_array_access]
|
2021-01-03 18:59:00 +03:00
|
|
|
fn (mut re RE) find_imp(in_txt string) (int,int) {
|
2020-12-24 08:27:46 +03:00
|
|
|
old_flag := re.flag
|
|
|
|
re.flag |= f_src // enable search mode
|
|
|
|
|
|
|
|
start, mut end := re.match_base(in_txt.str, in_txt.len + 1)
|
|
|
|
//print("Find [$start,$end] '${in_txt[start..end]}'")
|
|
|
|
if end > in_txt.len {
|
|
|
|
end = in_txt.len
|
|
|
|
}
|
|
|
|
re.flag = old_flag
|
|
|
|
|
|
|
|
if start >= 0 && end > start {
|
|
|
|
return start, end
|
|
|
|
}
|
|
|
|
return no_match_found, 0
|
|
|
|
}
|
2021-01-03 18:59:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
// find try to find the first match in the input string
|
|
|
|
[direct_array_access]
|
2021-05-08 13:32:29 +03:00
|
|
|
pub fn (mut re RE) find(in_txt string) (int, int) {
|
|
|
|
// old_flag := re.flag
|
|
|
|
// re.flag |= f_src // enable search mode
|
2021-03-20 02:54:12 +03:00
|
|
|
|
2021-01-03 18:59:00 +03:00
|
|
|
mut i := 0
|
|
|
|
for i < in_txt.len {
|
|
|
|
mut s := -1
|
|
|
|
mut e := -1
|
|
|
|
unsafe {
|
2021-07-14 22:20:05 +03:00
|
|
|
// tmp_str := tos(in_txt.str + i, in_txt.len - i)
|
|
|
|
// println("Check: [$tmp_str]")
|
|
|
|
s, e = re.match_base(in_txt.str + i, in_txt.len - i + 1)
|
|
|
|
|
|
|
|
if s >= 0 && e > s {
|
|
|
|
// println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]")
|
|
|
|
// re.flag = old_flag
|
2021-10-12 06:03:23 +03:00
|
|
|
mut gi := 0
|
|
|
|
for gi < re.groups.len {
|
|
|
|
re.groups[gi] += i
|
|
|
|
gi++
|
|
|
|
}
|
2021-07-14 22:20:05 +03:00
|
|
|
return i + s, i + e
|
|
|
|
}
|
2021-03-20 02:54:12 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
// re.flag = old_flag
|
2021-03-20 02:54:12 +03:00
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// find try to find the first match in the input string strarting from start index
|
|
|
|
[direct_array_access]
|
2021-05-08 13:32:29 +03:00
|
|
|
pub fn (mut re RE) find_from(in_txt string, start int) (int, int) {
|
2021-03-20 02:54:12 +03:00
|
|
|
old_flag := re.flag
|
2021-05-08 13:32:29 +03:00
|
|
|
re.flag |= f_src // enable search mode
|
2021-03-20 02:54:12 +03:00
|
|
|
|
|
|
|
mut i := start
|
|
|
|
if i < 0 {
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
for i < in_txt.len {
|
|
|
|
//--- speed references ---
|
2021-05-08 13:32:29 +03:00
|
|
|
|
2021-03-20 02:54:12 +03:00
|
|
|
mut s := -1
|
|
|
|
mut e := -1
|
2021-05-08 13:32:29 +03:00
|
|
|
|
2021-03-20 02:54:12 +03:00
|
|
|
unsafe {
|
2021-05-08 13:32:29 +03:00
|
|
|
tmp_str := tos(in_txt.str + i, in_txt.len - i)
|
|
|
|
s, e = re.match_string(tmp_str)
|
2021-03-20 02:54:12 +03:00
|
|
|
}
|
|
|
|
//------------------------
|
2021-05-08 13:32:29 +03:00
|
|
|
// s,e = re.find_imp(in_txt[i..])
|
2021-03-20 02:54:12 +03:00
|
|
|
//------------------------
|
|
|
|
if s >= 0 && e > s {
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]")
|
2021-03-20 02:54:12 +03:00
|
|
|
re.flag = old_flag
|
2021-10-12 06:03:23 +03:00
|
|
|
mut gi := 0
|
|
|
|
for gi < re.groups.len {
|
|
|
|
re.groups[gi] += i
|
|
|
|
gi++
|
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
return i + s, i + e
|
2021-01-03 18:59:00 +03:00
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 02:54:12 +03:00
|
|
|
re.flag = old_flag
|
2021-01-03 18:59:00 +03:00
|
|
|
return -1, -1
|
|
|
|
}
|
2020-12-24 08:27:46 +03:00
|
|
|
|
|
|
|
// find_all find all the non overlapping occurrences of the match pattern
|
|
|
|
[direct_array_access]
|
|
|
|
pub fn (mut re RE) find_all(in_txt string) []int {
|
2021-05-08 13:32:29 +03:00
|
|
|
// old_flag := re.flag
|
2021-07-14 22:20:05 +03:00
|
|
|
// re.flag |= f_src // enable search mode
|
2021-03-20 02:54:12 +03:00
|
|
|
|
2020-12-24 08:27:46 +03:00
|
|
|
mut i := 0
|
|
|
|
mut res := []int{}
|
|
|
|
|
|
|
|
for i < in_txt.len {
|
2020-12-26 23:08:56 +03:00
|
|
|
mut s := -1
|
|
|
|
mut e := -1
|
|
|
|
unsafe {
|
2021-07-14 22:20:05 +03:00
|
|
|
// tmp_str := in_txt[i..]
|
|
|
|
// tmp_str := tos(in_txt.str + i, in_txt.len - i)
|
|
|
|
// println("Check: [$tmp_str]")
|
|
|
|
s, e = re.match_base(in_txt.str + i, in_txt.len + 1 - i)
|
|
|
|
|
|
|
|
if s >= 0 && e > s {
|
|
|
|
res << i + s
|
|
|
|
res << i + e
|
|
|
|
i += e
|
|
|
|
continue
|
|
|
|
}
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-07-14 22:20:05 +03:00
|
|
|
i++
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
// re.flag = old_flag
|
2020-12-24 08:27:46 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// find_all_str find all the non overlapping occurrences of the match pattern, return a string list
|
|
|
|
[direct_array_access]
|
|
|
|
pub fn (mut re RE) find_all_str(in_txt string) []string {
|
2021-07-14 22:20:05 +03:00
|
|
|
// old_flag := re.flag
|
|
|
|
// re.flag |= f_src // enable search mode
|
|
|
|
|
2020-12-24 08:27:46 +03:00
|
|
|
mut i := 0
|
|
|
|
mut res := []string{}
|
|
|
|
|
|
|
|
for i < in_txt.len {
|
2020-12-26 23:08:56 +03:00
|
|
|
mut s := -1
|
|
|
|
mut e := -1
|
|
|
|
unsafe {
|
2021-07-14 22:20:05 +03:00
|
|
|
// tmp_str := in_txt[i..]
|
|
|
|
// tmp_str := tos(in_txt.str + i, in_txt.len - i)
|
|
|
|
// println("Check: [$tmp_str]")
|
|
|
|
s, e = re.match_base(in_txt.str + i, in_txt.len + 1 - i)
|
|
|
|
|
|
|
|
if s >= 0 && e > s {
|
|
|
|
tmp_str := tos(in_txt.str + i, in_txt.len - i)
|
2021-09-05 04:48:59 +03:00
|
|
|
mut tmp_e := if e > tmp_str.len { tmp_str.len } else { e }
|
2021-07-14 22:20:05 +03:00
|
|
|
// println("Found: $s:$e [${tmp_str[s..e]}]")
|
2021-09-05 04:48:59 +03:00
|
|
|
res << tmp_str[..tmp_e]
|
2021-07-14 22:20:05 +03:00
|
|
|
i += e
|
|
|
|
continue
|
|
|
|
}
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-07-14 22:20:05 +03:00
|
|
|
i++
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-07-14 22:20:05 +03:00
|
|
|
// re.flag = old_flag
|
2020-12-24 08:27:46 +03:00
|
|
|
return res
|
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
|
2020-12-24 08:27:46 +03:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Replacers
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2021-04-03 23:16:56 +03:00
|
|
|
// replace_simple return a string where the matches are replaced with the replace string
|
|
|
|
pub fn (mut re RE) replace_simple(in_txt string, repl string) string {
|
2020-12-24 08:27:46 +03:00
|
|
|
pos := re.find_all(in_txt)
|
|
|
|
|
|
|
|
if pos.len > 0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
mut res := ''
|
2020-12-24 08:27:46 +03:00
|
|
|
mut i := 0
|
|
|
|
|
|
|
|
mut s1 := 0
|
|
|
|
mut e1 := in_txt.len
|
|
|
|
|
|
|
|
for i < pos.len {
|
|
|
|
e1 = pos[i]
|
|
|
|
res += in_txt[s1..e1] + repl
|
2021-05-08 13:32:29 +03:00
|
|
|
s1 = pos[i + 1]
|
2020-12-24 08:27:46 +03:00
|
|
|
i += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
res += in_txt[s1..]
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return in_txt
|
|
|
|
}
|
|
|
|
|
|
|
|
// type of function used for custom replace
|
|
|
|
// in_txt source text
|
|
|
|
// start index of the start of the match in in_txt
|
|
|
|
// end index of the end of the match in in_txt
|
|
|
|
// the match is in in_txt[start..end]
|
2021-01-18 15:20:06 +03:00
|
|
|
pub type FnReplace = fn (re RE, in_txt string, start int, end int) string
|
2020-12-24 08:27:46 +03:00
|
|
|
|
|
|
|
// replace_by_fn return a string where the matches are replaced with the string from the repl_fn callback function
|
|
|
|
pub fn (mut re RE) replace_by_fn(in_txt string, repl_fn FnReplace) string {
|
2021-05-08 13:32:29 +03:00
|
|
|
mut i := 0
|
2021-03-20 02:54:12 +03:00
|
|
|
mut res := strings.new_builder(in_txt.len)
|
2021-05-08 13:32:29 +03:00
|
|
|
mut last_end := 0
|
2020-12-24 08:27:46 +03:00
|
|
|
|
|
|
|
for i < in_txt.len {
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("Find Start. $i [${in_txt[i..]}]")
|
|
|
|
s, e := re.find_from(in_txt, i)
|
|
|
|
// println("Find End.")
|
|
|
|
if s >= 0 && e > s {
|
|
|
|
// println("find match in: ${s},${e} [${in_txt[s..e]}]")
|
|
|
|
|
2021-03-20 02:54:12 +03:00
|
|
|
if last_end < s {
|
|
|
|
res.write_string(in_txt[last_end..s])
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-10-12 06:03:23 +03:00
|
|
|
/*
|
2021-05-08 13:32:29 +03:00
|
|
|
for g_i in 0 .. re.group_count {
|
2021-12-01 09:38:50 +03:00
|
|
|
re.groups[g_i * 2] += i
|
|
|
|
re.groups[(g_i * 2) + 1] += i
|
2021-03-20 02:54:12 +03:00
|
|
|
}
|
2021-10-12 06:03:23 +03:00
|
|
|
*/
|
2021-03-20 02:54:12 +03:00
|
|
|
repl := repl_fn(re, in_txt, s, e)
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("repl res: $repl")
|
2021-03-20 02:54:12 +03:00
|
|
|
res.write_string(repl)
|
2021-05-08 13:32:29 +03:00
|
|
|
// res.write_string("[[${in_txt[s..e]}]]")
|
|
|
|
|
2021-03-20 02:54:12 +03:00
|
|
|
last_end = e
|
|
|
|
i = e
|
2020-12-24 08:27:46 +03:00
|
|
|
} else {
|
2021-03-20 02:54:12 +03:00
|
|
|
break
|
2021-05-08 13:32:29 +03:00
|
|
|
// i++
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
// println(i)
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-03-20 02:54:12 +03:00
|
|
|
if last_end >= 0 && last_end < in_txt.len {
|
|
|
|
res.write_string(in_txt[last_end..])
|
|
|
|
}
|
|
|
|
return res.str()
|
2020-12-24 08:27:46 +03:00
|
|
|
}
|
2021-04-03 23:16:56 +03:00
|
|
|
|
|
|
|
fn (re RE) parsed_replace_string(in_txt string, repl string) string {
|
2021-05-08 13:32:29 +03:00
|
|
|
str_lst := repl.split('\\')
|
2021-04-03 23:16:56 +03:00
|
|
|
mut res := str_lst[0]
|
|
|
|
mut i := 1
|
|
|
|
for i < str_lst.len {
|
|
|
|
tmp := str_lst[i]
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("tmp: ${tmp}")
|
2021-04-03 23:16:56 +03:00
|
|
|
if tmp.len > 0 && tmp[0] >= `0` && tmp[0] <= `9` {
|
|
|
|
group_id := int(tmp[0] - `0`)
|
|
|
|
group := re.get_group_by_id(in_txt, group_id)
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("group: $group_id [$group]")
|
|
|
|
res += '$group${tmp[1..]}'
|
2021-04-03 23:16:56 +03:00
|
|
|
} else {
|
2021-05-08 13:32:29 +03:00
|
|
|
res += '\\' + tmp
|
2021-04-03 23:16:56 +03:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
// replace return a string where the matches are replaced with the repl_str string,
|
2021-04-03 23:16:56 +03:00
|
|
|
// this function support use groups in the replace string
|
|
|
|
pub fn (mut re RE) replace(in_txt string, repl_str string) string {
|
2021-05-08 13:32:29 +03:00
|
|
|
mut i := 0
|
2021-04-03 23:16:56 +03:00
|
|
|
mut res := strings.new_builder(in_txt.len)
|
2021-05-08 13:32:29 +03:00
|
|
|
mut last_end := 0
|
2021-04-03 23:16:56 +03:00
|
|
|
|
|
|
|
for i < in_txt.len {
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("Find Start. $i [${in_txt[i..]}]")
|
|
|
|
s, e := re.find_from(in_txt, i)
|
|
|
|
// println("Find End.")
|
|
|
|
if s >= 0 && e > s {
|
|
|
|
// println("find match in: ${s},${e} [${in_txt[s..e]}]")
|
|
|
|
|
2021-04-03 23:16:56 +03:00
|
|
|
if last_end < s {
|
|
|
|
res.write_string(in_txt[last_end..s])
|
|
|
|
}
|
2021-10-12 06:03:23 +03:00
|
|
|
/*
|
2021-05-08 13:32:29 +03:00
|
|
|
for g_i in 0 .. re.group_count {
|
2021-12-01 09:38:50 +03:00
|
|
|
re.groups[g_i * 2] += i
|
|
|
|
re.groups[(g_i * 2) + 1] += i
|
2021-04-03 23:16:56 +03:00
|
|
|
}
|
2021-10-12 06:03:23 +03:00
|
|
|
*/
|
2021-05-08 13:32:29 +03:00
|
|
|
// repl := repl_fn(re, in_txt, s, e)
|
2021-04-03 23:16:56 +03:00
|
|
|
repl := re.parsed_replace_string(in_txt, repl_str)
|
2021-05-08 13:32:29 +03:00
|
|
|
// println("repl res: $repl")
|
2021-04-03 23:16:56 +03:00
|
|
|
res.write_string(repl)
|
2021-05-08 13:32:29 +03:00
|
|
|
// res.write_string("[[${in_txt[s..e]}]]")
|
|
|
|
|
2021-04-03 23:16:56 +03:00
|
|
|
last_end = e
|
|
|
|
i = e
|
|
|
|
} else {
|
|
|
|
break
|
2021-05-08 13:32:29 +03:00
|
|
|
// i++
|
2021-04-03 23:16:56 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
// println(i)
|
2021-04-03 23:16:56 +03:00
|
|
|
}
|
|
|
|
if last_end >= 0 && last_end < in_txt.len {
|
|
|
|
res.write_string(in_txt[last_end..])
|
|
|
|
}
|
|
|
|
return res.str()
|
2021-05-08 13:32:29 +03:00
|
|
|
}
|