Commit 06d0c58305

Andrew Kelley <andrew@ziglang.org>
2024-02-12 06:18:53
std.mem: take advantage of length-based slicing
1 parent b47bd03
Changed files (1)
lib
lib/std/mem.zig
@@ -1338,7 +1338,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize
 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;
+        if (mem.eql(T, haystack[i..][0..needle.len], needle)) return i;
         if (i == 0) return null;
     }
 }
@@ -1349,7 +1349,7 @@ pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usiz
     var i: usize = start_index;
     const end = haystack.len - needle.len;
     while (i <= end) : (i += 1) {
-        if (eql(T, haystack[i .. i + needle.len], needle)) return i;
+        if (eql(T, haystack[i..][0..needle.len], needle)) return i;
     }
     return null;
 }