Skip to content

Commit

Permalink
Rollup merge of rust-lang#51807 - newpavlov:deprecate_str_slice, r=al…
Browse files Browse the repository at this point in the history
…excrichton

Deprecation of str::slice_unchecked(_mut)

Closes rust-lang#51715

I am not sure if 1.28.0 or 1.29.0 should be used for deprecation version, for now it's 1.28.0.

Additionally I've replaced `slice_unchecked` uses with `get_unchecked`. The only places where this method is still used are `src/liballoc/tests/str.rs` and `src/liballoc/tests/str.rs`.
  • Loading branch information
kennytm authored Jul 22, 2018
2 parents d3b3bc5 + 808bcfb commit 67cf3ba
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ impl str {
let mut result = String::new();
let mut last_end = 0;
for (start, part) in self.match_indices(from) {
result.push_str(unsafe { self.slice_unchecked(last_end, start) });
result.push_str(unsafe { self.get_unchecked(last_end..start) });
result.push_str(to);
last_end = start + part.len();
}
result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) });
result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
result
}

Expand Down Expand Up @@ -309,11 +309,11 @@ impl str {
let mut result = String::with_capacity(32);
let mut last_end = 0;
for (start, part) in self.match_indices(pat).take(count) {
result.push_str(unsafe { self.slice_unchecked(last_end, start) });
result.push_str(unsafe { self.get_unchecked(last_end..start) });
result.push_str(to);
last_end = start + part.len();
}
result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) });
result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
result
}

Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ impl String {

while idx < len {
let ch = unsafe {
self.slice_unchecked(idx, len).chars().next().unwrap()
self.get_unchecked(idx..len).chars().next().unwrap()
};
let ch_len = ch.len_utf8();

Expand Down
8 changes: 4 additions & 4 deletions src/liballoc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ fn test_join_for_different_lengths_with_long_separator() {

#[test]
fn test_unsafe_slice() {
assert_eq!("ab", unsafe {"abc".slice_unchecked(0, 2)});
assert_eq!("bc", unsafe {"abc".slice_unchecked(1, 3)});
assert_eq!("", unsafe {"abc".slice_unchecked(1, 1)});
assert_eq!("ab", unsafe {"abc".get_unchecked(0..2)});
assert_eq!("bc", unsafe {"abc".get_unchecked(1..3)});
assert_eq!("", unsafe {"abc".get_unchecked(1..1)});
fn a_million_letter_a() -> String {
let mut i = 0;
let mut rs = String::new();
Expand All @@ -200,7 +200,7 @@ fn test_unsafe_slice() {
}
let letters = a_million_letter_a();
assert_eq!(half_a_million_letter_a(),
unsafe { letters.slice_unchecked(0, 500000)});
unsafe { letters.get_unchecked(0..500000)});
}

#[test]
Expand Down
28 changes: 15 additions & 13 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
if !self.finished && (self.allow_trailing_empty || self.end - self.start > 0) {
self.finished = true;
unsafe {
let string = self.matcher.haystack().slice_unchecked(self.start, self.end);
let string = self.matcher.haystack().get_unchecked(self.start..self.end);
Some(string)
}
} else {
Expand All @@ -1070,7 +1070,7 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
let haystack = self.matcher.haystack();
match self.matcher.next_match() {
Some((a, b)) => unsafe {
let elt = haystack.slice_unchecked(self.start, a);
let elt = haystack.get_unchecked(self.start..a);
self.start = b;
Some(elt)
},
Expand All @@ -1095,13 +1095,13 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
let haystack = self.matcher.haystack();
match self.matcher.next_match_back() {
Some((a, b)) => unsafe {
let elt = haystack.slice_unchecked(b, self.end);
let elt = haystack.get_unchecked(b..self.end);
self.end = a;
Some(elt)
},
None => unsafe {
self.finished = true;
Some(haystack.slice_unchecked(self.start, self.end))
Some(haystack.get_unchecked(self.start..self.end))
},
}
}
Expand Down Expand Up @@ -1222,7 +1222,7 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
#[inline]
fn next(&mut self) -> Option<(usize, &'a str)> {
self.0.next_match().map(|(start, end)| unsafe {
(start, self.0.haystack().slice_unchecked(start, end))
(start, self.0.haystack().get_unchecked(start..end))
})
}

Expand All @@ -1231,7 +1231,7 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
where P::Searcher: ReverseSearcher<'a>
{
self.0.next_match_back().map(|(start, end)| unsafe {
(start, self.0.haystack().slice_unchecked(start, end))
(start, self.0.haystack().get_unchecked(start..end))
})
}
}
Expand Down Expand Up @@ -1274,7 +1274,7 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
fn next(&mut self) -> Option<&'a str> {
self.0.next_match().map(|(a, b)| unsafe {
// Indices are known to be on utf8 boundaries
self.0.haystack().slice_unchecked(a, b)
self.0.haystack().get_unchecked(a..b)
})
}

Expand All @@ -1284,7 +1284,7 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
{
self.0.next_match_back().map(|(a, b)| unsafe {
// Indices are known to be on utf8 boundaries
self.0.haystack().slice_unchecked(a, b)
self.0.haystack().get_unchecked(a..b)
})
}
}
Expand Down Expand Up @@ -2453,6 +2453,7 @@ impl str {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked(begin..end)` instead")]
#[inline]
pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
(begin..end).get_unchecked(self)
Expand Down Expand Up @@ -2483,6 +2484,7 @@ impl str {
/// * `begin` and `end` must be byte positions within the string slice.
/// * `begin` and `end` must lie on UTF-8 sequence boundaries.
#[stable(feature = "str_slice_mut", since = "1.5.0")]
#[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked_mut(begin..end)` instead")]
#[inline]
pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
(begin..end).get_unchecked_mut(self)
Expand Down Expand Up @@ -2524,8 +2526,8 @@ impl str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(mid) {
unsafe {
(self.slice_unchecked(0, mid),
self.slice_unchecked(mid, self.len()))
(self.get_unchecked(0..mid),
self.get_unchecked(mid..self.len()))
}
} else {
slice_error_fail(self, 0, mid)
Expand Down Expand Up @@ -3702,7 +3704,7 @@ impl str {
}
unsafe {
// Searcher is known to return valid indices
self.slice_unchecked(i, j)
self.get_unchecked(i..j)
}
}

Expand Down Expand Up @@ -3741,7 +3743,7 @@ impl str {
}
unsafe {
// Searcher is known to return valid indices
self.slice_unchecked(i, self.len())
self.get_unchecked(i..self.len())
}
}

Expand Down Expand Up @@ -3788,7 +3790,7 @@ impl str {
}
unsafe {
// Searcher is known to return valid indices
self.slice_unchecked(0, j)
self.get_unchecked(0..j)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
#[inline]
fn next_back(&mut self) -> SearchStep {
let old_finger = self.finger_back;
let slice = unsafe { self.haystack.slice_unchecked(self.finger, old_finger) };
let slice = unsafe { self.haystack.get_unchecked(self.finger..old_finger) };
let mut iter = slice.chars();
let old_len = iter.iter.len();
if let Some(ch) = iter.next_back() {
Expand Down

0 comments on commit 67cf3ba

Please sign in to comment.