Skip to content

Commit 1223bbc

Browse files
committed
Make str::replace smarter.
1 parent b3927d6 commit 1223bbc

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libcore/str.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,13 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str {
962962
} else if starts_with(s, from) {
963963
ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to);
964964
} else {
965-
ret unsafe_from_byte(s[0]) +
966-
replace(slice(s, 1u, byte_len(s)), from, to);
965+
let idx = find(s, from);
966+
if idx == -1 {
967+
ret s;
968+
}
969+
ret char_slice(s, 0u, idx as uint) + to +
970+
replace(char_slice(s, idx as uint + char_len(from), char_len(s)),
971+
from, to);
967972
}
968973
}
969974

0 commit comments

Comments
 (0)