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