]> git.lizzy.rs Git - rust.git/blob - library/core/src/str/error.rs
Rollup merge of #100885 - mzohreva:mz/sgx-export-cancel-type, r=Mark-Simulacrum
[rust.git] / library / core / src / str / error.rs
1 //! Defines utf8 error type.
2
3 #[cfg(not(bootstrap))]
4 use crate::error::Error;
5 use crate::fmt;
6
7 /// Errors which can occur when attempting to interpret a sequence of [`u8`]
8 /// as a string.
9 ///
10 /// As such, the `from_utf8` family of functions and methods for both [`String`]s
11 /// and [`&str`]s make use of this error, for example.
12 ///
13 /// [`String`]: ../../std/string/struct.String.html#method.from_utf8
14 /// [`&str`]: super::from_utf8
15 ///
16 /// # Examples
17 ///
18 /// This error type’s methods can be used to create functionality
19 /// similar to `String::from_utf8_lossy` without allocating heap memory:
20 ///
21 /// ```
22 /// fn from_utf8_lossy<F>(mut input: &[u8], mut push: F) where F: FnMut(&str) {
23 ///     loop {
24 ///         match std::str::from_utf8(input) {
25 ///             Ok(valid) => {
26 ///                 push(valid);
27 ///                 break
28 ///             }
29 ///             Err(error) => {
30 ///                 let (valid, after_valid) = input.split_at(error.valid_up_to());
31 ///                 unsafe {
32 ///                     push(std::str::from_utf8_unchecked(valid))
33 ///                 }
34 ///                 push("\u{FFFD}");
35 ///
36 ///                 if let Some(invalid_sequence_length) = error.error_len() {
37 ///                     input = &after_valid[invalid_sequence_length..]
38 ///                 } else {
39 ///                     break
40 ///                 }
41 ///             }
42 ///         }
43 ///     }
44 /// }
45 /// ```
46 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub struct Utf8Error {
49     pub(super) valid_up_to: usize,
50     pub(super) error_len: Option<u8>,
51 }
52
53 impl Utf8Error {
54     /// Returns the index in the given string up to which valid UTF-8 was
55     /// verified.
56     ///
57     /// It is the maximum index such that `from_utf8(&input[..index])`
58     /// would return `Ok(_)`.
59     ///
60     /// # Examples
61     ///
62     /// Basic usage:
63     ///
64     /// ```
65     /// use std::str;
66     ///
67     /// // some invalid bytes, in a vector
68     /// let sparkle_heart = vec![0, 159, 146, 150];
69     ///
70     /// // std::str::from_utf8 returns a Utf8Error
71     /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
72     ///
73     /// // the second byte is invalid here
74     /// assert_eq!(1, error.valid_up_to());
75     /// ```
76     #[stable(feature = "utf8_error", since = "1.5.0")]
77     #[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")]
78     #[must_use]
79     #[inline]
80     pub const fn valid_up_to(&self) -> usize {
81         self.valid_up_to
82     }
83
84     /// Provides more information about the failure:
85     ///
86     /// * `None`: the end of the input was reached unexpectedly.
87     ///   `self.valid_up_to()` is 1 to 3 bytes from the end of the input.
88     ///   If a byte stream (such as a file or a network socket) is being decoded incrementally,
89     ///   this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
90     ///
91     /// * `Some(len)`: an unexpected byte was encountered.
92     ///   The length provided is that of the invalid byte sequence
93     ///   that starts at the index given by `valid_up_to()`.
94     ///   Decoding should resume after that sequence
95     ///   (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of
96     ///   lossy decoding.
97     ///
98     /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
99     #[stable(feature = "utf8_error_error_len", since = "1.20.0")]
100     #[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")]
101     #[must_use]
102     #[inline]
103     pub const fn error_len(&self) -> Option<usize> {
104         // FIXME: This should become `map` again, once it's `const`
105         match self.error_len {
106             Some(len) => Some(len as usize),
107             None => None,
108         }
109     }
110 }
111
112 #[stable(feature = "rust1", since = "1.0.0")]
113 impl fmt::Display for Utf8Error {
114     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115         if let Some(error_len) = self.error_len {
116             write!(
117                 f,
118                 "invalid utf-8 sequence of {} bytes from index {}",
119                 error_len, self.valid_up_to
120             )
121         } else {
122             write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to)
123         }
124     }
125 }
126
127 #[cfg(not(bootstrap))]
128 #[stable(feature = "rust1", since = "1.0.0")]
129 impl Error for Utf8Error {
130     #[allow(deprecated)]
131     fn description(&self) -> &str {
132         "invalid utf-8: corrupt contents"
133     }
134 }
135
136 /// An error returned when parsing a `bool` using [`from_str`] fails
137 ///
138 /// [`from_str`]: super::FromStr::from_str
139 #[derive(Debug, Clone, PartialEq, Eq)]
140 #[non_exhaustive]
141 #[stable(feature = "rust1", since = "1.0.0")]
142 pub struct ParseBoolError;
143
144 #[stable(feature = "rust1", since = "1.0.0")]
145 impl fmt::Display for ParseBoolError {
146     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147         "provided string was not `true` or `false`".fmt(f)
148     }
149 }
150
151 #[cfg(not(bootstrap))]
152 #[stable(feature = "rust1", since = "1.0.0")]
153 impl Error for ParseBoolError {
154     #[allow(deprecated)]
155     fn description(&self) -> &str {
156         "failed to parse bool"
157     }
158 }