| ... | ... | @@ -895,7 +895,8 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void { |
| 895 | 895 | /// To start looking at a different index, slice the haystack first. |
| 896 | 896 | // Reverse boyer-moore-horspool algorithm |
| 897 | 897 | pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 898 | | if (needle.len > haystack.len or needle.len == 0) return null; |
| 898 | if (needle.len > haystack.len) return null; |
| 899 | if (needle.len == 0) return haystack.len; |
| 899 | 900 | |
| 900 | 901 | if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 32 or needle.len <= 2) |
| 901 | 902 | return lastIndexOfLinear(T, haystack, needle); |
| ... | ... | @@ -919,7 +920,8 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us |
| 919 | 920 | |
| 920 | 921 | // Boyer-moore-horspool algorithm |
| 921 | 922 | pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 922 | | if (needle.len > haystack.len or needle.len == 0) return null; |
| 923 | if (needle.len > haystack.len) return null; |
| 924 | if (needle.len == 0) return 0; |
| 923 | 925 | |
| 924 | 926 | if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 32 or needle.len <= 2) |
| 925 | 927 | return indexOfPosLinear(T, haystack, start_index, needle); |
| ... | ... | @@ -945,6 +947,9 @@ test "mem.indexOf" { |
| 945 | 947 | testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "two two") == null); |
| 946 | 948 | testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "two two") == null); |
| 947 | 949 | |
| 950 | testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "").? == 0); |
| 951 | testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "").? == 48); |
| 952 | |
| 948 | 953 | testing.expect(indexOf(u8, "one two three four", "four").? == 14); |
| 949 | 954 | testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14); |
| 950 | 955 | testing.expect(indexOf(u8, "one two three four", "gour") == null); |