Skip to content

Commit

Permalink
Rollup merge of rust-lang#49577 - tmccombs:string-splice-stabilize, r…
Browse files Browse the repository at this point in the history
…=TimNN

Stabilize String::replace_range

Fixes rust-lang#44643
  • Loading branch information
kennytm committed Apr 4, 2018
2 parents 7dad49f + e75c6a7 commit 29ab7d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 43 deletions.
22 changes: 0 additions & 22 deletions src/doc/unstable-book/src/library-features/splice.md

This file was deleted.

11 changes: 5 additions & 6 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ impl String {
}
}

/// Creates a splicing iterator that removes the specified range in the string,
/// Removes the specified range in the string,
/// and replaces it with the given string.
/// The given string doesn't need to be the same length as the range.
///
Expand All @@ -1537,21 +1537,20 @@ impl String {
/// Basic usage:
///
/// ```
/// #![feature(splice)]
/// let mut s = String::from("α is alpha, β is beta");
/// let beta_offset = s.find('β').unwrap_or(s.len());
///
/// // Replace the range up until the β from the string
/// s.splice(..beta_offset, "Α is capital alpha; ");
/// s.replace_range(..beta_offset, "Α is capital alpha; ");
/// assert_eq!(s, "Α is capital alpha; β is beta");
/// ```
#[unstable(feature = "splice", reason = "recently added", issue = "44643")]
pub fn splice<R>(&mut self, range: R, replace_with: &str)
#[stable(feature = "splice", since = "1.27.0")]
pub fn replace_range<R>(&mut self, range: R, replace_with: &str)
where R: RangeBounds<usize>
{
// Memory safety
//
// The String version of Splice does not have the memory safety issues
// Replace_range does not have the memory safety issues of a vector Splice.
// of the vector version. The data is just plain bytes.

match range.start() {
Expand Down
30 changes: 15 additions & 15 deletions src/liballoc/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,53 +443,53 @@ fn test_drain() {
}

#[test]
fn test_splice() {
fn test_replace_range() {
let mut s = "Hello, world!".to_owned();
s.splice(7..12, "世界");
s.replace_range(7..12, "世界");
assert_eq!(s, "Hello, 世界!");
}

#[test]
#[should_panic]
fn test_splice_char_boundary() {
fn test_replace_range_char_boundary() {
let mut s = "Hello, 世界!".to_owned();
s.splice(..8, "");
s.replace_range(..8, "");
}

#[test]
fn test_splice_inclusive_range() {
fn test_replace_range_inclusive_range() {
let mut v = String::from("12345");
v.splice(2..=3, "789");
v.replace_range(2..=3, "789");
assert_eq!(v, "127895");
v.splice(1..=2, "A");
v.replace_range(1..=2, "A");
assert_eq!(v, "1A895");
}

#[test]
#[should_panic]
fn test_splice_out_of_bounds() {
fn test_replace_range_out_of_bounds() {
let mut s = String::from("12345");
s.splice(5..6, "789");
s.replace_range(5..6, "789");
}

#[test]
#[should_panic]
fn test_splice_inclusive_out_of_bounds() {
fn test_replace_range_inclusive_out_of_bounds() {
let mut s = String::from("12345");
s.splice(5..=5, "789");
s.replace_range(5..=5, "789");
}

#[test]
fn test_splice_empty() {
fn test_replace_range_empty() {
let mut s = String::from("12345");
s.splice(1..2, "");
s.replace_range(1..2, "");
assert_eq!(s, "1345");
}

#[test]
fn test_splice_unbounded() {
fn test_replace_range_unbounded() {
let mut s = String::from("12345");
s.splice(.., "");
s.replace_range(.., "");
assert_eq!(s, "");
}

Expand Down

0 comments on commit 29ab7d8

Please sign in to comment.