Skip to content

Commit

Permalink
Make str::replace smarter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdm committed Jan 16, 2012
1 parent b3927d6 commit 1223bbc
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/libcore/str.rs
Expand Up @@ -962,8 +962,13 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str {
} else if starts_with(s, from) {
ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to);
} else {
ret unsafe_from_byte(s[0]) +
replace(slice(s, 1u, byte_len(s)), from, to);
let idx = find(s, from);
if idx == -1 {
ret s;
}
ret char_slice(s, 0u, idx as uint) + to +
replace(char_slice(s, idx as uint + char_len(from), char_len(s)),
from, to);
}
}

Expand Down

0 comments on commit 1223bbc

Please sign in to comment.