From 60638f0c82374714b55de094a5f3cec4d05e9e9b Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 19 Nov 2020 18:16:23 +0100 Subject: [PATCH] Nicer code for the error code path --- lib/std/fmt.zig | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index b98a9c4840..8a6e272b6b 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -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(