mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
e3424332d3
* `doc/langref` formatting * upgrade `.{ .path = "..." }` to `b.path("...")` * avoid using arguments named `self` * make `Build.Step.Id` usage more consistent * add `Build.pathResolve` * use `pathJoin` and `pathResolve` everywhere * make sure `Build.LazyPath.getPath2` returns an absolute path
42 lines
851 B
Zig
42 lines
851 B
Zig
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) {
|
|
'0'...'9' => c - '0',
|
|
'A'...'Z' => c - 'A' + 10,
|
|
'a'...'z' => c - 'a' + 10,
|
|
else => maxInt(u8),
|
|
};
|
|
}
|
|
|
|
test "parse u64" {
|
|
const result = try parseU64("1234", 10);
|
|
try std.testing.expect(result == 1234);
|
|
}
|
|
|
|
// test
|