Skip to content

Commit c0fa596

Browse files
dec05ebaandrewrk
authored andcommitted
Make lastIndexOf use the same cut-off between BMH as indexOf
Also update test to use a string longer than 52 characters to test both BMH and linear path.
1 parent 72064eb commit c0fa596

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/std/mem.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
907907
if (needle.len > haystack.len) return null;
908908
if (needle.len == 0) return haystack.len;
909909

910-
if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 32 or needle.len <= 2)
910+
if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 52 or needle.len <= 4)
911911
return lastIndexOfLinear(T, haystack, needle);
912912

913913
const haystack_bytes = sliceAsBytes(haystack);
@@ -951,10 +951,10 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee
951951
}
952952

953953
test "mem.indexOf" {
954-
testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "three four").? == 8);
955-
testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "three four").? == 8);
956-
testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "two two") == null);
957-
testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "two two") == null);
954+
testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
955+
testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
956+
testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null);
957+
testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null);
958958

959959
testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "").? == 0);
960960
testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "").? == 48);

0 commit comments

Comments
 (0)