| ... | @@ -7,6 +7,7 @@ const std = @import("std.zig"); | ... | @@ -7,6 +7,7 @@ const std = @import("std.zig"); |
| 7 | const math = std.math; | 7 | const math = std.math; |
| 8 | const assert = std.debug.assert; | 8 | const assert = std.debug.assert; |
| 9 | const mem = std.mem; | 9 | const mem = std.mem; |
| | 10 | const unicode = std.unicode; |
| 10 | const builtin = @import("builtin"); | 11 | const builtin = @import("builtin"); |
| 11 | const errol = @import("fmt/errol.zig"); | 12 | const errol = @import("fmt/errol.zig"); |
| 12 | const lossyCast = std.math.lossyCast; | 13 | const lossyCast = std.math.lossyCast; |
| ... | @@ -653,17 +654,17 @@ pub fn formatUnicodeCodepoint( | ... | @@ -653,17 +654,17 @@ pub fn formatUnicodeCodepoint( |
| 653 | options: FormatOptions, | 654 | options: FormatOptions, |
| 654 | writer: anytype, | 655 | writer: anytype, |
| 655 | ) !void { | 656 | ) !void { |
| 656 | var buf: [4]u8 = undefined; | 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(@truncate(u21, c), &buf) catch |err| switch (err) { |
| | 661 | error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => unreachable, |
| | 662 | }; |
| | 663 | return formatBuf(buf[0..len], options, writer); |
| | 664 | } |
| | 665 | |
| 657 | // In case of error output the replacement char U+FFFD | 666 | // In case of error output the replacement char U+FFFD |
| 658 | const len = std.unicode.utf8Encode(@truncate(u21, c), &buf) catch |err| switch (err) { | 667 | return formatBuf(&[_]u8{ 0xef, 0xbf, 0xbd }, options, writer); |
| 659 | error.Utf8CannotEncodeSurrogateHalf => { | | |
| 660 | return writer.writeAll(&[_]u8{ 0xef, 0xbf, 0xbd }); | | |
| 661 | }, | | |
| 662 | error.CodepointTooLarge => { | | |
| 663 | return writer.writeAll(&[_]u8{ 0xef, 0xbf, 0xbd }); | | |
| 664 | }, | | |
| 665 | }; | | |
| 666 | return writer.writeAll(buf[0..len]); | | |
| 667 | } | 668 | } |
| 668 | | 669 | |
| 669 | pub fn formatBuf( | 670 | pub fn formatBuf( |