Skip to main content

core/str/
lossy.rs

1use super::char::EscapeDebugExtArgs;
2use super::from_utf8_unchecked;
3use super::validations::utf8_char_width;
4use crate::fmt;
5use crate::fmt::{Formatter, Write};
6use crate::iter::FusedIterator;
7
8impl [u8] {
9    /// Creates an iterator over the contiguous valid UTF-8 ranges of this
10    /// slice, and the non-UTF-8 fragments in between.
11    ///
12    /// See the [`Utf8Chunk`] type for documentation of the items yielded by this iterator.
13    ///
14    /// # Examples
15    ///
16    /// This function formats arbitrary but mostly-UTF-8 bytes into Rust source
17    /// code in the form of a C-string literal (`c"..."`).
18    ///
19    /// ```
20    /// use std::fmt::Write as _;
21    ///
22    /// pub fn cstr_literal(bytes: &[u8]) -> String {
23    ///     let mut repr = String::new();
24    ///     repr.push_str("c\"");
25    ///     for chunk in bytes.utf8_chunks() {
26    ///         for ch in chunk.valid().chars() {
27    ///             // Escapes \0, \t, \r, \n, \\, \', \", and uses \u{...} for non-printable characters.
28    ///             write!(repr, "{}", ch.escape_debug()).unwrap();
29    ///         }
30    ///         for byte in chunk.invalid() {
31    ///             write!(repr, "\\x{:02X}", byte).unwrap();
32    ///         }
33    ///     }
34    ///     repr.push('"');
35    ///     repr
36    /// }
37    ///
38    /// fn main() {
39    ///     let lit = cstr_literal(b"\xferris the \xf0\x9f\xa6\x80\x07");
40    ///     let expected = stringify!(c"\xFErris the 🦀\u{7}");
41    ///     assert_eq!(lit, expected);
42    /// }
43    /// ```
44    #[stable(feature = "utf8_chunks", since = "1.79.0")]
45    #[ferrocene::prevalidated]
46    pub fn utf8_chunks(&self) -> Utf8Chunks<'_> {
47        Utf8Chunks { source: self }
48    }
49}
50
51/// An item returned by the [`Utf8Chunks`] iterator.
52///
53/// A `Utf8Chunk` stores a sequence of [`u8`] up to the first broken character
54/// when decoding a UTF-8 string.
55///
56/// # Examples
57///
58/// ```
59/// // An invalid UTF-8 string
60/// let bytes = b"foo\xF1\x80bar";
61///
62/// // Decode the first `Utf8Chunk`
63/// let chunk = bytes.utf8_chunks().next().unwrap();
64///
65/// // The first three characters are valid UTF-8
66/// assert_eq!("foo", chunk.valid());
67///
68/// // The fourth character is broken
69/// assert_eq!(b"\xF1\x80", chunk.invalid());
70/// ```
71#[stable(feature = "utf8_chunks", since = "1.79.0")]
72#[derive(Clone, Debug, PartialEq, Eq)]
73#[ferrocene::prevalidated]
74pub struct Utf8Chunk<'a> {
75    valid: &'a str,
76    invalid: &'a [u8],
77}
78
79impl<'a> Utf8Chunk<'a> {
80    /// Returns the next validated UTF-8 substring.
81    ///
82    /// This substring can be empty at the start of the string or between
83    /// broken UTF-8 characters.
84    #[must_use]
85    #[stable(feature = "utf8_chunks", since = "1.79.0")]
86    #[ferrocene::prevalidated]
87    pub fn valid(&self) -> &'a str {
88        self.valid
89    }
90
91    /// Returns the invalid sequence that caused a failure.
92    ///
93    /// The returned slice will have a maximum length of 3 and starts after the
94    /// substring given by [`valid`]. Decoding will resume after this sequence.
95    ///
96    /// If empty, this is the last chunk in the string. If non-empty, an
97    /// unexpected byte was encountered or the end of the input was reached
98    /// unexpectedly.
99    ///
100    /// Lossy decoding would replace this sequence with [`U+FFFD REPLACEMENT
101    /// CHARACTER`].
102    ///
103    /// [`valid`]: Self::valid
104    /// [`U+FFFD REPLACEMENT CHARACTER`]: crate::char::REPLACEMENT_CHARACTER
105    #[must_use]
106    #[stable(feature = "utf8_chunks", since = "1.79.0")]
107    #[ferrocene::prevalidated]
108    pub fn invalid(&self) -> &'a [u8] {
109        self.invalid
110    }
111}
112
113#[must_use]
114#[unstable(feature = "str_internals", issue = "none")]
115#[ferrocene::prevalidated]
116pub struct Debug<'a>(&'a [u8]);
117
118#[unstable(feature = "str_internals", issue = "none")]
119impl fmt::Debug for Debug<'_> {
120    #[ferrocene::prevalidated]
121    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
122        f.write_char('"')?;
123
124        for chunk in self.0.utf8_chunks() {
125            // Valid part.
126            // Here we partially parse UTF-8 again which is suboptimal.
127            {
128                let valid = chunk.valid();
129                let mut from = 0;
130                for (i, c) in valid.char_indices() {
131                    let esc = c.escape_debug_ext(EscapeDebugExtArgs {
132                        escape_grapheme_extended: true,
133                        escape_single_quote: false,
134                        escape_double_quote: true,
135                    });
136                    // If char needs escaping, flush backlog so far and write, else skip
137                    if esc.len() != 1 {
138                        f.write_str(&valid[from..i])?;
139                        for c in esc {
140                            f.write_char(c)?;
141                        }
142                        from = i + c.len_utf8();
143                    }
144                }
145                f.write_str(&valid[from..])?;
146            }
147
148            // Broken parts of string as hex escape.
149            for &b in chunk.invalid() {
150                write!(f, "\\x{:02X}", b)?;
151            }
152        }
153
154        f.write_char('"')
155    }
156}
157
158/// An iterator used to decode a slice of mostly UTF-8 bytes to string slices
159/// ([`&str`]) and byte slices ([`&[u8]`][byteslice]).
160///
161/// This struct is created by the [`utf8_chunks`] method on bytes slices.
162/// If you want a simple conversion from UTF-8 byte slices to string slices,
163/// [`from_utf8`] is easier to use.
164///
165/// See the [`Utf8Chunk`] type for documentation of the items yielded by this iterator.
166///
167/// [byteslice]: slice
168/// [`utf8_chunks`]: slice::utf8_chunks
169/// [`from_utf8`]: super::from_utf8
170///
171/// # Examples
172///
173/// This can be used to create functionality similar to
174/// [`String::from_utf8_lossy`] without allocating heap memory:
175///
176/// ```
177/// fn from_utf8_lossy<F>(input: &[u8], mut push: F) where F: FnMut(&str) {
178///     for chunk in input.utf8_chunks() {
179///         push(chunk.valid());
180///
181///         if !chunk.invalid().is_empty() {
182///             push("\u{FFFD}");
183///         }
184///     }
185/// }
186/// ```
187///
188/// [`String::from_utf8_lossy`]: ../../std/string/struct.String.html#method.from_utf8_lossy
189#[must_use = "iterators are lazy and do nothing unless consumed"]
190#[stable(feature = "utf8_chunks", since = "1.79.0")]
191#[derive(Clone)]
192#[ferrocene::prevalidated]
193pub struct Utf8Chunks<'a> {
194    source: &'a [u8],
195}
196
197impl<'a> Utf8Chunks<'a> {
198    #[doc(hidden)]
199    #[unstable(feature = "str_internals", issue = "none")]
200    #[ferrocene::prevalidated]
201    pub fn debug(&self) -> Debug<'_> {
202        Debug(self.source)
203    }
204}
205
206#[stable(feature = "utf8_chunks", since = "1.79.0")]
207impl<'a> Iterator for Utf8Chunks<'a> {
208    type Item = Utf8Chunk<'a>;
209
210    #[ferrocene::prevalidated]
211    fn next(&mut self) -> Option<Utf8Chunk<'a>> {
212        if self.source.is_empty() {
213            return None;
214        }
215
216        const TAG_CONT_U8: u8 = 128;
217        #[ferrocene::prevalidated]
218        fn safe_get(xs: &[u8], i: usize) -> u8 {
219            *xs.get(i).unwrap_or(&0)
220        }
221
222        let mut i = 0;
223        let mut valid_up_to = 0;
224        while i < self.source.len() {
225            // SAFETY: `i < self.source.len()` per previous line.
226            // For some reason the following are both significantly slower:
227            // while let Some(&byte) = self.source.get(i) {
228            // while let Some(byte) = self.source.get(i).copied() {
229            let byte = unsafe { *self.source.get_unchecked(i) };
230            i += 1;
231
232            if byte < 128 {
233                // This could be a `1 => ...` case in the match below, but for
234                // the common case of all-ASCII inputs, we bypass loading the
235                // sizeable UTF8_CHAR_WIDTH table into cache.
236            } else {
237                let w = utf8_char_width(byte);
238
239                match w {
240                    2 => {
241                        if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
242                            break;
243                        }
244                        i += 1;
245                    }
246                    3 => {
247                        match (byte, safe_get(self.source, i)) {
248                            (0xE0, 0xA0..=0xBF) => (),
249                            (0xE1..=0xEC, 0x80..=0xBF) => (),
250                            (0xED, 0x80..=0x9F) => (),
251                            (0xEE..=0xEF, 0x80..=0xBF) => (),
252                            _ => break,
253                        }
254                        i += 1;
255                        if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
256                            break;
257                        }
258                        i += 1;
259                    }
260                    4 => {
261                        match (byte, safe_get(self.source, i)) {
262                            (0xF0, 0x90..=0xBF) => (),
263                            (0xF1..=0xF3, 0x80..=0xBF) => (),
264                            (0xF4, 0x80..=0x8F) => (),
265                            _ => break,
266                        }
267                        i += 1;
268                        if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
269                            break;
270                        }
271                        i += 1;
272                        if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
273                            break;
274                        }
275                        i += 1;
276                    }
277                    _ => break,
278                }
279            }
280
281            valid_up_to = i;
282        }
283
284        // SAFETY: `i <= self.source.len()` because it is only ever incremented
285        // via `i += 1` and in between every single one of those increments, `i`
286        // is compared against `self.source.len()`. That happens either
287        // literally by `i < self.source.len()` in the while-loop's condition,
288        // or indirectly by `safe_get(self.source, i) & 192 != TAG_CONT_U8`. The
289        // loop is terminated as soon as the latest `i += 1` has made `i` no
290        // longer less than `self.source.len()`, which means it'll be at most
291        // equal to `self.source.len()`.
292        let (inspected, remaining) = unsafe { self.source.split_at_unchecked(i) };
293        self.source = remaining;
294
295        // SAFETY: `valid_up_to <= i` because it is only ever assigned via
296        // `valid_up_to = i` and `i` only increases.
297        let (valid, invalid) = unsafe { inspected.split_at_unchecked(valid_up_to) };
298
299        Some(Utf8Chunk {
300            // SAFETY: All bytes up to `valid_up_to` are valid UTF-8.
301            valid: unsafe { from_utf8_unchecked(valid) },
302            invalid,
303        })
304    }
305}
306
307#[stable(feature = "utf8_chunks", since = "1.79.0")]
308impl FusedIterator for Utf8Chunks<'_> {}
309
310#[stable(feature = "utf8_chunks", since = "1.79.0")]
311impl fmt::Debug for Utf8Chunks<'_> {
312    #[ferrocene::prevalidated]
313    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
314        f.debug_struct("Utf8Chunks").field("source", &self.debug()).finish()
315    }
316}