Skip to content

Commit

Permalink
gstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
snail007 committed Dec 26, 2023
1 parent 5554cae commit a0b61cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions util/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ func HasSuffixAny(str string, suffix ...string) bool {
return false
}

func ContainsAny(str string, sub ...string) bool {
if len(sub) == 0 {
return true
}
for _, v := range sub {
if strings.Contains(str, v) {
return true
}
}
return false
}

func ContainsAll(str string, sub ...string) bool {
if len(sub) == 0 {
return true
}
for _, v := range sub {
if !strings.Contains(str, v) {
return false
}
}
return true
}

func HasHTTPPrefix(str string) bool {
return HasPrefixAny(strings.ToLower(str), "http://", "https://")
}
Expand Down
12 changes: 12 additions & 0 deletions util/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ func TestReplace(t *testing.T) {
}
}
}

func TestContainsAny(t *testing.T) {
assert.False(t, ContainsAny("abcd", "aa", "aaa", "aaa"))
assert.True(t, ContainsAny("abcd", "ab", "bc", "cde"))
assert.True(t, ContainsAny("abcd"))
}

func TestContainsAll(t *testing.T) {
assert.False(t, ContainsAll("abcd", "a", "aaa", "aaa"))
assert.True(t, ContainsAll("abcd", "ab", "bc", "cd"))
assert.True(t, ContainsAll("abcd"))
}

0 comments on commit a0b61cd

Please sign in to comment.