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

Let str::replace take a pattern #29498

Merged
merged 2 commits into from
Jan 13, 2016
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
25 changes: 14 additions & 11 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,9 +857,10 @@ impl str {
Utf16Units { encoder: Utf16Encoder::new(self[..].chars()) }
}

/// Returns `true` if the given `&str` is a sub-slice of this string slice.
/// Returns `true` if the given pattern matches a sub-slice of
/// this string slice.
///
/// Returns `false` if it's not.
/// Returns `false` if it does not.
///
/// # Examples
///
Expand All @@ -876,9 +877,10 @@ impl str {
core_str::StrExt::contains(self, pat)
}

/// Returns `true` if the given `&str` is a prefix of this string slice.
/// Returns `true` if the given pattern matches a prefix of this
/// string slice.
///
/// Returns `false` if it's not.
/// Returns `false` if it does not.
///
/// # Examples
///
Expand All @@ -895,9 +897,10 @@ impl str {
core_str::StrExt::starts_with(self, pat)
}

/// Returns `true` if the given `&str` is a suffix of this string slice.
/// Returns `true` if the given pattern matches a suffix of this
/// string slice.
///
/// Returns `false` if not.
/// Returns `false` if it does not.
///
/// # Examples
///
Expand Down Expand Up @@ -1681,11 +1684,11 @@ impl str {
core_str::StrExt::parse(self)
}

/// Replaces all occurrences of one string with another.
/// Replaces all matches of a pattern with another string.
///
/// `replace` creates a new [`String`], and copies the data from this string slice into it.
/// While doing so, it attempts to find a sub-`&str`. If it finds it, it replaces it with
/// the replacement string slice.
/// While doing so, it attempts to find matches of a pattern. If it finds any, it
/// replaces them with the replacement string slice.
///
/// [`String`]: string/struct.String.html
///
Expand All @@ -1699,14 +1702,14 @@ impl str {
/// assert_eq!("this is new", s.replace("old", "new"));
/// ```
///
/// When a `&str` isn't found:
/// When the pattern doesn't match:
///
/// ```
/// let s = "this is old";
/// assert_eq!(s, s.replace("cookie monster", "little lamb"));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn replace(&self, from: &str, to: &str) -> String {
pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
let mut result = String::new();
let mut last_end = 0;
for (start, part) in self.match_indices(from) {
Expand Down
9 changes: 9 additions & 0 deletions src/libcollectionstest/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ fn test_replace_2d() {
assert_eq!(data.replace(d, repl), data);
}

#[test]
fn test_replace_pattern() {
let data = "abcdαβγδabcdαβγδ";
assert_eq!(data.replace("dαβ", "😺😺😺"), "abc😺😺😺γδabc😺😺😺γδ");
assert_eq!(data.replace('γ', "😺😺😺"), "abcdαβ😺😺😺δabcdαβ😺😺😺δ");
assert_eq!(data.replace(&['a', 'γ'] as &[_], "😺😺😺"), "😺😺😺bcdαβ😺😺😺δ😺😺😺bcdαβ😺😺😺δ");
assert_eq!(data.replace(|c| c == 'γ', "😺😺😺"), "abcdαβ😺😺😺δabcdαβ😺😺😺δ");
}

#[test]
fn test_slice() {
assert_eq!("ab", &"abc"[0..2]);
Expand Down