Skip to content

Making str allocate less #3: split_* #5555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
14 changes: 9 additions & 5 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn run_debuginfo_test(config: config, props: TestProps, testfile: &Path) {
// 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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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] {
Expand Down Expand Up @@ -596,8 +596,12 @@ fn split_maybe_args(argstr: Option<~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 => ~[]
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/libcore/num/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ impl_NumStrConv_Integer!(u16)
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
Expand Down Expand Up @@ -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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ pub fn env() -> ~[(~str,~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);
Expand Down
33 changes: 22 additions & 11 deletions src/libcore/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ impl ToStr for PosixPath {
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 }
Expand Down Expand Up @@ -504,9 +505,10 @@ impl GenericPath for PosixPath {
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,
Expand All @@ -515,7 +517,10 @@ impl GenericPath for 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 }
}
Expand Down Expand Up @@ -590,8 +595,10 @@ impl GenericPath for 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,
Expand Down Expand Up @@ -759,9 +766,10 @@ impl GenericPath for WindowsPath {
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
Expand All @@ -775,7 +783,10 @@ impl GenericPath for 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 }
}
Expand Down
4 changes: 3 additions & 1 deletion src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ impl RngUtil for @Rng {
*/
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
Expand Down
Loading