This function is specific to how the Win32 RC command line interprets max string literal length percent.
- (negative) prefix is allowedpub fn parsePercent(str: []const u8) error
pub fn parsePercent(str: []const u8) error{InvalidFormat}!u32 {
var result: u32 = 0;
const radix: u8 = 10;
var buf = str;
const Prefix = enum { none, minus };
var prefix: Prefix = .none;
switch (buf[0]) {
'-' => {
prefix = .minus;
buf = buf[1..];
},
else => {},
}
for (buf, 0..) |c, i| {
const digit = switch (c) {
// On invalid digit for the radix, just stop parsing but don't fail
'0'...'9' => std.fmt.charToDigit(c, radix) catch break,
else => {
// First digit must be valid
if (i == 0) {
return error.InvalidFormat;
}
break;
},
};
if (result != 0) {
result *%= radix;
}
result +%= digit;
}
switch (prefix) {
.none => {},
.minus => result = 0 -% result,
}
return result;
}