feature. See also
. The project being documented here (as the example) is the Zig library itself.
stdlib.parseDigitsWithSignGenericCharacter
fn parseDigitsWithSignGenericCharacter(
comptime T: type,
comptime Char: type,
noalias buf: [*:0]const Char,
noalias maybe_end: ?*[*:0]const Char,
base: u6,
comptime sign: enum
File
Code
fn parseDigitsWithSignGenericCharacter(
comptime T: type,
comptime Char: type,
noalias buf: [*:0]const Char,
noalias maybe_end: ?*[*:0]const Char,
base: u6,
comptime sign: enum { pos, neg },
) error{Overflow}!T {
assert(base >= 2 and base <= 36);
var current = buf;
defer if (maybe_end) |end| {
end.* = current;
};
const add = switch (sign) {
.pos => std.math.add,
.neg => std.math.sub,
};
var value: T = 0;
while (true) {
const c: u8 = std.math.cast(u8, current[0]) orelse break;
const digit: u6 = @intCast(std.fmt.charToDigit(c, base) catch break);
defer current += 1;
value = try std.math.mul(T, value, base);
value = try add(T, value, digit);
}
return value;
}