authorgravatar for dec05eba@protonmail.comdec05eba <dec05eba@protonmail.com> 2020-10-24 03:49:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-25 21:17:22-04:00
logc0fa5963ee692c9d4c46834978c274559d727ff5
tree8a7d16a162d4764adf8a0a83ff2482922addb529
parent72064eba233fd3fd8e3dc0ab6f5fc73c6bdf1922

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 files changed, 5 insertions(+), 5 deletions(-)

lib/std/mem.zig+5-5
...@@ -907,7 +907,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us...@@ -907,7 +907,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
907 if (needle.len > haystack.len) return null;907 if (needle.len > haystack.len) return null;
908 if (needle.len == 0) return haystack.len;908 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)
911 return lastIndexOfLinear(T, haystack, needle);911 return lastIndexOfLinear(T, haystack, needle);
912912
913 const haystack_bytes = sliceAsBytes(haystack);913 const haystack_bytes = sliceAsBytes(haystack);
...@@ -951,10 +951,10 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee...@@ -951,10 +951,10 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee
951}951}
952952
953test "mem.indexOf" {953test "mem.indexOf" {
954 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "three four").? == 8);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", "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", "two two") == null);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", "two two") == null);957 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null);
958958
959 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "").? == 0);959 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "").? == 0);
960 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "").? == 48);960 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "").? == 48);