auto merge of #5555 : Kimundi/rust/str-dealloc-3, r=catamorphism

- Most functions that used to return `~[~str]` for a list of substrings got turned into iterators over `&str` slices
- Some cleanup of apis, docs and code layout
This commit is contained in:
bors 2013-03-26 15:07:07 -07:00
commit 3d588c5286
25 changed files with 469 additions and 417 deletions

View File

@ -142,7 +142,8 @@ fn parse_check_line(line: ~str) -> Option<~str> {
fn parse_exec_env(line: ~str) -> Option<(~str, ~str)> {
do parse_name_value_directive(line, ~"exec-env").map |nv| {
// nv is either FOO or FOO=BAR
let strs = str::splitn_char(*nv, '=', 1u);
let mut strs = ~[];
for str::each_splitn_char(*nv, '=', 1u) |s| { strs.push(s.to_owned()); }
match strs.len() {
1u => (strs[0], ~""),
2u => (strs[0], strs[1]),

View File

@ -267,7 +267,7 @@ fn debugger() -> ~str { ~"gdb" }
// check if each line in props.check_lines appears in the
// output (in order)
let mut i = 0u;
for str::lines_each(ProcRes.stdout) |line| {
for str::each_line(ProcRes.stdout) |line| {
if props.check_lines[i].trim() == line.trim() {
i += 1u;
}
@ -297,7 +297,7 @@ fn check_error_patterns(props: TestProps,
let mut next_err_idx = 0u;
let mut next_err_pat = props.error_patterns[next_err_idx];
let mut done = false;
for str::lines_each(ProcRes.stderr) |line| {
for str::each_line(ProcRes.stderr) |line| {
if str::contains(line, next_err_pat) {
debug!("found error pattern %s", next_err_pat);
next_err_idx += 1u;
@ -347,7 +347,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
// filename:line1:col1: line2:col2: *warning:* msg
// where line1:col1: is the starting point, line2:col2:
// is the ending point, and * represents ANSI color codes.
for str::lines_each(ProcRes.stderr) |line| {
for str::each_line(ProcRes.stderr) |line| {
let mut was_expected = false;
for vec::eachi(expected_errors) |i, ee| {
if !found_flags[i] {
@ -596,8 +596,12 @@ fn rm_whitespace(v: ~[~str]) -> ~[~str] {
}
match argstr {
Some(s) => rm_whitespace(str::split_char(s, ' ')),
None => ~[]
Some(s) => {
let mut ss = ~[];
for str::each_split_char(s, ' ') |s| { ss.push(s.to_owned()) }
rm_whitespace(ss)
}
None => ~[]
}
}

View File

@ -130,6 +130,13 @@ impl NumStrConv for $t {
impl_NumStrConv_Integer!(u32)
impl_NumStrConv_Integer!(u64)
// Special value strings as [u8] consts.
static inf_buf: [u8*3] = ['i' as u8, 'n' as u8, 'f' as u8];
static positive_inf_buf: [u8*4] = ['+' as u8, 'i' as u8, 'n' as u8, 'f' as u8];
static negative_inf_buf: [u8*4] = ['-' as u8, 'i' as u8, 'n' as u8, 'f' as u8];
static nan_buf: [u8*3] = ['N' as u8, 'a' as u8, 'N' as u8];
/**
* Converts a number to its string representation as a byte vector.
* This is meant to be a common base implementation for all numeric string
@ -479,15 +486,15 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Ord+Copy+Div<T,T>+
}
if special {
if buf == str::inf_buf || buf == str::positive_inf_buf {
if buf == inf_buf || buf == positive_inf_buf {
return NumStrConv::inf();
} else if buf == str::negative_inf_buf {
} else if buf == negative_inf_buf {
if negative {
return NumStrConv::neg_inf();
} else {
return None;
}
} else if buf == str::nan_buf {
} else if buf == nan_buf {
return NumStrConv::NaN();
}
}

View File

@ -218,7 +218,8 @@ unsafe fn get_env_pairs() -> ~[~str] {
fn env_convert(input: ~[~str]) -> ~[(~str, ~str)] {
let mut pairs = ~[];
for input.each |p| {
let vs = str::splitn_char(*p, '=', 1);
let mut vs = ~[];
for str::each_splitn_char(*p, '=', 1) |s| { vs.push(s.to_owned()) }
debug!("splitting: len: %u",
vs.len());
fail_unless!(vs.len() == 2);

View File

@ -381,7 +381,8 @@ fn to_str(&self) -> ~str {
impl GenericPath for PosixPath {
fn from_str(s: &str) -> PosixPath {
let mut components = str::split_nonempty(s, |c| c == '/');
let mut components = ~[];
for str::each_split_nonempty(s, |c| c == '/') |s| { components.push(s.to_owned()) }
let is_absolute = (s.len() != 0 && s[0] == '/' as u8);
return PosixPath { is_absolute: is_absolute,
components: components }
@ -504,9 +505,10 @@ fn is_restricted(&self) -> bool {
fn push_many(&self, cs: &[~str]) -> PosixPath {
let mut v = copy self.components;
for cs.each |e| {
let mut ss = str::split_nonempty(
*e,
|c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
}
PosixPath { is_absolute: self.is_absolute,
@ -515,7 +517,10 @@ fn push_many(&self, cs: &[~str]) -> PosixPath {
fn push(&self, s: &str) -> PosixPath {
let mut v = copy self.components;
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
PosixPath { components: v, ..copy *self }
}
@ -590,8 +595,10 @@ fn from_str(s: &str) -> WindowsPath {
}
}
let mut components =
str::split_nonempty(rest, |c| windows::is_sep(c as u8));
let mut components = ~[];
for str::each_split_nonempty(rest, |c| windows::is_sep(c as u8)) |s| {
components.push(s.to_owned())
}
let is_absolute = (rest.len() != 0 && windows::is_sep(rest[0]));
return WindowsPath { host: host,
device: device,
@ -759,9 +766,10 @@ fn is_restricted(&self) -> bool {
fn push_many(&self, cs: &[~str]) -> WindowsPath {
let mut v = copy self.components;
for cs.each |e| {
let mut ss = str::split_nonempty(
*e,
|c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
}
// tedious, but as-is, we can't use ..self
@ -775,7 +783,10 @@ fn push_many(&self, cs: &[~str]) -> WindowsPath {
fn push(&self, s: &str) -> WindowsPath {
let mut v = copy self.components;
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
return WindowsPath { components: v, ..copy *self }
}

View File

@ -327,7 +327,9 @@ fn gen_char(&self) -> char {
*/
fn gen_char_from(&self, chars: &str) -> char {
fail_unless!(!chars.is_empty());
self.choose(str::chars(chars))
let mut cs = ~[];
for str::each_char(chars) |c| { cs.push(c) }
self.choose(cs)
}
/// Return a random bool

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
// except according to those terms.
// rust - central access to other rust tools
// FIXME #2238 Make commands run and test emit proper file endings on winds
// FIXME #2238 Make commands run and test emit proper file endings on windows
// FIXME #2238 Make run only accept source that emits an executable
#[link(name = "rust",
@ -29,10 +29,12 @@ enum ValidUsage {
}
impl ValidUsage {
fn is_valid(&self) -> bool { match *self {
Valid => true,
Invalid => false
}}
fn is_valid(&self) -> bool {
match *self {
Valid => true,
Invalid => false
}
}
}
enum Action {
@ -128,7 +130,9 @@ fn cmd_help(args: &[~str]) -> ValidUsage {
match command.usage_full {
UsgStr(msg) => io::println(fmt!("%s\n", msg)),
UsgExec(commandline) => {
let words = str::words(commandline);
let mut words = ~[];
for str::each_word(commandline) |word| { words.push(word.to_owned()) }
let words = words;
let (prog, args) = (words.head(), words.tail());
run::run_program(*prog, args);
}
@ -184,7 +188,9 @@ fn do_command(command: &Command, args: &[~str]) -> ValidUsage {
match command.action {
Call(f) => f(args),
Exec(commandline) => {
let words = str::words(commandline);
let mut words = ~[];
for str::each_word(commandline) |word| { words.push(word.to_owned()) }
let words = words;
let (prog, prog_args) = (words.head(), words.tail());
let exitstatus = run::run_program(
*prog,

View File

@ -120,7 +120,9 @@ pub fn get_used_libraries(cstore: @mut CStore) -> ~[~str] {
}
pub fn add_used_link_args(cstore: @mut CStore, args: &str) {
cstore.used_link_args.push_all(args.split_char(' '));
for args.each_split_char(' ') |s| {
cstore.used_link_args.push(s.to_owned());
}
}
pub fn get_used_link_args(cstore: @mut CStore) -> ~[~str] {

View File

@ -76,7 +76,7 @@
use syntax::opt_vec::OptVec;
use core::option::{Some, get, is_some, is_none};
use core::str::{connect, split_str};
use core::str::{connect, each_split_str};
use core::hashmap::linear::LinearMap;
use std::oldmap::HashMap;
@ -1696,7 +1696,8 @@ fn build_reduced_graph_for_external_crate(@mut self, root: @mut Module) {
entry: %s (%?)",
path_string, def_like);
let mut pieces = split_str(path_string, ~"::");
let mut pieces = ~[];
for each_split_str(path_string, "::") |s| { pieces.push(s.to_owned()) }
let final_ident_str = pieces.pop();
let final_ident = self.session.ident_of(final_ident_str);

View File

@ -183,7 +183,8 @@ fn first_sentence_(s: &str) -> ~str {
}
fn paragraphs(s: &str) -> ~[~str] {
let lines = str::lines_any(s);
let mut lines = ~[];
for str::each_line_any(s) |line| { lines.push(line.to_owned()); }
let mut whitespace_lines = 0;
let mut accum = ~"";
let paras = do vec::foldl(~[], lines) |paras, line| {

View File

@ -551,9 +551,11 @@ fn write_sig(ctxt: &Ctxt, sig: Option<~str>) {
}
fn code_block_indent(s: ~str) -> ~str {
let lines = str::lines_any(s);
let indented = vec::map(lines, |line| fmt!(" %s", *line) );
str::connect(indented, ~"\n")
let mut indented = ~[];
for str::each_line_any(s) |line| {
indented.push(fmt!(" %s", line));
}
str::connect(indented, "\n")
}
#[test]

View File

@ -104,8 +104,8 @@ fn sectionalize(desc: Option<~str>) -> (Option<~str>, ~[doc::Section]) {
if desc.is_none() {
return (None, ~[]);
}
let lines = str::lines((copy desc).get());
let mut lines = ~[];
for str::each_line_any(*desc.get_ref()) |line| { lines.push(line.to_owned()); }
let mut new_desc = None::<~str>;
let mut current_section = None;

View File

@ -33,7 +33,8 @@ pub fn mk_pass() -> Pass {
}
fn unindent(s: &str) -> ~str {
let lines = str::lines_any(s);
let mut lines = ~[];
for str::each_line_any(s) |line| { lines.push(line.to_owned()); }
let mut saw_first_line = false;
let mut saw_second_line = false;
let min_indent = do vec::foldl(uint::max_value, lines)

View File

@ -337,7 +337,8 @@ fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str)
-> Option<Repl> {
if line.starts_with(~":") {
let full = line.substr(1, line.len() - 1);
let split = str::words(full);
let mut split = ~[];
for str::each_word(full) |word| { split.push(word.to_owned()) }
let len = split.len();
if len > 0 {

View File

@ -270,14 +270,11 @@ impl Ctx {
fn sep_name_vers(in: ~str) -> (Option<~str>, Option<~str>) {
let mut name = None;
let mut vers = None;
let parts = str::split_char(in, '@');
if parts.len() >= 1 {
name = Some(parts[0]);
if parts.len() >= 2 {
vers = Some(parts[1]);
}
for str::each_split_char(in, '@') |s| {
if name.is_none() { name = Some(s.to_owned()); }
else if vers.is_none() { vers = Some(s.to_owned()); }
else { break; }
}
(name, vers)
@ -733,8 +730,12 @@ impl Ctx {
for package.bins.each |&bin| {
let path = Path(bin);
let name = str::split_char(path.file_path().to_str(), '-')[0];
let out = bin_dir.push(name);
let mut name = None;
for str::each_split_char(path.file_path().to_str(), '-') |s| {
name = Some(s.to_owned());
break;
}
let out = bin_dir.push(name.unwrap());
util::link_exe(&path, &out);
util::note(fmt!("linked %s", out.to_str()));
@ -847,8 +848,12 @@ impl Ctx {
for package.bins.each |&bin| {
let path = Path(bin);
let name = str::split_char(path.file_path().to_str(), '-')[0];
let out = bin_dir.push(name);
let mut name = None;
for str::each_split_char(path.file_path().to_str(), '-') |s| {
name = Some(s.to_owned());
break;
}
let out = bin_dir.push(name.unwrap());
if os::path_exists(&out) {
if os::remove_file(&out) {

View File

@ -44,10 +44,10 @@ pub fn is_cmd(cmd: ~str) -> bool {
}
pub fn parse_name(id: ~str) -> result::Result<~str, ~str> {
let parts = str::split_char(id, '.');
let mut last_part = None;
for parts.each |&part| {
for str::chars(part).each |&char| {
for str::each_split_char(id, '.') |part| {
for str::each_char(part) |char| {
if char::is_whitespace(char) {
return result::Err(
~"could not parse id: contains whitespace");
@ -56,9 +56,11 @@ pub fn parse_name(id: ~str) -> result::Result<~str, ~str> {
~"could not parse id: should be all lowercase");
}
}
last_part = Some(part.to_owned());
}
if last_part.is_none() { return result::Err(~"could not parse id: is empty"); }
result::Ok(copy *parts.last())
result::Ok(last_part.unwrap())
}
struct ListenerFn {

View File

@ -16,12 +16,16 @@ pub trait ToBase64 {
fn to_base64(&self) -> ~str;
}
static CHARS: [char * 64] = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
];
impl ToBase64 for &'self [u8] {
fn to_base64(&self) -> ~str {
let chars = str::chars(
~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
);
let mut s = ~"";
unsafe {
let len = self.len();
@ -35,10 +39,10 @@ fn to_base64(&self) -> ~str {
(self[i + 2u] as uint);
// This 24-bit number gets separated into four 6-bit numbers.
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]);
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;
}
@ -49,17 +53,17 @@ fn to_base64(&self) -> ~str {
0 => (),
1 => {
let n = (self[i] as uint) << 16u;
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 >> 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(&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 >> 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")

View File

@ -244,7 +244,8 @@ pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
let mut i_arg = None;
if cur[1] == '-' as u8 {
let tail = str::slice(cur, 2, curlen).to_owned();
let tail_eq = str::splitn_char(tail, '=', 1);
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)];
} else {
@ -601,7 +602,7 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
row += match short_name.len() {
0 => ~"",
1 => ~"-" + short_name + " ",
_ => fail!(~"the short name should only be 1 char long"),
_ => fail!(~"the short name should only be 1 ascii char long"),
};
// long option
@ -617,6 +618,7 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
Maybe => ~"[" + hint + ~"]",
};
// FIXME: #5516
// here we just need to indent the start of the description
let rowlen = row.len();
row += if rowlen < 24 {
@ -625,8 +627,22 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
desc_sep
};
// Normalize desc to contain words seperated by one space character
let mut desc_normalized_whitespace = ~"";
for str::each_word(desc) |word| {
desc_normalized_whitespace.push_str(word);
desc_normalized_whitespace.push_char(' ');
}
// FIXME: #5516
let mut desc_rows = ~[];
for str::each_split_within(desc_normalized_whitespace, 54) |substr| {
desc_rows.push(substr.to_owned());
}
// FIXME: #5516
// wrapped description
row += str::connect(str::split_within(desc, 54), desc_sep);
row += str::connect(desc_rows, desc_sep);
row
});

View File

@ -806,7 +806,8 @@ fn read_float(&self) -> float {
}
fn read_char(&self) -> char {
let v = str::chars(self.read_owned_str());
let mut v = ~[];
for str::each_char(self.read_owned_str()) |c| { v.push(c) }
if v.len() != 1 { fail!(~"string must have one character") }
v[0]
}

View File

@ -197,7 +197,9 @@ unsafe fn as_u32(&self) -> u32 {
}
}
pub fn parse_to_ipv4_rep(ip: &str) -> result::Result<Ipv4Rep, ~str> {
let parts = vec::map(str::split_char(ip, '.'), |s| {
let mut parts = ~[];
for str::each_split_char(ip, '.') |s| { parts.push(s.to_owned()) }
let parts = vec::map(parts, |s| {
match uint::from_str(*s) {
Some(n) if n <= 255 => n,
_ => 256

View File

@ -344,8 +344,8 @@ fn userinfo_to_str(userinfo: &UserInfo) -> ~str {
fn query_from_str(rawquery: &str) -> Query {
let mut query: Query = ~[];
if str::len(rawquery) != 0 {
for str::split_char(rawquery, '&').each |p| {
let (k, v) = split_char_first(*p, '=');
for str::each_split_char(rawquery, '&') |p| {
let (k, v) = split_char_first(p, '=');
// FIXME(#3722): unsafe only because decode_inner does (string) IO
unsafe {query.push((decode_component(k), decode_component(v)));}
};

View File

@ -99,7 +99,8 @@ fn block_trim(lines: ~[~str], chars: ~str, max: Option<uint>) -> ~[~str] {
}
return do lines.map |line| {
let chars = str::chars(*line);
let mut chars = ~[];
for str::each_char(*line) |c| { chars.push(c) }
if i > chars.len() {
~""
} else {
@ -116,7 +117,10 @@ fn block_trim(lines: ~[~str], chars: ~str, max: Option<uint>) -> ~[~str] {
}
if comment.starts_with(~"/*") {
let lines = str::lines_any(comment.slice(3u, comment.len() - 2u).to_owned());
let mut lines = ~[];
for str::each_line_any(comment.slice(3u, comment.len() - 2u)) |line| {
lines.push(line.to_owned())
}
let lines = vertical_trim(lines);
let lines = block_trim(lines, ~"\t ", None);
let lines = block_trim(lines, ~"*", Some(1u));

View File

@ -68,7 +68,8 @@ pub fn read(reader: @io::Reader) -> Sudoku {
let mut g = vec::from_fn(10u, { |_i| ~[0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8] });
while !reader.eof() {
let line = reader.read_line();
let comps = str::split_char(line.trim(), ',');
let mut comps = ~[];
for str::each_split_char(line.trim(), ',') |s| { comps.push(s.to_owned()) }
if vec::len(comps) == 3u {
let row = uint::from_str(comps[0]).get() as u8;
let col = uint::from_str(comps[1]).get() as u8;

View File

@ -17,8 +17,8 @@ pub fn main() {
fail_unless!((str::len(s) == 10u));
fail_unless!((str::char_len(s) == 4u));
fail_unless!((vec::len(str::chars(s)) == 4u));
fail_unless!((str::from_chars(str::chars(s)) == s));
fail_unless!((vec::len(str::to_chars(s)) == 4u));
fail_unless!((str::from_chars(str::to_chars(s)) == s));
fail_unless!((str::char_at(s, 0u) == 'e'));
fail_unless!((str::char_at(s, 1u) == 'é'));