Skip to content

Commit

Permalink
auto merge of #7060 : huonw/rust/more-str, r=thestinger
Browse files Browse the repository at this point in the history
There are now only half-a-dozen or so functions left `std::str` that should be methods.

Highlights:
- `.substr` was removed, since most of the uses of it in the code base were actually incorrect (it had a weird mixing of a byte index and a unicode character count), adding `.slice_chars` if one wants to handle characters, and the normal `.slice` method to handle bytes.
- Code duplication between the two impls for `connect` and `concat` was removed via a new `Str` trait, that is purely designed to allow an explicit -> `&str` conversion (`.as_slice()`)
- Deconfuse the 5 different functions for converting to `[u8]` (3 of which had actually incorrect documentation: implying that they didn't have the null terminator), into 3: `as_bytes` (all strings), `as_bytes_with_null` (`&'static str`, `@str` and `~str`) and `as_bytes_with_null_consume` (`~str`). None of these allocate, unlike the old versions.

(cc @thestinger)
  • Loading branch information
bors committed Jun 12, 2013
2 parents 7033dfc + 9f0c85a commit cc80652
Show file tree
Hide file tree
Showing 80 changed files with 820 additions and 791 deletions.
2 changes: 1 addition & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ let new_favorite_crayon_name = favorite_crayon_name.trim();
if favorite_crayon_name.len() > 5 {
// Create a substring
println(favorite_crayon_name.substr(0, 5));
println(favorite_crayon_name.slice_chars(0, 5));
}
~~~

Expand Down
8 changes: 4 additions & 4 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
if props.pp_exact.is_some() {
// Now we have to care about line endings
let cr = ~"\r";
actual = str::replace(actual, cr, "");
expected = str::replace(expected, cr, "");
actual = actual.replace(cr, "");
expected = expected.replace(cr, "");
}

compare_source(expected, actual);
Expand Down Expand Up @@ -238,7 +238,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
// do not optimize debuginfo tests
let mut config = match config.rustcflags {
Some(ref flags) => config {
rustcflags: Some(str::replace(*flags, "-O", "")),
rustcflags: Some(flags.replace("-O", "")),
.. copy *config
},
None => copy *config
Expand All @@ -254,7 +254,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
}

// write debugger script
let script_str = str::append(cmds, "\nquit\n");
let script_str = cmds.append("\nquit\n");
debug!("script_str = %s", script_str);
dump_output_file(config, testfile, script_str, "debugger.script");

Expand Down
19 changes: 9 additions & 10 deletions src/libextra/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use core::prelude::*;

use core::str;
use core::vec;

/// A trait for converting a value to base64 encoding.
Expand Down Expand Up @@ -111,7 +110,7 @@ impl<'self> ToBase64 for &'self str {
*
*/
fn to_base64(&self) -> ~str {
str::to_bytes(*self).to_base64()
self.as_bytes().to_base64()
}
}

Expand Down Expand Up @@ -224,7 +223,7 @@ impl<'self> FromBase64 for &'self str {
* ~~~
*/
fn from_base64(&self) -> ~[u8] {
str::to_bytes(*self).from_base64()
self.as_bytes().from_base64()
}
}

Expand All @@ -245,12 +244,12 @@ mod tests {

#[test]
fn test_from_base64() {
assert_eq!("".from_base64(), str::to_bytes(""));
assert_eq!("Zg==".from_base64(), str::to_bytes("f"));
assert_eq!("Zm8=".from_base64(), str::to_bytes("fo"));
assert_eq!("Zm9v".from_base64(), str::to_bytes("foo"));
assert_eq!("Zm9vYg==".from_base64(), str::to_bytes("foob"));
assert_eq!("Zm9vYmE=".from_base64(), str::to_bytes("fooba"))
assert_eq!("Zm9vYmFy".from_base64(), str::to_bytes("foobar"));
assert_eq!("".from_base64(), "".as_bytes().to_owned());
assert_eq!("Zg==".from_base64(), "f".as_bytes().to_owned());
assert_eq!("Zm8=".from_base64(), "fo".as_bytes().to_owned());
assert_eq!("Zm9v".from_base64(), "foo".as_bytes().to_owned());
assert_eq!("Zm9vYg==".from_base64(), "foob".as_bytes().to_owned());
assert_eq!("Zm9vYmE=".from_base64(), "fooba".as_bytes().to_owned());
assert_eq!("Zm9vYmFy".from_base64(), "foobar".as_bytes().to_owned());
}
}
5 changes: 2 additions & 3 deletions src/libextra/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ pub mod writer {

use core::cast;
use core::io;
use core::str;

// ebml writing
pub struct Encoder {
Expand Down Expand Up @@ -725,7 +724,7 @@ pub mod writer {
}

pub fn wr_tagged_str(&mut self, tag_id: uint, v: &str) {
str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b));
self.wr_tagged_bytes(tag_id, v.as_bytes());
}

pub fn wr_bytes(&mut self, b: &[u8]) {
Expand All @@ -735,7 +734,7 @@ pub mod writer {

pub fn wr_str(&mut self, s: &str) {
debug!("Write str: %?", s);
self.writer.write(str::to_bytes(s));
self.writer.write(s.as_bytes());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libextra/fileinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ mod test {
let mut buf : ~[u8] = vec::from_elem(6, 0u8);
let count = fi.read(buf, 10);
assert_eq!(count, 6);
assert_eq!(buf, "0\n1\n2\n".to_bytes());
assert_eq!(buf, "0\n1\n2\n".as_bytes().to_owned());
assert!(fi.eof())
assert_eq!(fi.state().line_num, 3);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/flatpipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub mod flatteners {
T: Decodable<D>>(
buf: &[u8])
-> T {
let buf = vec::to_owned(buf);
let buf = buf.to_owned();
let buf_reader = @BufReader::new(buf);
let reader = buf_reader as @Reader;
let mut deser: D = FromReader::from_reader(reader);
Expand Down
50 changes: 25 additions & 25 deletions src/libextra/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
}
i += 1;
}
return Ok(Matches {opts: vec::to_owned(opts),
return Ok(Matches {opts: opts.to_owned(),
vals: vals,
free: free});
}
Expand Down Expand Up @@ -447,7 +447,7 @@ pub fn opt_default(mm: &Matches, nm: &str, def: &str) -> Option<~str> {
let vals = opt_vals(mm, nm);
if vals.is_empty() { return None::<~str>; }
return match vals[0] { Val(ref s) => Some::<~str>(copy *s),
_ => Some::<~str>(str::to_owned(def)) }
_ => Some::<~str>(def.to_owned()) }
}

#[deriving(Eq)]
Expand Down Expand Up @@ -487,10 +487,10 @@ pub mod groups {
desc: &str, hint: &str) -> OptGroup {
let len = short_name.len();
assert!(len == 1 || len == 0);
return OptGroup { short_name: str::to_owned(short_name),
long_name: str::to_owned(long_name),
hint: str::to_owned(hint),
desc: str::to_owned(desc),
return OptGroup { short_name: short_name.to_owned(),
long_name: long_name.to_owned(),
hint: hint.to_owned(),
desc: desc.to_owned(),
hasarg: Yes,
occur: Req};
}
Expand All @@ -500,10 +500,10 @@ pub mod groups {
desc: &str, hint: &str) -> OptGroup {
let len = short_name.len();
assert!(len == 1 || len == 0);
return OptGroup {short_name: str::to_owned(short_name),
long_name: str::to_owned(long_name),
hint: str::to_owned(hint),
desc: str::to_owned(desc),
return OptGroup {short_name: short_name.to_owned(),
long_name: long_name.to_owned(),
hint: hint.to_owned(),
desc: desc.to_owned(),
hasarg: Yes,
occur: Optional};
}
Expand All @@ -513,10 +513,10 @@ pub mod groups {
desc: &str) -> OptGroup {
let len = short_name.len();
assert!(len == 1 || len == 0);
return OptGroup {short_name: str::to_owned(short_name),
long_name: str::to_owned(long_name),
return OptGroup {short_name: short_name.to_owned(),
long_name: long_name.to_owned(),
hint: ~"",
desc: str::to_owned(desc),
desc: desc.to_owned(),
hasarg: No,
occur: Optional};
}
Expand All @@ -526,10 +526,10 @@ pub mod groups {
desc: &str, hint: &str) -> OptGroup {
let len = short_name.len();
assert!(len == 1 || len == 0);
return OptGroup {short_name: str::to_owned(short_name),
long_name: str::to_owned(long_name),
hint: str::to_owned(hint),
desc: str::to_owned(desc),
return OptGroup {short_name: short_name.to_owned(),
long_name: long_name.to_owned(),
hint: hint.to_owned(),
desc: desc.to_owned(),
hasarg: Maybe,
occur: Optional};
}
Expand All @@ -542,10 +542,10 @@ pub mod groups {
desc: &str, hint: &str) -> OptGroup {
let len = short_name.len();
assert!(len == 1 || len == 0);
return OptGroup {short_name: str::to_owned(short_name),
long_name: str::to_owned(long_name),
hint: str::to_owned(hint),
desc: str::to_owned(desc),
return OptGroup {short_name: short_name.to_owned(),
long_name: long_name.to_owned(),
hint: hint.to_owned(),
desc: desc.to_owned(),
hasarg: Yes,
occur: Multi};
}
Expand Down Expand Up @@ -593,7 +593,7 @@ pub mod groups {
*/
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {

let desc_sep = ~"\n" + str::repeat(" ", 24);
let desc_sep = ~"\n" + " ".repeat(24);

let rows = vec::map(opts, |optref| {
let OptGroup{short_name: short_name,
Expand All @@ -603,7 +603,7 @@ pub mod groups {
hasarg: hasarg,
_} = copy *optref;

let mut row = str::repeat(" ", 4);
let mut row = " ".repeat(4);

// short option
row += match short_name.len() {
Expand All @@ -629,7 +629,7 @@ pub mod groups {
// here we just need to indent the start of the description
let rowlen = row.len();
row += if rowlen < 24 {
str::repeat(" ", 24 - rowlen)
" ".repeat(24 - rowlen)
} else {
copy desc_sep
};
Expand All @@ -654,7 +654,7 @@ pub mod groups {
row
});

return str::to_owned(brief) +
return brief.to_owned() +
"\n\nOptions:\n" +
rows.connect("\n") +
"\n\n";
Expand Down
5 changes: 2 additions & 3 deletions src/libextra/md4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use core::prelude::*;

use core::str;
use core::uint;
use core::vec;

Expand All @@ -30,7 +29,7 @@ pub fn md4(msg: &[u8]) -> Quad {
let orig_len: u64 = (msg.len() * 8u) as u64;

// pad message
let mut msg = vec::append(vec::to_owned(msg), [0x80u8]);
let mut msg = vec::append(msg.to_owned(), [0x80u8]);
let mut bitlen = orig_len + 8u64;
while (bitlen + 64u64) % 512u64 > 0u64 {
msg.push(0u8);
Expand Down Expand Up @@ -129,7 +128,7 @@ pub fn md4_str(msg: &[u8]) -> ~str {

/// Calculates the md4 hash of a string, returning the hex-encoded version of
/// the hash
pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
pub fn md4_text(msg: &str) -> ~str { md4_str(msg.as_bytes()) }

#[test]
fn test_md4() {
Expand Down
20 changes: 9 additions & 11 deletions src/libextra/net_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ mod test {
assert_eq!(net::ip::get_port(&sock.get_peer_addr()), 8887);

// Fulfill the protocol the test server expects
let resp_bytes = str::to_bytes("ping");
let resp_bytes = "ping".as_bytes().to_owned();
tcp_write_single(&sock, resp_bytes);
debug!("message sent");
sock.read(0u);
Expand Down Expand Up @@ -1756,9 +1756,7 @@ mod test {
buf_write(sock_buf, expected_req);

// so contrived!
let actual_resp = do str::as_bytes(&expected_resp.to_str()) |resp_buf| {
buf_read(sock_buf, resp_buf.len())
};
let actual_resp = buf_read(sock_buf, expected_resp.as_bytes().len());

let actual_req = server_result_po.recv();
debug!("REQ: expected: '%s' actual: '%s'",
Expand Down Expand Up @@ -1810,11 +1808,10 @@ mod test {

fn buf_write<W:io::Writer>(w: &W, val: &str) {
debug!("BUF_WRITE: val len %?", val.len());
do str::byte_slice(val) |b_slice| {
debug!("BUF_WRITE: b_slice len %?",
b_slice.len());
w.write(b_slice)
}
let b_slice = val.as_bytes();
debug!("BUF_WRITE: b_slice len %?",
b_slice.len());
w.write(b_slice)
}

fn buf_read<R:io::Reader>(r: &R, len: uint) -> ~str {
Expand Down Expand Up @@ -1877,7 +1874,8 @@ mod test {
server_ch.send(
str::from_bytes(data));
debug!("SERVER: before write");
tcp_write_single(&sock, str::to_bytes(resp_cell2.take()));
let s = resp_cell2.take();
tcp_write_single(&sock, s.as_bytes().to_owned());
debug!("SERVER: after write.. die");
kill_ch.send(None);
}
Expand Down Expand Up @@ -1949,7 +1947,7 @@ mod test {
}
else {
let sock = result::unwrap(connect_result);
let resp_bytes = str::to_bytes(resp);
let resp_bytes = resp.as_bytes().to_owned();
tcp_write_single(&sock, resp_bytes);
let read_result = sock.read(0u);
if read_result.is_err() {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/net_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ mod tests {
/*
assert_eq!(decode_form_urlencoded([]).len(), 0);
let s = str::to_bytes("a=1&foo+bar=abc&foo+bar=12+%3D+34");
let s = "a=1&foo+bar=abc&foo+bar=12+%3D+34".as_bytes();
let form = decode_form_urlencoded(s);
assert_eq!(form.len(), 2);
assert_eq!(form.get_ref(&~"a"), &~[~"1"]);
Expand Down
8 changes: 4 additions & 4 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ impl ToStrRadix for BigUint {
let s = uint::to_str_radix(*n as uint, radix);
str::from_chars(vec::from_elem(l - s.len(), '0')) + s
}).concat();
s.trim_left_chars(['0']).to_owned()
s.trim_left_chars(&'0').to_owned()
}
}
}
Expand All @@ -534,7 +534,7 @@ impl FromStrRadix for BigUint {

pub fn from_str_radix(s: &str, radix: uint)
-> Option<BigUint> {
BigUint::parse_bytes(str::to_bytes(s), radix)
BigUint::parse_bytes(s.as_bytes(), radix)
}
}

Expand Down Expand Up @@ -564,7 +564,7 @@ impl BigUint {
/// Creates and initializes an BigUint.

pub fn from_slice(slice: &[BigDigit]) -> BigUint {
return BigUint::new(vec::to_owned(slice));
return BigUint::new(slice.to_owned());
}

/// Creates and initializes an BigUint.
Expand Down Expand Up @@ -1090,7 +1090,7 @@ impl FromStrRadix for BigInt {
fn from_str_radix(s: &str, radix: uint)
-> Option<BigInt> {
BigInt::parse_bytes(str::to_bytes(s), radix)
BigInt::parse_bytes(s.as_bytes(), radix)
}
}
Expand Down
Loading

0 comments on commit cc80652

Please sign in to comment.