Make str::replace smarter.
This commit is contained in:
parent
b3927d6052
commit
1223bbc4de
1 changed files with 7 additions and 2 deletions
|
@ -962,8 +962,13 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str {
|
||||||
} else if starts_with(s, from) {
|
} else if starts_with(s, from) {
|
||||||
ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to);
|
ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to);
|
||||||
} else {
|
} else {
|
||||||
ret unsafe_from_byte(s[0]) +
|
let idx = find(s, from);
|
||||||
replace(slice(s, 1u, byte_len(s)), from, to);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue