Skip to content

Commit 9bb55dc

Browse files
committed
Auto merge of #76325 - lzutao:split-core-str, r=Amanieu
Split core/str/mod.rs to smaller files Note for reviewer: * I split to multiple commits for easier reviewing, but I could git squash them all to one if requested. * Recommend pulling this change locally and using advanced git diff viewer or this command: ```bash git show --reverse --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space master.. ``` --- I split `core/str/mod.rs` to these modules: * `converts`: Contains helper functions to convert from bytes to str. * `error`: For error structs like Utf8Error. * `iter`: For iterators of many str methods. * `traits`: For indexing operations and build in traits on str. * `validations`: For functions validating utf8 --- This name is awkward, maybe utf8.rs is better.
2 parents ef663a8 + dce7248 commit 9bb55dc

File tree

7 files changed

+2507
-2445
lines changed

7 files changed

+2507
-2445
lines changed

library/core/src/str/converts.rs

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
//! Ways to create a `str` from bytes slice.
2+
3+
use crate::mem;
4+
5+
use super::validations::run_utf8_validation;
6+
use super::Utf8Error;
7+
8+
/// Converts a slice of bytes to a string slice.
9+
///
10+
/// A string slice ([`&str`]) is made of bytes ([`u8`]), and a byte slice
11+
/// ([`&[u8]`][byteslice]) is made of bytes, so this function converts between
12+
/// the two. Not all byte slices are valid string slices, however: [`&str`] requires
13+
/// that it is valid UTF-8. `from_utf8()` checks to ensure that the bytes are valid
14+
/// UTF-8, and then does the conversion.
15+
///
16+
/// [`&str`]: str
17+
/// [byteslice]: ../../std/primitive.slice.html
18+
///
19+
/// If you are sure that the byte slice is valid UTF-8, and you don't want to
20+
/// incur the overhead of the validity check, there is an unsafe version of
21+
/// this function, [`from_utf8_unchecked`], which has the same
22+
/// behavior but skips the check.
23+
///
24+
/// If you need a `String` instead of a `&str`, consider
25+
/// [`String::from_utf8`][string].
26+
///
27+
/// [string]: ../../std/string/struct.String.html#method.from_utf8
28+
///
29+
/// Because you can stack-allocate a `[u8; N]`, and you can take a
30+
/// [`&[u8]`][byteslice] of it, this function is one way to have a
31+
/// stack-allocated string. There is an example of this in the
32+
/// examples section below.
33+
///
34+
/// [byteslice]: ../../std/primitive.slice.html
35+
///
36+
/// # Errors
37+
///
38+
/// Returns `Err` if the slice is not UTF-8 with a description as to why the
39+
/// provided slice is not UTF-8.
40+
///
41+
/// # Examples
42+
///
43+
/// Basic usage:
44+
///
45+
/// ```
46+
/// use std::str;
47+
///
48+
/// // some bytes, in a vector
49+
/// let sparkle_heart = vec![240, 159, 146, 150];
50+
///
51+
/// // We know these bytes are valid, so just use `unwrap()`.
52+
/// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
53+
///
54+
/// assert_eq!("💖", sparkle_heart);
55+
/// ```
56+
///
57+
/// Incorrect bytes:
58+
///
59+
/// ```
60+
/// use std::str;
61+
///
62+
/// // some invalid bytes, in a vector
63+
/// let sparkle_heart = vec![0, 159, 146, 150];
64+
///
65+
/// assert!(str::from_utf8(&sparkle_heart).is_err());
66+
/// ```
67+
///
68+
/// See the docs for [`Utf8Error`] for more details on the kinds of
69+
/// errors that can be returned.
70+
///
71+
/// A "stack allocated string":
72+
///
73+
/// ```
74+
/// use std::str;
75+
///
76+
/// // some bytes, in a stack-allocated array
77+
/// let sparkle_heart = [240, 159, 146, 150];
78+
///
79+
/// // We know these bytes are valid, so just use `unwrap()`.
80+
/// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
81+
///
82+
/// assert_eq!("💖", sparkle_heart);
83+
/// ```
84+
#[stable(feature = "rust1", since = "1.0.0")]
85+
pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
86+
run_utf8_validation(v)?;
87+
// SAFETY: Just ran validation.
88+
Ok(unsafe { from_utf8_unchecked(v) })
89+
}
90+
91+
/// Converts a mutable slice of bytes to a mutable string slice.
92+
///
93+
/// # Examples
94+
///
95+
/// Basic usage:
96+
///
97+
/// ```
98+
/// use std::str;
99+
///
100+
/// // "Hello, Rust!" as a mutable vector
101+
/// let mut hellorust = vec![72, 101, 108, 108, 111, 44, 32, 82, 117, 115, 116, 33];
102+
///
103+
/// // As we know these bytes are valid, we can use `unwrap()`
104+
/// let outstr = str::from_utf8_mut(&mut hellorust).unwrap();
105+
///
106+
/// assert_eq!("Hello, Rust!", outstr);
107+
/// ```
108+
///
109+
/// Incorrect bytes:
110+
///
111+
/// ```
112+
/// use std::str;
113+
///
114+
/// // Some invalid bytes in a mutable vector
115+
/// let mut invalid = vec![128, 223];
116+
///
117+
/// assert!(str::from_utf8_mut(&mut invalid).is_err());
118+
/// ```
119+
/// See the docs for [`Utf8Error`] for more details on the kinds of
120+
/// errors that can be returned.
121+
#[stable(feature = "str_mut_extras", since = "1.20.0")]
122+
pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
123+
run_utf8_validation(v)?;
124+
// SAFETY: Just ran validation.
125+
Ok(unsafe { from_utf8_unchecked_mut(v) })
126+
}
127+
128+
/// Converts a slice of bytes to a string slice without checking
129+
/// that the string contains valid UTF-8.
130+
///
131+
/// See the safe version, [`from_utf8`], for more information.
132+
///
133+
/// # Safety
134+
///
135+
/// This function is unsafe because it does not check that the bytes passed to
136+
/// it are valid UTF-8. If this constraint is violated, undefined behavior
137+
/// results, as the rest of Rust assumes that [`&str`]s are valid UTF-8.
138+
///
139+
/// [`&str`]: str
140+
///
141+
/// # Examples
142+
///
143+
/// Basic usage:
144+
///
145+
/// ```
146+
/// use std::str;
147+
///
148+
/// // some bytes, in a vector
149+
/// let sparkle_heart = vec![240, 159, 146, 150];
150+
///
151+
/// let sparkle_heart = unsafe {
152+
/// str::from_utf8_unchecked(&sparkle_heart)
153+
/// };
154+
///
155+
/// assert_eq!("💖", sparkle_heart);
156+
/// ```
157+
#[inline]
158+
#[stable(feature = "rust1", since = "1.0.0")]
159+
#[rustc_const_unstable(feature = "const_str_from_utf8_unchecked", issue = "75196")]
160+
#[allow_internal_unstable(const_fn_transmute)]
161+
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
162+
// SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8.
163+
// Also relies on `&str` and `&[u8]` having the same layout.
164+
unsafe { mem::transmute(v) }
165+
}
166+
167+
/// Converts a slice of bytes to a string slice without checking
168+
/// that the string contains valid UTF-8; mutable version.
169+
///
170+
/// See the immutable version, [`from_utf8_unchecked()`] for more information.
171+
///
172+
/// # Examples
173+
///
174+
/// Basic usage:
175+
///
176+
/// ```
177+
/// use std::str;
178+
///
179+
/// let mut heart = vec![240, 159, 146, 150];
180+
/// let heart = unsafe { str::from_utf8_unchecked_mut(&mut heart) };
181+
///
182+
/// assert_eq!("💖", heart);
183+
/// ```
184+
#[inline]
185+
#[stable(feature = "str_mut_extras", since = "1.20.0")]
186+
pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
187+
// SAFETY: the caller must guarantee that the bytes `v`
188+
// are valid UTF-8, thus the cast to `*mut str` is safe.
189+
// Also, the pointer dereference is safe because that pointer
190+
// comes from a reference which is guaranteed to be valid for writes.
191+
unsafe { &mut *(v as *mut [u8] as *mut str) }
192+
}

