Example
test parseYear4 {
const expectEqual = std.testing.expectEqual;
try expectEqual(@as(u16, 0), try parseYear4("0000"));
try expectEqual(@as(u16, 9999), try parseYear4("9999"));
try expectEqual(@as(u16, 1988), try parseYear4("1988"));
const expectError = std.testing.expectError;
try expectError(error.CertificateTimeInvalid, parseYear4("999b"));
try expectError(error.CertificateTimeInvalid, parseYear4("crap"));
try expectError(error.CertificateTimeInvalid, parseYear4("r:bQ"));
}
Source
pub fn parseYear4(text: *const [4]u8) !u16 {
const result = if (use_vectors) result: {
const nnnn: @Vector(4, u32) = .{ text[0], text[1], text[2], text[3] };
const zero: @Vector(4, u32) = .{ '0', '0', '0', '0' };
const mmmm: @Vector(4, u32) = .{ 1000, 100, 10, 1 };
break :result @reduce(.Add, (nnnn -% zero) *% mmmm);
} else std.fmt.parseInt(u16, text, 10) catch return error.CertificateTimeInvalid;
if (result > 9999) return error.CertificateTimeInvalid;
return @truncate(result);
}