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