From b0684bf084dbb5fe9ad7fb75dacf24cbd0a17231 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 17 Oct 2020 19:23:37 -0700 Subject: [PATCH] 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. --- lib/std/mem.zig | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index a3b5dd9228..dd1c736626 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -853,7 +853,9 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize /// Find the index in a slice of a sub-slice, searching from the end backwards. /// To start looking at a different index, slice the haystack first. -fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize { +/// Consider using `lastIndexOf` instead of this, which will automatically use a +/// more sophisticated algorithm on larger inputs. +pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize { var i: usize = haystack.len - needle.len; while (true) : (i -= 1) { 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) ? } } -fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { +/// Consider using `indexOfPos` instead of this, which will automatically use a +/// more sophisticated algorithm on larger inputs. +pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { var i: usize = start_index; const end = haystack.len - needle.len; while (i <= end) : (i += 1) { @@ -897,7 +901,8 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void { } /// Find the index in a slice of a sub-slice, searching from the end backwards. /// To start looking at a different index, slice the haystack first. -// Reverse boyer-moore-horspool algorithm +/// Uses the Reverse boyer-moore-horspool algorithm on large inputs; +/// `lastIndexOfLinear` on small inputs. pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { if (needle.len > haystack.len) return null; if (needle.len == 0) return haystack.len; @@ -922,7 +927,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us return null; } -// Boyer-moore-horspool algorithm +/// Uses Boyer-moore-horspool algorithm on large inputs; `indexOfPosLinear` on small inputs. pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { if (needle.len > haystack.len) return null; if (needle.len == 0) return 0;