Skip to content

Commit

Permalink
Merge pull request #446 from projectdiscovery/add_containsall_func
Browse files Browse the repository at this point in the history
add `stringsutil.ContainsAll` func
  • Loading branch information
dogancanbakir committed Jun 25, 2024
2 parents 23cc2c3 + 0b2dedf commit 08a61a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions strings/stringsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,13 @@ func IndexAny(s string, seps ...string) (int, string) {
}
return -1, ""
}

// ContainsAll returns true if s contains all specified substrings.
func ContainsAll(s string, ss ...string) bool {
for _, sub := range ss {
if !strings.Contains(s, sub) {
return false
}
}
return true
}
17 changes: 17 additions & 0 deletions strings/stringsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,20 @@ func TestIndexAny(t *testing.T) {
require.Equal(t, test.expectedSep, sep)
}
}

func TestContainsAll(t *testing.T) {
tests := []struct {
s string
ss []string
result bool
}{
{"abcdefg", []string{"a", "b"}, true},
{"abcdefg", []string{"a", "z"}, false},
{"abcdefg", []string{"a", "b", "c", "d", "e", "f", "g"}, true},
{"abcdefg", []string{"a", "b", "c", "d", "e", "f", "g", "z"}, false},
}
for _, test := range tests {
res := ContainsAll(test.s, test.ss...)
require.Equal(t, test.result, res)
}
}

0 comments on commit 08a61a6

Please sign in to comment.