Merge remote-tracking branch 'origin/master' into llvm16

This commit is contained in:
Andrew Kelley 2023-02-19 10:20:19 -07:00
commit b5b634e4e8
259 changed files with 2499 additions and 1299 deletions

View File

@ -239,7 +239,7 @@ const Tokenizer = struct {
.line_start = 0,
.line_end = 0,
};
for (self.buffer) |c, i| {
for (self.buffer, 0..) |c, i| {
if (i == token.start) {
loc.line_end = i;
while (loc.line_end < self.buffer.len and self.buffer[loc.line_end] != '\n') : (loc.line_end += 1) {}

View File

@ -2367,7 +2367,7 @@ test "iterate over an array" {
var some_integers: [100]i32 = undefined;
test "modify an array" {
for (some_integers) |*item, i| {
for (&some_integers, 0..) |*item, i| {
item.* = @intCast(i32, i);
}
try expect(some_integers[10] == 10);
@ -2408,7 +2408,7 @@ comptime {
// use compile-time code to initialize an array
var fancy_array = init: {
var initial_value: [10]Point = undefined;
for (initial_value) |*pt, i| {
for (&initial_value, 0..) |*pt, i| {
pt.* = Point{
.x = @intCast(i32, i),
.y = @intCast(i32, i) * 2,
@ -2461,8 +2461,8 @@ test "multidimensional arrays" {
try expect(mat4x4[1][1] == 1.0);
// Here we iterate with for loops.
for (mat4x4) |row, row_index| {
for (row) |cell, column_index| {
for (mat4x4, 0..) |row, row_index| {
for (row, 0..) |cell, column_index| {
if (row_index == column_index) {
try expect(cell == 1.0);
}
@ -3579,7 +3579,7 @@ test "tuple" {
} ++ .{false} ** 2;
try expect(values[0] == 1234);
try expect(values[4] == false);
inline for (values) |v, i| {
inline for (values, 0..) |v, i| {
if (i != 2) continue;
try expect(v);
}
@ -4659,10 +4659,10 @@ test "for basics" {
}
try expect(sum == 20);
// To access the index of iteration, specify a second capture value.
// This is zero-indexed.
// To access the index of iteration, specify a second condition as well
// as a second capture value.
var sum2: i32 = 0;
for (items) |_, i| {
for (items, 0..) |_, i| {
try expect(@TypeOf(i) == usize);
sum2 += @intCast(i32, i);
}
@ -4674,7 +4674,7 @@ test "for reference" {
// Iterate over the slice by reference by
// specifying that the capture value is a pointer.
for (items) |*value| {
for (&items) |*value| {
value.* += 1;
}
@ -5659,7 +5659,7 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {
var foos = try allocator.alloc(Foo, num);
errdefer allocator.free(foos);
for(foos) |*foo, i| {
for (foos, 0..) |*foo, i| {
foo.data = try allocator.create(u32);
// This errdefer does not last between iterations
errdefer allocator.destroy(foo.data);
@ -5700,14 +5700,14 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {
// Used to track how many foos have been initialized
// (including their data being allocated)
var num_allocated: usize = 0;
errdefer for(foos[0..num_allocated]) |foo| {
errdefer for (foos[0..num_allocated]) |foo| {
allocator.destroy(foo.data);
};
for(foos) |*foo, i| {
for (foos, 0..) |*foo, i| {
foo.data = try allocator.create(u32);
num_allocated += 1;
if(i >= 3) return error.TooManyFoos;
if (i >= 3) return error.TooManyFoos;
foo.data.* = try getData();
}
@ -7265,7 +7265,7 @@ const Writer = struct {
comptime var state = State.start;
comptime var next_arg: usize = 0;
inline for (format) |c, i| {
inline for (format, 0..) |c, i| {
switch (state) {
State.start => switch (c) {
'{' => {
@ -8629,7 +8629,7 @@ test "integer cast panic" {
This function is a low level intrinsic with no safety mechanisms. Most code
should not use this function, instead using something like this:
</p>
<pre>{#syntax#}for (source[0..byte_count]) |b, i| dest[i] = b;{#endsyntax#}</pre>
<pre>{#syntax#}for (dest, source[0..byte_count]) |*d, s| d.* = s;{#endsyntax#}</pre>
<p>
The optimizer is intelligent enough to turn the above snippet into a memcpy.
</p>
@ -11116,7 +11116,7 @@ pub fn main() !void {
const args = try std.process.argsAlloc(gpa);
defer std.process.argsFree(gpa, args);
for (args) |arg, i| {
for (args, 0..) |arg, i| {
std.debug.print("{}: {s}\n", .{ i, arg });
}
}
@ -11142,7 +11142,7 @@ pub fn main() !void {
const preopens = try fs.wasi.preopensAlloc(arena);
for (preopens.names) |preopen, i| {
for (preopens.names, 0..) |preopen, i| {
std.debug.print("{}: {s}\n", .{ i, preopen });
}
}
@ -11551,7 +11551,8 @@ fn readU32Be() u32 {}
</p>
<p>
Each LF may be immediately preceded by a single CR (byte value 0x0d, code point U+000d, {#syntax#}'\r'{#endsyntax#})
to form a Windows style line ending, but this is discouraged.
to form a Windows style line ending, but this is discouraged. Note that in mulitline strings, CRLF sequences will
be encoded as LF when compiled into a zig program.
A CR in any other context is not allowed.
</p>
<p>

View File

@ -260,6 +260,8 @@ Integer and Float Operations
| ✓ | __gedf2 | f64 | f64 | i32 | .. |
| ✓ | __getf2 | f128 | f128 | i32 | .. |
| ✓ | __gexf2 | f80 | f80 | i32 | .. |
| ✓ | __aeabi_fcmpge | f32 | f32 | i32 | .. ARM |
| ✓ | __aeabi_dcmpge | f64 | f64 | i32 | .. ARM |
| ✓ | __gekf2 | f128 | f128 | i32 | .. PPC |
| ✓ | _Qp_fge |*f128 |*f128 | bool | .. SPARC |
| ✓ | __lthf2 | f16 | f16 | i32 | `(a!=Nan) and (b!=Nan) and (a<b) -> output<0` |
@ -285,6 +287,8 @@ Integer and Float Operations
| ✓ | __gtdf2 | f64 | f64 | i32 | .. |
| ✓ | __gttf2 | f128 | f128 | i32 | .. |
| ✓ | __gtxf2 | f80 | f80 | i32 | .. |
| ✓ | __aeabi_fcmpgt | f32 | f32 | i32 | .. ARM |
| ✓ | __aeabi_dcmpgt | f64 | f64 | i32 | .. ARM |
| ✓ | __gtkf2 | f128 | f128 | i32 | .. PPC |
| ✓ | _Qp_fgt |*f128 |*f128 | bool | .. SPARC |
| | | | | | **Float Arithmetic** |

View File

@ -151,7 +151,7 @@ fn __atomic_compare_exchange(
_ = failure;
var sl = spinlocks.get(@ptrToInt(ptr));
defer sl.release();
for (ptr[0..size]) |b, i| {
for (ptr[0..size], 0..) |b, i| {
if (expected[i] != b) break;
} else {
// The two objects, ptr and expected, are equal

View File

@ -94,8 +94,8 @@ fn generateVector(comptime a: f64, comptime b: f64) TestVector {
const test_vectors = init: {
@setEvalBranchQuota(10000);
var vectors: [arguments.len * arguments.len]TestVector = undefined;
for (arguments[0..]) |arg_i, i| {
for (arguments[0..]) |arg_j, j| {
for (arguments[0..], 0..) |arg_i, i| {
for (arguments[0..], 0..) |arg_j, j| {
vectors[(i * arguments.len) + j] = generateVector(arg_i, arg_j);
}
}

View File

@ -94,8 +94,8 @@ fn generateVector(comptime a: f32, comptime b: f32) TestVector {
const test_vectors = init: {
@setEvalBranchQuota(10000);
var vectors: [arguments.len * arguments.len]TestVector = undefined;
for (arguments[0..]) |arg_i, i| {
for (arguments[0..]) |arg_j, j| {
for (arguments[0..], 0..) |arg_i, i| {
for (arguments[0..], 0..) |arg_j, j| {
vectors[(i * arguments.len) + j] = generateVector(arg_i, arg_j);
}
}

View File

@ -21,11 +21,3 @@ pub fn __gehf2(a: f16, b: f16) callconv(.C) i32 {
pub fn __gthf2(a: f16, b: f16) callconv(.C) i32 {
return __gehf2(a, b);
}
fn __aeabi_fcmpge(a: f16, b: f16) callconv(.AAPCS) i32 {
return @boolToInt(comparef.cmpf2(f16, comparef.GE, a, b) != .Less);
}
fn __aeabi_fcmpgt(a: f16, b: f16) callconv(.AAPCS) i32 {
return @boolToInt(comparef.cmpf2(f16, comparef.LE, a, b) == .Greater);
}

View File

@ -650,7 +650,7 @@ pub fn dupe(self: *Build, bytes: []const u8) []u8 {
/// Duplicates an array of strings without the need to handle out of memory.
pub fn dupeStrings(self: *Build, strings: []const []const u8) [][]u8 {
const array = self.allocator.alloc([]u8, strings.len) catch @panic("OOM");
for (strings) |s, i| {
for (strings, 0..) |s, i| {
array[i] = self.dupe(s);
}
return array;
@ -1051,7 +1051,7 @@ pub fn standardTargetOptions(self: *Build, args: StandardTargetOptionsArgs) Cros
const all_features = whitelist_cpu.arch.allFeaturesList();
var populated_cpu_features = whitelist_cpu.model.features;
populated_cpu_features.populateDependencies(all_features);
for (all_features) |feature, i_usize| {
for (all_features, 0..) |feature, i_usize| {
const i = @intCast(std.Target.Cpu.Feature.Set.Index, i_usize);
const in_cpu_set = populated_cpu_features.isEnabled(i);
if (in_cpu_set) {
@ -1059,7 +1059,7 @@ pub fn standardTargetOptions(self: *Build, args: StandardTargetOptionsArgs) Cros
}
}
log.err(" Remove: ", .{});
for (all_features) |feature, i_usize| {
for (all_features, 0..) |feature, i_usize| {
const i = @intCast(std.Target.Cpu.Feature.Set.Index, i_usize);
const in_cpu_set = populated_cpu_features.isEnabled(i);
const in_actual_set = selected_cpu.features.isEnabled(i);
@ -1748,7 +1748,7 @@ pub fn serializeCpu(allocator: Allocator, cpu: std.Target.Cpu) ![]const u8 {
var mcpu_buffer = ArrayList(u8).init(allocator);
try mcpu_buffer.appendSlice(cpu.model.name);
for (all_features) |feature, i_usize| {
for (all_features, 0..) |feature, i_usize| {
const i = @intCast(std.Target.Cpu.Feature.Set.Index, i_usize);
const in_cpu_set = populated_cpu_features.isEnabled(i);
const in_actual_set = cpu.features.isEnabled(i);

View File

@ -1016,7 +1016,7 @@ pub fn addVcpkgPaths(self: *CompileStep, linkage: CompileStep.Linkage) !void {
pub fn setExecCmd(self: *CompileStep, args: []const ?[]const u8) void {
assert(self.kind == .@"test");
const duped_args = self.builder.allocator.alloc(?[]u8, args.len) catch @panic("OOM");
for (args) |arg, i| {
for (args, 0..) |arg, i| {
duped_args[i] = if (arg) |a| self.builder.dupe(a) else null;
}
self.exec_cmd_args = duped_args;
@ -1040,7 +1040,7 @@ fn appendModuleArgs(
{
const keys = module.dependencies.keys();
for (module.dependencies.values()) |sub_module, i| {
for (module.dependencies.values(), 0..) |sub_module, i| {
const sub_name = keys[i];
try cs.appendModuleArgs(zig_args, sub_name, sub_module);
}
@ -1575,7 +1575,7 @@ fn make(step: *Step) !void {
{
const keys = self.modules.keys();
for (self.modules.values()) |module, i| {
for (self.modules.values(), 0..) |module, i| {
const name = keys[i];
try self.appendModuleArgs(&zig_args, name, module);
}
@ -1750,7 +1750,7 @@ fn make(step: *Step) !void {
const args_to_escape = zig_args.items[2..];
var escaped_args = try ArrayList([]const u8).initCapacity(builder.allocator, args_to_escape.len);
arg_blk: for (args_to_escape) |arg| {
for (arg) |c, arg_idx| {
for (arg, 0..) |c, arg_idx| {
if (c == '\\' or c == '"') {
// Slow path for arguments that need to be escaped. We'll need to allocate and copy
var escaped = try ArrayList(u8).initCapacity(builder.allocator, arg.len + 1);

View File

@ -350,7 +350,7 @@ fn render_blank(
try output.appendSlice("\n");
const values = defines.values();
for (defines.keys()) |name, i| {
for (defines.keys(), 0..) |name, i| {
try renderValueC(output, name, values[i]);
}
@ -361,7 +361,7 @@ fn render_blank(
fn render_nasm(output: *std.ArrayList(u8), defines: std.StringArrayHashMap(Value)) !void {
const values = defines.values();
for (defines.keys()) |name, i| {
for (defines.keys(), 0..) |name, i| {
try renderValueNasm(output, name, values[i]);
}
}

View File

@ -19,7 +19,7 @@ pub fn create(builder: *std.Build, paths: []const []const u8) *FmtStep {
self.argv[0] = builder.zig_exe;
self.argv[1] = "fmt";
for (paths) |path, i| {
for (paths, 0..) |path, i| {
self.argv[2 + i] = builder.pathFromRoot(path);
}
return self;

View File

@ -188,6 +188,10 @@ fn stdIoActionToBehavior(action: StdIoAction) std.ChildProcess.StdIo {
}
fn needOutputCheck(self: RunStep) bool {
switch (self.condition) {
.always => return false,
.output_outdated => {},
}
if (self.extra_file_dependencies.len > 0) return true;
for (self.argv.items) |arg| switch (arg) {
@ -195,10 +199,7 @@ fn needOutputCheck(self: RunStep) bool {
else => continue,
};
return switch (self.condition) {
.always => false,
.output_outdated => true,
};
return false;
}
fn make(step: *Step) !void {

View File

@ -341,7 +341,7 @@ test "Condition - wait and signal" {
};
var multi_wait = MultiWait{};
for (multi_wait.threads) |*t| {
for (&multi_wait.threads) |*t| {
t.* = try std.Thread.spawn(.{}, MultiWait.run, .{&multi_wait});
}
@ -389,7 +389,7 @@ test "Condition - signal" {
};
var signal_test = SignalTest{};
for (signal_test.threads) |*t| {
for (&signal_test.threads) |*t| {
t.* = try std.Thread.spawn(.{}, SignalTest.run, .{&signal_test});
}
@ -457,7 +457,7 @@ test "Condition - multi signal" {
var threads = [_]std.Thread{undefined} ** num_threads;
// Create a circle of paddles which hit each other
for (threads) |*t, i| {
for (&threads, 0..) |*t, i| {
const paddle = &paddles[i];
const hit_to = &paddles[(i + 1) % paddles.len];
t.* = try std.Thread.spawn(.{}, Paddle.run, .{ paddle, hit_to });
@ -468,7 +468,7 @@ test "Condition - multi signal" {
for (threads) |t| t.join();
// The first paddle will be hit one last time by the last paddle.
for (paddles) |p, i| {
for (paddles, 0..) |p, i| {
const expected = @as(u32, num_iterations) + @boolToInt(i == 0);
try testing.expectEqual(p.value, expected);
}
@ -513,7 +513,7 @@ test "Condition - broadcasting" {
};
var broadcast_test = BroadcastTest{};
for (broadcast_test.threads) |*t| {
for (&broadcast_test.threads) |*t| {
t.* = try std.Thread.spawn(.{}, BroadcastTest.run, .{&broadcast_test});
}
@ -584,7 +584,7 @@ test "Condition - broadcasting - wake all threads" {
var broadcast_test = BroadcastTest{};
var thread_id: usize = 1;
for (broadcast_test.threads) |*t| {
for (&broadcast_test.threads) |*t| {
t.* = try std.Thread.spawn(.{}, BroadcastTest.run, .{ &broadcast_test, thread_id });
thread_id += 1;
}

View File

@ -895,7 +895,7 @@ test "Futex - signaling" {
var threads = [_]std.Thread{undefined} ** num_threads;
// Create a circle of paddles which hit each other
for (threads) |*t, i| {
for (&threads, 0..) |*t, i| {
const paddle = &paddles[i];
const hit_to = &paddles[(i + 1) % paddles.len];
t.* = try std.Thread.spawn(.{}, Paddle.run, .{ paddle, hit_to });
@ -950,14 +950,14 @@ test "Futex - broadcasting" {
threads: [num_threads]std.Thread = undefined,
fn run(self: *@This()) !void {
for (self.barriers) |*barrier| {
for (&self.barriers) |*barrier| {
try barrier.wait();
}
}
};
var broadcast = Broadcast{};
for (broadcast.threads) |*t| t.* = try std.Thread.spawn(.{}, Broadcast.run, .{&broadcast});
for (&broadcast.threads) |*t| t.* = try std.Thread.spawn(.{}, Broadcast.run, .{&broadcast});
for (broadcast.threads) |t| t.join();
}

View File

@ -245,7 +245,7 @@ const NonAtomicCounter = struct {
}
fn inc(self: *NonAtomicCounter) void {
for (@bitCast([2]u64, self.get() + 1)) |v, i| {
for (@bitCast([2]u64, self.get() + 1), 0..) |v, i| {
@ptrCast(*volatile u64, &self.value[i]).* = v;
}
}
@ -277,7 +277,7 @@ test "Mutex - many uncontended" {
};
var runners = [_]Runner{.{}} ** num_threads;
for (runners) |*r| r.thread = try Thread.spawn(.{}, Runner.run, .{r});
for (&runners) |*r| r.thread = try Thread.spawn(.{}, Runner.run, .{r});
for (runners) |r| r.thread.join();
for (runners) |r| try testing.expectEqual(r.counter.get(), num_increments);
}
@ -312,7 +312,7 @@ test "Mutex - many contended" {
var runner = Runner{};
var threads: [num_threads]Thread = undefined;
for (threads) |*t| t.* = try Thread.spawn(.{}, Runner.run, .{&runner});
for (&threads) |*t| t.* = try Thread.spawn(.{}, Runner.run, .{&runner});
for (threads) |t| t.join();
try testing.expectEqual(runner.counter.get(), num_increments * num_threads);

View File

@ -274,7 +274,7 @@ test "ResetEvent - broadcast" {
var ctx = Context{};
var threads: [num_threads - 1]std.Thread = undefined;
for (threads) |*t| t.* = try std.Thread.spawn(.{}, Context.run, .{&ctx});
for (&threads) |*t| t.* = try std.Thread.spawn(.{}, Context.run, .{&ctx});
defer for (threads) |t| t.join();
ctx.run();

View File

@ -364,7 +364,7 @@ test "RwLock - concurrent access" {
var runner = Runner{};
var threads: [num_writers + num_readers]std.Thread = undefined;
for (threads[0..num_writers]) |*t, i| t.* = try std.Thread.spawn(.{}, Runner.writer, .{ &runner, i });
for (threads[0..num_writers], 0..) |*t, i| t.* = try std.Thread.spawn(.{}, Runner.writer, .{ &runner, i });
for (threads[num_writers..]) |*t| t.* = try std.Thread.spawn(.{}, Runner.reader, .{&runner});
for (threads) |t| t.join();

View File

@ -54,7 +54,7 @@ test "Thread.Semaphore" {
var n: i32 = 0;
var ctx = TestContext{ .sem = &sem, .n = &n };
for (threads) |*t| t.* = try std.Thread.spawn(.{}, TestContext.worker, .{&ctx});
for (&threads) |*t| t.* = try std.Thread.spawn(.{}, TestContext.worker, .{&ctx});
for (threads) |t| t.join();
sem.wait();
try testing.expect(n == num_threads);

View File

@ -715,7 +715,7 @@ pub fn ArrayHashMapUnmanaged(
const slice = self.entries.slice();
const hashes_array = slice.items(.hash);
const keys_array = slice.items(.key);
for (keys_array) |*item_key, i| {
for (keys_array, 0..) |*item_key, i| {
if (hashes_array[i] == h and checkedEql(ctx, key, item_key.*, i)) {
return GetOrPutResult{
.key_ptr = item_key,
@ -946,7 +946,7 @@ pub fn ArrayHashMapUnmanaged(
const slice = self.entries.slice();
const hashes_array = slice.items(.hash);
const keys_array = slice.items(.key);
for (keys_array) |*item_key, i| {
for (keys_array, 0..) |*item_key, i| {
if (hashes_array[i] == h and checkedEql(ctx, key, item_key.*, i)) {
return i;
}
@ -1285,7 +1285,7 @@ pub fn ArrayHashMapUnmanaged(
const slice = self.entries.slice();
const hashes_array = if (store_hash) slice.items(.hash) else {};
const keys_array = slice.items(.key);
for (keys_array) |*item_key, i| {
for (keys_array, 0..) |*item_key, i| {
const hash_match = if (store_hash) hashes_array[i] == key_hash else true;
if (hash_match and key_ctx.eql(key, item_key.*, i)) {
const removed_entry: KV = .{
@ -1326,7 +1326,7 @@ pub fn ArrayHashMapUnmanaged(
const slice = self.entries.slice();
const hashes_array = if (store_hash) slice.items(.hash) else {};
const keys_array = slice.items(.key);
for (keys_array) |*item_key, i| {
for (keys_array, 0..) |*item_key, i| {
const hash_match = if (store_hash) hashes_array[i] == key_hash else true;
if (hash_match and key_ctx.eql(key, item_key.*, i)) {
switch (removal_type) {
@ -1634,7 +1634,7 @@ pub fn ArrayHashMapUnmanaged(
const items = if (store_hash) slice.items(.hash) else slice.items(.key);
const indexes = header.indexes(I);
entry_loop: for (items) |key, i| {
entry_loop: for (items, 0..) |key, i| {
const h = if (store_hash) key else checkedHash(ctx, key);
const start_index = safeTruncate(usize, h);
const end_index = start_index +% indexes.len;
@ -1730,7 +1730,7 @@ pub fn ArrayHashMapUnmanaged(
const indexes = header.indexes(I);
if (indexes.len == 0) return;
var is_empty = false;
for (indexes) |idx, i| {
for (indexes, 0..) |idx, i| {
if (idx.isEmpty()) {
is_empty = true;
} else {
@ -1826,7 +1826,7 @@ const min_bit_index = 5;
const max_capacity = (1 << max_bit_index) - 1;
const index_capacities = blk: {
var caps: [max_bit_index + 1]u32 = undefined;
for (caps[0..max_bit_index]) |*item, i| {
for (caps[0..max_bit_index], 0..) |*item, i| {
item.* = (1 << i) * 3 / 5;
}
caps[max_bit_index] = max_capacity;
@ -2025,7 +2025,7 @@ test "iterator hash map" {
try testing.expect(count == 3);
try testing.expect(it.next() == null);
for (buffer) |_, i| {
for (buffer, 0..) |_, i| {
try testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
}
@ -2037,7 +2037,7 @@ test "iterator hash map" {
if (count >= 2) break;
}
for (buffer[0..2]) |_, i| {
for (buffer[0..2], 0..) |_, i| {
try testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
}
@ -2299,7 +2299,7 @@ test "sort" {
map.sort(C{ .keys = map.keys() });
var x: i32 = 1;
for (map.keys()) |key, i| {
for (map.keys(), 0..) |key, i| {
try testing.expect(key == x);
try testing.expect(map.values()[i] == x * 3);
x += 1;

View File

@ -183,7 +183,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
mem.copy(T, range, new_items);
const after_subrange = start + new_items.len;
for (self.items[after_range..]) |item, i| {
for (self.items[after_range..], 0..) |item, i| {
self.items[after_subrange..][i] = item;
}
@ -216,7 +216,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
if (newlen == i) return self.pop();
const old_item = self.items[i];
for (self.items[i..newlen]) |*b, j| b.* = self.items[i + 1 + j];
for (self.items[i..newlen], 0..) |*b, j| b.* = self.items[i + 1 + j];
self.items[newlen] = undefined;
self.items.len = newlen;
return old_item;
@ -666,7 +666,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
if (newlen == i) return self.pop();
const old_item = self.items[i];
for (self.items[i..newlen]) |*b, j| b.* = self.items[i + 1 + j];
for (self.items[i..newlen], 0..) |*b, j| b.* = self.items[i + 1 + j];
self.items[newlen] = undefined;
self.items.len = newlen;
return old_item;
@ -1069,7 +1069,7 @@ test "std.ArrayList/ArrayListUnmanaged.basic" {
}
}
for (list.items) |v, i| {
for (list.items, 0..) |v, i| {
try testing.expect(v == @intCast(i32, i + 1));
}
@ -1119,7 +1119,7 @@ test "std.ArrayList/ArrayListUnmanaged.basic" {
}
}
for (list.items) |v, i| {
for (list.items, 0..) |v, i| {
try testing.expect(v == @intCast(i32, i + 1));
}

View File

@ -272,7 +272,7 @@ test "ASCII character classes" {
/// Asserts `output.len >= ascii_string.len`.
pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
std.debug.assert(output.len >= ascii_string.len);
for (ascii_string) |c, i| {
for (ascii_string, 0..) |c, i| {
output[i] = toLower(c);
}
return output[0..ascii_string.len];
@ -301,7 +301,7 @@ test "allocLowerString" {
/// Asserts `output.len >= ascii_string.len`.
pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
std.debug.assert(output.len >= ascii_string.len);
for (ascii_string) |c, i| {
for (ascii_string, 0..) |c, i| {
output[i] = toUpper(c);
}
return output[0..ascii_string.len];
@ -329,7 +329,7 @@ test "allocUpperString" {
/// Compares strings `a` and `b` case-insensitively and returns whether they are equal.
pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
if (a.len != b.len) return false;
for (a) |a_c, i| {
for (a, 0..) |a_c, i| {
if (toLower(a_c) != toLower(b[i])) return false;
}
return true;

View File

@ -548,7 +548,7 @@ test "Atomic.bitSet" {
var x = Atomic(Int).init(0);
const bit_array = @as([@bitSizeOf(Int)]void, undefined);
for (bit_array) |_, bit_index| {
for (bit_array, 0..) |_, bit_index| {
const bit = @intCast(std.math.Log2Int(Int), bit_index);
const mask = @as(Int, 1) << bit;
@ -562,7 +562,7 @@ test "Atomic.bitSet" {
try testing.expect(x.load(.SeqCst) & mask != 0);
// all the previous bits should have not changed (still be set)
for (bit_array[0..bit_index]) |_, prev_bit_index| {
for (bit_array[0..bit_index], 0..) |_, prev_bit_index| {
const prev_bit = @intCast(std.math.Log2Int(Int), prev_bit_index);
const prev_mask = @as(Int, 1) << prev_bit;
try testing.expect(x.load(.SeqCst) & prev_mask != 0);
@ -578,7 +578,7 @@ test "Atomic.bitReset" {
var x = Atomic(Int).init(0);
const bit_array = @as([@bitSizeOf(Int)]void, undefined);
for (bit_array) |_, bit_index| {
for (bit_array, 0..) |_, bit_index| {
const bit = @intCast(std.math.Log2Int(Int), bit_index);
const mask = @as(Int, 1) << bit;
x.storeUnchecked(x.loadUnchecked() | mask);
@ -593,7 +593,7 @@ test "Atomic.bitReset" {
try testing.expect(x.load(.SeqCst) & mask == 0);
// all the previous bits should have not changed (still be reset)
for (bit_array[0..bit_index]) |_, prev_bit_index| {
for (bit_array[0..bit_index], 0..) |_, prev_bit_index| {
const prev_bit = @intCast(std.math.Log2Int(Int), prev_bit_index);
const prev_mask = @as(Int, 1) << prev_bit;
try testing.expect(x.load(.SeqCst) & prev_mask == 0);
@ -609,7 +609,7 @@ test "Atomic.bitToggle" {
var x = Atomic(Int).init(0);
const bit_array = @as([@bitSizeOf(Int)]void, undefined);
for (bit_array) |_, bit_index| {
for (bit_array, 0..) |_, bit_index| {
const bit = @intCast(std.math.Log2Int(Int), bit_index);
const mask = @as(Int, 1) << bit;
@ -623,7 +623,7 @@ test "Atomic.bitToggle" {
try testing.expect(x.load(.SeqCst) & mask == 0);
// all the previous bits should have not changed (still be toggled back)
for (bit_array[0..bit_index]) |_, prev_bit_index| {
for (bit_array[0..bit_index], 0..) |_, prev_bit_index| {
const prev_bit = @intCast(std.math.Log2Int(Int), prev_bit_index);
const prev_mask = @as(Int, 1) << prev_bit;
try testing.expect(x.load(.SeqCst) & prev_mask == 0);

View File

@ -212,11 +212,11 @@ test "std.atomic.Queue" {
try expect(context.queue.isEmpty());
var putters: [put_thread_count]std.Thread = undefined;
for (putters) |*t| {
for (&putters) |*t| {
t.* = try std.Thread.spawn(.{}, startPuts, .{&context});
}
var getters: [put_thread_count]std.Thread = undefined;
for (getters) |*t| {
for (&getters) |*t| {
t.* = try std.Thread.spawn(.{}, startGets, .{&context});
}

View File

@ -117,11 +117,11 @@ test "std.atomic.stack" {
}
} else {
var putters: [put_thread_count]std.Thread = undefined;
for (putters) |*t| {
for (&putters) |*t| {
t.* = try std.Thread.spawn(.{}, startPuts, .{&context});
}
var getters: [put_thread_count]std.Thread = undefined;
for (getters) |*t| {
for (&getters) |*t| {
t.* = try std.Thread.spawn(.{}, startGets, .{&context});
}

View File

@ -140,7 +140,7 @@ pub const Base64Decoder = struct {
};
var char_in_alphabet = [_]bool{false} ** 256;
for (alphabet_chars) |c, i| {
for (alphabet_chars, 0..) |c, i| {
assert(!char_in_alphabet[c]);
assert(pad_char == null or c != pad_char.?);
@ -185,7 +185,7 @@ pub const Base64Decoder = struct {
var acc_len: u4 = 0;
var dest_idx: usize = 0;
var leftover_idx: ?usize = null;
for (source) |c, src_idx| {
for (source, 0..) |c, src_idx| {
const d = decoder.char_to_index[c];
if (d == invalid_char) {
if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter;
@ -258,7 +258,7 @@ pub const Base64DecoderWithIgnore = struct {
var acc_len: u4 = 0;
var dest_idx: usize = 0;
var leftover_idx: ?usize = null;
for (source) |c, src_idx| {
for (source, 0..) |c, src_idx| {
if (decoder_with_ignore.char_is_ignored[c]) continue;
const d = decoder.char_to_index[c];
if (d == Base64Decoder.invalid_char) {

View File

@ -494,14 +494,14 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
/// Flips all bits in this bit set which are present
/// in the toggles bit set.
pub fn toggleSet(self: *Self, toggles: Self) void {
for (self.masks) |*mask, i| {
for (&self.masks, 0..) |*mask, i| {
mask.* ^= toggles.masks[i];
}
}
/// Flips every bit in the bit set.
pub fn toggleAll(self: *Self) void {
for (self.masks) |*mask| {
for (&self.masks) |*mask| {
mask.* = ~mask.*;
}
@ -515,7 +515,7 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
/// result in the first one. Bits in the result are
/// set if the corresponding bits were set in either input.
pub fn setUnion(self: *Self, other: Self) void {
for (self.masks) |*mask, i| {
for (&self.masks, 0..) |*mask, i| {
mask.* |= other.masks[i];
}
}
@ -524,7 +524,7 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
/// the result in the first one. Bits in the result are
/// set if the corresponding bits were set in both inputs.
pub fn setIntersection(self: *Self, other: Self) void {
for (self.masks) |*mask, i| {
for (&self.masks, 0..) |*mask, i| {
mask.* &= other.masks[i];
}
}
@ -544,7 +544,7 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
/// If no bits are set, returns null.
pub fn toggleFirstSet(self: *Self) ?usize {
var offset: usize = 0;
const mask = for (self.masks) |*mask| {
const mask = for (&self.masks) |*mask| {
if (mask.* != 0) break mask;
offset += @bitSizeOf(MaskInt);
} else return null;
@ -869,7 +869,7 @@ pub const DynamicBitSetUnmanaged = struct {
pub fn toggleSet(self: *Self, toggles: Self) void {
assert(toggles.bit_length == self.bit_length);
const num_masks = numMasks(self.bit_length);
for (self.masks[0..num_masks]) |*mask, i| {
for (self.masks[0..num_masks], 0..) |*mask, i| {
mask.* ^= toggles.masks[i];
}
}
@ -897,7 +897,7 @@ pub const DynamicBitSetUnmanaged = struct {
pub fn setUnion(self: *Self, other: Self) void {
assert(other.bit_length == self.bit_length);
const num_masks = numMasks(self.bit_length);
for (self.masks[0..num_masks]) |*mask, i| {
for (self.masks[0..num_masks], 0..) |*mask, i| {
mask.* |= other.masks[i];
}
}
@ -909,7 +909,7 @@ pub const DynamicBitSetUnmanaged = struct {
pub fn setIntersection(self: *Self, other: Self) void {
assert(other.bit_length == self.bit_length);
const num_masks = numMasks(self.bit_length);
for (self.masks[0..num_masks]) |*mask, i| {
for (self.masks[0..num_masks], 0..) |*mask, i| {
mask.* &= other.masks[i];
}
}

View File

@ -169,7 +169,7 @@ pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type {
} else {
mem.copy(T, range, new_items);
const after_subrange = start + new_items.len;
for (self.constSlice()[after_range..]) |item, i| {
for (self.constSlice()[after_range..], 0..) |item, i| {
self.slice()[after_subrange..][i] = item;
}
self.len -= len - new_items.len;
@ -197,7 +197,7 @@ pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type {
const newlen = self.len - 1;
if (newlen == i) return self.pop();
const old_item = self.get(i);
for (self.slice()[i..newlen]) |*b, j| b.* = self.get(i + 1 + j);
for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
self.set(newlen, undefined);
self.len = newlen;
return old_item;

View File

@ -975,6 +975,7 @@ pub const panic_messages = struct {
pub const unwrap_error = "attempt to unwrap error";
pub const index_out_of_bounds = "index out of bounds";
pub const start_index_greater_than_end = "start index is larger than end index";
pub const for_len_mismatch = "for loop over objects with non-equal lengths";
};
pub noinline fn returnError(st: *StackTrace) void {

View File

@ -604,7 +604,7 @@ pub const ChildProcess = struct {
const arena = arena_allocator.allocator();
const argv_buf = try arena.allocSentinel(?[*:0]u8, self.argv.len, null);
for (self.argv) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr;
for (self.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr;
const envp = if (self.env_map) |env_map| m: {
const envp_buf = try createNullDelimitedEnvMap(arena, env_map);
@ -712,7 +712,7 @@ pub const ChildProcess = struct {
// Therefore, we do all the allocation for the execve() before the fork().
// This means we must do the null-termination of argv and env vars here.
const argv_buf = try arena.allocSentinel(?[*:0]u8, self.argv.len, null);
for (self.argv) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr;
for (self.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr;
const envp = m: {
if (self.env_map) |env_map| {
@ -1424,7 +1424,7 @@ fn windowsCreateCommandLine(allocator: mem.Allocator, argv: []const []const u8)
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();
for (argv) |arg, arg_i| {
for (argv, 0..) |arg, arg_i| {
if (arg_i != 0) try buf.append(' ');
if (mem.indexOfAny(u8, arg, " \t\n\"") == null) {
try buf.appendSlice(arg);

View File

@ -1223,7 +1223,7 @@ pub const Coff = struct {
pub fn getSectionHeadersAlloc(self: *const Coff, allocator: mem.Allocator) ![]SectionHeader {
const section_headers = self.getSectionHeaders();
const out_buff = try allocator.alloc(SectionHeader, section_headers.len);
for (out_buff) |*section_header, i| {
for (out_buff, 0..) |*section_header, i| {
section_header.* = section_headers[i];
}

View File

@ -159,7 +159,7 @@ fn levels(compression: Compression) CompressionLevel {
fn matchLen(a: []u8, b: []u8, max: u32) u32 {
var bounded_a = a[0..max];
var bounded_b = b[0..max];
for (bounded_a) |av, i| {
for (bounded_a, 0..) |av, i| {
if (bounded_b[i] != av) {
return @intCast(u32, i);
}
@ -312,14 +312,14 @@ pub fn Compressor(comptime WriterType: anytype) type {
// Iterate over slices instead of arrays to avoid copying
// the entire table onto the stack (https://golang.org/issue/18625).
for (self.hash_prev) |v, i| {
for (self.hash_prev, 0..) |v, i| {
if (v > delta) {
self.hash_prev[i] = @intCast(u32, v - delta);
} else {
self.hash_prev[i] = 0;
}
}
for (self.hash_head) |v, i| {
for (self.hash_head, 0..) |v, i| {
if (v > delta) {
self.hash_head[i] = @intCast(u32, v - delta);
} else {
@ -391,7 +391,7 @@ pub fn Compressor(comptime WriterType: anytype) type {
var dst = self.hash_match[0..dst_size];
_ = self.bulk_hasher(to_check, dst);
var new_h: u32 = 0;
for (dst) |val, i| {
for (dst, 0..) |val, i| {
var di = i + index;
new_h = val;
var hh = &self.hash_head[new_h & hash_mask];
@ -1102,7 +1102,7 @@ test "bulkHash4" {
defer testing.allocator.free(dst);
_ = bulkHash4(y, dst);
for (dst) |got, i| {
for (dst, 0..) |got, i| {
var want = hash4(y[i..]);
try testing.expectEqual(want, got);
}

View File

@ -171,7 +171,7 @@ test "deflate/inflate" {
var large_data_chunk = try testing.allocator.alloc(u8, 100_000);
defer testing.allocator.free(large_data_chunk);
// fill with random data
for (large_data_chunk) |_, i| {
for (large_data_chunk, 0..) |_, i| {
large_data_chunk[i] = @truncate(u8, i) *% @truncate(u8, i);
}
try testToFromWithLimit(large_data_chunk, limits);
@ -205,7 +205,7 @@ test "very long sparse chunk" {
n -= cur - s.l;
cur = s.l;
}
for (b[0..n]) |_, i| {
for (b[0..n], 0..) |_, i| {
if (s.cur + i >= s.l -| (1 << 16)) {
b[i] = 1;
} else {
@ -451,7 +451,7 @@ test "inflate reset" {
defer compressed_strings[0].deinit();
defer compressed_strings[1].deinit();
for (strings) |s, i| {
for (strings, 0..) |s, i| {
var comp = try compressor(
testing.allocator,
compressed_strings[i].writer(),
@ -498,7 +498,7 @@ test "inflate reset dictionary" {
defer compressed_strings[0].deinit();
defer compressed_strings[1].deinit();
for (strings) |s, i| {
for (strings, 0..) |s, i| {
var comp = try compressor(
testing.allocator,
compressed_strings[i].writer(),

View File

@ -165,7 +165,7 @@ const HuffmanDecoder = struct {
}
}
for (lengths) |n, li| {
for (lengths, 0..) |n, li| {
if (n == 0) {
continue;
}
@ -213,7 +213,7 @@ const HuffmanDecoder = struct {
// Above we've sanity checked that we never overwrote
// an existing entry. Here we additionally check that
// we filled the tables completely.
for (self.chunks) |chunk, i| {
for (self.chunks, 0..) |chunk, i| {
// As an exception, in the degenerate
// single-code case, we allow odd
// chunks to be missing.

View File

@ -264,7 +264,7 @@ pub const DeflateFast = struct {
var a = src[@intCast(usize, s)..@intCast(usize, s1)];
b = b[0..a.len];
// Extend the match to be as long as possible.
for (a) |_, i| {
for (a, 0..) |_, i| {
if (a[i] != b[i]) {
return @intCast(i32, i);
}
@ -285,7 +285,7 @@ pub const DeflateFast = struct {
b = b[0..a.len];
}
a = a[0..b.len];
for (b) |_, i| {
for (b, 0..) |_, i| {
if (a[i] != b[i]) {
return @intCast(i32, i);
}
@ -301,7 +301,7 @@ pub const DeflateFast = struct {
// Continue looking for more matches in the current block.
a = src[@intCast(usize, s + n)..@intCast(usize, s1)];
b = src[0..a.len];
for (a) |_, i| {
for (a, 0..) |_, i| {
if (a[i] != b[i]) {
return @intCast(i32, i) + n;
}
@ -330,7 +330,7 @@ pub const DeflateFast = struct {
fn shiftOffsets(self: *Self) void {
if (self.prev_len == 0) {
// We have no history; just clear the table.
for (self.table) |_, i| {
for (self.table, 0..) |_, i| {
self.table[i] = TableEntry{ .val = 0, .offset = 0 };
}
self.cur = max_match_offset + 1;
@ -338,7 +338,7 @@ pub const DeflateFast = struct {
}
// Shift down everything in the table that isn't already too far away.
for (self.table) |_, i| {
for (self.table, 0..) |_, i| {
var v = self.table[i].offset - self.cur + max_match_offset + 1;
if (v < 0) {
// We want to reset self.cur to max_match_offset + 1, so we need to shift

View File

@ -18,7 +18,7 @@ test "best speed" {
var abcabc = try testing.allocator.alloc(u8, 131_072);
defer testing.allocator.free(abcabc);
for (abcabc) |_, i| {
for (abcabc, 0..) |_, i| {
abcabc[i] = @intCast(u8, i % 128);
}

View File

@ -378,7 +378,7 @@ test "dictionary decoder" {
_ = try want.write(".");
var str = poem;
for (poem_refs) |ref, i| {
for (poem_refs, 0..) |ref, i| {
_ = i;
if (ref.dist == 0) {
try util.writeString(&dd, got, str[0..ref.length]);

View File

@ -197,7 +197,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
lit_enc: *hm_code.HuffmanEncoder,
off_enc: *hm_code.HuffmanEncoder,
) void {
for (self.codegen_freq) |_, i| {
for (self.codegen_freq, 0..) |_, i| {
self.codegen_freq[i] = 0;
}
@ -208,12 +208,12 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
var codegen = self.codegen; // cache
// Copy the concatenated code sizes to codegen. Put a marker at the end.
var cgnl = codegen[0..num_literals];
for (cgnl) |_, i| {
for (cgnl, 0..) |_, i| {
cgnl[i] = @intCast(u8, lit_enc.codes[i].len);
}
cgnl = codegen[num_literals .. num_literals + num_offsets];
for (cgnl) |_, i| {
for (cgnl, 0..) |_, i| {
cgnl[i] = @intCast(u8, off_enc.codes[i].len);
}
codegen[num_literals + num_offsets] = bad_code;
@ -600,10 +600,10 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
var num_literals: u32 = 0;
var num_offsets: u32 = 0;
for (self.literal_freq) |_, i| {
for (self.literal_freq, 0..) |_, i| {
self.literal_freq[i] = 0;
}
for (self.offset_freq) |_, i| {
for (self.offset_freq, 0..) |_, i| {
self.offset_freq[i] = 0;
}
@ -691,7 +691,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
}
// Clear histogram
for (self.literal_freq) |_, i| {
for (self.literal_freq, 0..) |_, i| {
self.literal_freq[i] = 0;
}

View File

@ -71,7 +71,7 @@ pub const HuffmanEncoder = struct {
// Number of non-zero literals
var count: u32 = 0;
// Set list to be the set of all non-zero literals and their frequencies
for (freq) |f, i| {
for (freq, 0..) |f, i| {
if (f != 0) {
list[count] = LiteralNode{ .literal = @intCast(u16, i), .freq = f };
count += 1;
@ -86,7 +86,7 @@ pub const HuffmanEncoder = struct {
if (count <= 2) {
// Handle the small cases here, because they are awkward for the general case code. With
// two or fewer literals, everything has bit length 1.
for (list) |node, i| {
for (list, 0..) |node, i| {
// "list" is in order of increasing literal value.
self.codes[node.literal].set(@intCast(u16, i), 1);
}
@ -103,7 +103,7 @@ pub const HuffmanEncoder = struct {
pub fn bitLength(self: *HuffmanEncoder, freq: []u16) u32 {
var total: u32 = 0;
for (freq) |f, i| {
for (freq, 0..) |f, i| {
if (f != 0) {
total += @intCast(u32, f) * @intCast(u32, self.codes[i].len);
}
@ -258,7 +258,7 @@ pub const HuffmanEncoder = struct {
var code = @as(u16, 0);
var list = list_arg;
for (bit_count) |bits, n| {
for (bit_count, 0..) |bits, n| {
code <<= 1;
if (n == 0 or bits == 0) {
continue;
@ -340,7 +340,7 @@ pub fn generateFixedLiteralEncoding(allocator: Allocator) !HuffmanEncoder {
pub fn generateFixedOffsetEncoding(allocator: Allocator) !HuffmanEncoder {
var h = try newHuffmanEncoder(allocator, 30);
var codes = h.codes;
for (codes) |_, ch| {
for (codes, 0..) |_, ch| {
codes[ch] = HuffCode{ .code = bu.bitReverse(u16, @intCast(u16, ch), 5), .len = 5 };
}
return h;

View File

@ -143,7 +143,7 @@ pub const DecoderState = struct {
}
self.lzma_props = new_props;
for (self.pos_slot_decoder) |*t| t.reset();
for (&self.pos_slot_decoder) |*t| t.reset();
self.align_decoder.reset();
self.pos_decoders = .{0x400} ** 115;
self.is_match = .{0x400} ** 192;

View File

@ -174,8 +174,8 @@ pub const LenDecoder = struct {
pub fn reset(self: *LenDecoder) void {
self.choice = 0x400;
self.choice2 = 0x400;
for (self.low_coder) |*t| t.reset();
for (self.mid_coder) |*t| t.reset();
for (&self.low_coder) |*t| t.reset();
for (&self.mid_coder) |*t| t.reset();
self.high_coder.reset();
}
};

View File

@ -21,7 +21,7 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type {
return a.key.len < b.key.len;
}
}).lenAsc;
for (kvs_list) |kv, i| {
for (kvs_list, 0..) |kv, i| {
if (V != void) {
sorted_kvs[i] = .{ .key = kv.@"0", .value = kv.@"1" };
} else {

View File

@ -344,7 +344,7 @@ pub const Ed25519 = struct {
var a_batch: [count]Curve = undefined;
var expected_r_batch: [count]Curve = undefined;
for (signature_batch) |signature, i| {
for (signature_batch, 0..) |signature, i| {
const r = signature.sig.r;
const s = signature.sig.s;
try Curve.scalar.rejectNonCanonical(s);
@ -360,7 +360,7 @@ pub const Ed25519 = struct {
}
var hram_batch: [count]Curve.scalar.CompressedScalar = undefined;
for (signature_batch) |signature, i| {
for (signature_batch, 0..) |signature, i| {
var h = Sha512.init(.{});
h.update(&r_batch[i]);
h.update(&signature.public_key.bytes);
@ -371,20 +371,20 @@ pub const Ed25519 = struct {
}
var z_batch: [count]Curve.scalar.CompressedScalar = undefined;
for (z_batch) |*z| {
for (&z_batch) |*z| {
crypto.random.bytes(z[0..16]);
mem.set(u8, z[16..], 0);
}
var zs_sum = Curve.scalar.zero;
for (z_batch) |z, i| {
for (z_batch, 0..) |z, i| {
const zs = Curve.scalar.mul(z, s_batch[i]);
zs_sum = Curve.scalar.add(zs_sum, zs);
}
zs_sum = Curve.scalar.mul8(zs_sum);
var zhs: [count]Curve.scalar.CompressedScalar = undefined;
for (z_batch) |z, i| {
for (z_batch, 0..) |z, i| {
zhs[i] = Curve.scalar.mul(z, hram_batch[i]);
}

View File

@ -161,7 +161,7 @@ pub const Edwards25519 = struct {
fn slide(s: [32]u8) [2 * 32]i8 {
const reduced = if ((s[s.len - 1] & 0x80) == 0) s else scalar.reduce(s);
var e: [2 * 32]i8 = undefined;
for (reduced) |x, i| {
for (reduced, 0..) |x, i| {
e[i * 2 + 0] = @as(i8, @truncate(u4, x));
e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4));
}
@ -308,7 +308,7 @@ pub const Edwards25519 = struct {
var bpc: [9]Edwards25519 = undefined;
mem.copy(Edwards25519, bpc[0..], basePointPc[0..bpc.len]);
for (ps) |p, i| {
for (ps, 0..) |p, i| {
if (p.is_base) {
pcs[i] = bpc;
} else {
@ -317,13 +317,13 @@ pub const Edwards25519 = struct {
}
}
var es: [count][2 * 32]i8 = undefined;
for (ss) |s, i| {
for (ss, 0..) |s, i| {
es[i] = slide(s);
}
var q = Edwards25519.identityElement;
var pos: usize = 2 * 32 - 1;
while (true) : (pos -= 1) {
for (es) |e, i| {
for (es, 0..) |e, i| {
const slot = e[pos];
if (slot > 0) {
q = q.add(pcs[i][@intCast(usize, slot)]);

View File

@ -1092,7 +1092,7 @@ pub const rsa = struct {
if (exponent_elem.identifier.tag != .integer) return error.CertificateFieldHasWrongDataType;
// Skip over meaningless zeroes in the modulus.
const modulus_raw = pub_key[modulus_elem.slice.start..modulus_elem.slice.end];
const modulus_offset = for (modulus_raw) |byte, i| {
const modulus_offset = for (modulus_raw, 0..) |byte, i| {
if (byte != 0) break i;
} else modulus_raw.len;
return .{

View File

@ -170,7 +170,7 @@ pub const Aegis128L = struct {
}
const computed_tag = state.mac(ad.len, m.len);
var acc: u8 = 0;
for (computed_tag) |_, j| {
for (computed_tag, 0..) |_, j| {
acc |= (computed_tag[j] ^ tag[j]);
}
if (acc != 0) {
@ -339,7 +339,7 @@ pub const Aegis256 = struct {
}
const computed_tag = state.mac(ad.len, m.len);
var acc: u8 = 0;
for (computed_tag) |_, j| {
for (computed_tag, 0..) |_, j| {
acc |= (computed_tag[j] ^ tag[j]);
}
if (acc != 0) {
@ -562,7 +562,7 @@ test "Aegis256 test vector 3" {
test "Aegis MAC" {
const key = [_]u8{0x00} ** Aegis128LMac.key_length;
var msg: [64]u8 = undefined;
for (msg) |*m, i| {
for (&msg, 0..) |*m, i| {
m.* = @truncate(u8, i);
}
const st_init = Aegis128LMac.init(&key);

View File

@ -115,11 +115,11 @@ test "expand 128-bit key" {
const dec = Aes128.initDec(key);
var exp: [16]u8 = undefined;
for (enc.key_schedule.round_keys) |round_key, i| {
for (enc.key_schedule.round_keys, 0..) |round_key, i| {
_ = try std.fmt.hexToBytes(&exp, exp_enc[i]);
try testing.expectEqualSlices(u8, &exp, &round_key.toBytes());
}
for (dec.key_schedule.round_keys) |round_key, i| {
for (dec.key_schedule.round_keys, 0..) |round_key, i| {
_ = try std.fmt.hexToBytes(&exp, exp_dec[i]);
try testing.expectEqualSlices(u8, &exp, &round_key.toBytes());
}
@ -154,11 +154,11 @@ test "expand 256-bit key" {
const dec = Aes256.initDec(key);
var exp: [16]u8 = undefined;
for (enc.key_schedule.round_keys) |round_key, i| {
for (enc.key_schedule.round_keys, 0..) |round_key, i| {
_ = try std.fmt.hexToBytes(&exp, exp_enc[i]);
try testing.expectEqualSlices(u8, &exp, &round_key.toBytes());
}
for (dec.key_schedule.round_keys) |round_key, i| {
for (dec.key_schedule.round_keys, 0..) |round_key, i| {
_ = try std.fmt.hexToBytes(&exp, exp_dec[i]);
try testing.expectEqualSlices(u8, &exp, &round_key.toBytes());
}

View File

@ -200,7 +200,7 @@ fn KeySchedule(comptime Aes: type) type {
fn expand128(t1: *Block) Self {
var round_keys: [11]Block = undefined;
const rcs = [_]u8{ 1, 2, 4, 8, 16, 32, 64, 128, 27, 54 };
inline for (rcs) |rc, round| {
inline for (rcs, 0..) |rc, round| {
round_keys[round] = t1.*;
t1.repr = drc(false, rc, t1.repr, t1.repr);
}
@ -212,7 +212,7 @@ fn KeySchedule(comptime Aes: type) type {
var round_keys: [15]Block = undefined;
const rcs = [_]u8{ 1, 2, 4, 8, 16, 32 };
round_keys[0] = t1.*;
inline for (rcs) |rc, round| {
inline for (rcs, 0..) |rc, round| {
round_keys[round * 2 + 1] = t2.*;
t1.repr = drc(false, rc, t2.repr, t1.repr);
round_keys[round * 2 + 2] = t1.*;

View File

@ -250,7 +250,7 @@ fn KeySchedule(comptime Aes: type) type {
fn expand128(t1: *Block) Self {
var round_keys: [11]Block = undefined;
const rcs = [_]u8{ 1, 2, 4, 8, 16, 32, 64, 128, 27, 54 };
inline for (rcs) |rc, round| {
inline for (rcs, 0..) |rc, round| {
round_keys[round] = t1.*;
t1.repr = drc128(rc, t1.repr);
}
@ -262,7 +262,7 @@ fn KeySchedule(comptime Aes: type) type {
var round_keys: [15]Block = undefined;
const rcs = [_]u8{ 1, 2, 4, 8, 16, 32 };
round_keys[0] = t1.*;
inline for (rcs) |rc, round| {
inline for (rcs, 0..) |rc, round| {
round_keys[round * 2 + 1] = t2.*;
t1.repr = drc256(false, rc, t2.repr, t1.repr);
round_keys[round * 2 + 2] = t1.*;

View File

@ -420,7 +420,7 @@ const powx = init: {
var array: [16]u8 = undefined;
var value = 1;
for (array) |*power| {
for (&array) |*power| {
power.* = value;
value = mul(value, 2);
}
@ -471,7 +471,7 @@ fn generateSbox(invert: bool) [256]u8 {
fn generateTable(invert: bool) [4][256]u32 {
var table: [4][256]u32 = undefined;
for (generateSbox(invert)) |value, index| {
for (generateSbox(invert), 0..) |value, index| {
table[0][index] = mul(value, if (invert) 0xb else 0x3);
table[0][index] |= math.shl(u32, mul(value, if (invert) 0xd else 0x1), 8);
table[0][index] |= math.shl(u32, mul(value, if (invert) 0x9 else 0x1), 16);

View File

@ -50,7 +50,7 @@ fn AesGcm(comptime Aes: anytype) type {
mem.writeIntBig(u64, final_block[8..16], m.len * 8);
mac.update(&final_block);
mac.final(tag);
for (t) |x, i| {
for (t, 0..) |x, i| {
tag[i] ^= x;
}
}
@ -82,12 +82,12 @@ fn AesGcm(comptime Aes: anytype) type {
mac.update(&final_block);
var computed_tag: [Ghash.mac_length]u8 = undefined;
mac.final(&computed_tag);
for (t) |x, i| {
for (t, 0..) |x, i| {
computed_tag[i] ^= x;
}
var acc: u8 = 0;
for (computed_tag) |_, p| {
for (computed_tag, 0..) |_, p| {
acc |= (computed_tag[p] ^ tag[p]);
}
if (acc != 0) {

View File

@ -155,7 +155,7 @@ fn AesOcb(comptime Aes: anytype) type {
xorWith(&offset, lx.star);
var pad = offset;
aes_enc_ctx.encrypt(&pad, &pad);
for (m[i * 16 ..]) |x, j| {
for (m[i * 16 ..], 0..) |x, j| {
c[i * 16 + j] = pad[j] ^ x;
}
var e = [_]u8{0} ** 16;
@ -220,7 +220,7 @@ fn AesOcb(comptime Aes: anytype) type {
xorWith(&offset, lx.star);
var pad = offset;
aes_enc_ctx.encrypt(&pad, &pad);
for (c[i * 16 ..]) |x, j| {
for (c[i * 16 ..], 0..) |x, j| {
m[i * 16 + j] = pad[j] ^ x;
}
var e = [_]u8{0} ** 16;
@ -242,14 +242,14 @@ fn AesOcb(comptime Aes: anytype) type {
inline fn xorBlocks(x: Block, y: Block) Block {
var z: Block = x;
for (z) |*v, i| {
for (&z, 0..) |*v, i| {
v.* = x[i] ^ y[i];
}
return z;
}
inline fn xorWith(x: *Block, y: Block) void {
for (x) |*v, i| {
for (x, 0..) |*v, i| {
v.* ^= y[i];
}
}

View File

@ -188,13 +188,13 @@ fn initBlocks(
mem.writeIntLittle(u32, h0[Blake2b512.digest_length..][0..4], 0);
blake2bLong(&block0, h0);
for (blocks.items[j + 0]) |*v, i| {
for (&blocks.items[j + 0], 0..) |*v, i| {
v.* = mem.readIntLittle(u64, block0[i * 8 ..][0..8]);
}
mem.writeIntLittle(u32, h0[Blake2b512.digest_length..][0..4], 1);
blake2bLong(&block0, h0);
for (blocks.items[j + 1]) |*v, i| {
for (&blocks.items[j + 1], 0..) |*v, i| {
v.* = mem.readIntLittle(u64, block0[i * 8 ..][0..8]);
}
}
@ -352,7 +352,7 @@ fn processBlockGeneric(
comptime xor: bool,
) void {
var t: [block_length]u64 = undefined;
for (t) |*v, i| {
for (&t, 0..) |*v, i| {
v.* = in1[i] ^ in2[i];
}
var i: usize = 0;
@ -375,11 +375,11 @@ fn processBlockGeneric(
}
}
if (xor) {
for (t) |v, j| {
for (t, 0..) |v, j| {
out[j] ^= in1[j] ^ in2[j] ^ v;
}
} else {
for (t) |v, j| {
for (t, 0..) |v, j| {
out[j] = in1[j] ^ in2[j] ^ v;
}
}
@ -428,12 +428,12 @@ fn finalize(
const lanes = memory / threads;
var lane: u24 = 0;
while (lane < threads - 1) : (lane += 1) {
for (blocks.items[(lane * lanes) + lanes - 1]) |v, i| {
for (blocks.items[(lane * lanes) + lanes - 1], 0..) |v, i| {
blocks.items[memory - 1][i] ^= v;
}
}
var block: [1024]u8 = undefined;
for (blocks.items[memory - 1]) |v, i| {
for (blocks.items[memory - 1], 0..) |v, i| {
mem.writeIntLittle(u64, block[i * 8 ..][0..8], v);
}
blake2bLong(out, &block);

View File

@ -74,7 +74,7 @@ pub fn State(comptime endian: builtin.Endian) type {
/// Byte-swap the entire state if the architecture doesn't match the required endianness.
pub fn endianSwap(self: *Self) void {
for (self.st) |*w| {
for (&self.st) |*w| {
w.* = mem.toNative(u64, w.*, endian);
}
}

View File

@ -437,7 +437,7 @@ pub fn bcrypt(
}
var ct: [ct_length]u8 = undefined;
for (cdata) |c, i| {
for (cdata, 0..) |c, i| {
mem.writeIntBig(u32, ct[i * 4 ..][0..4], c);
}
return ct[0..dk_length].*;
@ -505,7 +505,7 @@ const pbkdf_prf = struct {
// copy out
var out: [32]u8 = undefined;
for (cdata) |v, i| {
for (cdata, 0..) |v, i| {
std.mem.writeIntLittle(u32, out[4 * i ..][0..4], v);
}

View File

@ -133,7 +133,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
mem.set(u8, d.buf[d.buf_len..], 0);
d.t += d.buf_len;
d.round(d.buf[0..], true);
for (d.h) |*x| x.* = mem.nativeToLittle(u32, x.*);
for (&d.h) |*x| x.* = mem.nativeToLittle(u32, x.*);
mem.copy(u8, out[0..], @ptrCast(*[digest_length]u8, &d.h));
}
@ -141,7 +141,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
var m: [16]u32 = undefined;
var v: [16]u32 = undefined;
for (m) |*r, i| {
for (&m, 0..) |*r, i| {
r.* = mem.readIntLittle(u32, b[4 * i ..][0..4]);
}
@ -180,7 +180,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
}
}
for (d.h) |*r, i| {
for (&d.h, 0..) |*r, i| {
r.* ^= v[i] ^ v[i + 8];
}
}
@ -568,7 +568,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
mem.set(u8, d.buf[d.buf_len..], 0);
d.t += d.buf_len;
d.round(d.buf[0..], true);
for (d.h) |*x| x.* = mem.nativeToLittle(u64, x.*);
for (&d.h) |*x| x.* = mem.nativeToLittle(u64, x.*);
mem.copy(u8, out[0..], @ptrCast(*[digest_length]u8, &d.h));
}
@ -576,7 +576,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
var m: [16]u64 = undefined;
var v: [16]u64 = undefined;
for (m) |*r, i| {
for (&m, 0..) |*r, i| {
r.* = mem.readIntLittle(u64, b[8 * i ..][0..8]);
}
@ -615,7 +615,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
}
}
for (d.h) |*r, i| {
for (&d.h, 0..) |*r, i| {
r.* ^= v[i] ^ v[i + 8];
}
}

View File

@ -192,7 +192,7 @@ const CompressGeneric = struct {
for (MSG_SCHEDULE) |schedule| {
round(&state, block_words, schedule);
}
for (chaining_value) |_, i| {
for (chaining_value, 0..) |_, i| {
state[i] ^= state[i + 8];
state[i + 8] ^= chaining_value[i];
}
@ -211,7 +211,7 @@ fn first8Words(words: [16]u32) [8]u32 {
fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 {
var words: [count]u32 = undefined;
for (words) |*word, i| {
for (&words, 0..) |*word, i| {
word.* = mem.readIntSliceLittle(u32, bytes[4 * i ..]);
}
return words;
@ -658,7 +658,7 @@ fn testBlake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) !void {
// Setup input pattern
var input_pattern: [251]u8 = undefined;
for (input_pattern) |*e, i| e.* = @truncate(u8, i);
for (&input_pattern, 0..) |*e, i| e.* = @truncate(u8, i);
// Write repeating input pattern to hasher
var input_counter = input_len;

View File

@ -197,7 +197,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize) type {
fn hchacha20(input: [16]u8, key: [32]u8) [32]u8 {
var c: [4]u32 = undefined;
for (c) |_, i| {
for (c, 0..) |_, i| {
c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]);
}
const ctx = initContext(keyToWords(key), c);
@ -338,7 +338,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type {
fn hchacha20(input: [16]u8, key: [32]u8) [32]u8 {
var c: [4]u32 = undefined;
for (c) |_, i| {
for (c, 0..) |_, i| {
c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]);
}
const ctx = initContext(keyToWords(key), c);
@ -543,7 +543,7 @@ fn ChaChaPoly1305(comptime rounds_nb: usize) type {
mac.final(computedTag[0..]);
var acc: u8 = 0;
for (computedTag) |_, i| {
for (computedTag, 0..) |_, i| {
acc |= computedTag[i] ^ tag[i];
}
if (acc != 0) {

View File

@ -46,19 +46,19 @@ pub fn Cmac(comptime BlockCipher: type) type {
const left = block_length - self.pos;
var m = msg;
if (m.len > left) {
for (self.buf[self.pos..]) |*b, i| b.* ^= m[i];
for (self.buf[self.pos..], 0..) |*b, i| b.* ^= m[i];
m = m[left..];
self.cipher_ctx.encrypt(&self.buf, &self.buf);
self.pos = 0;
}
while (m.len > block_length) {
for (self.buf[0..block_length]) |*b, i| b.* ^= m[i];
for (self.buf[0..block_length], 0..) |*b, i| b.* ^= m[i];
m = m[block_length..];
self.cipher_ctx.encrypt(&self.buf, &self.buf);
self.pos = 0;
}
if (m.len > 0) {
for (self.buf[self.pos..][0..m.len]) |*b, i| b.* ^= m[i];
for (self.buf[self.pos..][0..m.len], 0..) |*b, i| b.* ^= m[i];
self.pos += m.len;
}
}
@ -69,7 +69,7 @@ pub fn Cmac(comptime BlockCipher: type) type {
mac = self.k2;
mac[self.pos] ^= 0x80;
}
for (mac) |*b, i| b.* ^= self.buf[i];
for (&mac, 0..) |*b, i| b.* ^= self.buf[i];
self.cipher_ctx.encrypt(out, &mac);
}

View File

@ -320,7 +320,7 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type {
if (st.leftover > 0) {
const want = math.min(block_length - st.leftover, mb.len);
const mc = mb[0..want];
for (mc) |x, i| {
for (mc, 0..) |x, i| {
st.buf[st.leftover + i] = x;
}
mb = mb[want..];
@ -337,7 +337,7 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type {
mb = mb[want..];
}
if (mb.len > 0) {
for (mb) |x, i| {
for (mb, 0..) |x, i| {
st.buf[st.leftover + i] = x;
}
st.leftover += mb.len;

View File

@ -45,7 +45,7 @@ pub const State = struct {
}
inline fn endianSwap(self: *Self) void {
for (self.data) |*w| {
for (&self.data) |*w| {
w.* = mem.littleToNative(u32, w.*);
}
}
@ -228,7 +228,7 @@ pub const Hash = struct {
while (in.len > 0) {
const left = State.RATE - self.buf_off;
const ps = math.min(in.len, left);
for (buf[self.buf_off .. self.buf_off + ps]) |*p, i| {
for (buf[self.buf_off .. self.buf_off + ps], 0..) |*p, i| {
p.* ^= in[i];
}
self.buf_off += ps;
@ -329,12 +329,12 @@ pub const Aead = struct {
// exactly one final non-full block, in the same way as Gimli-Hash.
var data = ad;
while (data.len >= State.RATE) : (data = data[State.RATE..]) {
for (buf[0..State.RATE]) |*p, i| {
for (buf[0..State.RATE], 0..) |*p, i| {
p.* ^= data[i];
}
state.permute();
}
for (buf[0..data.len]) |*p, i| {
for (buf[0..data.len], 0..) |*p, i| {
p.* ^= data[i];
}
@ -371,13 +371,13 @@ pub const Aead = struct {
in = in[State.RATE..];
out = out[State.RATE..];
}) {
for (in[0..State.RATE]) |v, i| {
for (in[0..State.RATE], 0..) |v, i| {
buf[i] ^= v;
}
mem.copy(u8, out[0..State.RATE], buf[0..State.RATE]);
state.permute();
}
for (in[0..]) |v, i| {
for (in[0..], 0..) |v, i| {
buf[i] ^= v;
out[i] = buf[i];
}
@ -414,13 +414,13 @@ pub const Aead = struct {
out = out[State.RATE..];
}) {
const d = in[0..State.RATE].*;
for (d) |v, i| {
for (d, 0..) |v, i| {
out[i] = buf[i] ^ v;
}
mem.copy(u8, buf[0..State.RATE], d[0..State.RATE]);
state.permute();
}
for (buf[0..in.len]) |*p, i| {
for (buf[0..in.len], 0..) |*p, i| {
const d = in[i];
out[i] = p.* ^ d;
p.* = d;

View File

@ -46,11 +46,11 @@ pub fn Hmac(comptime Hash: type) type {
mem.copy(u8, scratch[0..], key);
}
for (ctx.o_key_pad) |*b, i| {
for (&ctx.o_key_pad, 0..) |*b, i| {
b.* = scratch[i] ^ 0x5c;
}
for (i_key_pad) |*b, i| {
for (&i_key_pad, 0..) |*b, i| {
b.* = scratch[i] ^ 0x36;
}

View File

@ -110,7 +110,7 @@ pub const Md5 = struct {
d.round(d.buf[0..]);
for (d.s) |s, j| {
for (d.s, 0..) |s, j| {
mem.writeIntLittle(u32, out[4 * j ..][0..4], s);
}
}

View File

@ -138,7 +138,7 @@ pub fn pbkdf2(dk: []u8, password: []const u8, salt: []const u8, rounds: u32, com
mem.copy(u8, prev_block[0..], new_block[0..]);
// F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
for (dk_block) |_, j| {
for (dk_block, 0..) |_, j| {
dk_block[j] ^= new_block[j];
}
}

View File

@ -65,7 +65,7 @@ pub fn Field(comptime params: FieldParams) type {
/// Swap the endianness of an encoded element.
pub fn orderSwap(s: [encoded_length]u8) [encoded_length]u8 {
var t = s;
for (s) |x, i| t[t.len - 1 - i] = x;
for (s, 0..) |x, i| t[t.len - 1 - i] = x;
return t;
}

View File

@ -321,7 +321,7 @@ pub const P256 = struct {
fn slide(s: [32]u8) [2 * 32 + 1]i8 {
var e: [2 * 32 + 1]i8 = undefined;
for (s) |x, i| {
for (s, 0..) |x, i| {
e[i * 2 + 0] = @as(i8, @truncate(u4, x));
e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4));
}

View File

@ -187,7 +187,7 @@ const ScalarDouble = struct {
var s = s_;
if (endian == .Big) {
for (s_) |x, i| s[s.len - 1 - i] = x;
for (s_, 0..) |x, i| s[s.len - 1 - i] = x;
}
var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero };
{

View File

@ -321,7 +321,7 @@ pub const P384 = struct {
fn slide(s: [48]u8) [2 * 48 + 1]i8 {
var e: [2 * 48 + 1]i8 = undefined;
for (s) |x, i| {
for (s, 0..) |x, i| {
e[i * 2 + 0] = @as(i8, @truncate(u4, x));
e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4));
}

View File

@ -175,7 +175,7 @@ const ScalarDouble = struct {
var s = s_;
if (endian == .Big) {
for (s_) |x, i| s[s.len - 1 - i] = x;
for (s_, 0..) |x, i| s[s.len - 1 - i] = x;
}
var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero };
{

View File

@ -349,7 +349,7 @@ pub const Secp256k1 = struct {
fn slide(s: [32]u8) [2 * 32 + 1]i8 {
var e: [2 * 32 + 1]i8 = undefined;
for (s) |x, i| {
for (s, 0..) |x, i| {
e[i * 2 + 0] = @as(i8, @truncate(u4, x));
e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4));
}

View File

@ -187,7 +187,7 @@ const ScalarDouble = struct {
var s = s_;
if (endian == .Big) {
for (s_) |x, i| s[s.len - 1 - i] = x;
for (s_, 0..) |x, i| s[s.len - 1 - i] = x;
}
var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero };
{

View File

@ -82,7 +82,7 @@ pub const Poly1305 = struct {
if (st.leftover > 0) {
const want = std.math.min(block_length - st.leftover, mb.len);
const mc = mb[0..want];
for (mc) |x, i| {
for (mc, 0..) |x, i| {
st.buf[st.leftover + i] = x;
}
mb = mb[want..];
@ -103,7 +103,7 @@ pub const Poly1305 = struct {
// store leftover
if (mb.len > 0) {
for (mb) |x, i| {
for (mb, 0..) |x, i| {
st.buf[st.leftover + i] = x;
}
st.leftover += mb.len;

View File

@ -157,7 +157,7 @@ fn SalsaVecImpl(comptime rounds: comptime_int) type {
fn hsalsa(input: [16]u8, key: [32]u8) [32]u8 {
var c: [4]u32 = undefined;
for (c) |_, i| {
for (c, 0..) |_, i| {
c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]);
}
const ctx = initContext(keyToWords(key), c);
@ -240,7 +240,7 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type {
}
fn hashToBytes(out: *[64]u8, x: BlockVec) void {
for (x) |w, i| {
for (x, 0..) |w, i| {
mem.writeIntLittle(u32, out[i * 4 ..][0..4], w);
}
}
@ -282,7 +282,7 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type {
fn hsalsa(input: [16]u8, key: [32]u8) [32]u8 {
var c: [4]u32 = undefined;
for (c) |_, i| {
for (c, 0..) |_, i| {
c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]);
}
const ctx = initContext(keyToWords(key), c);
@ -413,7 +413,7 @@ pub const XSalsa20Poly1305 = struct {
var computedTag: [tag_length]u8 = undefined;
mac.final(&computedTag);
var acc: u8 = 0;
for (computedTag) |_, i| {
for (computedTag, 0..) |_, i| {
acc |= computedTag[i] ^ tag[i];
}
if (acc != 0) {

View File

@ -31,7 +31,7 @@ fn blockCopy(dst: []align(16) u32, src: []align(16) const u32, n: usize) void {
}
fn blockXor(dst: []align(16) u32, src: []align(16) const u32, n: usize) void {
for (src[0 .. n * 16]) |v, i| {
for (src[0 .. n * 16], 0..) |v, i| {
dst[i] ^= v;
}
}
@ -90,7 +90,7 @@ fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16)
var x = @alignCast(16, xy[0 .. 32 * r]);
var y = @alignCast(16, xy[32 * r ..]);
for (x) |*v1, j| {
for (x, 0..) |*v1, j| {
v1.* = mem.readIntSliceLittle(u32, b[4 * j ..]);
}
@ -115,7 +115,7 @@ fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16)
blockMix(&tmp, y, x, r);
}
for (x) |v1, j| {
for (x, 0..) |v1, j| {
mem.writeIntLittle(u32, b[4 * j ..][0..4], v1);
}
}
@ -350,7 +350,7 @@ const crypt_format = struct {
fn intDecode(comptime T: type, src: *const [(@bitSizeOf(T) + 5) / 6]u8) !T {
var v: T = 0;
for (src) |x, i| {
for (src, 0..) |x, i| {
const vi = mem.indexOfScalar(u8, &map64, x) orelse return EncodingError.InvalidEncoding;
v |= @intCast(T, vi) << @intCast(math.Log2Int(T), i * 6);
}
@ -365,10 +365,10 @@ const crypt_format = struct {
}
const leftover = src[i * 4 ..];
var v: u24 = 0;
for (leftover) |_, j| {
for (leftover, 0..) |_, j| {
v |= @as(u24, try intDecode(u6, leftover[j..][0..1])) << @intCast(u5, j * 6);
}
for (dst[i * 3 ..]) |*x, j| {
for (dst[i * 3 ..], 0..) |*x, j| {
x.* = @truncate(u8, v >> @intCast(u5, j * 8));
}
}
@ -381,7 +381,7 @@ const crypt_format = struct {
}
const leftover = src[i * 3 ..];
var v: u24 = 0;
for (leftover) |x, j| {
for (leftover, 0..) |x, j| {
v |= @as(u24, x) << @intCast(u5, j * 8);
}
intEncode(dst[i * 4 ..], v);

View File

@ -105,7 +105,7 @@ pub const Sha1 = struct {
d.round(d.buf[0..]);
for (d.s) |s, j| {
for (d.s, 0..) |s, j| {
mem.writeIntBig(u32, out[4 * j ..][0..4], s);
}
}

View File

@ -175,7 +175,7 @@ fn Sha2x32(comptime params: Sha2Params32) type {
// May truncate for possible 224 output
const rr = d.s[0 .. params.digest_bits / 32];
for (rr) |s, j| {
for (rr, 0..) |s, j| {
mem.writeIntBig(u32, out[4 * j ..][0..4], s);
}
}
@ -199,7 +199,7 @@ fn Sha2x32(comptime params: Sha2Params32) type {
fn round(d: *Self, b: *const [64]u8) void {
var s: [64]u32 align(16) = undefined;
for (@ptrCast(*align(1) const [16]u32, b)) |*elem, i| {
for (@ptrCast(*align(1) const [16]u32, b), 0..) |*elem, i| {
s[i] = mem.readIntBig(u32, mem.asBytes(elem));
}
@ -665,7 +665,7 @@ fn Sha2x64(comptime params: Sha2Params64) type {
// May truncate for possible 384 output
const rr = d.s[0 .. params.digest_bits / 64];
for (rr) |s, j| {
for (rr, 0..) |s, j| {
mem.writeIntBig(u64, out[8 * j ..][0..8], s);
}
}

View File

@ -43,7 +43,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
// absorb
while (len >= rate) {
for (d.s[offset .. offset + rate]) |*r, i|
for (d.s[offset .. offset + rate], 0..) |*r, i|
r.* ^= b[ip..][i];
keccakF(1600, &d.s);
@ -54,7 +54,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
offset = 0;
}
for (d.s[offset .. offset + len]) |*r, i|
for (d.s[offset .. offset + len], 0..) |*r, i|
r.* ^= b[ip..][i];
d.offset = offset + len;
@ -126,7 +126,7 @@ fn keccakF(comptime F: usize, d: *[F / 8]u8) void {
var t = [_]u64{0} ** 1;
var c = [_]u64{0} ** 5;
for (s) |*r, i| {
for (&s, 0..) |*r, i| {
r.* = mem.readIntLittle(u64, d[8 * i ..][0..8]);
}
@ -171,7 +171,7 @@ fn keccakF(comptime F: usize, d: *[F / 8]u8) void {
s[0] ^= round;
}
for (s) |r, i| {
for (s, 0..) |r, i| {
mem.writeIntLittle(u64, d[8 * i ..][0..8], r);
}
}

View File

@ -339,7 +339,7 @@ test "siphash64-2-4 sanity" {
const siphash = SipHash64(2, 4);
var buffer: [64]u8 = undefined;
for (vectors) |vector, i| {
for (vectors, 0..) |vector, i| {
buffer[i] = @intCast(u8, i);
var out: [siphash.mac_length]u8 = undefined;
@ -419,7 +419,7 @@ test "siphash128-2-4 sanity" {
const siphash = SipHash128(2, 4);
var buffer: [64]u8 = undefined;
for (vectors) |vector, i| {
for (vectors, 0..) |vector, i| {
buffer[i] = @intCast(u8, i);
var out: [siphash.mac_length]u8 = undefined;
@ -430,7 +430,7 @@ test "siphash128-2-4 sanity" {
test "iterative non-divisible update" {
var buf: [1024]u8 = undefined;
for (buf) |*e, i| {
for (&buf, 0..) |*e, i| {
e.* = @truncate(u8, i);
}

View File

@ -13,7 +13,7 @@ pub fn assertEqualHash(comptime Hasher: anytype, comptime expected_hex: *const [
// Assert `expected` == hex(`input`) where `input` is a bytestring
pub fn assertEqual(comptime expected_hex: [:0]const u8, input: []const u8) !void {
var expected_bytes: [expected_hex.len / 2]u8 = undefined;
for (expected_bytes) |*r, i| {
for (&expected_bytes, 0..) |*r, i| {
r.* = fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable;
}

View File

@ -344,7 +344,7 @@ pub inline fn array(comptime elem_size: comptime_int, bytes: anytype) [2 + bytes
pub inline fn enum_array(comptime E: type, comptime tags: []const E) [2 + @sizeOf(E) * tags.len]u8 {
assert(@sizeOf(E) == 2);
var result: [tags.len * 2]u8 = undefined;
for (tags) |elem, i| {
for (tags, 0..) |elem, i| {
result[i * 2] = @truncate(u8, @enumToInt(elem) >> 8);
result[i * 2 + 1] = @truncate(u8, @enumToInt(elem));
}

View File

@ -18,7 +18,7 @@ pub fn timingSafeEql(comptime T: type, a: T, b: T) bool {
@compileError("Elements to be compared must be integers");
}
var acc = @as(C, 0);
for (a) |x, i| {
for (a, 0..) |x, i| {
acc |= x ^ b[i];
}
const s = @typeInfo(C).Int.bits;
@ -64,7 +64,7 @@ pub fn timingSafeCompare(comptime T: type, a: []const T, b: []const T, endian: E
eq &= @truncate(T, (@as(Cext, (x2 ^ x1)) -% 1) >> bits);
}
} else {
for (a) |x1, i| {
for (a, 0..) |x1, i| {
const x2 = b[i];
gt |= @truncate(T, (@as(Cext, x2) -% @as(Cext, x1)) >> bits) & eq;
eq &= @truncate(T, (@as(Cext, (x2 ^ x1)) -% 1) >> bits);

View File

@ -66,7 +66,7 @@ pub const State = struct {
/// XOR bytes into the beginning of the state.
pub fn addBytes(self: *State, bytes: []const u8) void {
self.endianSwap();
for (self.asBytes()[0..bytes.len]) |*byte, i| {
for (self.asBytes()[0..bytes.len], 0..) |*byte, i| {
byte.* ^= bytes[i];
}
self.endianSwap();

View File

@ -213,7 +213,7 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT
var addr_buf_stack: [32]usize = undefined;
const addr_buf = if (addr_buf_stack.len > addrs.len) addr_buf_stack[0..] else addrs;
const n = walkStackWindows(addr_buf[0..]);
const first_index = for (addr_buf[0..n]) |addr, i| {
const first_index = for (addr_buf[0..n], 0..) |addr, i| {
if (addr == first_addr) {
break i;
}
@ -224,13 +224,13 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT
const end_index = math.min(first_index + addrs.len, n);
const slice = addr_buf[first_index..end_index];
// We use a for loop here because slice and addrs may alias.
for (slice) |addr, i| {
for (slice, 0..) |addr, i| {
addrs[i] = addr;
}
stack_trace.index = slice.len;
} else {
var it = StackIterator.init(first_address, null);
for (stack_trace.instruction_addresses) |*addr, i| {
for (stack_trace.instruction_addresses, 0..) |*addr, i| {
addr.* = it.next() orelse {
stack_trace.index = i;
return;
@ -621,7 +621,7 @@ pub fn writeCurrentStackTraceWindows(
const n = walkStackWindows(addr_buf[0..]);
const addrs = addr_buf[0..n];
var start_i: usize = if (start_addr) |saddr| blk: {
for (addrs) |addr, i| {
for (addrs, 0..) |addr, i| {
if (addr == saddr) break :blk i;
}
return;
@ -2138,7 +2138,7 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
) catch return;
return;
};
for (t.addrs[0..end]) |frames_array, i| {
for (t.addrs[0..end], 0..) |frames_array, i| {
stderr.print("{s}:\n", .{t.notes[i]}) catch return;
var frames_array_mutable = frames_array;
const frames = mem.sliceTo(frames_array_mutable[0..], 0);

View File

@ -1064,7 +1064,7 @@ pub const DwarfInfo = struct {
.has_children = table_entry.has_children,
};
try result.attrs.resize(allocator, table_entry.attrs.items.len);
for (table_entry.attrs.items) |attr, i| {
for (table_entry.attrs.items, 0..) |attr, i| {
result.attrs.items[i] = Die.Attr{
.id = attr.attr_id,
.value = try parseFormValue(

View File

@ -35,7 +35,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
pub fn valuesFromFields(comptime E: type, comptime fields: []const EnumField) []const E {
comptime {
var result: [fields.len]E = undefined;
for (fields) |f, i| {
for (fields, 0..) |f, i| {
result[i] = @field(E, f.name);
}
return &result;
@ -1331,7 +1331,7 @@ pub fn EnumIndexer(comptime E: type) type {
pub const Key = E;
pub const count = fields_len;
pub fn indexOf(e: E) usize {
for (keys) |k, i| {
for (keys, 0..) |k, i| {
if (k == e) return i;
}
unreachable;

View File

@ -278,7 +278,7 @@ pub const Loop = struct {
const empty_kevs = &[0]os.Kevent{};
for (self.eventfd_resume_nodes) |*eventfd_node, i| {
for (self.eventfd_resume_nodes, 0..) |*eventfd_node, i| {
eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{
.data = ResumeNode.EventFd{
.base = ResumeNode{
@ -343,7 +343,7 @@ pub const Loop = struct {
const empty_kevs = &[0]os.Kevent{};
for (self.eventfd_resume_nodes) |*eventfd_node, i| {
for (self.eventfd_resume_nodes, 0..) |*eventfd_node, i| {
eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{
.data = ResumeNode.EventFd{
.base = ResumeNode{

View File

@ -570,7 +570,7 @@ pub fn formatType(
return writer.writeAll("{ ... }");
}
try writer.writeAll("{");
inline for (info.fields) |f, i| {
inline for (info.fields, 0..) |f, i| {
if (i == 0) {
try writer.writeAll(" ");
} else {
@ -585,7 +585,7 @@ pub fn formatType(
return writer.writeAll("{ ... }");
}
try writer.writeAll("{");
inline for (info.fields) |f, i| {
inline for (info.fields, 0..) |f, i| {
if (i == 0) {
try writer.writeAll(" .");
} else {
@ -612,7 +612,7 @@ pub fn formatType(
}
}
if (comptime std.meta.trait.isZigString(info.child)) {
for (value) |item, i| {
for (value, 0..) |item, i| {
comptime checkTextFmt(actual_fmt);
if (i != 0) try formatBuf(", ", options, writer);
try formatBuf(item, options, writer);
@ -659,7 +659,7 @@ pub fn formatType(
}
}
try writer.writeAll("{ ");
for (value) |elem, i| {
for (value, 0..) |elem, i| {
try formatType(elem, actual_fmt, options, writer, max_depth - 1);
if (i != value.len - 1) {
try writer.writeAll(", ");
@ -684,7 +684,7 @@ pub fn formatType(
}
}
try writer.writeAll("{ ");
for (value) |elem, i| {
for (value, 0..) |elem, i| {
try formatType(elem, actual_fmt, options, writer, max_depth - 1);
if (i < value.len - 1) {
try writer.writeAll(", ");

View File

@ -475,7 +475,7 @@ pub fn Decimal(comptime T: type) type {
const x = pow2_to_pow5_table[shift];
// Compare leading digits of current to check if lexicographically less than cutoff.
for (x.cutoff) |p5, i| {
for (x.cutoff, 0..) |p5, i| {
if (i >= self.num_digits) {
return x.delta - 1;
} else if (self.digits[i] == p5 - '0') { // digits are stored as integers

View File

@ -48,7 +48,7 @@ fn joinSepMaybeZ(allocator: Allocator, separator: u8, comptime sepPredicate: fn
// Find first non-empty path index.
const first_path_index = blk: {
for (paths) |path, index| {
for (paths, 0..) |path, index| {
if (path.len == 0) continue else break :blk index;
}
@ -476,7 +476,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
var drive_kind = WindowsPath.Kind.None;
var have_abs_path = false;
var first_index: usize = 0;
for (paths) |p, i| {
for (paths, 0..) |p, i| {
const parsed = windowsParsePath(p);
if (parsed.is_abs) {
have_abs_path = true;
@ -504,7 +504,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
first_index = 0;
var correct_disk_designator = false;
for (paths) |p, i| {
for (paths, 0..) |p, i| {
const parsed = windowsParsePath(p);
if (parsed.kind != WindowsPath.Kind.None) {
if (parsed.kind == drive_kind) {

View File

@ -15,7 +15,7 @@ pub const Preopens = struct {
names: []const []const u8,
pub fn find(p: Preopens, name: []const u8) ?os.fd_t {
for (p.names) |elem_name, i| {
for (p.names, 0..) |elem_name, i| {
if (mem.eql(u8, elem_name, name)) {
return @intCast(os.fd_t, i);
}

View File

@ -35,7 +35,7 @@ pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type {
@as(I, algorithm.polynomial) << (@bitSizeOf(I) - @bitSizeOf(W));
var table: [256]I = undefined;
for (table) |*e, i| {
for (&table, 0..) |*e, i| {
var crc: I = i;
if (algorithm.reflect_input) {
var j: usize = 0;
@ -124,7 +124,7 @@ pub fn Crc32WithPoly(comptime poly: Polynomial) type {
@setEvalBranchQuota(20000);
var tables: [8][256]u32 = undefined;
for (tables[0]) |*e, i| {
for (&tables[0], 0..) |*e, i| {
var crc = @intCast(u32, i);
var j: usize = 0;
while (j < 8) : (j += 1) {
@ -217,7 +217,7 @@ pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
const lookup_table = block: {
var table: [16]u32 = undefined;
for (table) |*e, i| {
for (&table, 0..) |*e, i| {
var crc = @intCast(u32, i * 16);
var j: usize = 0;
while (j < 8) : (j += 1) {

View File

@ -207,7 +207,7 @@ test "test vectors streaming" {
test "iterative non-divisible update" {
var buf: [8192]u8 = undefined;
for (buf) |*e, i| {
for (&buf, 0..) |*e, i| {
e.* = @truncate(u8, i);
}

View File

@ -2119,7 +2119,7 @@ test "std.hash_map getOrPutAdapted" {
var real_keys: [keys.len]u64 = undefined;
inline for (keys) |key_str, i| {
inline for (keys, 0..) |key_str, i| {
const result = try map.getOrPutAdapted(key_str, AdaptedContext{});
try testing.expect(!result.found_existing);
real_keys[i] = std.fmt.parseInt(u64, key_str, 10) catch unreachable;
@ -2129,7 +2129,7 @@ test "std.hash_map getOrPutAdapted" {
try testing.expectEqual(map.count(), keys.len);
inline for (keys) |key_str, i| {
inline for (keys, 0..) |key_str, i| {
const result = try map.getOrPutAdapted(key_str, AdaptedContext{});
try testing.expect(result.found_existing);
try testing.expectEqual(real_keys[i], result.key_ptr.*);

View File

@ -724,7 +724,7 @@ pub fn testAllocator(base_allocator: mem.Allocator) !void {
var slice = try allocator.alloc(*i32, 100);
try testing.expect(slice.len == 100);
for (slice) |*item, i| {
for (slice, 0..) |*item, i| {
item.* = try allocator.create(i32);
item.*.* = @intCast(i32, i);
}
@ -732,7 +732,7 @@ pub fn testAllocator(base_allocator: mem.Allocator) !void {
slice = try allocator.realloc(slice, 20000);
try testing.expect(slice.len == 20000);
for (slice[0..100]) |item, i| {
for (slice[0..100], 0..) |item, i| {
try testing.expect(item.* == @intCast(i32, i));
allocator.destroy(item);
}

View File

@ -62,7 +62,7 @@ const FreeBlock = struct {
fn useRecycled(self: FreeBlock, num_pages: usize, log2_align: u8) usize {
@setCold(true);
for (self.data) |segment, i| {
for (self.data, 0..) |segment, i| {
const spills_into_next = @bitCast(i128, segment) < 0;
const has_enough_bits = @popCount(segment) >= num_pages;

View File

@ -349,7 +349,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
/// Emits log messages for leaks and then returns whether there were any leaks.
pub fn detectLeaks(self: *Self) bool {
var leaks = false;
for (self.buckets) |optional_bucket, bucket_i| {
for (self.buckets, 0..) |optional_bucket, bucket_i| {
const first_bucket = optional_bucket orelse continue;
const size_class = @as(usize, 1) << @intCast(math.Log2Int(usize), bucket_i);
const used_bits_count = usedBitsCount(size_class);

View File

@ -1280,7 +1280,7 @@ fn parsedEqual(a: anytype, b: @TypeOf(a)) bool {
}
},
.Array => {
for (a) |e, i|
for (a, 0..) |e, i|
if (!parsedEqual(e, b[i])) return false;
return true;
},
@ -1294,7 +1294,7 @@ fn parsedEqual(a: anytype, b: @TypeOf(a)) bool {
.One => return parsedEqual(a.*, b.*),
.Slice => {
if (a.len != b.len) return false;
for (a) |e, i|
for (a, 0..) |e, i|
if (!parsedEqual(e, b[i])) return false;
return true;
},
@ -1518,7 +1518,7 @@ fn parseInternal(
var r: T = undefined;
var fields_seen = [_]bool{false} ** structInfo.fields.len;
errdefer {
inline for (structInfo.fields) |field, i| {
inline for (structInfo.fields, 0..) |field, i| {
if (fields_seen[i] and !field.is_comptime) {
parseFree(field.type, @field(r, field.name), options);
}
@ -1533,7 +1533,7 @@ fn parseInternal(
var child_options = options;
child_options.allow_trailing_data = true;
var found = false;
inline for (structInfo.fields) |field, i| {
inline for (structInfo.fields, 0..) |field, i| {
// TODO: using switches here segfault the compiler (#2727?)
if ((stringToken.escapes == .None and mem.eql(u8, field.name, key_source_slice)) or (stringToken.escapes == .Some and (field.name.len == stringToken.decodedLength() and encodesTo(field.name, key_source_slice)))) {
// if (switch (stringToken.escapes) {
@ -1584,7 +1584,7 @@ fn parseInternal(
else => return error.UnexpectedToken,
}
}
inline for (structInfo.fields) |field, i| {
inline for (structInfo.fields, 0..) |field, i| {
if (!fields_seen[i]) {
if (field.default_value) |default_ptr| {
if (!field.is_comptime) {
@ -2367,7 +2367,7 @@ pub fn stringify(
if (child_options.whitespace) |*whitespace| {
whitespace.indent_level += 1;
}
for (value) |x, i| {
for (value, 0..) |x, i| {
if (i != 0) {
try out_stream.writeByte(',');
}

View File

@ -2717,7 +2717,7 @@ test "string copy option" {
const copy_addr = &obj_copy.get("noescape").?.String[0];
var found_nocopy = false;
for (input) |_, index| {
for (input, 0..) |_, index| {
try testing.expect(copy_addr != &input[index]);
if (nocopy_addr == &input[index]) {
found_nocopy = true;

View File

@ -1478,11 +1478,11 @@ pub const Mutable = struct {
// const x_trailing = std.mem.indexOfScalar(Limb, x.limbs[0..x.len], 0).?;
// const y_trailing = std.mem.indexOfScalar(Limb, y.limbs[0..y.len], 0).?;
const x_trailing = for (x.limbs[0..x.len]) |xi, i| {
const x_trailing = for (x.limbs[0..x.len], 0..) |xi, i| {
if (xi != 0) break i;
} else unreachable;
const y_trailing = for (y.limbs[0..y.len]) |yi, i| {
const y_trailing = for (y.limbs[0..y.len], 0..) |yi, i| {
if (yi != 0) break i;
} else unreachable;
@ -2108,7 +2108,7 @@ pub const Const = struct {
if (@sizeOf(UT) <= @sizeOf(Limb)) {
r = @intCast(UT, self.limbs[0]);
} else {
for (self.limbs[0..self.limbs.len]) |_, ri| {
for (self.limbs[0..self.limbs.len], 0..) |_, ri| {
const limb = self.limbs[self.limbs.len - ri - 1];
r <<= limb_bits;
r |= limb;
@ -3594,7 +3594,7 @@ fn lldiv1(quo: []Limb, rem: *Limb, a: []const Limb, b: Limb) void {
assert(quo.len >= a.len);
rem.* = 0;
for (a) |_, ri| {
for (a, 0..) |_, ri| {
const i = a.len - ri - 1;
const pdiv = ((@as(DoubleLimb, rem.*) << limb_bits) | a[i]);
@ -3620,7 +3620,7 @@ fn lldiv0p5(quo: []Limb, rem: *Limb, a: []const Limb, b: HalfLimb) void {
assert(quo.len >= a.len);
rem.* = 0;
for (a) |_, ri| {
for (a, 0..) |_, ri| {
const i = a.len - ri - 1;
const ai_high = a[i] >> half_limb_bits;
const ai_low = a[i] & ((1 << half_limb_bits) - 1);
@ -4028,7 +4028,7 @@ fn llsquareBasecase(r: []Limb, x: []const Limb) void {
// - Each mixed-product term appears twice for each column,
// - Squares are always in the 2k (0 <= k < N) column
for (x_norm) |v, i| {
for (x_norm, 0..) |v, i| {
// Accumulate all the x[i]*x[j] (with x!=j) products
const overflow = llmulLimb(.add, r[2 * i + 1 ..], x_norm[i + 1 ..], v);
assert(!overflow);
@ -4037,7 +4037,7 @@ fn llsquareBasecase(r: []Limb, x: []const Limb) void {
// Each product appears twice, multiply by 2
llshl(r, r[0 .. 2 * x_norm.len], 1);
for (x_norm) |v, i| {
for (x_norm, 0..) |v, i| {
// Compute and add the squares
const overflow = llmulLimb(.add, r[2 * i ..], x[i .. i + 1], v);
assert(!overflow);

View File

@ -70,7 +70,7 @@ pub const Rational = struct {
start += 1;
}
for (str) |c, i| {
for (str, 0..) |c, i| {
switch (state) {
State.Integer => {
switch (c) {

View File

@ -169,7 +169,7 @@ test "Allocator.resize" {
var values = try testing.allocator.alloc(T, 100);
defer testing.allocator.free(values);
for (values) |*v, i| v.* = @intCast(T, i);
for (values, 0..) |*v, i| v.* = @intCast(T, i);
if (!testing.allocator.resize(values, values.len + 10)) return error.OutOfMemory;
values = values.ptr[0 .. values.len + 10];
try testing.expect(values.len == 110);
@ -185,7 +185,7 @@ test "Allocator.resize" {
var values = try testing.allocator.alloc(T, 100);
defer testing.allocator.free(values);
for (values) |*v, i| v.* = @intToFloat(T, i);
for (values, 0..) |*v, i| v.* = @intToFloat(T, i);
if (!testing.allocator.resize(values, values.len + 10)) return error.OutOfMemory;
values = values.ptr[0 .. values.len + 10];
try testing.expect(values.len == 110);
@ -201,7 +201,7 @@ pub fn copy(comptime T: type, dest: []T, source: []const T) void {
// this and automatically omit safety checks for loops
@setRuntimeSafety(false);
assert(dest.len >= source.len);
for (source) |s, i|
for (source, 0..) |s, i|
dest[i] = s;
}
@ -445,7 +445,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
var value: T = undefined;
inline for (struct_info.fields) |field, i| {
inline for (struct_info.fields, 0..) |field, i| {
if (field.is_comptime) {
continue;
}
@ -611,7 +611,7 @@ test "lessThan" {
pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
if (a.len != b.len) return false;
if (a.ptr == b.ptr) return true;
for (a) |item, index| {
for (a, 0..) |item, index| {
if (b[index] != item) return false;
}
return true;
@ -1261,7 +1261,7 @@ pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: Endian)
},
.Little => {
const ShiftType = math.Log2Int(ReturnType);
for (bytes) |b, index| {
for (bytes, 0..) |b, index| {
result = result | (@as(ReturnType, b) << @intCast(ShiftType, index * 8));
}
},
@ -1328,7 +1328,7 @@ pub fn readVarPackedInt(
},
.Little => {
int = read_bytes[0] >> bit_shift;
for (read_bytes[1..]) |elem, i| {
for (read_bytes[1..], 0..) |elem, i| {
int |= (@as(uN, elem) << @intCast(Log2N, (8 * (i + 1) - bit_shift)));
}
},
@ -2907,7 +2907,7 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {
assert(slice.len > 0);
var best = slice[0];
var index: usize = 0;
for (slice[1..]) |item, i| {
for (slice[1..], 0..) |item, i| {
if (item < best) {
best = item;
index = i + 1;
@ -2928,7 +2928,7 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {
assert(slice.len > 0);
var best = slice[0];
var index: usize = 0;
for (slice[1..]) |item, i| {
for (slice[1..], 0..) |item, i| {
if (item > best) {
best = item;
index = i + 1;
@ -2952,7 +2952,7 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { index_min: usi
var maxVal = slice[0];
var minIdx: usize = 0;
var maxIdx: usize = 0;
for (slice[1..]) |item, i| {
for (slice[1..], 0..) |item, i| {
if (item < minVal) {
minVal = item;
minIdx = i + 1;
@ -3117,7 +3117,7 @@ test "replace" {
/// Replace all occurences of `needle` with `replacement`.
pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) void {
for (slice) |e, i| {
for (slice, 0..) |e, i| {
if (e == needle) {
slice[i] = replacement;
}
@ -3372,7 +3372,7 @@ test "asBytes" {
try testing.expect(eql(u8, asBytes(&deadbeef), deadbeef_bytes));
var codeface = @as(u32, 0xC0DEFACE);
for (asBytes(&codeface).*) |*b|
for (asBytes(&codeface)) |*b|
b.* = 0;
try testing.expect(codeface == 0);

Some files were not shown because too many files have changed in this diff Show More