This function is specific to how the Win32 RC command line interprets language IDs specified as integers.
- (negative) prefix is allowedpub fn parseInt(str: []const u8) error
pub fn parseInt(str: []const u8) error{InvalidLanguageId}!u16 {
var result: u16 = 0;
const radix: u8 = 16;
var buf = str;
const Prefix = enum { none, minus };
var prefix: Prefix = .none;
switch (buf[0]) {
'-' => {
prefix = .minus;
buf = buf[1..];
},
else => {},
}
if (buf.len > 2 and buf[0] == '0' and buf[1] == 'x') {
buf = buf[2..];
}
for (buf, 0..) |c, i| {
const digit = switch (c) {
// On invalid digit for the radix, just stop parsing but don't fail
'a'...'f', 'A'...'F', '0'...'9' => std.fmt.charToDigit(c, radix) catch break,
else => {
// First digit must be valid
if (i == 0) {
return error.InvalidLanguageId;
}
break;
},
};
if (result != 0) {
result *%= radix;
}
result +%= digit;
}
switch (prefix) {
.none => {},
.minus => result = 0 -% result,
}
return result;
}