zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 599641357cda1ff86ebc515d0761f98fb8c507a8 (tree)
parent 1063035be6b5886d34b3ccd62680bbb52cb97a90
Author: Karl Seguin <karlseguin@users.noreply.github.com>
Date:   Thu, 28 Sep 2023 23:40:08 +0800

std.mem: use for loop instead of while in indexOf* to reduce bound checking


Diffstat:
Mlib/std/mem.zig | 18+++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/lib/std/mem.zig b/lib/std/mem.zig @@ -1015,9 +1015,9 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { } pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize { - var i: usize = start_index; - while (i < slice.len) : (i += 1) { - if (slice[i] == value) return i; + if (start_index >= slice.len) return null; + for (slice[start_index..], start_index..) |c, i| { + if (c == value) return i; } return null; } @@ -1038,10 +1038,10 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us } pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { - var i: usize = start_index; - while (i < slice.len) : (i += 1) { + if (start_index >= slice.len) return null; + for (slice[start_index..], start_index..) |c, i| { for (values) |value| { - if (slice[i] == value) return i; + if (c == value) return i; } } return null; @@ -1074,10 +1074,10 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u /// /// Comparable to `strspn` in the C standard library. pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { - var i: usize = start_index; - outer: while (i < slice.len) : (i += 1) { + if (start_index >= slice.len) return null; + outer: for (slice[start_index..], start_index..) |c, i| { for (values) |value| { - if (slice[i] == value) continue :outer; + if (c == value) continue :outer; } return i; }