diff --git a/core/strings/strings.odin b/core/strings/strings.odin index 6ace03b48..9179a9046 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -479,7 +479,7 @@ last_index :: proc(s, substr: string) -> int { return -1 } - +// index_any returns the index of the first char of `chars` found in `s`. -1 if not found. index_any :: proc(s, chars: string) -> int { if chars == "" { return -1 @@ -504,8 +504,8 @@ index_any :: proc(s, chars: string) -> int { } } - for c, i in chars { - if index_rune(chars, c) >= 0 { + for c in chars { + if i := index_rune(s, c); i >= 0 { return i } } diff --git a/tests/core/strings/test_core_strings.odin b/tests/core/strings/test_core_strings.odin new file mode 100644 index 000000000..c9da5a688 --- /dev/null +++ b/tests/core/strings/test_core_strings.odin @@ -0,0 +1,29 @@ +package test_core_image + +import "core:strings" +import "core:testing" + +@test +test_index_any_small_string_not_found :: proc(t: ^testing.T) { + index := strings.index_any(".", "/:\"") + testing.log(t, index) + testing.expect(t, index == -1, "index_any should be negative") +} + +@test +test_index_any_larger_string_not_found :: proc(t: ^testing.T) { + index := strings.index_any("aaaaaaaa.aaaaaaaa", "/:\"") + testing.expect(t, index == -1, "index_any should be negative") +} + +@test +test_index_any_small_string_found :: proc(t: ^testing.T) { + index := strings.index_any(".", "/:.\"") + testing.expect(t, index == 0, "index_any should be 0") +} + +@test +test_index_any_larger_string_found :: proc(t: ^testing.T) { + index := strings.index_any("aaaaaaaa:aaaaaaaa", "/:\"") + testing.expect(t, index == 8, "index_any should be 8") +}