From 1720d9f663c07422b91cbb5fc857b625d120155c Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Wed, 29 May 2013 20:10:16 +0200 Subject: Remove a bunch of unnecessary allocations and copies --- src/libextra/getopts.rs | 23 ++++++++++++----------- src/libextra/net_ip.rs | 5 ++--- src/libextra/net_url.rs | 7 +++---- src/libextra/sha1.rs | 3 +-- src/libextra/time.rs | 6 +++--- src/libextra/workcache.rs | 2 +- 6 files changed, 22 insertions(+), 24 deletions(-) (limited to 'src/libextra') diff --git a/src/libextra/getopts.rs b/src/libextra/getopts.rs index 294b8fec042..678d795d8f8 100644 --- a/src/libextra/getopts.rs +++ b/src/libextra/getopts.rs @@ -110,10 +110,11 @@ pub struct Opt { } fn mkname(nm: &str) -> Name { - let unm = str::to_owned(nm); - return if nm.len() == 1u { - Short(str::char_at(unm, 0u)) - } else { Long(unm) }; + if nm.len() == 1u { + Short(str::char_at(nm, 0u)) + } else { + Long(nm.to_owned()) + } } /// Create an option that is required and takes an argument @@ -195,19 +196,19 @@ pub enum Fail_ { pub fn fail_str(f: Fail_) -> ~str { return match f { ArgumentMissing(ref nm) => { - ~"Argument to option '" + *nm + "' missing." + fmt!("Argument to option '%s' missing.", *nm) } UnrecognizedOption(ref nm) => { - ~"Unrecognized option: '" + *nm + "'." + fmt!("Unrecognized option: '%s'.", *nm) } OptionMissing(ref nm) => { - ~"Required option '" + *nm + "' missing." + fmt!("Required option '%s' missing.", *nm) } OptionDuplicated(ref nm) => { - ~"Option '" + *nm + "' given more than once." + fmt!("Option '%s' given more than once.", *nm) } UnexpectedArgument(ref nm) => { - ~"Option " + *nm + " does not take an argument." + fmt!("Option '%s' does not take an argument.", *nm) } }; } @@ -245,11 +246,11 @@ pub fn getopts(args: &[~str], opts: &[Opt]) -> Result { let mut names; let mut i_arg = None; if cur[1] == '-' as u8 { - let tail = str::slice(cur, 2, curlen).to_owned(); + let tail = str::slice(cur, 2, curlen); let mut tail_eq = ~[]; for str::each_splitn_char(tail, '=', 1) |s| { tail_eq.push(s.to_owned()) } if tail_eq.len() <= 1 { - names = ~[Long(tail)]; + names = ~[Long(tail.to_owned())]; } else { names = ~[Long(copy tail_eq[0])]; diff --git a/src/libextra/net_ip.rs b/src/libextra/net_ip.rs index e92523726df..160d06ec20d 100644 --- a/src/libextra/net_ip.rs +++ b/src/libextra/net_ip.rs @@ -230,7 +230,7 @@ pub mod v4 { let input_is_inaddr_none = result::get(&ip_rep_result).as_u32() == INADDR_NONE; - let new_addr = uv_ip4_addr(str::to_owned(ip), 22); + let new_addr = uv_ip4_addr(ip, 22); let reformatted_name = uv_ip4_name(&new_addr); debug!("try_parse_addr: input ip: %s reparsed ip: %s", ip, reformatted_name); @@ -259,7 +259,6 @@ pub mod v6 { use uv_ip6_name = uv::ll::ip6_name; use core::result; - use core::str; /** * Convert a str to `ip_addr` @@ -285,7 +284,7 @@ pub mod v6 { pub fn try_parse_addr(ip: &str) -> result::Result { unsafe { // need to figure out how to establish a parse failure.. - let new_addr = uv_ip6_addr(str::to_owned(ip), 22); + let new_addr = uv_ip6_addr(ip, 22); let reparsed_name = uv_ip6_name(&new_addr); debug!("v6::try_parse_addr ip: '%s' reparsed '%s'", ip, reparsed_name); diff --git a/src/libextra/net_url.rs b/src/libextra/net_url.rs index fa7295923a0..80957a8c8ef 100644 --- a/src/libextra/net_url.rs +++ b/src/libextra/net_url.rs @@ -585,7 +585,7 @@ fn get_path(rawurl: &str, authority: bool) -> } } - return Ok((decode_component(str::slice(rawurl, 0, end).to_owned()), + return Ok((decode_component(str::slice(rawurl, 0, end)), str::slice(rawurl, end, len).to_owned())); } @@ -596,14 +596,13 @@ fn get_query_fragment(rawurl: &str) -> if str::starts_with(rawurl, "#") { let f = decode_component(str::slice(rawurl, 1, - str::len(rawurl)).to_owned()); + str::len(rawurl))); return Ok((~[], Some(f))); } else { return Ok((~[], None)); } } - let (q, r) = split_char_first(str::slice(rawurl, 1, - str::len(rawurl)).to_owned(), '#'); + let (q, r) = split_char_first(str::slice(rawurl, 1, rawurl.len()), '#'); let f = if str::len(r) != 0 { Some(decode_component(r)) } else { None }; return Ok((query_from_str(q), f)); diff --git a/src/libextra/sha1.rs b/src/libextra/sha1.rs index e970d34ff91..80b4ab02e5f 100644 --- a/src/libextra/sha1.rs +++ b/src/libextra/sha1.rs @@ -399,8 +399,7 @@ mod tests { let mut left = len; while left > 0u { let take = (left + 1u) / 2u; - sh.input_str(str::slice(t.input, len - left, - take + len - left).to_owned()); + sh.input_str(t.input.slice(len - left, take + len - left)); left = left - take; } let out = sh.result(); diff --git a/src/libextra/time.rs b/src/libextra/time.rs index 8603d0f814a..e3289d01750 100644 --- a/src/libextra/time.rs +++ b/src/libextra/time.rs @@ -293,7 +293,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result { let mut i = 0u; while i < digits { - let range = str::char_range_at(str::to_owned(ss), pos); + let range = str::char_range_at(ss, pos); pos = range.next; match range.ch { @@ -632,7 +632,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result { } } - do io::with_str_reader(str::to_owned(format)) |rdr| { + do io::with_str_reader(format) |rdr| { let mut tm = Tm { tm_sec: 0_i32, tm_min: 0_i32, @@ -844,7 +844,7 @@ priv fn do_strftime(format: &str, tm: &Tm) -> ~str { let mut buf = ~""; - do io::with_str_reader(str::to_owned(format)) |rdr| { + do io::with_str_reader(format) |rdr| { while !rdr.eof() { match rdr.read_char() { '%' => buf += parse_type(rdr.read_char(), tm), diff --git a/src/libextra/workcache.rs b/src/libextra/workcache.rs index 19913fb92f4..47a717a56a5 100644 --- a/src/libextra/workcache.rs +++ b/src/libextra/workcache.rs @@ -201,7 +201,7 @@ struct Logger { pub impl Logger { fn info(&self, i: &str) { - io::println(~"workcache: " + i.to_owned()); + io::println(~"workcache: " + i); } } -- cgit 1.4.1-3-g733a5