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 {...@@ -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 algorithm889// Reverse boyer-moore-horspool algorithm
890pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {890pub 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);
893893
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..]);
897901
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 }
904908
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
907911
908// Boyer-moore-horspool algorithm912// Boyer-moore-horspool algorithm
909pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {913pub 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);
912916
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..]);
916924
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 }
922930
923 return null;931 return null;