Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize String::replace_range #49577

Merged
merged 3 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the last part of the original sentence is still left over here.

// 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