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(
654654 options: FormatOptions,
655655 writer: anytype,
656656) !void {
657 if (unicode.utf8ValidCodepoint(c)) {
658 var buf: [4]u8 = undefined;
659 // The codepoint is surely valid, hence the use of unreachable
660 const len = std.unicode.utf8Encode(c, &buf) catch |err| switch (err) {
661 error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => unreachable,
662 };
663 return formatBuf(buf[0..len], options, writer);
664 }
665
666 // In case of error output the replacement char U+FFFD
667 return formatBuf(&[_]u8{ 0xef, 0xbf, 0xbd }, options, writer);
657 var buf: [4]u8 = undefined;
658 const len = std.unicode.utf8Encode(c, &buf) catch |err| switch (err) {
659 error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => {
660 // In case of error output the replacement char U+FFFD
661 return formatBuf(&[_]u8{ 0xef, 0xbf, 0xbd }, options, writer);
662 },
663 };
664 return formatBuf(buf[0..len], options, writer);
668665}
669666
670667pub fn formatBuf(