Skip to content

Commit

Permalink
Merge pull request #108 from projectdiscovery/issue-105-containsanyi
Browse files Browse the repository at this point in the history
ContainsAnyI func
  • Loading branch information
Mzack9999 authored Mar 18, 2023
2 parents 2c9818e + 6fcea80 commit 111dea2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion strings/stringsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func Reverse(s string) string {
return string(rune)
}

// ContainsAny returns true is s contains any specified substring
// ContainsAny returns true if s contains any specified substring.
func ContainsAny(s string, ss ...string) bool {
for _, sss := range ss {
if strings.Contains(s, sss) {
Expand All @@ -140,6 +140,17 @@ func ContainsAny(s string, ss ...string) bool {
return false
}

// ContainsAnyI returns true if s contains any specified substring (case-insensitive).
func ContainsAnyI(s string, ss ...string) bool {
s = strings.ToLower(s)
for _, sss := range ss {
if strings.Contains(s, strings.ToLower(sss)) {
return true
}
}
return false
}

// EqualFoldAny returns true if s is equal to any specified substring
func EqualFoldAny(s string, ss ...string) bool {
for _, sss := range ss {
Expand Down
14 changes: 14 additions & 0 deletions strings/stringsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ func TestContainsAny(t *testing.T) {
}
}

func TestContainsAnyI(t *testing.T) {
tests := map[string]containstest{
"abc": {Items: []string{"A", "b"}, Result: true},
"abcd": {Items: []string{"X", "b"}, Result: true},
"A b C": {Items: []string{"X"}, Result: false},
"aaa": {Items: []string{"A"}, Result: true},
"Hello!": {Items: []string{"hELLO", "world"}, Result: true},
}
for str, test := range tests {
res := ContainsAnyI(str, test.Items...)
require.Equalf(t, test.Result, res, "test: %+v", res)
}
}

func TestEqualFoldAny(t *testing.T) {
tests := map[string]containstest{
"abc": {Items: []string{"a", "Abc"}, Result: true},
Expand Down

0 comments on commit 111dea2

Please sign in to comment.