authorgravatar for dec05eba@protonmail.comdec05eba <dec05eba@protonmail.com> 2020-09-05 14:17:38+02:00
committergravatar for dec05eba@protonmail.comdec05eba <dec05eba@protonmail.com> 2020-09-05 14:17:38+02:00
loga394a6300cb79ced98d445917c8c73937107e4d1
tree2960e5eaea876ca9e2e16332a7e005f5b3e7ccbb
parentf65f3d24f8c6fcdc54da3a904999405508f0e706

Fix lastIndexOf and add tests that do not fallback to linear search

Use sliceAsBytes to convert []const T to []const u8. Cleanup.

1 files changed, 41 insertions(+), 28 deletions(-)

lib/std/mem.zig+41-28
...@@ -853,9 +853,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize...@@ -853,9 +853,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize
853853
854/// Find the index in a slice of a sub-slice, searching from the end backwards.854/// Find the index in a slice of a sub-slice, searching from the end backwards.
855/// To start looking at a different index, slice the haystack first.855/// To start looking at a different index, slice the haystack first.
856fn lastIndexOfNaive(comptime T: type, haystack: []const T, needle: []const T) ?usize {856fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize {
857 if (needle.len > haystack.len) return null;
858
859 var i: usize = haystack.len - needle.len;857 var i: usize = haystack.len - needle.len;
860 while (true) : (i -= 1) {858 while (true) : (i -= 1) {
861 if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i;859 if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i;
...@@ -863,9 +861,7 @@ fn lastIndexOfNaive(comptime T: type, haystack: []const T, needle: []const T) ?u...@@ -863,9 +861,7 @@ fn lastIndexOfNaive(comptime T: type, haystack: []const T, needle: []const T) ?u
863 }861 }
864}862}
865863
866fn indexOfPosNaive(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {864fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
867 if (needle.len > haystack.len) return null;
868
869 var i: usize = start_index;865 var i: usize = start_index;
870 const end = haystack.len - needle.len;866 const end = haystack.len - needle.len;
871 while (i <= end) : (i += 1) {867 while (i <= end) : (i += 1) {
...@@ -874,6 +870,17 @@ fn indexOfPosNaive(comptime T: type, haystack: []const T, start_index: usize, ne...@@ -874,6 +870,17 @@ fn indexOfPosNaive(comptime T: type, haystack: []const T, start_index: usize, ne
874 return null;870 return null;
875}871}
876872
873fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: []usize) void {
874 for (table) |*c| {
875 c.* = pattern.len;
876 }
877
878 var i: usize = pattern.len - 1;
879 while (i > 0) : (i -= 1) {
880 table[pattern[i]] = i;
881 }
882}
883
877fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: []usize) void {884fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: []usize) void {
878 for (table) |*c| {885 for (table) |*c| {
879 c.* = pattern.len;886 c.* = pattern.len;
...@@ -888,22 +895,23 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: []usize) void {...@@ -888,22 +895,23 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: []usize) void {
888/// To start looking at a different index, slice the haystack first.895/// To start looking at a different index, slice the haystack first.
889// Reverse boyer-moore-horspool algorithm896// Reverse boyer-moore-horspool algorithm
890pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {897pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
891 if (!isValidAlign(T.bit_count) or haystack.len < 32 or needle.len <= 2)
892 return lastIndexOfNaive(T, haystack, needle);
893
894 if (needle.len > haystack.len or needle.len == 0) return null;898 if (needle.len > haystack.len or needle.len == 0) return null;
895899
896 const haystackU8 = sliceAsBytes(haystack);900 if (!isValidAlign(T.bit_count) or haystack.len < 32 or needle.len <= 2)
897 const needleU8 = sliceAsBytes(needle);901 return lastIndexOfLinear(T, haystack, needle);
898902
899 var table: [256]usize = undefined;903 const haystack_bytes = sliceAsBytes(haystack);
900 boyerMooreHorspoolPreprocess(needleU8, table[0..]);904 const needle_bytes = sliceAsBytes(needle);
901905
902 var i: usize = 0;906 var skip_table: [256]usize = undefined;
903 while (i <= haystackU8.len - needleU8.len) {907 boyerMooreHorspoolPreprocessReverse(needle_bytes, skip_table[0..]);
904 const reverseIndex = haystackU8.len - i - needleU8.len - 1;908
905 if (mem.eql(u8, haystackU8[reverseIndex .. reverseIndex + needleU8.len], needleU8)) return i;909 var i: usize = haystack_bytes.len - needle_bytes.len;
906 i += table[haystackU8[reverseIndex + needleU8.len - 1]];910 while (true) {
911 if (mem.eql(u8, haystack_bytes[i .. i + needle_bytes.len], needle_bytes)) return i;
912 const skip = skip_table[haystack_bytes[i]];
913 if (skip > i) break;
914 i -= skip;
907 }915 }
908916
909 return null;917 return null;
...@@ -911,27 +919,32 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us...@@ -911,27 +919,32 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
911919
912// Boyer-moore-horspool algorithm920// Boyer-moore-horspool algorithm
913pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {921pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
914 if (!isValidAlign(T.bit_count) or haystack.len < 32 or needle.len <= 2)
915 return indexOfPosNaive(T, haystack, start_index, needle);
916
917 if (needle.len > haystack.len or needle.len == 0) return null;922 if (needle.len > haystack.len or needle.len == 0) return null;
918923
919 const haystackU8 = sliceAsBytes(haystack);924 if (!isValidAlign(T.bit_count) or haystack.len < 32 or needle.len <= 2)
920 const needleU8 = sliceAsBytes(needle);925 return indexOfPosLinear(T, haystack, start_index, needle);
926
927 const haystack_bytes = sliceAsBytes(haystack);
928 const needle_bytes = sliceAsBytes(needle);
921929
922 var table: [256]usize = undefined;930 var skip_table: [256]usize = undefined;
923 boyerMooreHorspoolPreprocess(needleU8, table[0..]);931 boyerMooreHorspoolPreprocess(needle_bytes, skip_table[0..]);
924932
925 var i: usize = start_index;933 var i: usize = start_index;
926 while (i <= haystackU8.len - needleU8.len) {934 while (i <= haystack_bytes.len - needle_bytes.len) {
927 if (mem.eql(u8, haystackU8[i .. i + needleU8.len], needleU8)) return i;935 if (mem.eql(u8, haystack_bytes[i .. i + needle_bytes.len], needle_bytes)) return i;
928 i += table[haystackU8[i + needleU8.len - 1]];936 i += skip_table[haystack_bytes[i + needle_bytes.len - 1]];
929 }937 }
930938
931 return null;939 return null;
932}940}
933941
934test "mem.indexOf" {942test "mem.indexOf" {
943 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "three four").? == 8);
944 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "three four").? == 8);
945 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "two two") == null);
946 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "two two") == null);
947
935 testing.expect(indexOf(u8, "one two three four", "four").? == 14);948 testing.expect(indexOf(u8, "one two three four", "four").? == 14);
936 testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14);949 testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14);
937 testing.expect(indexOf(u8, "one two three four", "gour") == null);950 testing.expect(indexOf(u8, "one two three four", "gour") == null);