-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase32.zig
36 lines (31 loc) · 851 Bytes
/
base32.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! https://www.crockford.com/base32.html
const std = @import("std");
const string = []const u8;
const alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
pub fn decode(alloc: std.mem.Allocator, input: string) ![]const u5 {
var list = std.ArrayList(u5).init(alloc);
errdefer list.deinit();
for (input) |c| {
for (alphabet, 0..) |d, i| {
if (c == d) {
try list.append(@intCast(i));
}
}
}
return list.toOwnedSlice();
}
pub fn formatInt(comptime T: type, n: T, buf: []u8) void {
const l: T = @intCast(alphabet.len);
var x = n;
var i = buf.len;
for (0..i) |j| {
buf[j] = alphabet[0];
}
while (true) {
const a: usize = @intCast(x % l);
x = x / l;
buf[i - 1] = alphabet[a];
i -= 1;
if (x == 0) break;
}
}