Skip to content

Commit

Permalink
[strutil] Add method 'ReplaceIgnoreCase'
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 16, 2023
1 parent 3154691 commit fb373c0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.81.0

* `[strutil]` Added method `ReplaceIgnoreCase`

### 12.80.0

* `[system]` Added ANSI color info to `OSInfo`
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.80.0"
const VERSION = "12.81.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
9 changes: 9 additions & 0 deletions strutil/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\""))

Expand Down
35 changes: 32 additions & 3 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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", "????")
}
}

0 comments on commit fb373c0

Please sign in to comment.