From 82896f05d5843aa7312f0d0d43b6e565bfde15aa Mon Sep 17 00:00:00 2001 From: Alexey Slivkin <88584136+alexey-slivkin@users.noreply.github.com> Date: Fri, 12 Apr 2024 01:44:31 +0300 Subject: [PATCH 1/2] Update helper.go Performance boost --- helper.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/helper.go b/helper.go index 16d4a61..069e353 100644 --- a/helper.go +++ b/helper.go @@ -7,10 +7,11 @@ import ( "unicode" ) +var selectCapitalRegexp = regexp.MustCompile(SelectCapital) + func caseHelper(input string, isCamel bool, rule ...string) []string { if !isCamel { - re := regexp.MustCompile(SelectCapital) - input = re.ReplaceAllString(input, ReplaceCapital) + input = selectCapitalRegexp.ReplaceAllString(input, ReplaceCapital) } input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ") if len(rule) > 0 && len(rule)%2 != 0 { From b4c25dec915eb8c763235fcd178ec2d6f7bdd48f Mon Sep 17 00:00:00 2001 From: Alexey Slivkin <88584136+alexey-slivkin@users.noreply.github.com> Date: Fri, 12 Apr 2024 01:56:13 +0300 Subject: [PATCH 2/2] Update stringy.go Remove recompilation regexp --- stringy.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stringy.go b/stringy.go index d8a0161..bfcad0d 100644 --- a/stringy.go +++ b/stringy.go @@ -10,6 +10,8 @@ import ( "unicode" ) +var matchWordRegexp = regexp.MustCompile(`[\s]*[\W]\pN`) + // input is struct that holds input from user and result type input struct { Input string @@ -228,9 +230,8 @@ func (i *input) LcFirst() string { // Lines returns slice of strings by removing white space characters func (i *input) Lines() []string { - input := getInput(*i) - matchWord := regexp.MustCompile(`[\s]*[\W]\pN`) - result := matchWord.ReplaceAllString(input, " ") + input := getInput(*i) + result := matchWordRegexp.ReplaceAllString(input, " ") return strings.Fields(strings.TrimSpace(result)) }