about summary refs log tree commit diff
path: root/library/alloc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-07-22 11:30:18 +0000
committerbors <bors@rust-lang.org>2023-07-22 11:30:18 +0000
commit42f5419dd29046612138413cbde4567def218341 (patch)
tree431c9244d4af6f7b6c34ba19cf98a1721492a807 /library/alloc
parentdcb810414e4143ed1bde8ac567d5c6f216d710ab (diff)
parent37cd63431c88395478292dd805b5b8ccde66f64e (diff)
downloadrust-42f5419dd29046612138413cbde4567def218341.tar.gz
rust-42f5419dd29046612138413cbde4567def218341.zip
Auto merge of #113954 - matthiaskrgr:rollup-e2r9suz, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #112490 (Remove `#[cfg(all())]` workarounds from `c_char`)
 - #113252 (Update the tracking issue for `const_cstr_from_ptr`)
 - #113442 (Allow limited access to `OsString` bytes)
 - #113876 (fix docs & example for `std::os::unix::prelude::FileExt::write_at`)
 - #113898 (Fix size_hint for EncodeUtf16)
 - #113934 (Multibyte character removal in String::pop and String::remove doctests)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/alloc')
-rw-r--r--library/alloc/src/string.rs16
-rw-r--r--library/alloc/tests/str.rs22
2 files changed, 30 insertions, 8 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index ad7b77f5497..0019f51b3d7 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -1290,11 +1290,11 @@ impl String {
     /// Basic usage:
     ///
     /// ```
-    /// let mut s = String::from("foo");
+    /// let mut s = String::from("abč");
     ///
-    /// assert_eq!(s.pop(), Some('o'));
-    /// assert_eq!(s.pop(), Some('o'));
-    /// assert_eq!(s.pop(), Some('f'));
+    /// assert_eq!(s.pop(), Some('č'));
+    /// assert_eq!(s.pop(), Some('b'));
+    /// assert_eq!(s.pop(), Some('a'));
     ///
     /// assert_eq!(s.pop(), None);
     /// ```
@@ -1324,11 +1324,11 @@ impl String {
     /// Basic usage:
     ///
     /// ```
-    /// let mut s = String::from("foo");
+    /// let mut s = String::from("abç");
     ///
-    /// assert_eq!(s.remove(0), 'f');
-    /// assert_eq!(s.remove(1), 'o');
-    /// assert_eq!(s.remove(0), 'o');
+    /// assert_eq!(s.remove(0), 'a');
+    /// assert_eq!(s.remove(1), 'ç');
+    /// assert_eq!(s.remove(0), 'b');
     /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs
index 82c1a9f9ad7..8a4b4ac4e8d 100644
--- a/library/alloc/tests/str.rs
+++ b/library/alloc/tests/str.rs
@@ -1739,6 +1739,28 @@ fn test_utf16_code_units() {
 }
 
 #[test]
+fn test_utf16_size_hint() {
+    assert_eq!("".encode_utf16().size_hint(), (0, Some(0)));
+    assert_eq!("123".encode_utf16().size_hint(), (1, Some(3)));
+    assert_eq!("1234".encode_utf16().size_hint(), (2, Some(4)));
+    assert_eq!("12345678".encode_utf16().size_hint(), (3, Some(8)));
+
+    fn hint_vec(src: &str) -> Vec<(usize, Option<usize>)> {
+        let mut it = src.encode_utf16();
+        let mut result = Vec::new();
+        result.push(it.size_hint());
+        while it.next().is_some() {
+            result.push(it.size_hint())
+        }
+        result
+    }
+
+    assert_eq!(hint_vec("12"), [(1, Some(2)), (1, Some(1)), (0, Some(0))]);
+    assert_eq!(hint_vec("\u{101234}"), [(2, Some(4)), (1, Some(1)), (0, Some(0))]);
+    assert_eq!(hint_vec("\u{101234}a"), [(2, Some(5)), (2, Some(2)), (1, Some(1)), (0, Some(0))]);
+}
+
+#[test]
 fn starts_with_in_unicode() {
     assert!(!"├── Cargo.toml".starts_with("# "));
 }