authorgravatar for fabio@fabioarnold.deFabio Arnold <fabio@fabioarnold.de> 2021-11-26 02:56:38+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-11-25 20:56:38-05:00
logda7baf7daec175f63e336082d4bcada9ccf4d55c
tree1cbc50980680185b05f8c1d9d58ff4d463c46cc4
parentb891ee1f232a1aa626dc5f394e33495c474615ad
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.mem.indexOfPos should return start_index when needle length is zero (#10220)

Closes #10216

1 files changed, 5 insertions(+), 1 deletions(-)

lib/std/mem.zig+5-1
...@@ -1162,7 +1162,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us...@@ -1162,7 +1162,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
1162/// Uses Boyer-moore-horspool algorithm on large inputs; `indexOfPosLinear` on small inputs.1162/// Uses Boyer-moore-horspool algorithm on large inputs; `indexOfPosLinear` on small inputs.
1163pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {1163pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
1164 if (needle.len > haystack.len) return null;1164 if (needle.len > haystack.len) return null;
1165 if (needle.len == 0) return 0;1165 if (needle.len == 0) return start_index;
11661166
1167 if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 52 or needle.len <= 4)1167 if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 52 or needle.len <= 4)
1168 return indexOfPosLinear(T, haystack, start_index, needle);1168 return indexOfPosLinear(T, haystack, start_index, needle);
...@@ -1237,6 +1237,10 @@ test "mem.indexOf multibyte" {...@@ -1237,6 +1237,10 @@ test "mem.indexOf multibyte" {
1237 }1237 }
1238}1238}
12391239
1240test "mem.indexOfPos empty needle" {
1241 try testing.expectEqual(indexOfPos(u8, "abracadabra", 5, ""), 5);
1242}
1243
1240/// Returns the number of needles inside the haystack1244/// Returns the number of needles inside the haystack
1241/// needle.len must be > 01245/// needle.len must be > 0
1242/// does not count overlapping needles1246/// does not count overlapping needles