| ... | ... | @@ -2,18 +2,6 @@ const std = @import("std"); |
| 2 | 2 | const string = []const u8; |
| 3 | 3 | const assert = std.debug.assert; |
| 4 | 4 | |
| 5 | | /// Use this as a way to increment an index using a for loop. Works with both |
| 6 | | /// runtime and comptime integers. |
| 7 | | /// |
| 8 | | /// ```zig |
| 9 | | /// for (range(10)) |_, i| { |
| 10 | | /// // 'i' will increment from 0 -> 9 |
| 11 | | /// } |
| 12 | | /// ``` |
| 13 | | pub fn range(len: usize) []const void { |
| 14 | | return @as([*]void, undefined)[0..len]; |
| 15 | | } |
| 16 | | |
| 17 | 5 | pub fn fmtByteCountIEC(alloc: std.mem.Allocator, b: u64) !string { |
| 18 | 6 | return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZYRQ"); |
| 19 | 7 | } |
| ... | ... | @@ -73,7 +61,7 @@ pub fn base64EncodeAlloc(alloc: std.mem.Allocator, input: string) !string { |
| 73 | 61 | |
| 74 | 62 | pub fn asciiUpper(alloc: std.mem.Allocator, input: string) ![]u8 { |
| 75 | 63 | var buf = try alloc.dupe(u8, input); |
| 76 | | for (range(buf.len)) |_, i| { |
| 64 | for (0..buf.len) |i| { |
| 77 | 65 | buf[i] = std.ascii.toUpper(buf[i]); |
| 78 | 66 | } |
| 79 | 67 | return buf; |
| ... | ... | @@ -109,7 +97,7 @@ pub fn sliceToInt(comptime T: type, comptime E: type, slice: []const E) !T { |
| 109 | 97 | if (a < b * slice.len) return error.Overflow; |
| 110 | 98 | |
| 111 | 99 | var n: T = 0; |
| 112 | | for (slice) |item, i| { |
| 100 | for (slice, 0..) |item, i| { |
| 113 | 101 | const shift = @intCast(std.math.Log2Int(T), b * (slice.len - 1 - i)); |
| 114 | 102 | n = n | (@as(T, item) << shift); |
| 115 | 103 | } |
| ... | ... | @@ -236,7 +224,7 @@ pub fn containsString(haystack: []const string, needle: string) bool { |
| 236 | 224 | pub fn FieldsTuple(comptime T: type) type { |
| 237 | 225 | const fields = std.meta.fields(T); |
| 238 | 226 | var types: [fields.len]type = undefined; |
| 239 | | for (fields) |item, i| { |
| 227 | for (fields, 0..) |item, i| { |
| 240 | 228 | types[i] = item.type; |
| 241 | 229 | } |
| 242 | 230 | return std.meta.Tuple(&types); |
| ... | ... | @@ -244,7 +232,7 @@ pub fn FieldsTuple(comptime T: type) type { |
| 244 | 232 | |
| 245 | 233 | pub fn positionalInit(comptime T: type, args: FieldsTuple(T)) T { |
| 246 | 234 | var t: T = undefined; |
| 247 | | inline for (std.meta.fields(T)) |field, i| { |
| 235 | inline for (std.meta.fields(T), 0..) |field, i| { |
| 248 | 236 | @field(t, field.name) = args[i]; |
| 249 | 237 | } |
| 250 | 238 | return t; |
| ... | ... | @@ -288,7 +276,7 @@ pub fn readEnumBig(reader: anytype, comptime E: type) !E { |
| 288 | 276 | } |
| 289 | 277 | |
| 290 | 278 | pub fn readExpected(reader: anytype, expected: []const u8) !bool { |
| 291 | | for (expected) |item, i| { |
| 279 | for (expected, 0..) |item, i| { |
| 292 | 280 | const actual = try reader.readByte(); |
| 293 | 281 | if (actual != item) { |
| 294 | 282 | std.log.err("expected '{d}' at index {d}, found: '{d}'", .{ item, i, actual }); |
| ... | ... | @@ -372,7 +360,7 @@ pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) ! |
| 372 | 360 | }, |
| 373 | 361 | .Array => |t| { |
| 374 | 362 | var s: T = undefined; |
| 375 | | for (range(t.len)) |_, i| { |
| 363 | for (0..t.len) |i| { |
| 376 | 364 | s[i] = try readType(reader, t.child, endian); |
| 377 | 365 | } |
| 378 | 366 | return s; |