about summary refs log tree commit diff
path: root/library/alloc/src/string.rs
diff options
context:
space:
mode:
authordylni <46035563+dylni@users.noreply.github.com>2020-08-16 21:47:12 -0400
committerdylni <46035563+dylni@users.noreply.github.com>2020-08-16 21:47:12 -0400
commitd04e6b8de5fe6bbf203c534c35e6f55e8960ab46 (patch)
treec089016b8e43034b0809767b0787b8fa29076405 /library/alloc/src/string.rs
parented02b90e9b76b9ce2e8e99a99dbadd96ab4dfb42 (diff)
downloadrust-d04e6b8de5fe6bbf203c534c35e6f55e8960ab46.tar.gz
rust-d04e6b8de5fe6bbf203c534c35e6f55e8960ab46.zip
Replace ad hoc implementations with `slice::check_range`
Diffstat (limited to 'library/alloc/src/string.rs')
-rw-r--r--library/alloc/src/string.rs20
1 files changed, 6 insertions, 14 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index d7d7b6bd157..b3ac4397f17 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -47,7 +47,7 @@ use core::fmt;
 use core::hash;
 use core::iter::{FromIterator, FusedIterator};
 use core::ops::Bound::{Excluded, Included, Unbounded};
-use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeBounds};
+use core::ops::{self, Add, AddAssign, Index, IndexMut, Range, RangeBounds};
 use core::ptr;
 use core::str::{lossy, pattern::Pattern};
 
@@ -1506,23 +1506,15 @@ impl String {
         // of the vector version. The data is just plain bytes.
         // Because the range removal happens in Drop, if the Drain iterator is leaked,
         // the removal will not happen.
-        let len = self.len();
-        let start = match range.start_bound() {
-            Included(&n) => n,
-            Excluded(&n) => n + 1,
-            Unbounded => 0,
-        };
-        let end = match range.end_bound() {
-            Included(&n) => n + 1,
-            Excluded(&n) => n,
-            Unbounded => len,
-        };
+        let Range { start, end } = self.as_bytes().check_range(range);
+        assert!(self.is_char_boundary(start));
+        assert!(self.is_char_boundary(end));
 
         // Take out two simultaneous borrows. The &mut String won't be accessed
         // until iteration is over, in Drop.
         let self_ptr = self as *mut _;
-        // slicing does the appropriate bounds checks
-        let chars_iter = self[start..end].chars();
+        // SAFETY: `check_range` and `is_char_boundary` do the appropriate bounds checks.
+        let chars_iter = unsafe { self.get_unchecked(start..end) }.chars();
 
         Drain { start, end, iter: chars_iter, string: self_ptr }
     }