diff --git a/pkg/util/tools.go b/pkg/util/tools.go index 3fb56bcc9..1637144e8 100644 --- a/pkg/util/tools.go +++ b/pkg/util/tools.go @@ -16,6 +16,28 @@ type RegexRepl struct { Repl string } +// ToRegexRepl convert slice of regex into a slice of RegexRepl +func ToRegexRepl(in []string) RegexReplList { + out := make([]RegexRepl, 0) + idx := 0 + for idx < len(in)-1 { + regex, repl := in[idx], in[idx+1] + out = append(out, RegexRepl{ + Regex: regexp.MustCompile(regex), + Repl: repl, + }) + idx = idx + 2 + } + return out +} + +func (rr RegexReplList) Replace(str string) string { + for _, aa := range rr { + str = aa.Regex.ReplaceAllLiteralString(str, aa.Repl) + } + return str +} + // DecodeHexInString decode and replace all hex value in a given string constitued of "key=value". func DecodeHexInString(str string) string { toDecode := []string{"name", "comm", "profile"} @@ -45,25 +67,3 @@ func RemoveDuplicate[T comparable](inlist []T) []T { } return list } - -// ToRegexRepl convert slice of regex into a slice of RegexRepl -func ToRegexRepl(in []string) RegexReplList { - out := make([]RegexRepl, 0) - idx := 0 - for idx < len(in)-1 { - regex, repl := in[idx], in[idx+1] - out = append(out, RegexRepl{ - Regex: regexp.MustCompile(regex), - Repl: repl, - }) - idx = idx + 2 - } - return out -} - -func (rr RegexReplList) Replace(str string) string { - for _, aa := range rr { - str = aa.Regex.ReplaceAllLiteralString(str, aa.Repl) - } - return str -}