2024-04-25 01:41:47 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const maxInt = std.math.maxInt;
|
|
|
|
|
|
|
|
pub fn parseU64(buf: []const u8, radix: u8) !u64 {
|
|
|
|
var x: u64 = 0;
|
|
|
|
|
|
|
|
for (buf) |c| {
|
|
|
|
const digit = charToDigit(c);
|
|
|
|
|
|
|
|
if (digit >= radix) {
|
|
|
|
return error.InvalidChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
// x *= radix
|
|
|
|
var ov = @mulWithOverflow(x, radix);
|
|
|
|
if (ov[1] != 0) return error.OverFlow;
|
|
|
|
|
|
|
|
// x += digit
|
|
|
|
ov = @addWithOverflow(ov[0], digit);
|
|
|
|
if (ov[1] != 0) return error.OverFlow;
|
|
|
|
x = ov[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn charToDigit(c: u8) u8 {
|
|
|
|
return switch (c) {
|
2024-05-04 19:29:17 +01:00
|
|
|
'0'...'9' => c - '0',
|
|
|
|
'A'...'Z' => c - 'A' + 10,
|
|
|
|
'a'...'z' => c - 'a' + 10,
|
2024-04-25 01:41:47 +01:00
|
|
|
else => maxInt(u8),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "parse u64" {
|
|
|
|
const result = try parseU64("1234", 10);
|
|
|
|
try std.testing.expect(result == 1234);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test
|