From fb373c0d367edffcd70dd59baa6a48a57814e6a3 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 16 Oct 2023 13:46:42 +0300 Subject: [PATCH] [strutil] Add method 'ReplaceIgnoreCase' --- CHANGELOG.md | 4 ++++ ek.go | 2 +- strutil/example_test.go | 9 +++++++++ strutil/strutil.go | 35 ++++++++++++++++++++++++++++++++--- strutil/strutil_test.go | 13 +++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63237135..9dc4f53f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### 12.81.0 + +* `[strutil]` Added method `ReplaceIgnoreCase` + ### 12.80.0 * `[system]` Added ANSI color info to `OSInfo` diff --git a/ek.go b/ek.go index 71e3c8e3..d5cfa933 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.80.0" +const VERSION = "12.81.0" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/strutil/example_test.go b/strutil/example_test.go index 3f0464cd..b9205a79 100644 --- a/strutil/example_test.go +++ b/strutil/example_test.go @@ -122,6 +122,15 @@ func ExampleReplaceAll() { // M???ag? } +func ExampleReplaceIgnoreCase() { + fmt.Println(ReplaceIgnoreCase( + "User bob has no item. Add items to user Bob?", "bob", "[Bob]", + )) + + // Output: + // User [Bob] has no item. Add items to user [Bob]? +} + func ExampleFields() { fmt.Printf("%#v\n", Fields("Bob Alice, 'Mary Key', \"John Dow\"")) diff --git a/strutil/strutil.go b/strutil/strutil.go index 7666d431..186904ae 100644 --- a/strutil/strutil.go +++ b/strutil/strutil.go @@ -234,8 +234,8 @@ func SuffixSize(str string, suffix rune) int { return result } -// ReplaceAll replaces all symbols in given string -func ReplaceAll(source, from, to string) string { +// ReplaceAll replaces all symbols from replset in string +func ReplaceAll(source, replset, to string) string { if source == "" { return "" } @@ -244,7 +244,7 @@ func ReplaceAll(source, from, to string) string { SOURCELOOP: for _, sourceSym := range source { - for _, fromSym := range from { + for _, fromSym := range replset { if fromSym == sourceSym { result.WriteString(to) continue SOURCELOOP @@ -257,6 +257,35 @@ SOURCELOOP: return result.String() } +// ReplaceIgnoreCase replaces part of the string ignoring case +func ReplaceIgnoreCase(source, from, to string) string { + if source == "" || from == "" { + return source + } + + var result strings.Builder + + from = strings.ToLower(from) + lowSource := strings.ToLower(source) + + for { + index := strings.Index(lowSource, from) + + if index == -1 { + result.WriteString(source) + break + } + + result.WriteString(source[:index]) + result.WriteString(to) + + source = source[index+len(from):] + lowSource = lowSource[index+len(from):] + } + + return result.String() +} + // Exclude excludes substring from given string func Exclude(data, substr string) string { if len(data) == 0 || len(substr) == 0 { diff --git a/strutil/strutil_test.go b/strutil/strutil_test.go index 5e2f38f1..0acb6a86 100644 --- a/strutil/strutil_test.go +++ b/strutil/strutil_test.go @@ -125,6 +125,13 @@ func (s *StrUtilSuite) TestReplaceAll(c *C) { c.Assert(ReplaceAll("", "AB12", "?"), Equals, "") } +func (s *StrUtilSuite) TestReplaceIgnoreCase(c *C) { + c.Assert(ReplaceIgnoreCase("ABCD1234abcd1234AbCd11ABcd", "abcd", "????"), Equals, "????1234????1234????11????") + c.Assert(ReplaceIgnoreCase("TESTtestTEST", "abcd", "????"), Equals, "TESTtestTEST") + c.Assert(ReplaceIgnoreCase("", "abcd", "????"), Equals, "") + c.Assert(ReplaceIgnoreCase("ABCD1234abcd1234AbCd11ABcd", "abcd", ""), Equals, "1234123411") +} + func (s *StrUtilSuite) TestFields(c *C) { c.Assert(Fields(""), IsNil) c.Assert(Fields(""), HasLen, 0) @@ -252,3 +259,9 @@ func (s *StrUtilSuite) BenchmarkReplaceAll(c *C) { ReplaceAll("ABCDABCD12341234", "AB12", "?") } } + +func (s *StrUtilSuite) BenchmarkReplaceIgnoreCase(c *C) { + for i := 0; i < c.N; i++ { + ReplaceIgnoreCase("ABCD1234abcd1234AbCd11ABcd", "abcd", "????") + } +}