authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-17 19:23:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-17 19:24:52-07:00
logb0684bf084dbb5fe9ad7fb75dacf24cbd0a17231
treeb2e5bfe30168420a7a28f5c9c8dc8ce1e038e64f
parent644400054c5769a397ded4f569e2ac0600d65305

std.mem: expose the simpler linear functions

The new defaults that came in with 644400054c5769a397ded4f569e2ac0600d65305 are nice, however, it is still possible that someone knows their inputs are always small and wants to use the simpler implementations. We keep the default to make the choice at runtime, but expose the linear functions in the public interface of std.mem. Also improved the doc comments.

1 files changed, 9 insertions(+), 4 deletions(-)

lib/std/mem.zig+9-4
...@@ -853,7 +853,9 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize...@@ -853,7 +853,9 @@ 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 lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize {856/// Consider using `lastIndexOf` instead of this, which will automatically use a
857/// more sophisticated algorithm on larger inputs.
858pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize {
857 var i: usize = haystack.len - needle.len;859 var i: usize = haystack.len - needle.len;
858 while (true) : (i -= 1) {860 while (true) : (i -= 1) {
859 if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i;861 if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i;
...@@ -861,7 +863,9 @@ fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?...@@ -861,7 +863,9 @@ fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?
861 }863 }
862}864}
863865
864fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {866/// Consider using `indexOfPos` instead of this, which will automatically use a
867/// more sophisticated algorithm on larger inputs.
868pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
865 var i: usize = start_index;869 var i: usize = start_index;
866 const end = haystack.len - needle.len;870 const end = haystack.len - needle.len;
867 while (i <= end) : (i += 1) {871 while (i <= end) : (i += 1) {
...@@ -897,7 +901,8 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void {...@@ -897,7 +901,8 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void {
897}901}
898/// Find the index in a slice of a sub-slice, searching from the end backwards.902/// Find the index in a slice of a sub-slice, searching from the end backwards.
899/// To start looking at a different index, slice the haystack first.903/// To start looking at a different index, slice the haystack first.
900// Reverse boyer-moore-horspool algorithm904/// Uses the Reverse boyer-moore-horspool algorithm on large inputs;
905/// `lastIndexOfLinear` on small inputs.
901pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {906pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
902 if (needle.len > haystack.len) return null;907 if (needle.len > haystack.len) return null;
903 if (needle.len == 0) return haystack.len;908 if (needle.len == 0) return haystack.len;
...@@ -922,7 +927,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us...@@ -922,7 +927,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
922 return null;927 return null;
923}928}
924929
925// Boyer-moore-horspool algorithm930/// Uses Boyer-moore-horspool algorithm on large inputs; `indexOfPosLinear` on small inputs.
926pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {931pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
927 if (needle.len > haystack.len) return null;932 if (needle.len > haystack.len) return null;
928 if (needle.len == 0) return 0;933 if (needle.len == 0) return 0;