| ... | @@ -3,6 +3,11 @@ const assert = std.debug.assert; | ... | @@ -3,6 +3,11 @@ const assert = std.debug.assert; |
| 3 | const testing = std.testing; | 3 | const testing = std.testing; |
| 4 | const mem = std.mem; | 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 | /// Returns how many bytes the UTF-8 representation would require | 11 | /// Returns how many bytes the UTF-8 representation would require |
| 7 | /// for the given codepoint. | 12 | /// for the given codepoint. |
| 8 | pub fn utf8CodepointSequenceLength(c: u21) !u3 { | 13 | pub fn utf8CodepointSequenceLength(c: u21) !u3 { |
| ... | @@ -777,15 +782,14 @@ fn formatUtf16le( | ... | @@ -777,15 +782,14 @@ fn formatUtf16le( |
| 777 | options: std.fmt.FormatOptions, | 782 | options: std.fmt.FormatOptions, |
| 778 | writer: anytype, | 783 | writer: anytype, |
| 779 | ) !void { | 784 | ) !void { |
| 780 | const unknown_codepoint = 0xfffd; | | |
| 781 | _ = fmt; | 785 | _ = fmt; |
| 782 | _ = options; | 786 | _ = options; |
| 783 | var buf: [300]u8 = undefined; // just a random size I chose | 787 | var buf: [300]u8 = undefined; // just a random size I chose |
| 784 | var it = Utf16LeIterator.init(utf16le); | 788 | var it = Utf16LeIterator.init(utf16le); |
| 785 | var u8len: usize = 0; | 789 | var u8len: usize = 0; |
| 786 | while (it.nextCodepoint() catch unknown_codepoint) |codepoint| { | 790 | while (it.nextCodepoint() catch replacement_character) |codepoint| { |
| 787 | u8len += utf8Encode(codepoint, buf[u8len..]) catch | 791 | u8len += utf8Encode(codepoint, buf[u8len..]) catch |
| 788 | utf8Encode(unknown_codepoint, buf[u8len..]) catch unreachable; | 792 | utf8Encode(replacement_character, buf[u8len..]) catch unreachable; |
| 789 | if (u8len + 3 >= buf.len) { | 793 | if (u8len + 3 >= buf.len) { |
| 790 | try writer.writeAll(buf[0..u8len]); | 794 | try writer.writeAll(buf[0..u8len]); |
| 791 | u8len = 0; | 795 | u8len = 0; |