提交 d48396c9 编写于 作者: P Patrick Walton

libcore: De-mode str

上级 49af969e
......@@ -104,7 +104,7 @@ fn readclose(fd: c_int) -> ~str {
let mut buf = ~"";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
str::push_str(buf, str::from_bytes(bytes));
str::push_str(&mut buf, str::from_bytes(bytes));
}
os::fclose(file);
return buf;
......
......@@ -141,9 +141,9 @@ fn escape_unicode(c: char) -> ~str {
else { ('U', 8u) });
assert str::len(s) <= pad;
let mut out = ~"\\";
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);
str::push_str(&mut out, str::from_char(c));
for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); }
str::push_str(&mut out, s);
move out
}
......
......@@ -293,9 +293,9 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
let mut s : ~str = int_to_str_prec(i, radix, prec);
if 0 <= i {
if have_flag(cv.flags, flag_sign_always) {
unsafe { str::unshift_char(s, '+') };
unsafe { str::unshift_char(&mut s, '+') };
} else if have_flag(cv.flags, flag_space_for_sign) {
unsafe { str::unshift_char(s, ' ') };
unsafe { str::unshift_char(&mut s, ' ') };
}
}
return unsafe { pad(cv, s, PadSigned) };
......@@ -463,13 +463,13 @@ fn pad(cv: Conv, &s: ~str, mode: PadMode) -> ~str {
// instead.
if signed && zero_padding && str::len(s) > 0u {
let head = str::shift_char(s);
let head = str::shift_char(&mut s);
if head == '+' || head == '-' || head == ' ' {
let headstr = str::from_chars(vec::from_elem(1u, head));
return headstr + padstr + s;
}
else {
str::unshift_char(s, head);
str::unshift_char(&mut s, head);
}
}
return padstr + s;
......@@ -500,9 +500,9 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
let mut s : ~str = int_to_str_prec(i, radix, prec);
if 0 <= i {
if have_flag(cv.flags, flag_sign_always) {
unsafe { str::unshift_char(s, '+') };
unsafe { str::unshift_char(&mut s, '+') };
} else if have_flag(cv.flags, flag_space_for_sign) {
unsafe { str::unshift_char(s, ' ') };
unsafe { str::unshift_char(&mut s, ' ') };
}
}
return unsafe { pad(cv, s, PadSigned) };
......@@ -670,13 +670,13 @@ fn pad(cv: Conv, &s: ~str, mode: PadMode) -> ~str {
// instead.
if signed && zero_padding && str::len(s) > 0u {
let head = str::shift_char(s);
let head = str::shift_char(&mut s);
if head == '+' || head == '-' || head == ' ' {
let headstr = str::from_chars(vec::from_elem(1u, head));
return headstr + padstr + s;
}
else {
str::unshift_char(s, head);
str::unshift_char(&mut s, head);
}
}
return padstr + s;
......
......@@ -7,9 +7,13 @@
* some heavy-duty uses, try std::rope.
*/
#[warn(deprecated_mode)];
#[warn(deprecated_pattern)];
use cmp::{Eq, Ord};
use libc::size_t;
use io::WriterUtil;
use to_str::ToStr;
export
// Creating a string
......@@ -154,7 +158,7 @@
}
/// Appends a character at the end of a string
fn push_char(&s: ~str, ch: char) {
fn push_char(s: &const ~str, ch: char) {
unsafe {
let code = ch as uint;
let nb = if code < max_one_b { 1u }
......@@ -163,11 +167,11 @@ fn push_char(&s: ~str, ch: char) {
else if code < max_four_b { 4u }
else if code < max_five_b { 5u }
else { 6u };
let len = len(s);
let len = len(*s);
let new_len = len + nb;
reserve_at_least(s, new_len);
let off = len;
do as_buf(s) |buf, _len| {
do as_buf(*s) |buf, _len| {
let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
if nb == 1u {
*ptr::mut_offset(buf, off) =
......@@ -227,7 +231,7 @@ fn push_char(&s: ~str, ch: char) {
/// Convert a char to a string
pure fn from_char(ch: char) -> ~str {
let mut buf = ~"";
unsafe { push_char(buf, ch); }
unsafe { push_char(&mut buf, ch); }
move buf
}
......@@ -235,9 +239,9 @@ fn push_char(&s: ~str, ch: char) {
pure fn from_chars(chs: &[char]) -> ~str {
let mut buf = ~"";
unsafe {
reserve(buf, chs.len());
reserve(&mut buf, chs.len());
for vec::each(chs) |ch| {
push_char(buf, *ch);
push_char(&mut buf, *ch);
}
}
move buf
......@@ -245,12 +249,12 @@ fn push_char(&s: ~str, ch: char) {
/// Appends a string slice to the back of a string, without overallocating
#[inline(always)]
fn push_str_no_overallocate(&lhs: ~str, rhs: &str) {
fn push_str_no_overallocate(lhs: &const ~str, rhs: &str) {
unsafe {
let llen = lhs.len();
let rlen = rhs.len();
reserve(lhs, llen + rlen);
do as_buf(lhs) |lbuf, _llen| {
do as_buf(*lhs) |lbuf, _llen| {
do as_buf(rhs) |rbuf, _rlen| {
let dst = ptr::offset(lbuf, llen);
let dst = ::cast::transmute_mut_unsafe(dst);
......@@ -262,12 +266,12 @@ fn push_str_no_overallocate(&lhs: ~str, rhs: &str) {
}
/// Appends a string slice to the back of a string
#[inline(always)]
fn push_str(&lhs: ~str, rhs: &str) {
fn push_str(lhs: &const ~str, rhs: &str) {
unsafe {
let llen = lhs.len();
let rlen = rhs.len();
reserve_at_least(lhs, llen + rlen);
do as_buf(lhs) |lbuf, _llen| {
do as_buf(*lhs) |lbuf, _llen| {
do as_buf(rhs) |rbuf, _rlen| {
let dst = ptr::offset(lbuf, llen);
let dst = ::cast::transmute_mut_unsafe(dst);
......@@ -283,7 +287,7 @@ fn push_str(&lhs: ~str, rhs: &str) {
pure fn append(+lhs: ~str, rhs: &str) -> ~str {
let mut v <- lhs;
unsafe {
push_str_no_overallocate(v, rhs);
push_str_no_overallocate(&mut v, rhs);
}
move v
}
......@@ -293,7 +297,7 @@ fn push_str(&lhs: ~str, rhs: &str) {
pure fn concat(v: &[~str]) -> ~str {
let mut s: ~str = ~"";
for vec::each(v) |ss| {
unsafe { push_str(s, *ss) };
unsafe { push_str(&mut s, *ss) };
}
move s
}
......@@ -302,8 +306,8 @@ fn push_str(&lhs: ~str, rhs: &str) {
pure fn connect(v: &[~str], sep: &str) -> ~str {
let mut s = ~"", first = true;
for vec::each(v) |ss| {
if first { first = false; } else { unsafe { push_str(s, sep); } }
unsafe { push_str(s, *ss) };
if first { first = false; } else { unsafe { push_str(&mut s, sep); } }
unsafe { push_str(&mut s, *ss) };
}
move s
}
......@@ -319,10 +323,10 @@ fn push_str(&lhs: ~str, rhs: &str) {
*
* If the string does not contain any characters
*/
fn pop_char(&s: ~str) -> char {
let end = len(s);
fn pop_char(s: &const ~str) -> char {
let end = len(*s);
assert end > 0u;
let {ch, prev} = char_range_at_reverse(s, end);
let {ch, prev} = char_range_at_reverse(*s, end);
unsafe { raw::set_len(s, prev); }
return ch;
}
......@@ -334,9 +338,9 @@ fn pop_char(&s: ~str) -> char {
*
* If the string does not contain any characters
*/
fn shift_char(&s: ~str) -> char {
let {ch, next} = char_range_at(s, 0u);
s = unsafe { raw::slice_bytes(s, next, len(s)) };
fn shift_char(s: &mut ~str) -> char {
let {ch, next} = char_range_at(*s, 0u);
*s = unsafe { raw::slice_bytes(*s, next, len(*s)) };
return ch;
}
......@@ -357,7 +361,9 @@ fn view_shift_char(s: &a/str) -> (char, &a/str) {
}
/// Prepend a char to a string
fn unshift_char(&s: ~str, ch: char) { s = from_char(ch) + s; }
fn unshift_char(s: &mut ~str, ch: char) {
*s = from_char(ch) + *s;
}
/**
* Returns a string with leading `chars_to_trim` removed.
......@@ -666,7 +672,7 @@ fn view_shift_char(s: &a/str) -> (char, &a/str) {
let l = len(s);
let mut cp = copy s;
if l > 0u && s[l - 1u] == '\r' as u8 {
unsafe { raw::set_len(cp, l - 1u); }
unsafe { raw::set_len(&mut cp, l - 1u); }
}
move cp
})
......@@ -707,8 +713,12 @@ fn view_shift_char(s: &a/str) -> (char, &a/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 { unsafe {push_str(result, to); }}
unsafe { push_str(result, raw::slice_bytes(s, start, end)); }
if first {
first = false;
} else {
unsafe { push_str(&mut result, to); }
}
unsafe { push_str(&mut result, raw::slice_bytes(s, start, end)); }
}
move result
}
......@@ -950,9 +960,9 @@ impl @str : Ord {
pure fn map(ss: &str, ff: fn(char) -> char) -> ~str {
let mut result = ~"";
unsafe {
reserve(result, len(ss));
reserve(&mut result, len(ss));
for chars_each(ss) |cc| {
str::push_char(result, ff(cc));
str::push_char(&mut result, ff(cc));
}
}
move result
......@@ -1638,8 +1648,8 @@ fn is_alphanumeric(s: &str) -> bool {
pure fn from_utf16(v: &[u16]) -> ~str {
let mut buf = ~"";
unsafe {
reserve(buf, vec::len(v));
utf16_chars(v, |ch| push_char(buf, ch));
reserve(&mut buf, vec::len(v));
utf16_chars(v, |ch| push_char(&mut buf, ch));
}
move buf
}
......@@ -1890,9 +1900,9 @@ fn is_alphanumeric(s: &str) -> bool {
* let i = str::as_bytes("Hello World") { |bytes| vec::len(bytes) };
* ~~~
*/
pure fn as_bytes<T>(s: ~str, f: fn(~[u8]) -> T) -> T {
pure fn as_bytes<T>(s: &const ~str, f: fn(~[u8]) -> T) -> T {
unsafe {
let v: *~[u8] = ::cast::reinterpret_cast(&ptr::addr_of(s));
let v: *~[u8] = cast::transmute(copy s);
f(*v)
}
}
......@@ -1971,9 +1981,9 @@ fn is_alphanumeric(s: &str) -> bool {
* * s - A string
* * n - The number of bytes to reserve space for
*/
fn reserve(&s: ~str, n: uint) {
fn reserve(s: &const ~str, n: uint) {
unsafe {
let v: *mut ~[u8] = ::cast::reinterpret_cast(&ptr::addr_of(s));
let v: *mut ~[u8] = cast::transmute(copy s);
vec::reserve(*v, n + 1);
}
}
......@@ -1998,7 +2008,7 @@ fn reserve(&s: ~str, n: uint) {
* * s - A string
* * n - The number of bytes to reserve space for
*/
fn reserve_at_least(&s: ~str, n: uint) {
fn reserve_at_least(s: &const ~str, n: uint) {
reserve(s, uint::next_power_of_two(n + 1u) - 1u)
}
......@@ -2006,7 +2016,7 @@ fn reserve_at_least(&s: ~str, n: uint) {
* Returns the number of single-byte characters the string can hold without
* reallocating
*/
pure fn capacity(&&s: ~str) -> uint {
pure fn capacity(s: &const ~str) -> uint {
do as_bytes(s) |buf| {
let vcap = vec::capacity(buf);
assert vcap > 0u;
......@@ -2018,9 +2028,9 @@ fn reserve_at_least(&s: ~str, n: uint) {
pure fn escape_default(s: &str) -> ~str {
let mut out: ~str = ~"";
unsafe {
reserve_at_least(out, str::len(s));
reserve_at_least(&mut out, str::len(s));
for chars_each(s) |c| {
push_str(out, char::escape_default(c));
push_str(&mut out, char::escape_default(c));
}
}
move out
......@@ -2030,9 +2040,9 @@ fn reserve_at_least(&s: ~str, n: uint) {
pure fn escape_unicode(s: &str) -> ~str {
let mut out: ~str = ~"";
unsafe {
reserve_at_least(out, str::len(s));
reserve_at_least(&mut out, str::len(s));
for chars_each(s) |c| {
push_str(out, char::escape_unicode(c));
push_str(&mut out, char::escape_unicode(c));
}
}
move out
......@@ -2159,9 +2169,9 @@ unsafe fn view_bytes(s: &str, begin: uint, end: uint) -> &str {
}
/// Appends a byte to a string. (Not UTF-8 safe).
unsafe fn push_byte(&s: ~str, b: u8) {
unsafe fn push_byte(s: &const ~str, b: u8) {
reserve_at_least(s, s.len() + 1);
do as_buf(s) |buf, len| {
do as_buf(*s) |buf, len| {
let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
*ptr::mut_offset(buf, len) = b;
}
......@@ -2169,14 +2179,14 @@ unsafe fn push_byte(&s: ~str, b: u8) {
}
/// Appends a vector of bytes to a string. (Not UTF-8 safe).
unsafe fn push_bytes(&s: ~str, bytes: ~[u8]) {
unsafe fn push_bytes(s: &const ~str, bytes: &[u8]) {
reserve_at_least(s, s.len() + bytes.len());
for vec::each(bytes) |byte| { push_byte(s, *byte); }
}
/// Removes the last byte from a string and returns it. (Not UTF-8 safe).
unsafe fn pop_byte(&s: ~str) -> u8 {
let len = len(s);
unsafe fn pop_byte(s: &const ~str) -> u8 {
let len = len(*s);
assert (len > 0u);
let b = s[len - 1u];
unsafe { set_len(s, len - 1u) };
......@@ -2184,17 +2194,18 @@ unsafe fn pop_byte(&s: ~str) -> u8 {
}
/// Removes the first byte from a string and returns it. (Not UTF-8 safe).
unsafe fn shift_byte(&s: ~str) -> u8 {
let len = len(s);
unsafe fn shift_byte(s: &mut ~str) -> u8 {
let len = len(*s);
assert (len > 0u);
let b = s[0];
s = unsafe { raw::slice_bytes(s, 1u, len) };
*s = unsafe { raw::slice_bytes(*s, 1u, len) };
return b;
}
/// Sets the length of the string and adds the null terminator
unsafe fn set_len(&v: ~str, new_len: uint) {
let repr: *vec::raw::VecRepr = ::cast::reinterpret_cast(&v);
unsafe fn set_len(v: &const ~str, new_len: uint) {
let v: **vec::raw::VecRepr = cast::transmute(copy v);
let repr: *vec::raw::VecRepr = *v;
(*repr).unboxed.fill = new_len + 1u;
let null = ptr::mut_offset(ptr::mut_addr_of((*repr).unboxed.data),
new_len);
......@@ -2457,7 +2468,7 @@ fn test_rfind_char() {
#[test]
fn test_pop_char() {
let mut data = ~"ประเทศไทย中华";
let cc = pop_char(data);
let cc = pop_char(&mut data);
assert ~"ประเทศไทย中" == data;
assert '华' == cc;
}
......@@ -2465,7 +2476,7 @@ fn test_pop_char() {
#[test]
fn test_pop_char_2() {
let mut data2 = ~"华";
let cc2 = pop_char(data2);
let cc2 = pop_char(&mut data2);
assert ~"" == data2;
assert '华' == cc2;
}
......@@ -2475,12 +2486,12 @@ fn test_pop_char_2() {
#[ignore(cfg(windows))]
fn test_pop_char_fail() {
let mut data = ~"";
let _cc3 = pop_char(data);
let _cc3 = pop_char(&mut data);
}
#[test]
fn test_split_char() {
fn t(s: ~str, c: char, u: ~[~str]) {
fn t(s: &str, c: char, u: &[~str]) {
log(debug, ~"split_byte: " + s);
let v = split_char(s, c);
debug!("split_byte to: %?", v);
......@@ -2509,7 +2520,7 @@ fn test_split_char_2() {
#[test]
fn test_splitn_char() {
fn t(s: ~str, c: char, n: uint, u: ~[~str]) {
fn t(s: &str, c: char, n: uint, u: &[~str]) {
log(debug, ~"splitn_byte: " + s);
let v = splitn_char(s, c, n);
debug!("split_byte to: %?", v);
......@@ -2560,9 +2571,10 @@ fn test_splitn_char_3() {
#[test]
fn test_split_str() {
fn t(s: ~str, sep: &a/str, i: int, k: ~str) {
fn t(s: &str, sep: &a/str, i: int, k: &str) {
fn borrow(x: &a/str) -> &a/str { x }
let v = split_str(s, sep);
assert v[i] == k;
assert borrow(v[i]) == k;
}
t(~"--1233345--", ~"12345", 0, ~"--1233345--");
......@@ -2689,8 +2701,8 @@ fn test_find_str_between() {
#[test]
fn test_substr() {
fn t(a: ~str, b: ~str, start: int) {
assert substr(a, start as uint, len(b)) == b;
fn t(a: &str, b: &str, start: int) {
assert substr(a, start as uint, len(b)) == b.to_str();
}
t(~"hello", ~"llo", 2);
t(~"hello", ~"el", 1);
......@@ -2699,7 +2711,9 @@ fn t(a: ~str, b: ~str, start: int) {
#[test]
fn test_concat() {
fn t(v: ~[~str], s: ~str) { assert concat(v) == s; }
fn t(v: &[~str], s: &str) {
assert concat(v) == s.to_str();
}
t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"], ~"youknowI'mnogood");
let v: ~[~str] = ~[];
t(v, ~"");
......@@ -2708,8 +2722,8 @@ fn test_concat() {
#[test]
fn test_connect() {
fn t(v: ~[~str], sep: ~str, s: ~str) {
assert connect(v, sep) == s;
fn t(v: &[~str], sep: &str, s: &str) {
assert connect(v, sep) == s.to_str();
}
t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"],
~" ", ~"you know I'm no good");
......@@ -2746,13 +2760,13 @@ fn test_unsafe_slice() {
fn a_million_letter_a() -> ~str {
let mut i = 0;
let mut rs = ~"";
while i < 100000 { push_str(rs, ~"aaaaaaaaaa"); i += 1; }
while i < 100000 { push_str(&mut rs, ~"aaaaaaaaaa"); i += 1; }
return rs;
}
fn half_a_million_letter_a() -> ~str {
let mut i = 0;
let mut rs = ~"";
while i < 100000 { push_str(rs, ~"aaaaa"); i += 1; }
while i < 100000 { push_str(&mut rs, ~"aaaaa"); i += 1; }
return rs;
}
assert half_a_million_letter_a() ==
......@@ -2856,13 +2870,16 @@ fn test_slice() {
fn a_million_letter_X() -> ~str {
let mut i = 0;
let mut rs = ~"";
while i < 100000 { push_str(rs, ~"华华华华华华华华华华"); i += 1; }
while i < 100000 {
push_str(&mut rs, ~"华华华华华华华华华华");
i += 1;
}
return rs;
}
fn half_a_million_letter_X() -> ~str {
let mut i = 0;
let mut rs = ~"";
while i < 100000 { push_str(rs, ~"华华华华华"); i += 1; }
while i < 100000 { push_str(&mut rs, ~"华华华华华"); i += 1; }
return rs;
}
assert half_a_million_letter_X() ==
......@@ -2975,7 +2992,7 @@ fn test_is_ascii() {
#[test]
fn test_shift_byte() {
let mut s = ~"ABC";
let b = unsafe { raw::shift_byte(s) };
let b = unsafe { raw::shift_byte(&mut s) };
assert (s == ~"BC");
assert (b == 65u8);
}
......@@ -2983,7 +3000,7 @@ fn test_shift_byte() {
#[test]
fn test_pop_byte() {
let mut s = ~"ABC";
let b = unsafe { raw::pop_byte(s) };
let b = unsafe { raw::pop_byte(&mut s) };
assert (s == ~"AB");
assert (b == 67u8);
}
......@@ -3045,7 +3062,7 @@ fn test_from_buf() {
#[should_fail]
fn test_as_bytes_fail() {
// Don't double free
as_bytes::<()>(~"", |_bytes| fail );
as_bytes::<()>(&~"", |_bytes| fail );
}
#[test]
......
......@@ -83,10 +83,10 @@ fn to_str() -> ~str {
let mut acc = ~"[", first = true;
for vec::each(self) |elt| {
if first { first = false; }
else { str::push_str(acc, ~", "); }
str::push_str(acc, elt.to_str());
else { str::push_str(&mut acc, ~", "); }
str::push_str(&mut acc, elt.to_str());
}
str::push_char(acc, ']');
str::push_char(&mut acc, ']');
move acc
}
}
......
......@@ -14,7 +14,7 @@ fn to_base64() -> ~str {
let len = self.len();
let mut s = ~"";
str::reserve(s, ((len + 3u) / 4u) * 3u);
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
let mut i = 0u;
......@@ -24,10 +24,10 @@ fn to_base64() -> ~str {
(self[i + 2u] as uint);
// This 24-bit number gets separated into four 6-bit numbers.
str::push_char(s, chars[(n >> 18u) & 63u]);
str::push_char(s, chars[(n >> 12u) & 63u]);
str::push_char(s, chars[(n >> 6u) & 63u]);
str::push_char(s, chars[n & 63u]);
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
str::push_char(&mut s, chars[n & 63u]);
i += 3u;
}
......@@ -38,17 +38,17 @@ fn to_base64() -> ~str {
0 => (),
1 => {
let n = (self[i] as uint) << 16u;
str::push_char(s, chars[(n >> 18u) & 63u]);
str::push_char(s, chars[(n >> 12u) & 63u]);
str::push_char(s, '=');
str::push_char(s, '=');
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
str::push_char(&mut s, '=');
str::push_char(&mut s, '=');
}
2 => {
let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u;
str::push_char(s, chars[(n >> 18u) & 63u]);
str::push_char(s, chars[(n >> 12u) & 63u]);
str::push_char(s, chars[(n >> 6u) & 63u]);
str::push_char(s, '=');
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
str::push_char(&mut s, '=');
}
_ => fail ~"Algebra is broken, please alert the math police"
}
......
......@@ -92,7 +92,7 @@ fn to_writer(wr: io::Writer, j: Json) {
fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
fn spaces(n: uint) -> ~str {
let mut ss = ~"";
for n.times { str::push_str(ss, " "); }
for n.times { str::push_str(&mut ss, " "); }
return ss;
}
......@@ -428,14 +428,14 @@ fn parse_str() -> Result<@~str, Error> {
if (escape) {
match self.ch {
'"' => str::push_char(res, '"'),
'\\' => str::push_char(res, '\\'),
'/' => str::push_char(res, '/'),
'b' => str::push_char(res, '\x08'),
'f' => str::push_char(res, '\x0c'),
'n' => str::push_char(res, '\n'),
'r' => str::push_char(res, '\r'),
't' => str::push_char(res, '\t'),
'"' => str::push_char(&mut res, '"'),
'\\' => str::push_char(&mut res, '\\'),
'/' => str::push_char(&mut res, '/'),
'b' => str::push_char(&mut res, '\x08'),
'f' => str::push_char(&mut res, '\x0c'),
'n' => str::push_char(&mut res, '\n'),
'r' => str::push_char(&mut res, '\r'),
't' => str::push_char(&mut res, '\t'),
'u' => {
// Parse \u1234.
let mut i = 0u;
......@@ -464,7 +464,7 @@ fn parse_str() -> Result<@~str, Error> {
~"invalid \\u escape (not four digits)");
}
str::push_char(res, n as char);
str::push_char(&mut res, n as char);
}
_ => return self.error(~"invalid escape")
}
......@@ -476,7 +476,7 @@ fn parse_str() -> Result<@~str, Error> {
self.bump();
return Ok(@res);
}
str::push_char(res, self.ch);
str::push_char(&mut res, self.ch);
}
}
......
......@@ -61,7 +61,7 @@ fn encode_inner(s: &str, full_url: bool) -> ~str {
'a' .. 'z' |
'0' .. '9' |
'-' | '.' | '_' | '~' => {
str::push_char(out, ch);
str::push_char(&mut out, ch);
}
_ => {
if full_url {
......@@ -72,7 +72,7 @@ fn encode_inner(s: &str, full_url: bool) -> ~str {
// sub-delims:
'!' | '$' | '&' | '"' | '(' | ')' | '*' |
'+' | ',' | ';' | '=' => {
str::push_char(out, ch);
str::push_char(&mut out, ch);
}
_ => out += #fmt("%%%X", ch as uint)
......@@ -127,18 +127,18 @@ fn decode_inner(s: &str, full_url: bool) -> ~str {
// sub-delims:
'!' | '$' | '&' | '"' | '(' | ')' | '*' |
'+' | ',' | ';' | '=' => {
str::push_char(out, '%');
str::push_char(out, bytes[0u] as char);
str::push_char(out, bytes[1u] as char);
str::push_char(&mut out, '%');
str::push_char(&mut out, bytes[0u] as char);
str::push_char(&mut out, bytes[1u] as char);
}
ch => str::push_char(out, ch)
ch => str::push_char(&mut out, ch)
}
} else {
str::push_char(out, ch);
str::push_char(&mut out, ch);
}
}
ch => str::push_char(out, ch)
ch => str::push_char(&mut out, ch)
}
}
......@@ -170,9 +170,9 @@ fn encode_plus(s: &str) -> ~str {
let ch = rdr.read_byte() as char;
match ch {
'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '.' | '-' => {
str::push_char(out, ch);
str::push_char(&mut out, ch);
}
' ' => str::push_char(out, '+'),
' ' => str::push_char(&mut out, '+'),
_ => out += #fmt("%%%X", ch as uint)
}
}
......@@ -195,7 +195,7 @@ fn encode_form_urlencoded(m: HashMap<~str, @DVec<@~str>>) -> ~str {
if first {
first = false;
} else {
str::push_char(out, '&');
str::push_char(&mut out, '&');
first = false;
}
......@@ -248,9 +248,9 @@ fn decode_form_urlencoded(s: ~[u8]) ->
};
if parsing_key {
str::push_char(key, ch)
str::push_char(&mut key, ch)
} else {
str::push_char(value, ch)
str::push_char(&mut value, ch)
}
}
}
......
......@@ -271,7 +271,10 @@ fn test() unsafe {
fn a_million_letter_a() -> ~str {
let mut i = 0;
let mut rs = ~"";
while i < 100000 { str::push_str(rs, ~"aaaaaaaaaa"); i += 1; }
while i < 100000 {
str::push_str(&mut rs, ~"aaaaaaaaaa");
i += 1;
}
return rs;
}
// Test messages from FIPS 180-1
......
......@@ -800,7 +800,7 @@ fn parse_type(ch: char, tm: &Tm) -> ~str {
while !rdr.eof() {
match rdr.read_char() {
'%' => buf += parse_type(rdr.read_char(), &tm),
ch => str::push_char(buf, ch)
ch => str::push_char(&mut buf, ch)
}
}
}
......
......@@ -255,14 +255,14 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
// indent past |name:## | and the 0-offset column location
let mut left = str::len(fm.name) + digits + lo.col + 3u;
let mut s = ~"";
while left > 0u { str::push_char(s, ' '); left -= 1u; }
while left > 0u { str::push_char(&mut s, ' '); left -= 1u; }
s += ~"^";
let hi = codemap::lookup_char_pos(cm, sp.hi);
if hi.col != lo.col {
// the ^ already takes up one space
let mut width = hi.col - lo.col - 1u;
while width > 0u { str::push_char(s, '~'); width -= 1u; }
while width > 0u { str::push_char(&mut s, '~'); width -= 1u; }
}
io::stderr().write_str(s + ~"\n");
}
......
......@@ -236,11 +236,11 @@ fn finish<T: qq_helper>
str2 += repl;
}
match copy state {
active => str::push_char(str2, ch),
active => str::push_char(&mut str2, ch),
skip(1u) => state = blank,
skip(sk) => state = skip (sk-1u),
blank if is_space(ch) => str::push_char(str2, ch),
blank => str::push_char(str2, ' ')
blank if is_space(ch) => str::push_char(&mut str2, ch),
blank => str::push_char(&mut str2, ' ')
}
i += 1u;
if (j < g_len && i == cx.gather[j].hi) {
......
......@@ -117,7 +117,7 @@ fn block_trim(lines: ~[~str], chars: ~str, max: Option<uint>) -> ~[~str] {
fn read_to_eol(rdr: string_reader) -> ~str {
let mut val = ~"";
while rdr.curr != '\n' && !is_eof(rdr) {
str::push_char(val, rdr.curr);
str::push_char(&mut val, rdr.curr);
bump(rdr);
}
if rdr.curr == '\n' { bump(rdr); }
......@@ -242,7 +242,7 @@ fn read_block_comment(rdr: string_reader, code_to_the_left: bool,
curr_line = ~"";
bump(rdr);
} else {
str::push_char(curr_line, rdr.curr);
str::push_char(&mut curr_line, rdr.curr);
if rdr.curr == '/' && nextch(rdr) == '*' {
bump(rdr);
bump(rdr);
......
......@@ -214,7 +214,7 @@ fn consume_any_line_comment(rdr: string_reader)
let start_chpos = rdr.chpos - 2u;
let mut acc = ~"//";
while rdr.curr != '\n' && !is_eof(rdr) {
str::push_char(acc, rdr.curr);
str::push_char(&mut acc, rdr.curr);
bump(rdr);
}
return Some({
......@@ -253,7 +253,7 @@ fn consume_block_comment(rdr: string_reader)
let start_chpos = rdr.chpos - 2u;
let mut acc = ~"/*";
while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) {
str::push_char(acc, rdr.curr);
str::push_char(&mut acc, rdr.curr);
bump(rdr);
}
if is_eof(rdr) {
......@@ -288,11 +288,11 @@ fn scan_exponent(rdr: string_reader) -> Option<~str> {
let mut c = rdr.curr;
let mut rslt = ~"";
if c == 'e' || c == 'E' {
str::push_char(rslt, c);
str::push_char(&mut rslt, c);
bump(rdr);
c = rdr.curr;
if c == '-' || c == '+' {
str::push_char(rslt, c);
str::push_char(&mut rslt, c);
bump(rdr);
}
let exponent = scan_digits(rdr, 10u);
......@@ -309,7 +309,7 @@ fn scan_digits(rdr: string_reader, radix: uint) -> ~str {
if c == '_' { bump(rdr); loop; }
match char::to_digit(c, radix) {
Some(_) => {
str::push_char(rslt, c);
str::push_char(&mut rslt, c);
bump(rdr);
}
_ => return rslt
......@@ -447,7 +447,7 @@ fn next_token_inner(rdr: string_reader) -> token::token {
|| (c >= '0' && c <= '9')
|| c == '_'
|| (c > 'z' && char::is_XID_continue(c)) {
str::push_char(accum_str, c);
str::push_char(&mut accum_str, c);
bump(rdr);
c = rdr.curr;
}
......@@ -599,28 +599,31 @@ fn binop(rdr: string_reader, op: token::binop) -> token::token {
let escaped = rdr.curr;
bump(rdr);
match escaped {
'n' => str::push_char(accum_str, '\n'),
'r' => str::push_char(accum_str, '\r'),
't' => str::push_char(accum_str, '\t'),
'\\' => str::push_char(accum_str, '\\'),
'\'' => str::push_char(accum_str, '\''),
'"' => str::push_char(accum_str, '"'),
'n' => str::push_char(&mut accum_str, '\n'),
'r' => str::push_char(&mut accum_str, '\r'),
't' => str::push_char(&mut accum_str, '\t'),
'\\' => str::push_char(&mut accum_str, '\\'),
'\'' => str::push_char(&mut accum_str, '\''),
'"' => str::push_char(&mut accum_str, '"'),
'\n' => consume_whitespace(rdr),
'x' => {
str::push_char(accum_str, scan_numeric_escape(rdr, 2u));
str::push_char(&mut accum_str,
scan_numeric_escape(rdr, 2u));
}
'u' => {
str::push_char(accum_str, scan_numeric_escape(rdr, 4u));
str::push_char(&mut accum_str,
scan_numeric_escape(rdr, 4u));
}
'U' => {
str::push_char(accum_str, scan_numeric_escape(rdr, 8u));
str::push_char(&mut accum_str,
scan_numeric_escape(rdr, 8u));
}
c2 => {
rdr.fatal(fmt!("unknown string escape: %d", c2 as int));
}
}
}
_ => str::push_char(accum_str, ch)
_ => str::push_char(&mut accum_str, ch)
}
}
bump(rdr);
......
......@@ -2093,13 +2093,13 @@ fn fn_header_info_to_str(opt_sty: Option<ast::self_ty_>,
match opt_purity {
Some(ast::impure_fn) => { }
Some(purity) => {
str::push_str(s, purity_to_str(purity));
str::push_char(s, ' ');
str::push_str(&mut s, purity_to_str(purity));
str::push_char(&mut s, ' ');
}
None => {}
}
str::push_str(s, opt_proto_to_str(opt_p));
str::push_str(&mut s, opt_proto_to_str(opt_p));
return s;
}
......
......@@ -548,10 +548,10 @@ fn sanitize(s: ~str) -> ~str {
'a' .. 'z'
| 'A' .. 'Z'
| '0' .. '9'
| '_' => str::push_char(result,c),
| '_' => str::push_char(&mut result, c),
_ => {
if c > 'z' && char::is_XID_continue(c) {
str::push_char(result,c);
str::push_char(&mut result, c);
}
}
}
......
......@@ -1175,7 +1175,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
//
// vec::from_slice(metadata_encoding_version) +
(do str::as_bytes(~"rust\x00\x00\x00\x01") |bytes| {
(do str::as_bytes(&~"rust\x00\x00\x00\x01") |bytes| {
vec::slice(bytes, 0, 8)
}) + flate::deflate_bytes(wr.buf.check_out(|buf| buf))
}
......
......@@ -44,8 +44,8 @@ fn make_random_fasta(wr: io::Writer, id: ~str, desc: ~str, genelist: ~[aminoacid
let rng = @{mut last: rand::Rng().next()};
let mut op: ~str = ~"";
for uint::range(0u, n as uint) |_i| {
str::push_char(op, select_random(myrandom_next(rng, 100u32),
genelist));
str::push_char(&mut op, select_random(myrandom_next(rng, 100u32),
genelist));
if str::len(op) >= LINE_LENGTH() {
wr.write_line(op);
op = ~"";
......@@ -59,7 +59,7 @@ fn make_repeat_fasta(wr: io::Writer, id: ~str, desc: ~str, s: ~str, n: int) unsa
let mut op: ~str = ~"";
let sl: uint = str::len(s);
for uint::range(0u, n as uint) |i| {
str::raw::push_byte(op, s[i % sl]);
str::raw::push_byte(&mut op, s[i % sl]);
if str::len(op) >= LINE_LENGTH() {
wr.write_line(op);
op = ~"";
......
......@@ -18,12 +18,12 @@ fn main() {
assert (!str::is_utf8(~[0xc0_u8, 0x10_u8]));
let mut stack = ~"a×c€";
assert (str::pop_char(stack) == '€');
assert (str::pop_char(stack) == 'c');
str::push_char(stack, 'u');
assert (str::pop_char(&mut stack) == '€');
assert (str::pop_char(&mut stack) == 'c');
str::push_char(&mut stack, 'u');
assert (stack == ~"a×u");
assert (str::shift_char(stack) == 'a');
assert (str::shift_char(stack) == '×');
str::unshift_char(stack, 'ß');
assert (str::shift_char(&mut stack) == 'a');
assert (str::shift_char(&mut stack) == '×');
str::unshift_char(&mut stack, 'ß');
assert (stack == ~"ßu");
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册