| ... | ... | @@ -3,6 +3,11 @@ const assert = std.debug.assert; |
| 3 | 3 | const testing = std.testing; |
| 4 | 4 | const mem = std.mem; |
| 5 | 5 | |
| 6 | /// Use this to replace an unknown, unrecognized, or unrepresentable character. |
| 7 | /// |
| 8 | /// See also: https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character |
| 9 | pub const replacement_character: u21 = 0xFFFD; |
| 10 | |
| 6 | 11 | /// Returns how many bytes the UTF-8 representation would require |
| 7 | 12 | /// for the given codepoint. |
| 8 | 13 | pub fn utf8CodepointSequenceLength(c: u21) !u3 { |
| ... | ... | @@ -777,15 +782,14 @@ fn formatUtf16le( |
| 777 | 782 | options: std.fmt.FormatOptions, |
| 778 | 783 | writer: anytype, |
| 779 | 784 | ) !void { |
| 780 | | const unknown_codepoint = 0xfffd; |
| 781 | 785 | _ = fmt; |
| 782 | 786 | _ = options; |
| 783 | 787 | var buf: [300]u8 = undefined; // just a random size I chose |
| 784 | 788 | var it = Utf16LeIterator.init(utf16le); |
| 785 | 789 | var u8len: usize = 0; |
| 786 | | while (it.nextCodepoint() catch unknown_codepoint) |codepoint| { |
| 790 | while (it.nextCodepoint() catch replacement_character) |codepoint| { |
| 787 | 791 | u8len += utf8Encode(codepoint, buf[u8len..]) catch |
| 788 | | utf8Encode(unknown_codepoint, buf[u8len..]) catch unreachable; |
| 792 | utf8Encode(replacement_character, buf[u8len..]) catch unreachable; |
| 789 | 793 | if (u8len + 3 >= buf.len) { |
| 790 | 794 | try writer.writeAll(buf[0..u8len]); |
| 791 | 795 | u8len = 0; |