authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-26 13:02:20 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-26 13:02:20 -08:00
log1da74fba8c47fde0af1459afa152742821590100
tree0264186b5c41e5420083b6fa13c3212a60c8d9d8
parent6ebab3dab47268e28599ac396e785f8a76acc7b6

update to zig master 0.11.0-dev.1681+0bb178bbb


2 files changed, 7 insertions(+), 19 deletions(-)

src/lib.zig+6-18
...@@ -2,18 +2,6 @@ const std = @import("std");...@@ -2,18 +2,6 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const assert = std.debug.assert;3const assert = std.debug.assert;
44
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/// ```
13pub fn range(len: usize) []const void {
14 return @as([*]void, undefined)[0..len];
15}
16
17pub fn fmtByteCountIEC(alloc: std.mem.Allocator, b: u64) !string {5pub 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 {
7361
74pub fn asciiUpper(alloc: std.mem.Allocator, input: string) ![]u8 {62pub 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;
11098
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 {
236pub fn FieldsTuple(comptime T: type) type {224pub 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 {
244232
245pub fn positionalInit(comptime T: type, args: FieldsTuple(T)) T {233pub 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}
289277
290pub fn readExpected(reader: anytype, expected: []const u8) !bool {278pub 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;
zig.mod+1-1
...@@ -3,4 +3,4 @@ name: extras...@@ -3,4 +3,4 @@ name: extras
3main: src/lib.zig3main: src/lib.zig
4license: MIT4license: MIT
5description: An assortment of random utility functions that aren't in std and don't deserve their own pacakge.5description: An assortment of random utility functions that aren't in std and don't deserve their own pacakge.
6min_zig_version: 0.10.0-dev.3017+da94227f76min_zig_version: 0.11.0-dev.1681+0bb178bbb