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 {...@@ -1015,9 +1015,9 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
1015}1015}
10161016
1017pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {1017pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
1018 var i: usize = start_index;1018 if (start_index >= slice.len) return null;
1019 while (i < slice.len) : (i += 1) {1019 for (slice[start_index..], start_index..) |c, i| {
1020 if (slice[i] == value) return i;1020 if (c == value) return i;
1021 }1021 }
1022 return null;1022 return null;
1023}1023}
...@@ -1038,10 +1038,10 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us...@@ -1038,10 +1038,10 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us
1038}1038}
10391039
1040pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {1040pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {
1041 var i: usize = start_index;1041 if (start_index >= slice.len) return null;
1042 while (i < slice.len) : (i += 1) {1042 for (slice[start_index..], start_index..) |c, i| {
1043 for (values) |value| {1043 for (values) |value| {
1044 if (slice[i] == value) return i;1044 if (c == value) return i;
1045 }1045 }
1046 }1046 }
1047 return null;1047 return null;
...@@ -1074,10 +1074,10 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u...@@ -1074,10 +1074,10 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u
1074///1074///
1075/// Comparable to `strspn` in the C standard library.1075/// Comparable to `strspn` in the C standard library.
1076pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {1076pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {
1077 var i: usize = start_index;1077 if (start_index >= slice.len) return null;
1078 outer: while (i < slice.len) : (i += 1) {1078 outer: for (slice[start_index..], start_index..) |c, i| {
1079 for (values) |value| {1079 for (values) |value| {
1080 if (slice[i] == value) continue :outer;1080 if (c == value) continue :outer;
1081 }1081 }
1082 return i;1082 return i;
1083 }1083 }