Skip to content

Commit

Permalink
Merge pull request #4556 from cornishon/index_multi
Browse files Browse the repository at this point in the history
improve `strings.index_multi`
  • Loading branch information
laytan authored Dec 4, 2024
2 parents c79466a + ce51b79 commit cdb86d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/strings/strings.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,8 @@ index_multi :: proc(s: string, substrs: []string) -> (idx: int, width: int) {
lowest_index := len(s)
found := false
for substr in substrs {
if i := index(s, substr); i >= 0 {
haystack := s[:min(len(s), lowest_index + len(substr))]
if i := index(haystack, substr); i >= 0 {
if i < lowest_index {
lowest_index = i
width = len(substr)
Expand Down
19 changes: 19 additions & 0 deletions tests/core/strings/test_core_strings.odin
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ test_last_index_any_small_string_not_found :: proc(t: ^testing.T) {
testing.expect(t, index == -1, "last_index_any should be -1")
}

@test
test_index_multi_overlapping_substrs :: proc(t: ^testing.T) {
index, width := strings.index_multi("some example text", {"ample", "exam"})
testing.expect_value(t, index, 5)
testing.expect_value(t, width, 4)
}

@test
test_index_multi_not_found :: proc(t: ^testing.T) {
index, _ := strings.index_multi("some example text", {"ey", "tey"})
testing.expect_value(t, index, -1)
}

@test
test_index_multi_with_empty_string :: proc(t: ^testing.T) {
index, _ := strings.index_multi("some example text", {"ex", ""})
testing.expect_value(t, index, -1)
}

Cut_Test :: struct {
input: string,
offset: int,
Expand Down

0 comments on commit cdb86d6

Please sign in to comment.