diff options
author | Rutger Broekhoff | 2023-11-01 16:48:02 +0100 |
---|---|---|
committer | Rutger Broekhoff | 2023-11-01 16:48:02 +0100 |
commit | e33788715ce08f9410290dcea9ca0ddb26692eed (patch) | |
tree | c64bce995bd38f89d855983caf929a98c5ffe825 /src | |
parent | 04252bac0d7a9383f943337366d6a6631ce44e91 (diff) | |
download | zig-nkeys-e33788715ce08f9410290dcea9ca0ddb26692eed.tar.gz zig-nkeys-e33788715ce08f9410290dcea9ca0ddb26692eed.zip |
Update for Zig 0.11.0
Diffstat (limited to 'src')
-rw-r--r-- | src/base32.zig | 14 | ||||
-rw-r--r-- | src/crc16.zig | 4 | ||||
-rw-r--r-- | src/main.zig | 12 |
3 files changed, 15 insertions, 15 deletions
diff --git a/src/base32.zig b/src/base32.zig index 4e79a73..bce6254 100644 --- a/src/base32.zig +++ b/src/base32.zig | |||
@@ -66,8 +66,8 @@ pub const Encoder = struct { | |||
66 | fn frontBits(self: *const Self) ?u5 { | 66 | fn frontBits(self: *const Self) ?u5 { |
67 | const index = self.index orelse return null; | 67 | const index = self.index orelse return null; |
68 | const bits = self.buffer[index]; | 68 | const bits = self.buffer[index]; |
69 | if (self.bit_off >= 4) return @truncate(u5, bits << (self.bit_off - 3)); | 69 | if (self.bit_off >= 4) return @as(u5, @truncate(bits << (self.bit_off - 3))); |
70 | return @truncate(u5, bits >> (3 - self.bit_off)); | 70 | return @as(u5, @truncate(bits >> (3 - self.bit_off))); |
71 | } | 71 | } |
72 | 72 | ||
73 | /// Get the bits of `self.buffer[self.index]` with the maximum amount specified by the `bits` parameter, | 73 | /// Get the bits of `self.buffer[self.index]` with the maximum amount specified by the `bits` parameter, |
@@ -85,7 +85,7 @@ pub const Encoder = struct { | |||
85 | fn backBits(self: *const Self, bits: u3) u5 { | 85 | fn backBits(self: *const Self, bits: u3) u5 { |
86 | std.debug.assert(bits <= 5); | 86 | std.debug.assert(bits <= 5); |
87 | if (bits == 0 or self.index == null) return 0; | 87 | if (bits == 0 or self.index == null) return 0; |
88 | return @truncate(u5, self.buffer[self.index.?] >> (7 - bits + 1)); | 88 | return @as(u5, @truncate(self.buffer[self.index.?] >> (7 - bits + 1))); |
89 | } | 89 | } |
90 | 90 | ||
91 | /// Get the next 5-bit integer, read from `self.buffer`. | 91 | /// Get the next 5-bit integer, read from `self.buffer`. |
@@ -172,10 +172,10 @@ pub const Decoder = struct { | |||
172 | /// Get a character from the buffer. | 172 | /// Get a character from the buffer. |
173 | fn decodeChar(c: u8) DecodeError!u5 { | 173 | fn decodeChar(c: u8) DecodeError!u5 { |
174 | if (c >= 'A' and c <= 'Z') { | 174 | if (c >= 'A' and c <= 'Z') { |
175 | return @truncate(u5, c - @as(u8, 'A')); | 175 | return @as(u5, @truncate(c - @as(u8, 'A'))); |
176 | } else if (c >= '2' and c <= '9') { | 176 | } else if (c >= '2' and c <= '9') { |
177 | // '2' -> 26 | 177 | // '2' -> 26 |
178 | return @truncate(u5, c - @as(u8, '2') + 26); | 178 | return @as(u5, @truncate(c - @as(u8, '2') + 26)); |
179 | } else { | 179 | } else { |
180 | return error.CorruptInput; | 180 | return error.CorruptInput; |
181 | } | 181 | } |
@@ -200,7 +200,7 @@ pub const Decoder = struct { | |||
200 | // Calculate how many bits of the decoded remain when we've written part of it to the buffer. | 200 | // Calculate how many bits of the decoded remain when we've written part of it to the buffer. |
201 | const c_remaining_len = 5 - buf_write_len; | 201 | const c_remaining_len = 5 - buf_write_len; |
202 | // Write (the first) part of the decoded character to the buffer. | 202 | // Write (the first) part of the decoded character to the buffer. |
203 | self.buf |= (@as(u8, c) << 3) >> @truncate(u3, self.buf_len); | 203 | self.buf |= (@as(u8, c) << 3) >> @as(u3, @truncate(self.buf_len)); |
204 | self.buf_len += buf_write_len; | 204 | self.buf_len += buf_write_len; |
205 | if (self.buf_len == 8) { | 205 | if (self.buf_len == 8) { |
206 | // The buffer is full, we can return a byte. | 206 | // The buffer is full, we can return a byte. |
@@ -210,7 +210,7 @@ pub const Decoder = struct { | |||
210 | if (buf_write_len != 5) { | 210 | if (buf_write_len != 5) { |
211 | // We didn't write the entire decoded character to the buffer. | 211 | // We didn't write the entire decoded character to the buffer. |
212 | // Write the remaining part to the beginning of the buffer. | 212 | // Write the remaining part to the beginning of the buffer. |
213 | self.buf = @as(u8, c) << @truncate(u3, buf_write_len + 3); | 213 | self.buf = @as(u8, c) << @as(u3, @truncate(buf_write_len + 3)); |
214 | } | 214 | } |
215 | return ret; | 215 | return ret; |
216 | } | 216 | } |
diff --git a/src/crc16.zig b/src/crc16.zig index 3ea5a15..1257ea2 100644 --- a/src/crc16.zig +++ b/src/crc16.zig | |||
@@ -7,7 +7,7 @@ const crc16tab: [256]u16 = tab: { | |||
7 | const poly: u32 = 0x1021; | 7 | const poly: u32 = 0x1021; |
8 | var table: [256]u16 = undefined; | 8 | var table: [256]u16 = undefined; |
9 | 9 | ||
10 | for (table) |*crc, i| { | 10 | for (&table, 0..) |*crc, i| { |
11 | crc.* = @as(u16, i) << 8; | 11 | crc.* = @as(u16, i) << 8; |
12 | var j = 0; | 12 | var j = 0; |
13 | while (j < 8) : (j += 1) { | 13 | while (j < 8) : (j += 1) { |
@@ -25,7 +25,7 @@ const crc16tab: [256]u16 = tab: { | |||
25 | pub fn update(crc: u16, with_data: []const u8) u16 { | 25 | pub fn update(crc: u16, with_data: []const u8) u16 { |
26 | var new_crc = crc; | 26 | var new_crc = crc; |
27 | for (with_data) |b| { | 27 | for (with_data) |b| { |
28 | new_crc = (new_crc << 8) ^ crc16tab[@truncate(u8, new_crc >> 8) ^ b]; | 28 | new_crc = (new_crc << 8) ^ crc16tab[@as(u8, @truncate(new_crc >> 8)) ^ b]; |
29 | } | 29 | } |
30 | return new_crc; | 30 | return new_crc; |
31 | } | 31 | } |
diff --git a/src/main.zig b/src/main.zig index 6f68295..18c86d1 100644 --- a/src/main.zig +++ b/src/main.zig | |||
@@ -393,7 +393,7 @@ pub fn areKeySectionContentsValid(contents: []const u8) bool { | |||
393 | return true; | 393 | return true; |
394 | } | 394 | } |
395 | 395 | ||
396 | pub fn findKeySection(line_it: *std.mem.SplitIterator(u8)) ?[]const u8 { | 396 | pub fn findKeySection(line_it: *std.mem.SplitIterator(u8, .scalar)) ?[]const u8 { |
397 | while (true) { | 397 | while (true) { |
398 | const opening_line = line_it.next() orelse return null; | 398 | const opening_line = line_it.next() orelse return null; |
399 | if (!isKeySectionBarrier(opening_line, true)) continue; | 399 | if (!isKeySectionBarrier(opening_line, true)) continue; |
@@ -409,12 +409,12 @@ pub fn findKeySection(line_it: *std.mem.SplitIterator(u8)) ?[]const u8 { | |||
409 | } | 409 | } |
410 | 410 | ||
411 | pub fn parseDecoratedJwt(contents: []const u8) []const u8 { | 411 | pub fn parseDecoratedJwt(contents: []const u8) []const u8 { |
412 | var line_it = mem.split(u8, contents, "\n"); | 412 | var line_it = mem.splitScalar(u8, contents, '\n'); |
413 | return findKeySection(&line_it) orelse return contents; | 413 | return findKeySection(&line_it) orelse return contents; |
414 | } | 414 | } |
415 | 415 | ||
416 | pub fn parseDecoratedNkey(contents: []const u8) NoNkeySeedFoundError!SeedKeyPair { | 416 | pub fn parseDecoratedNkey(contents: []const u8) NoNkeySeedFoundError!SeedKeyPair { |
417 | var line_it = mem.split(u8, contents, "\n"); | 417 | var line_it = mem.splitScalar(u8, contents, '\n'); |
418 | var seed: ?[]const u8 = null; | 418 | var seed: ?[]const u8 = null; |
419 | if (findKeySection(&line_it) != null) | 419 | if (findKeySection(&line_it) != null) |
420 | seed = findKeySection(&line_it); | 420 | seed = findKeySection(&line_it); |
@@ -444,8 +444,8 @@ fn isValidCredsNkey(text: []const u8) bool { | |||
444 | fn findNkey(text: []const u8) ?[]const u8 { | 444 | fn findNkey(text: []const u8) ?[]const u8 { |
445 | var line_it = std.mem.split(u8, text, "\n"); | 445 | var line_it = std.mem.split(u8, text, "\n"); |
446 | while (line_it.next()) |line| { | 446 | while (line_it.next()) |line| { |
447 | for (line) |c, i| { | 447 | for (line, 0..) |c, i| { |
448 | if (!ascii.isSpace(c)) { | 448 | if (!ascii.isWhitespace(c)) { |
449 | if (isValidCredsNkey(line[i..])) return line[i..]; | 449 | if (isValidCredsNkey(line[i..])) return line[i..]; |
450 | break; | 450 | break; |
451 | } | 451 | } |
@@ -554,7 +554,7 @@ test "different key types" { | |||
554 | 554 | ||
555 | test "validation" { | 555 | test "validation" { |
556 | const roles = @typeInfo(Role).Enum.fields; | 556 | const roles = @typeInfo(Role).Enum.fields; |
557 | inline for (roles) |field, i| { | 557 | inline for (roles, 0..) |field, i| { |
558 | const role = @field(Role, field.name); | 558 | const role = @field(Role, field.name); |
559 | const next_role = next: { | 559 | const next_role = next: { |
560 | const next_field_i = if (i == roles.len - 1) 0 else i + 1; | 560 | const next_field_i = if (i == roles.len - 1) 0 else i + 1; |