remove while loops where a for would be more efficient (#8131)

* remove while loops where a for would be more efficient

* this needs to be a stack copy

* this can use the better loop

* this was translated wrong

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Meghan Denny
2024-01-13 19:13:54 -08:00
committed by GitHub
parent 56f1e5a362
commit be0896e204
33 changed files with 128 additions and 275 deletions

View File

@@ -200,9 +200,8 @@ pub fn repeatingBuf(self: []u8, char: u8) void {
}
pub fn indexOfCharNeg(self: string, char: u8) i32 {
var i: u32 = 0;
while (i < self.len) : (i += 1) {
if (self[i] == char) return @as(i32, @intCast(i));
for (self, 0..) |c, i| {
if (c == char) return @as(i32, @intCast(i));
}
return -1;
}
@@ -1002,8 +1001,7 @@ pub inline fn append3(allocator: std.mem.Allocator, self: string, other: string,
pub inline fn joinBuf(out: []u8, parts: anytype, comptime parts_len: usize) []u8 {
var remain = out;
var count: usize = 0;
comptime var i: usize = 0;
inline while (i < parts_len) : (i += 1) {
inline for (0..parts_len) |i| {
const part = parts[i];
bun.copy(u8, remain, part);
remain = remain[part.len..];
@@ -2395,8 +2393,7 @@ pub fn escapeHTMLForLatin1Input(allocator: std.mem.Allocator, latin1: []const u8
@memcpy(buf.items[0..copy_len], latin1[0..copy_len]);
buf.items.len = copy_len;
any_needs_escape = true;
comptime var i: usize = 0;
inline while (i < ascii_vector_size) : (i += 1) {
inline for (0..ascii_vector_size) |i| {
switch (vec[i]) {
'"' => {
buf.ensureUnusedCapacity((ascii_vector_size - i) + "&quot;".len) catch unreachable;
@@ -2449,8 +2446,7 @@ pub fn escapeHTMLForLatin1Input(allocator: std.mem.Allocator, latin1: []const u8
@as(AsciiVectorU1, @bitCast((vec == vecs[4])))) == 1)
{
buf.ensureUnusedCapacity(ascii_vector_size + 6) catch unreachable;
comptime var i: usize = 0;
inline while (i < ascii_vector_size) : (i += 1) {
inline for (0..ascii_vector_size) |i| {
switch (vec[i]) {
'"' => {
buf.ensureUnusedCapacity((ascii_vector_size - i) + "&quot;".len) catch unreachable;
@@ -3109,7 +3105,6 @@ pub fn utf16EqlString(text: []const u16, str: string) bool {
var i: usize = 0;
// TODO: is it safe to just make this u32 or u21?
var r1: i32 = undefined;
var k: u4 = 0;
while (i < n) : (i += 1) {
r1 = text[i];
if (r1 >= 0xD800 and r1 <= 0xDBFF and i + 1 < n) {
@@ -3124,8 +3119,7 @@ pub fn utf16EqlString(text: []const u16, str: string) bool {
if (j + width > str.len) {
return false;
}
k = 0;
while (k < width) : (k += 1) {
for (0..width) |k| {
if (temp[k] != str[j]) {
return false;
}
@@ -3409,8 +3403,7 @@ pub fn firstNonASCIIWithType(comptime Type: type, slice: Type) ?u32 {
const first_set_byte = @ctz(mask) / 8;
if (comptime Environment.allow_assert) {
std.debug.assert(remaining[first_set_byte] > 127);
var j: usize = 0;
while (j < first_set_byte) : (j += 1) {
for (0..first_set_byte) |j| {
std.debug.assert(remaining[j] <= 127);
}
}
@@ -3427,8 +3420,7 @@ pub fn firstNonASCIIWithType(comptime Type: type, slice: Type) ?u32 {
const first_set_byte = @ctz(mask) / 8;
if (comptime Environment.allow_assert) {
std.debug.assert(remaining[first_set_byte] > 127);
var j: usize = 0;
while (j < first_set_byte) : (j += 1) {
for (0..first_set_byte) |j| {
std.debug.assert(remaining[j] <= 127);
}
}
@@ -3472,8 +3464,7 @@ pub fn firstNonASCIIWithType(comptime Type: type, slice: Type) ?u32 {
const first_set_byte = @ctz(mask) / 8;
if (comptime Environment.allow_assert) {
std.debug.assert(remaining[first_set_byte] > 127);
var j: usize = 0;
while (j < first_set_byte) : (j += 1) {
for (0..first_set_byte) |j| {
std.debug.assert(remaining[j] <= 127);
}
}
@@ -4217,8 +4208,7 @@ pub fn @"nextUTF16NonASCIIOr$`\\"(
test "indexOfNotChar" {
{
var yes: [312]u8 = undefined;
var i: usize = 0;
while (i < yes.len) {
for (0..yes.len) |i| {
@memset(yes, 'a');
yes[i] = 'b';
if (comptime Environment.allow_assert) std.debug.assert(indexOfNotChar(&yes, 'a').? == i);
@@ -4770,8 +4760,7 @@ pub fn NewCodePointIterator(comptime CodePointType: type, comptime zeroValue: co
defer it.i = original_i;
var end_ix = original_i;
var found: usize = 0;
while (found < n) : (found += 1) {
for (0..n) |_| {
const next_codepoint = it.nextCodepointSlice() orelse return it.bytes[original_i..];
end_ix += next_codepoint.len;
}