Skip to content

Commit 80fca59

Browse files
committed
---
yaml --- r: 3985 b: refs/heads/master c: 7e55061 h: refs/heads/master i: 3983: 55a213b v: v3
1 parent 5f893b1 commit 80fca59

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 689f5f487c7c32b2fa543350d1458e75eb20922c
2+
refs/heads/master: 7e55061def5c09567ab51da9d9dcaa5016183806

trunk/src/lib/str.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export bytes_ivec;
5252
export unsafe_from_bytes_ivec;
5353
export is_empty;
5454
export is_not_empty;
55+
export replace;
5556

5657
native "rust" mod rustrt {
5758
type sbuf;
@@ -511,6 +512,22 @@ fn to_upper(str s) -> str {
511512
}
512513
ret outstr;
513514
}
515+
516+
// FIXME: This is super-inefficient
517+
fn replace(str s, str from, str to) : is_not_empty(from) -> str {
518+
// FIXME (694): Shouldn't have to check this
519+
check is_not_empty(from);
520+
if (byte_len(s) == 0u) {
521+
ret "";
522+
} else if (starts_with(s, from)) {
523+
ret to + replace(slice(s, byte_len(from), byte_len(s)),
524+
from, to);
525+
} else {
526+
ret unsafe_from_byte(s.(0))
527+
+ replace(slice(s, 1u, byte_len(s)), from, to);
528+
}
529+
}
530+
514531
// Local Variables:
515532
// mode: rust;
516533
// fill-column: 78;

trunk/src/test/run-pass/lib-str.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ fn test_is_not_empty() {
132132
assert !str::is_not_empty("");
133133
}
134134

135+
fn test_replace() {
136+
auto a = "a";
137+
check str::is_not_empty(a);
138+
assert str::replace("", a, "b") == "";
139+
assert str::replace("a", a, "b") == "b";
140+
assert str::replace("ab", a, "b") == "bb";
141+
auto test = "test";
142+
check str::is_not_empty(test);
143+
assert str::replace(" test test ", test, "toast")
144+
== " toast toast ";
145+
assert str::replace(" test test ", test, "") == " ";
146+
}
147+
135148
fn main() {
136149
test_bytes_len();
137150
test_index_and_rindex();
@@ -145,4 +158,15 @@ fn main() {
145158
test_ends_with();
146159
test_is_empty();
147160
test_is_not_empty();
148-
}
161+
test_replace();
162+
}
163+
164+
165+
// Local Variables:
166+
// mode: rust;
167+
// fill-column: 78;
168+
// indent-tabs-mode: nil
169+
// c-basic-offset: 4
170+
// buffer-file-coding-system: utf-8-unix
171+
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
172+
// End:

0 commit comments

Comments
 (0)