Skip to content

Commit

Permalink
Updated for loops.
Browse files Browse the repository at this point in the history
Previous syntax for index of for loop elements:
for (items) |item, index|

New syntax for index of for loop elements:
for (items, 0..) |item, index|
  • Loading branch information
jernejp21 committed Mar 8, 2023
1 parent 5c7edb4 commit b51c894
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion android/build/auto-detect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig
len = @intCast(DWORD, buffer.len);
res = RegGetValueA(key, null, value, RRF_RT_REG_SZ, null, buffer.ptr, &len);
if (res == ERROR_SUCCESS) {
for (buffer[0..len]) |c, i| {
for (buffer[0..len], 0..) |c, i| {
if (c == 0) return buffer[0..i];
}
return buffer[0..len];
Expand Down
4 changes: 2 additions & 2 deletions src/containers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn ColumnLayout(peer: Callbacks, widgets: []Widget) void {
var childY: f32 = 0.0;
// Child X is different from 0 only when 'wrapping' property is set to true
var childX: f32 = 0.0;
for (widgets) |widget, i| {
for (widgets, 0..) |widget, i| {
const isLastWidget = i == widgets.len - 1;
if (widget.peer) |widgetPeer| {
const minimumSize = widget.getPreferredSize(Size.init(1, 1));
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn RowLayout(peer: Callbacks, widgets: []Widget) void {
var childX: f32 = 0.0;
// Child Y is different from 0 only when 'wrapping' property is set to true
var childY: f32 = 0.0;
for (widgets) |widget, i| {
for (widgets, 0..) |widget, i| {
const isLastWidget = i == widgets.len - 1;
if (widget.peer) |widgetPeer| {
const minimumSize = widget.getPreferredSize(Size.init(1, 1));
Expand Down
2 changes: 1 addition & 1 deletion src/data.zig
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ pub fn FormatDataWrapper(allocator: std.mem.Allocator, comptime fmt: []const u8,
fn format(ptr: *Self) void {
const TupleType = std.meta.Tuple(childTypes);
var tuple: TupleType = undefined;
inline for (std.meta.fields(@TypeOf(ptr.childs))) |childF, i| {
inline for (std.meta.fields(@TypeOf(ptr.childs)), 0..) |childF, i| {
const child = @field(ptr.childs, childF.name);
tuple[i] = child.get();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn stepEventLoop(stepType: EventLoopStep) bool {
data._animatedDataWrappersMutex.lock();
defer data._animatedDataWrappersMutex.unlock();

for (data._animatedDataWrappers.items) |item, i| {
for (data._animatedDataWrappers.items, 0..) |item, i| {
if (item.fnPtr(item.userdata) == false) { // animation ended
_ = data._animatedDataWrappers.swapRemove(i);
}
Expand Down
8 changes: 4 additions & 4 deletions vendor/zigimg/src/formats/jpeg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const HuffmanTable = struct {
if (JPEG_VERY_DEBUG) std.debug.print(" Decoded huffman codes map:\n", .{});

var code: u16 = 0;
for (code_counts) |count, i| {
for (code_counts, 0..) |count, i| {
if (JPEG_VERY_DEBUG) {
std.debug.print(" Length {}: ", .{i + 1});
if (count == 0) {
Expand Down Expand Up @@ -592,7 +592,7 @@ const Scan = struct {
}

fn decodeMCU(frame: *Frame, scan_header: ScanHeader, mcu_id: usize, reader: *HuffmanReader, prediction_values: *[3]i12) ImageReadError!void {
for (scan_header.components) |maybe_component, component_id| {
for (scan_header.components, 0..) |maybe_component, component_id| {
_ = component_id;
if (maybe_component == null)
break;
Expand All @@ -606,7 +606,7 @@ const Scan = struct {
// file size can be reduced that way. Therefore we need to select the correct
// destination for this component.
const component_destination = blk: {
for (frame.frame_header.components) |frame_component, i| {
for (frame.frame_header.components, 0..) |frame_component, i| {
if (frame_component.id == component.component_selector) {
break :blk i;
}
Expand Down Expand Up @@ -800,7 +800,7 @@ const Frame = struct {
fn dequantize(self: *Frame) !void {
var mcu_id: usize = 0;
while (mcu_id < self.mcu_storage.len) : (mcu_id += 1) {
for (self.frame_header.components) |component, component_id| {
for (self.frame_header.components, 0..) |component, component_id| {
const mcu = &self.mcu_storage[mcu_id][component_id];

if (self.quantization_tables[component.quantization_table_id]) |quantization_table| {
Expand Down
4 changes: 2 additions & 2 deletions vendor/zigimg/src/formats/netpbm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ fn Netpbm(comptime image_format: Image.Format, comptime header_numbers: []const
switch (pixels) {
.grayscale16 => {
const pixels_len = pixels.len();
for (pixels.grayscale16) |entry, index| {
for (pixels.grayscale16, 0..) |entry, index| {
try writer.print("{}", .{entry.value});

if (index != (pixels_len - 1)) {
Expand All @@ -460,7 +460,7 @@ fn Netpbm(comptime image_format: Image.Format, comptime header_numbers: []const
},
.grayscale8 => {
const pixels_len = pixels.len();
for (pixels.grayscale8) |entry, index| {
for (pixels.grayscale8, 0..) |entry, index| {
try writer.print("{}", .{entry.value});

if (index != (pixels_len - 1)) {
Expand Down
4 changes: 2 additions & 2 deletions vendor/zigimg/src/formats/png/filtering.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn filter(writer: anytype, pixels: color.PixelStorage, filter_choice: Filter

try writer.writeByte(@enumToInt(filter_type));

for (scanline) |sample, i| {
for (scanline, 0..) |sample, i| {
const previous: u8 = if (i >= pixel_len) scanline[i - pixel_len] else 0;
const above: u8 = if (previous_scanline) |b| b[i] else 0;
const above_previous = if (previous_scanline) |b| (if (i >= pixel_len) b[i - pixel_len] else 0) else 0;
Expand Down Expand Up @@ -77,7 +77,7 @@ fn filterChoiceHeuristic(scanline: []const u8, previous_scanline: ?[]const u8, p
var combo: usize = 0;
var score: usize = 0;

for (scanline) |sample, i| {
for (scanline, 0..) |sample, i| {
const previous: u8 = if (i >= pixel_len) scanline[i - pixel_len] else 0;
const above: u8 = if (previous_scanline) |b| b[i] else 0;
const above_previous = if (previous_scanline) |b| (if (i >= pixel_len) b[i - pixel_len] else 0) else 0;
Expand Down
4 changes: 2 additions & 2 deletions vendor/zigimg/src/formats/png/reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ fn readAllData(
result_palette
else
try options.temp_allocator.alloc(color.Rgba32, palette.len);
for (palette) |entry, n| {
for (palette, 0..) |entry, n| {
destination_palette[n] = color.Rgba32.initRgb(entry.r, entry.g, entry.b);
}
try callPaletteProcessors(options, destination_palette);
Expand Down Expand Up @@ -705,7 +705,7 @@ pub const TrnsProcessor = struct {
self.processed = true;
switch (self.trns_data) {
.index_alpha => |index_alpha| {
for (index_alpha) |alpha, i| {
for (index_alpha, 0..) |alpha, i| {
data.palette[i].a = alpha;
}
},
Expand Down
2 changes: 1 addition & 1 deletion vendor/zigimg/src/formats/qoi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub const QOI = struct {
var previous_pixel = QoiColor{ .r = 0, .g = 0, .b = 0, .a = 0xFF };
var run_length: usize = 0;

for (pixels_data) |current_color, i| {
for (pixels_data, 0..) |current_color, i| {
const pixel = QoiColor.from(current_color);

defer previous_pixel = pixel;
Expand Down
2 changes: 1 addition & 1 deletion vendor/zigimg/src/octree_quantizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const OctTreeQuantizerNode = struct {

pub fn removeLeaves(self: *Self) i32 {
var result: i32 = 0;
for (self.children) |childOptional, i| {
for (self.children, 0..) |childOptional, i| {
if (childOptional) |child| {
self.red += child.red;
self.green += child.green;
Expand Down
4 changes: 2 additions & 2 deletions vendor/zigimg/src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ pub const StructReadError = error{ EndOfStream, InvalidData } || io.StreamSource

pub fn toMagicNumberNative(magic: []const u8) u32 {
var result: u32 = 0;
for (magic) |character, index| {
for (magic, 0..) |character, index| {
result |= (@as(u32, character) << @intCast(u5, (index * 8)));
}
return result;
}

pub fn toMagicNumberForeign(magic: []const u8) u32 {
var result: u32 = 0;
for (magic) |character, index| {
for (magic, 0..) |character, index| {
result |= (@as(u32, character) << @intCast(u5, (magic.len - 1 - index) * 8));
}
return result;
Expand Down
12 changes: 6 additions & 6 deletions vendor/zigimg/tests/color_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ test "Colorf32.toFromU32Rgba()" {
color.Colorf32.initRgba(0x26.0 / 255.0, 0xce.0 / 255.0, 0x05.0 / 255.0, 0x89.0 / 255.0),
color.Colorf32.initRgba(0xf5.0 / 255.0, 0x0f.0 / 255.0, 0x68.0 / 255.0, 0xea.0 / 255.0),
};
for (expected_u32) |expected, i| {
for (expected_u32, 0..) |expected, i| {
const actual = color.Colorf32.fromU32Rgba(expected);
try helpers.expectEq(actual, expected_c32[i]);
try helpers.expectEq(actual.toU32Rgba(), expected);
Expand All @@ -186,7 +186,7 @@ test "Colorf32.toFromU64Rgba()" {
color.Colorf32.initRgba(0x68bb.0 / 65535.0, 0x87b3.0 / 65535.0, 0x93a1.0 / 65535.0, 0xc104.0 / 65535.0),
color.Colorf32.initRgba(0x48b7.0 / 65535.0, 0xf617.0 / 65535.0, 0xa452.0 / 65535.0, 0x0099.0 / 65535.0),
};
for (expected_u64) |expected, i| {
for (expected_u64, 0..) |expected, i| {
const actual = color.Colorf32.fromU64Rgba(expected);
try helpers.expectEq(actual, expected_c32[i]);
try helpers.expectEq(actual.toU64Rgba(), expected);
Expand Down Expand Up @@ -246,7 +246,7 @@ test "Rgb.toFromU32Rgba()" {
color.Rgb48.initRgb(0xce * 257, 0x05 * 257, 0x89 * 257),
color.Rgb48.initRgb(0x0f * 257, 0x68 * 257, 0xea * 257),
};
for (expected_u32) |expected, i| {
for (expected_u32, 0..) |expected, i| {
const actual24_from_rgba = color.Rgb24.fromU32Rgba(expected);
try helpers.expectEq(actual24_from_rgba, expected_rgb24_from_rgba[i]);
try helpers.expectEq(actual24_from_rgba.toU32Rgba(), expected | 0xff);
Expand Down Expand Up @@ -299,7 +299,7 @@ test "Rgb.toFromU64Rgba()" {
expected_rgb48_from_rgba[3].toColorf32(),
expected_rgb48_from_rgba[4].toColorf32(),
};
for (expected_u64) |expected, i| {
for (expected_u64, 0..) |expected, i| {
const actual_from_rgba = color.Rgb48.fromU64Rgba(expected);
try helpers.expectEq(actual_from_rgba, expected_rgb48_from_rgba[i]);
try helpers.expectEq(actual_from_rgba.toU64Rgba(), expected | 0xffff);
Expand Down Expand Up @@ -337,7 +337,7 @@ test "Rgba.toFromU32Rgba()" {
color.Rgba64.initRgba(0x26 * 257, 0xce * 257, 0x05 * 257, 0x89 * 257),
color.Rgba64.initRgba(0xf5 * 257, 0x0f * 257, 0x68 * 257, 0xea * 257),
};
for (expected_u32) |expected, i| {
for (expected_u32, 0..) |expected, i| {
const actual32_from_rgba = color.Rgba32.fromU32Rgba(expected);
try helpers.expectEq(actual32_from_rgba, expected_rgba32_from_rgba[i]);
try helpers.expectEq(actual32_from_rgba.toU32Rgba(), expected);
Expand Down Expand Up @@ -371,7 +371,7 @@ test "Rgba.toFromU64Rgba())" {
expected_rgba64_from_rgba[3].toColorf32(),
expected_rgba64_from_rgba[4].toColorf32(),
};
for (expected_u64) |expected, i| {
for (expected_u64, 0..) |expected, i| {
const actual_from_rgba = color.Rgba64.fromU64Rgba(expected);
try helpers.expectEq(actual_from_rgba, expected_rgba64_from_rgba[i]);
try helpers.expectEq(actual_from_rgba.toU64Rgba(), expected);
Expand Down
20 changes: 10 additions & 10 deletions vendor/zigimg/tests/formats/netpbm_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ test "Write bitmap(grayscale1) ASCII PBM file" {
defer source_image.deinit();

const source = source_image.pixels;
for (grayscales) |value, index| {
for (grayscales, 0..) |value, index| {
source.grayscale1[index].value = value;
}

Expand All @@ -247,7 +247,7 @@ test "Write bitmap(grayscale1) ASCII PBM file" {

try testing.expect(read_pixels == .grayscale1);

for (grayscales) |grayscale_value, index| {
for (grayscales, 0..) |grayscale_value, index| {
try helpers.expectEq(read_pixels.grayscale1[index].value, grayscale_value);
}
}
Expand All @@ -270,7 +270,7 @@ test "Write bitmap(Grayscale1) binary PBM file" {

const source = source_image.pixels;

for (grayscales) |value, index| {
for (grayscales, 0..) |value, index| {
source.grayscale1[index].value = value;
}

Expand All @@ -292,7 +292,7 @@ test "Write bitmap(Grayscale1) binary PBM file" {

try testing.expect(read_pixels == .grayscale1);

for (grayscales) |grayscale_value, index| {
for (grayscales, 0..) |grayscale_value, index| {
try helpers.expectEq(read_pixels.grayscale1[index].value, grayscale_value);
}
}
Expand All @@ -311,7 +311,7 @@ test "Write grayscale8 ASCII PGM file" {
defer source_image.deinit();

const source = source_image.pixels;
for (grayscales) |value, index| {
for (grayscales, 0..) |value, index| {
source.grayscale8[index].value = value;
}

Expand All @@ -333,7 +333,7 @@ test "Write grayscale8 ASCII PGM file" {

try testing.expect(read_pixels == .grayscale8);

for (grayscales) |grayscale_value, index| {
for (grayscales, 0..) |grayscale_value, index| {
try helpers.expectEq(read_pixels.grayscale8[index].value, grayscale_value);
}
}
Expand All @@ -352,7 +352,7 @@ test "Write grayscale8 binary PGM file" {
defer source_image.deinit();

const source = source_image.pixels;
for (grayscales) |value, index| {
for (grayscales, 0..) |value, index| {
source.grayscale8[index].value = value;
}

Expand All @@ -373,7 +373,7 @@ test "Write grayscale8 binary PGM file" {
const read_pixels = read_image.pixels;
try testing.expect(read_pixels == .grayscale8);

for (grayscales) |grayscale_value, index| {
for (grayscales, 0..) |grayscale_value, index| {
try helpers.expectEq(read_pixels.grayscale8[index].value, grayscale_value);
}
}
Expand Down Expand Up @@ -425,7 +425,7 @@ test "Writing Rgb24 ASCII PPM format" {

try testing.expect(read_image_pixels == .rgb24);

for (expected_colors) |hex_color, index| {
for (expected_colors, 0..) |hex_color, index| {
try helpers.expectEq(read_image_pixels.rgb24[index].toU32Rgb(), hex_color);
}
}
Expand Down Expand Up @@ -477,7 +477,7 @@ test "Writing Rgb24 binary PPM format" {

try testing.expect(read_image_pixels == .rgb24);

for (expected_colors) |hex_color, index| {
for (expected_colors, 0..) |hex_color, index| {
try helpers.expectEq(read_image_pixels.rgb24[index].toU32Rgb(), hex_color);
}
}

0 comments on commit b51c894

Please sign in to comment.