| ... | ... | @@ -888,18 +888,22 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: []usize) void { |
| 888 | 888 | /// To start looking at a different index, slice the haystack first. |
| 889 | 889 | // Reverse boyer-moore-horspool algorithm |
| 890 | 890 | pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 891 | | if (T != u8 or haystack.len < 32 or needle.len <= 2) |
| 891 | if (haystack.len < 32 or needle.len <= 2) |
| 892 | 892 | return lastIndexOfNaive(T, haystack, needle); |
| 893 | 893 | |
| 894 | 894 | if (needle.len > haystack.len or needle.len == 0) return null; |
| 895 | |
| 896 | const haystackU8 = @bitCast([]const u8, haystack); |
| 897 | const needleU8 = @bitCast([]const u8, needle); |
| 898 | |
| 895 | 899 | var table: [256]usize = undefined; |
| 896 | | boyerMooreHorspoolPreprocess(needle, table[0..]); |
| 900 | boyerMooreHorspoolPreprocess(needleU8, table[0..]); |
| 897 | 901 | |
| 898 | 902 | var i: usize = 0; |
| 899 | | while (i <= haystack.len - needle.len) { |
| 900 | | const reverseIndex = haystack.len - i - needle.len - 1; |
| 901 | | if (mem.eql(T, haystack[reverseIndex .. reverseIndex + needle.len], needle)) return i; |
| 902 | | i += table[haystack[reverseIndex + needle.len - 1]]; |
| 903 | while (i <= haystackU8.len - needleU8.len) { |
| 904 | const reverseIndex = haystackU8.len - i - needleU8.len - 1; |
| 905 | if (mem.eql(u8, haystackU8[reverseIndex .. reverseIndex + needleU8.len], needleU8)) return i; |
| 906 | i += table[haystackU8[reverseIndex + needleU8.len - 1]]; |
| 903 | 907 | } |
| 904 | 908 | |
| 905 | 909 | return null; |
| ... | ... | @@ -907,17 +911,21 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us |
| 907 | 911 | |
| 908 | 912 | // Boyer-moore-horspool algorithm |
| 909 | 913 | pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 910 | | if (T != u8 or haystack.len < 32 or needle.len <= 2) |
| 914 | if (haystack.len < 32 or needle.len <= 2) |
| 911 | 915 | return indexOfPosNaive(T, haystack, start_index, needle); |
| 912 | 916 | |
| 913 | 917 | if (needle.len > haystack.len or needle.len == 0) return null; |
| 918 | |
| 919 | const haystackU8 = @bitCast([]const u8, haystack); |
| 920 | const needleU8 = @bitCast([]const u8, needle); |
| 921 | |
| 914 | 922 | var table: [256]usize = undefined; |
| 915 | | boyerMooreHorspoolPreprocess(needle, table[0..]); |
| 923 | boyerMooreHorspoolPreprocess(needleU8, table[0..]); |
| 916 | 924 | |
| 917 | 925 | var i: usize = start_index; |
| 918 | | while (i <= haystack.len - needle.len) { |
| 919 | | if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i; |
| 920 | | i += table[haystack[i + needle.len - 1]]; |
| 926 | while (i <= haystackU8.len - needleU8.len) { |
| 927 | if (mem.eql(u8, haystackU8[i .. i + needleU8.len], needleU8)) return i; |
| 928 | i += table[haystackU8[i + needleU8.len - 1]]; |
| 921 | 929 | } |
| 922 | 930 | |
| 923 | 931 | return null; |