Skip to content

Commit

Permalink
Move string append to libraries. Closes #2710.
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan committed Jul 6, 2012
1 parent 8c64a98 commit ee0177b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ fn escape_unicode(c: char) -> str {
else { ('U', 8u) });
assert str::len(s) <= pad;
let mut out = "\\";
out += str::from_char(c);
for uint::range(str::len(s), pad) |_i| { out += "0"; }
out += s;
str::push_str(out, str::from_char(c));
for uint::range(str::len(s), pad) |_i| { str::push_str(out, "0"); }
str::push_str(out, s);
ret out;
}

Expand Down
6 changes: 3 additions & 3 deletions src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ impl extensions for rng {
* Return a random string of the specified length composed of A-Z,a-z,0-9
*/
fn gen_str(len: uint) -> str {
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789";
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789";
let mut s = "";
let mut i = 0u;
while (i < len) {
Expand Down
58 changes: 47 additions & 11 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export
from_byte,
from_char,
from_chars,
append,
concat,
connect,

Expand All @@ -25,6 +26,7 @@ export
unpack_slice,

// Adding things to and removing things from a string
push_str,
push_char,
pop_char,
shift_char,
Expand Down Expand Up @@ -233,19 +235,47 @@ pure fn from_chars(chs: &[const char]) -> str {
ret buf;
}

/// Appends a string slice to the back of a string
#[inline(always)]
fn push_str(&lhs: str, rhs: str/&) {
unsafe {
let llen = lhs.len();
let rlen = rhs.len();
reserve(lhs, llen + rlen);
do as_buf(lhs) |lbuf| {
do unpack_slice(rhs) |rbuf, _rlen| {
let dst = ptr::offset(lbuf, llen);
ptr::memcpy(dst, rbuf, rlen);
}
}
unsafe::set_len(lhs, llen + rlen);
}
}

/// Concatenate two strings together
#[inline(always)]
pure fn append(+lhs: str, rhs: str/&) -> str {
let mut v <- lhs;
unchecked {
push_str(v, rhs);
}
ret v;
}


/// Concatenate a vector of strings
pure fn concat(v: &[const str]) -> str {
let mut s: str = "";
for vec::each(v) |ss| { s += ss; }
for vec::each(v) |ss| { unchecked { push_str(s, ss) }; }
ret s;
}

/// Concatenate a vector of strings, placing a given separator between each
pure fn connect(v: &[const str], sep: str) -> str {
let mut s = "", first = true;
for vec::each(v) |ss| {
if first { first = false; } else { s += sep; }
s += ss;
if first { first = false; } else { unchecked { push_str(s, sep); } }
unchecked { push_str(s, ss) };
}
ret s;
}
Expand Down Expand Up @@ -576,8 +606,8 @@ pure fn to_upper(s: str/&) -> str {
pure fn replace(s: str, from: str, to: str) -> str {
let mut result = "", first = true;
do iter_between_matches(s, from) |start, end| {
if first { first = false; } else { result += to; }
unsafe { result += unsafe::slice_bytes(s, start, end); }
if first { first = false; } else { unchecked {push_str(result, to); }}
unsafe { push_str(result, unsafe::slice_bytes(s, start, end)); }
}
result
}
Expand Down Expand Up @@ -1694,7 +1724,7 @@ pure fn escape_default(s: str/&) -> str {
let mut out: str = "";
unchecked {
reserve_at_least(out, str::len(s));
chars_iter(s, |c| out += char::escape_default(c));
chars_iter(s, |c| push_str(out, char::escape_default(c)));
}
ret out;
}
Expand All @@ -1704,7 +1734,7 @@ pure fn escape_unicode(s: str/&) -> str {
let mut out: str = "";
unchecked {
reserve_at_least(out, str::len(s));
chars_iter(s, |c| out += char::escape_unicode(c));
chars_iter(s, |c| push_str(out, char::escape_unicode(c)));
}
ret out;
}
Expand Down Expand Up @@ -1863,6 +1893,12 @@ impl extensions for str {
/// Returns a string with trailing whitespace removed
#[inline]
fn trim_right() -> str { trim_right(self) }

/// Concatenate two strings: operator version
#[inline(always)]
pure fn +(rhs: str/&) -> str {
append(self, rhs)
}
}

/// Extension methods for strings
Expand Down Expand Up @@ -2311,13 +2347,13 @@ mod tests {
fn a_million_letter_a() -> str {
let mut i = 0;
let mut rs = "";
while i < 100000 { rs += "aaaaaaaaaa"; i += 1; }
while i < 100000 { push_str(rs, "aaaaaaaaaa"); i += 1; }
ret rs;
}
fn half_a_million_letter_a() -> str {
let mut i = 0;
let mut rs = "";
while i < 100000 { rs += "aaaaa"; i += 1; }
while i < 100000 { push_str(rs, "aaaaa"); i += 1; }
ret rs;
}
assert eq(half_a_million_letter_a(),
Expand Down Expand Up @@ -2422,13 +2458,13 @@ mod tests {
fn a_million_letter_X() -> str {
let mut i = 0;
let mut rs = "";
while i < 100000 { rs += "华华华华华华华华华华"; i += 1; }
while i < 100000 { push_str(rs, "华华华华华华华华华华"); i += 1; }
ret rs;
}
fn half_a_million_letter_X() -> str {
let mut i = 0;
let mut rs = "";
while i < 100000 { rs += "华华华华华"; i += 1; }
while i < 100000 { push_str(rs, "华华华华华"); i += 1; }
ret rs;
}
assert eq(half_a_million_letter_X(),
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/to_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl <A: to_str> of to_str for ~[A] {
let mut acc = "[", first = true;
for vec::each(self) |elt| {
if first { first = false; }
else { acc += ", "; }
acc += elt.to_str();
else { str::push_str(acc, ", "); }
str::push_str(acc, elt.to_str());
}
acc += "]";
str::push_char(acc, ']');
acc
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/rustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2963,8 +2963,8 @@ fn is_binopable(_cx: ctxt, ty: t, op: ast::binop) -> bool {
/*bool*/ ~[f, f, f, f, t, t, t, t],
/*int*/ ~[t, t, t, t, t, t, t, f],
/*float*/ ~[t, t, t, f, t, t, f, f],
/*str*/ ~[t, f, f, f, t, t, f, f],
/*vec*/ ~[t, f, f, f, t, t, f, f],
/*str*/ ~[f, f, f, f, t, t, f, f],
/*vec*/ ~[f, f, f, f, t, t, f, f],
/*bot*/ ~[f, f, f, f, t, t, f, f],
/*struct*/ ~[t, t, t, t, t, t, t, t]];

Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/module-polymorphism.rc
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ mod f32 {
#[path = "template.rs"]
mod template;

}
}
2 changes: 0 additions & 2 deletions src/test/run-pass/module-polymorphism4.rc
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[no_core];


#[path = "module-polymorphism4-files"]
mod cat {
Expand Down

0 comments on commit ee0177b

Please sign in to comment.