Skip to content

Commit 1bc1ce6

Browse files
authored
Merge pull request #134 from projectdiscovery/feat-index-any
Adding IndexAny strings helper
2 parents 4543cf4 + 0f3a0aa commit 1bc1ce6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

strings/stringsutil.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,13 @@ func Truncate(data string, maxSize int) string {
282282
}
283283
return data
284284
}
285+
286+
// IndexAny returns the index of the first instance of any of the specified substrings in s, or -1 if s does not contain any of the substrings.
287+
func IndexAny(s string, seps ...string) (int, string) {
288+
for _, sep := range seps {
289+
if idx := strings.Index(s, sep); idx >= 0 {
290+
return idx, sep
291+
}
292+
}
293+
return -1, ""
294+
}

strings/stringsutil_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,21 @@ func TestTruncate(t *testing.T) {
359359
require.Equalf(t, test.result, res, "test:%s maxsize: %d result: %s", test.test, test.maxSize, res)
360360
}
361361
}
362+
363+
func TestIndexAny(t *testing.T) {
364+
tests := []struct {
365+
s string
366+
seps []string
367+
expectedIdx int
368+
expectedSep string
369+
}{
370+
{"abcdefg", []string{"a", "b"}, 0, "a"},
371+
{"abcdefg", []string{"z", "b"}, 1, "b"},
372+
{"abcdefg", []string{"z", "zz"}, -1, ""},
373+
}
374+
for _, test := range tests {
375+
idx, sep := IndexAny(test.s, test.seps...)
376+
require.Equal(t, test.expectedIdx, idx)
377+
require.Equal(t, test.expectedSep, sep)
378+
}
379+
}

0 commit comments

Comments
 (0)