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
853853
854854/// Find the index in a slice of a sub-slice, searching from the end backwards.
855855/// 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 {
857859 var i: usize = haystack.len - needle.len;
858860 while (true) : (i -= 1) {
859861 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) ?
861863 }
862864}
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 {
865869 var i: usize = start_index;
866870 const end = haystack.len - needle.len;
867871 while (i <= end) : (i += 1) {
......@@ -897,7 +901,8 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void {
897901}
898902/// Find the index in a slice of a sub-slice, searching from the end backwards.
899903/// To start looking at a different index, slice the haystack first.
900// Reverse boyer-moore-horspool algorithm
904/// Uses the Reverse boyer-moore-horspool algorithm on large inputs;
905/// `lastIndexOfLinear` on small inputs.
901906pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
902907 if (needle.len > haystack.len) return null;
903908 if (needle.len == 0) return haystack.len;
......@@ -922,7 +927,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
922927 return null;
923928}
924929
925// Boyer-moore-horspool algorithm
930/// Uses Boyer-moore-horspool algorithm on large inputs; `indexOfPosLinear` on small inputs.
926931pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
927932 if (needle.len > haystack.len) return null;
928933 if (needle.len == 0) return 0;