summaryrefslogtreecommitdiffstats
path: root/library/alloc/src/string.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:03:36 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:03:36 +0000
commit17d40c6057c88f4c432b0d7bac88e1b84cb7e67f (patch)
tree3f66c4a5918660bb8a758ab6cda5ff8ee4f6cdcd /library/alloc/src/string.rs
parentAdding upstream version 1.64.0+dfsg1. (diff)
downloadrustc-17d40c6057c88f4c432b0d7bac88e1b84cb7e67f.tar.xz
rustc-17d40c6057c88f4c432b0d7bac88e1b84cb7e67f.zip
Adding upstream version 1.65.0+dfsg1.upstream/1.65.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/alloc/src/string.rs')
-rw-r--r--library/alloc/src/string.rs39
1 files changed, 30 insertions, 9 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index a5118e533..f2448396c 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -44,6 +44,8 @@
#[cfg(not(no_global_oom_handling))]
use core::char::{decode_utf16, REPLACEMENT_CHARACTER};
+#[cfg(not(bootstrap))]
+use core::error::Error;
use core::fmt;
use core::hash;
use core::iter::FusedIterator;
@@ -58,9 +60,9 @@ use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
use core::ptr;
use core::slice;
-#[cfg(not(no_global_oom_handling))]
-use core::str::lossy;
use core::str::pattern::Pattern;
+#[cfg(not(no_global_oom_handling))]
+use core::str::Utf8Chunks;
#[cfg(not(no_global_oom_handling))]
use crate::borrow::{Cow, ToOwned};
@@ -628,11 +630,11 @@ impl String {
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
- let mut iter = lossy::Utf8Lossy::from_bytes(v).chunks();
+ let mut iter = Utf8Chunks::new(v);
let first_valid = if let Some(chunk) = iter.next() {
- let lossy::Utf8LossyChunk { valid, broken } = chunk;
- if broken.is_empty() {
+ let valid = chunk.valid();
+ if chunk.invalid().is_empty() {
debug_assert_eq!(valid.len(), v.len());
return Cow::Borrowed(valid);
}
@@ -647,9 +649,9 @@ impl String {
res.push_str(first_valid);
res.push_str(REPLACEMENT);
- for lossy::Utf8LossyChunk { valid, broken } in iter {
- res.push_str(valid);
- if !broken.is_empty() {
+ for chunk in iter {
+ res.push_str(chunk.valid());
+ if !chunk.invalid().is_empty() {
res.push_str(REPLACEMENT);
}
}
@@ -1080,7 +1082,8 @@ impl String {
/// current length. The allocator may reserve more space to speculatively
/// avoid frequent allocations. After calling `try_reserve`, capacity will be
/// greater than or equal to `self.len() + additional` if it returns
- /// `Ok(())`. Does nothing if capacity is already sufficient.
+ /// `Ok(())`. Does nothing if capacity is already sufficient. This method
+ /// preserves the contents even if an error occurs.
///
/// # Errors
///
@@ -1938,6 +1941,24 @@ impl fmt::Display for FromUtf16Error {
}
}
+#[cfg(not(bootstrap))]
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for FromUtf8Error {
+ #[allow(deprecated)]
+ fn description(&self) -> &str {
+ "invalid utf-8"
+ }
+}
+
+#[cfg(not(bootstrap))]
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for FromUtf16Error {
+ #[allow(deprecated)]
+ fn description(&self) -> &str {
+ "invalid utf-16"
+ }
+}
+
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
impl Clone for String {