Nicer code for the error code path

This commit is contained in:
LemonBoy
2020-11-19 18:16:23 +01:00
parent 53c1624074
commit 60638f0c82

View File

@@ -654,17 +654,14 @@ pub fn formatUnicodeCodepoint(
options: FormatOptions,
writer: anytype,
) !void {
if (unicode.utf8ValidCodepoint(c)) {
var buf: [4]u8 = undefined;
// The codepoint is surely valid, hence the use of unreachable
const len = std.unicode.utf8Encode(c, &buf) catch |err| switch (err) {
error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => unreachable,
};
return formatBuf(buf[0..len], options, writer);
}
// In case of error output the replacement char U+FFFD
return formatBuf(&[_]u8{ 0xef, 0xbf, 0xbd }, options, writer);
var buf: [4]u8 = undefined;
const len = std.unicode.utf8Encode(c, &buf) catch |err| switch (err) {
error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => {
// In case of error output the replacement char U+FFFD
return formatBuf(&[_]u8{ 0xef, 0xbf, 0xbd }, options, writer);
},
};
return formatBuf(buf[0..len], options, writer);
}
pub fn formatBuf(