authorgravatar for dec05eba@protonmail.comdec05eba <dec05eba@protonmail.com> 2020-09-05 11:46:41+02:00
committergravatar for dec05eba@protonmail.comdec05eba <dec05eba@protonmail.com> 2020-09-05 12:02:18+02:00
logd012507a8fc911b662085a70bee2d580a404bb81
tree476bcce3284e7952dbb5b1a360e7b8084c46d10f
parente297b4815ca6a30c6d20fc8dc8aa186faa1384b7

Use boyer-moore-horspool with all types of T


1 files changed, 19 insertions(+), 11 deletions(-)

lib/std/mem.zig+19-11
......@@ -888,18 +888,22 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: []usize) void {
888888/// To start looking at a different index, slice the haystack first.
889889// Reverse boyer-moore-horspool algorithm
890890pub 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)
892892 return lastIndexOfNaive(T, haystack, needle);
893893
894894 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
895899 var table: [256]usize = undefined;
896 boyerMooreHorspoolPreprocess(needle, table[0..]);
900 boyerMooreHorspoolPreprocess(needleU8, table[0..]);
897901
898902 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]];
903907 }
904908
905909 return null;
......@@ -907,17 +911,21 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
907911
908912// Boyer-moore-horspool algorithm
909913pub 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)
911915 return indexOfPosNaive(T, haystack, start_index, needle);
912916
913917 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
914922 var table: [256]usize = undefined;
915 boyerMooreHorspoolPreprocess(needle, table[0..]);
923 boyerMooreHorspoolPreprocess(needleU8, table[0..]);
916924
917925 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]];
921929 }
922930
923931 return null;