Stabilize String::replace_range

Fixes #44643
This commit is contained in:
Thayne McCombs 2018-04-01 22:50:22 -06:00
parent 06fa27d7c8
commit 196b1426be
2 changed files with 20 additions and 21 deletions

View File

@ -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.
///
@ -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() {

View File

@ -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, "");
}