authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-19 18:16:23+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-19 18:16:23+01:00
log60638f0c82374714b55de094a5f3cec4d05e9e9b
tree6e82a053cc9b34ac498e8479155b42e0079ed9b4
parent53c1624074f55973b8d6c160ba0d55b2a5ca09ad

Nicer code for the error code path


1 files changed, 8 insertions(+), 11 deletions(-)

lib/std/fmt.zig+8-11
...@@ -654,17 +654,14 @@ pub fn formatUnicodeCodepoint(...@@ -654,17 +654,14 @@ pub fn formatUnicodeCodepoint(
654 options: FormatOptions,654 options: FormatOptions,
655 writer: anytype,655 writer: anytype,
656) !void {656) !void {
657 if (unicode.utf8ValidCodepoint(c)) {657 var buf: [4]u8 = undefined;
658 var buf: [4]u8 = undefined;658 const len = std.unicode.utf8Encode(c, &buf) catch |err| switch (err) {
659 // The codepoint is surely valid, hence the use of unreachable659 error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => {
660 const len = std.unicode.utf8Encode(c, &buf) catch |err| switch (err) {660 // In case of error output the replacement char U+FFFD
661 error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => unreachable,661 return formatBuf(&[_]u8{ 0xef, 0xbf, 0xbd }, options, writer);
662 };662 },
663 return formatBuf(buf[0..len], options, writer);663 };
664 }664 return formatBuf(buf[0..len], options, writer);
665
666 // In case of error output the replacement char U+FFFD
667 return formatBuf(&[_]u8{ 0xef, 0xbf, 0xbd }, options, writer);
668}665}
669666
670pub fn formatBuf(667pub fn formatBuf(