]>
git.proxmox.com Git - rustc.git/blob - library/core/src/str/error.rs
1 //! Defines utf8 error type.
5 /// Errors which can occur when attempting to interpret a sequence of [`u8`]
8 /// As such, the `from_utf8` family of functions and methods for both [`String`]s
9 /// and [`&str`]s make use of this error, for example.
11 /// [`String`]: ../../std/string/struct.String.html#method.from_utf8
12 /// [`&str`]: super::from_utf8
16 /// This error type’s methods can be used to create functionality
17 /// similar to `String::from_utf8_lossy` without allocating heap memory:
20 /// fn from_utf8_lossy<F>(mut input: &[u8], mut push: F) where F: FnMut(&str) {
22 /// match std::str::from_utf8(input) {
28 /// let (valid, after_valid) = input.split_at(error.valid_up_to());
30 /// push(std::str::from_utf8_unchecked(valid))
34 /// if let Some(invalid_sequence_length) = error.error_len() {
35 /// input = &after_valid[invalid_sequence_length..]
44 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub struct Utf8Error
{
47 pub(super) valid_up_to
: usize,
48 pub(super) error_len
: Option
<u8>,
52 /// Returns the index in the given string up to which valid UTF-8 was
55 /// It is the maximum index such that `from_utf8(&input[..index])`
56 /// would return `Ok(_)`.
65 /// // some invalid bytes, in a vector
66 /// let sparkle_heart = vec![0, 159, 146, 150];
68 /// // std::str::from_utf8 returns a Utf8Error
69 /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
71 /// // the second byte is invalid here
72 /// assert_eq!(1, error.valid_up_to());
74 #[stable(feature = "utf8_error", since = "1.5.0")]
76 pub fn valid_up_to(&self) -> usize {
80 /// Provides more information about the failure:
82 /// * `None`: the end of the input was reached unexpectedly.
83 /// `self.valid_up_to()` is 1 to 3 bytes from the end of the input.
84 /// If a byte stream (such as a file or a network socket) is being decoded incrementally,
85 /// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
87 /// * `Some(len)`: an unexpected byte was encountered.
88 /// The length provided is that of the invalid byte sequence
89 /// that starts at the index given by `valid_up_to()`.
90 /// Decoding should resume after that sequence
91 /// (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of
94 /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
95 #[stable(feature = "utf8_error_error_len", since = "1.20.0")]
97 pub fn error_len(&self) -> Option
<usize> {
98 self.error_len
.map(|len
| len
as usize)
102 #[stable(feature = "rust1", since = "1.0.0")]
103 impl fmt
::Display
for Utf8Error
{
104 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
105 if let Some(error_len
) = self.error_len
{
108 "invalid utf-8 sequence of {} bytes from index {}",
109 error_len
, self.valid_up_to
112 write
!(f
, "incomplete utf-8 byte sequence from index {}", self.valid_up_to
)
117 /// An error returned when parsing a `bool` using [`from_str`] fails
119 /// [`from_str`]: super::FromStr::from_str
120 #[derive(Debug, Clone, PartialEq, Eq)]
122 #[stable(feature = "rust1", since = "1.0.0")]
123 pub struct ParseBoolError
;
125 #[stable(feature = "rust1", since = "1.0.0")]
126 impl fmt
::Display
for ParseBoolError
{
127 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
128 "provided string was not `true` or `false`".fmt(f
)