aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaytan <laytanlaats@hotmail.com>2024-12-04 22:01:49 +0100
committerGitHub <noreply@github.com>2024-12-04 22:01:49 +0100
commitcdb86d69b3056eb798688f1db1645dfc4e239099 (patch)
tree9c8ca4d2d40ed9994a890ad13702d38bead5936b
parentc79466ab3cdc95d436752d37370336423c749211 (diff)
parentce51b79a37f61b56c40cdac489c815a792ad445f (diff)
Merge pull request #4556 from cornishon/index_multi
improve `strings.index_multi`
-rw-r--r--core/strings/strings.odin3
-rw-r--r--tests/core/strings/test_core_strings.odin19
2 files changed, 21 insertions, 1 deletions
diff --git a/core/strings/strings.odin b/core/strings/strings.odin
index af93ff33c..c014d2b2b 100644
--- a/core/strings/strings.odin
+++ b/core/strings/strings.odin
@@ -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)
diff --git a/tests/core/strings/test_core_strings.odin b/tests/core/strings/test_core_strings.odin
index 0d94b9c62..44c79ce73 100644
--- a/tests/core/strings/test_core_strings.odin
+++ b/tests/core/strings/test_core_strings.odin
@@ -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,