library/core/src/str/error.rs

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//! Defines utf8 error type.
2+
3+
use crate::fmt;
4+
5+
/// Errors which can occur when attempting to interpret a sequence of [`u8`]
6+
/// as a string.
7+
///
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.
10+
///
11+
/// [`String`]: ../../std/string/struct.String.html#method.from_utf8
12+
/// [`&str`]: super::from_utf8
13+
///
14+
/// # Examples
15+
///
16+
/// This error type’s methods can be used to create functionality
17+
/// similar to `String::from_utf8_lossy` without allocating heap memory:
18+
///
19+
/// ```
20+
/// fn from_utf8_lossy<F>(mut input: &[u8], mut push: F) where F: FnMut(&str) {
21+
/// loop {
22+
/// match std::str::from_utf8(input) {
23+
/// Ok(valid) => {
24+
/// push(valid);
25+
/// break
26+
/// }
27+
/// Err(error) => {
28+
/// let (valid, after_valid) = input.split_at(error.valid_up_to());
29+
/// unsafe {
30+
/// push(std::str::from_utf8_unchecked(valid))
31+
/// }
32+
/// push("\u{FFFD}");
33+
///
34+
/// if let Some(invalid_sequence_length) = error.error_len() {
35+
/// input = &after_valid[invalid_sequence_length..]
36+
/// } else {
37+
/// break
38+
/// }
39+
/// }
40+
/// }
41+
/// }
42+
/// }
43+
/// ```
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>,
49+
}
50+
51+
impl Utf8Error {
52+
/// Returns the index in the given string up to which valid UTF-8 was
53+
/// verified.
54+
///
55+
/// It is the maximum index such that `from_utf8(&input[..index])`
56+
/// would return `Ok(_)`.
57+
///
58+
/// # Examples
59+
///
60+
/// Basic usage:
61+
///
62+
/// ```
63+
/// use std::str;
64+
///
65+
/// // some invalid bytes, in a vector
66+
/// let sparkle_heart = vec![0, 159, 146, 150];
67+
///
68+
/// // std::str::from_utf8 returns a Utf8Error
69+
/// let error = str::from_utf8(&sparkle_heart).unwrap_err();
70+
///
71+
/// // the second byte is invalid here
72+
/// assert_eq!(1, error.valid_up_to());
73+
/// ```
74+
#[stable(feature = "utf8_error", since = "1.5.0")]
75+
pub fn valid_up_to(&self) -> usize {
76+
self.valid_up_to
77+
}
78+
79+
/// Provides more information about the failure:
80+
///
81+
/// * `None`: the end of the input was reached unexpectedly.
82+
/// `self.valid_up_to()` is 1 to 3 bytes from the end of the input.
83+
/// If a byte stream (such as a file or a network socket) is being decoded incrementally,
84+
/// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
85+
///
86+
/// * `Some(len)`: an unexpected byte was encountered.
87+
/// The length provided is that of the invalid byte sequence
88+
/// that starts at the index given by `valid_up_to()`.
89+
/// Decoding should resume after that sequence
90+
/// (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of
91+
/// lossy decoding.
92+
///
93+
/// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
94+
#[stable(feature = "utf8_error_error_len", since = "1.20.0")]
95+
pub fn error_len(&self) -> Option<usize> {
96+
self.error_len.map(|len| len as usize)
97+
}
98+
}
99+
100+
#[stable(feature = "rust1", since = "1.0.0")]
101+
impl fmt::Display for Utf8Error {
102+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103+
if let Some(error_len) = self.error_len {
104+
write!(
105+
f,
106+
"invalid utf-8 sequence of {} bytes from index {}",
107+
error_len, self.valid_up_to
108+
)
109+
} else {
110+
write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to)
111+
}
112+
}
113+
}
114+
115+
/// An error returned when parsing a `bool` using [`from_str`] fails
116+
///
117+
/// [`from_str`]: super::FromStr::from_str
118+
#[derive(Debug, Clone, PartialEq, Eq)]
119+
#[stable(feature = "rust1", since = "1.0.0")]
120+
pub struct ParseBoolError {
121+
pub(super) _priv: (),
122+
}
123+
124+
#[stable(feature = "rust1", since = "1.0.0")]
125+
impl fmt::Display for ParseBoolError {
126+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127+
"provided string was not `true` or `false`".fmt(f)
128+
}
129+
}

0 commit comments

Comments
 (0)