authorgravatar for karlseguin@users.noreply.github.comKarl Seguin <karlseguin@users.noreply.github.com> 2023-09-28 23:40:08+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-09-28 15:40:08+00:00
log599641357cda1ff86ebc515d0761f98fb8c507a8
tree0bcbb3e266786f674f22bc7b515b902143819950
parent1063035be6b5886d34b3ccd62680bbb52cb97a90
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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


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

lib/std/mem.zig+9-9
......@@ -1015,9 +1015,9 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
10151015}
10161016
10171017pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
1018 var i: usize = start_index;
1019 while (i < slice.len) : (i += 1) {
1020 if (slice[i] == value) return i;
1018 if (start_index >= slice.len) return null;
1019 for (slice[start_index..], start_index..) |c, i| {
1020 if (c == value) return i;
10211021 }
10221022 return null;
10231023}
......@@ -1038,10 +1038,10 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us
10381038}
10391039
10401040pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {
1041 var i: usize = start_index;
1042 while (i < slice.len) : (i += 1) {
1041 if (start_index >= slice.len) return null;
1042 for (slice[start_index..], start_index..) |c, i| {
10431043 for (values) |value| {
1044 if (slice[i] == value) return i;
1044 if (c == value) return i;
10451045 }
10461046 }
10471047 return null;
......@@ -1074,10 +1074,10 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u
10741074///
10751075/// Comparable to `strspn` in the C standard library.
10761076pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {
1077 var i: usize = start_index;
1078 outer: while (i < slice.len) : (i += 1) {
1077 if (start_index >= slice.len) return null;
1078 outer: for (slice[start_index..], start_index..) |c, i| {
10791079 for (values) |value| {
1080 if (slice[i] == value) continue :outer;
1080 if (c == value) continue :outer;
10811081 }
10821082 return i;
10831083 }