2 use crate::fmt::{self, Write};
4 use crate::str as core_str;
6 // ignore-tidy-undocumented-unsafe
8 /// Lossy UTF-8 string.
9 #[unstable(feature = "str_internals", issue = "none")]
10 pub struct Utf8Lossy {
15 pub fn from_str(s: &str) -> &Utf8Lossy {
16 Utf8Lossy::from_bytes(s.as_bytes())
19 pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
20 unsafe { mem::transmute(bytes) }
23 pub fn chunks(&self) -> Utf8LossyChunksIter<'_> {
24 Utf8LossyChunksIter { source: &self.bytes }
28 /// Iterator over lossy UTF-8 string
29 #[unstable(feature = "str_internals", issue = "none")]
30 #[allow(missing_debug_implementations)]
31 pub struct Utf8LossyChunksIter<'a> {
35 #[unstable(feature = "str_internals", issue = "none")]
36 #[derive(PartialEq, Eq, Debug)]
37 pub struct Utf8LossyChunk<'a> {
38 /// Sequence of valid chars.
39 /// Can be empty between broken UTF-8 chars.
41 /// Single broken char, empty if none.
42 /// Empty iff iterator item is last.
46 impl<'a> Iterator for Utf8LossyChunksIter<'a> {
47 type Item = Utf8LossyChunk<'a>;
49 fn next(&mut self) -> Option<Utf8LossyChunk<'a>> {
50 if self.source.is_empty() {
54 const TAG_CONT_U8: u8 = 128;
55 fn safe_get(xs: &[u8], i: usize) -> u8 {
56 *xs.get(i).unwrap_or(&0)
60 while i < self.source.len() {
63 let byte = unsafe { *self.source.get_unchecked(i) };
68 let w = core_str::utf8_char_width(byte);
73 let r = Utf8LossyChunk {
74 valid: core_str::from_utf8_unchecked(&self.source[0..i_]),
75 broken: &self.source[i_..i],
77 self.source = &self.source[i..];
85 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
91 match (byte, safe_get(self.source, i)) {
92 (0xE0, 0xA0..=0xBF) => (),
93 (0xE1..=0xEC, 0x80..=0xBF) => (),
94 (0xED, 0x80..=0x9F) => (),
95 (0xEE..=0xEF, 0x80..=0xBF) => (),
101 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
107 match (byte, safe_get(self.source, i)) {
108 (0xF0, 0x90..=0xBF) => (),
109 (0xF1..=0xF3, 0x80..=0xBF) => (),
110 (0xF4, 0x80..=0x8F) => (),
116 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
120 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
132 let r = Utf8LossyChunk {
133 valid: unsafe { core_str::from_utf8_unchecked(self.source) },
141 impl fmt::Display for Utf8Lossy {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 // If we're the empty string then our iterator won't actually yield
144 // anything, so perform the formatting manually
145 if self.bytes.is_empty() {
149 for Utf8LossyChunk { valid, broken } in self.chunks() {
150 // If we successfully decoded the whole chunk as a valid string then
151 // we can return a direct formatting of the string which will also
152 // respect various formatting flags if possible.
153 if valid.len() == self.bytes.len() {
154 assert!(broken.is_empty());
159 if !broken.is_empty() {
160 f.write_char(char::REPLACEMENT_CHARACTER)?;
167 impl fmt::Debug for Utf8Lossy {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171 for Utf8LossyChunk { valid, broken } in self.chunks() {
173 // Here we partially parse UTF-8 again which is suboptimal.
176 for (i, c) in valid.char_indices() {
177 let esc = c.escape_debug();
178 // If char needs escaping, flush backlog so far and write, else skip
180 f.write_str(&valid[from..i])?;
184 from = i + c.len_utf8();
187 f.write_str(&valid[from..])?;
190 // Broken parts of string as hex escape.
192 write!(f, "\\x{:02x}", b)?;