Skip to content

Commit

Permalink
auto merge of rust-lang#7032 : huonw/rust/each-fn-kill, r=thestinger
Browse files Browse the repository at this point in the history
Continuation of rust-lang#7015, and rust-lang#6995.

Rewrites the character-based `each_split` functions in `str` to use an iterator, removes a few redundant methods, and replaces all uses of `len`, `is_empty` and `slice` functions with the methods (and deletes the the functions).

Update: Ok, this has turned into a major makeover for `str`, turning a lot of functions into methods, and removing redundant ones. Each commit is essentially a single such change.

(Unscientific benchmarks suggest that the external `split_iter` is approximately 10% faster than the internal one. I'm not quite sure why this would be true.)

(@thestinger is probably interested in this.)
  • Loading branch information
bors committed Jun 10, 2013
2 parents 2ff6b29 + e8782ee commit 1310212
Show file tree
Hide file tree
Showing 96 changed files with 1,533 additions and 2,356 deletions.
11 changes: 3 additions & 8 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -803,19 +803,14 @@ An example of `use` declarations:

~~~~
use std::float::sin;
use std::str::{slice, contains};
use std::option::Some;
use std::option::{Some, None};
fn main() {
// Equivalent to 'info!(std::float::sin(1.0));'
info!(sin(1.0));
// Equivalent to 'info!(std::option::Some(1.0));'
info!(Some(1.0));
// Equivalent to
// 'info!(std::str::contains(std::str::slice("foo", 0, 1), "oo"));'
info!(contains(slice("foo", 0, 1), "oo"));
// Equivalent to 'info!(~[std::option::Some(1.0), std::option::None]);'
info!(~[Some(1.0), None]);
}
~~~~

Expand Down
4 changes: 2 additions & 2 deletions src/compiletest/compiletest.rc
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ pub fn is_test(config: &config, testfile: &Path) -> bool {
let mut valid = false;

for valid_extensions.each |ext| {
if str::ends_with(name, *ext) { valid = true; }
if name.ends_with(*ext) { valid = true; }
}

for invalid_prefixes.each |pre| {
if str::starts_with(name, *pre) { valid = false; }
if name.starts_with(*pre) { valid = false; }
}

return valid;
Expand Down
11 changes: 5 additions & 6 deletions src/compiletest/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use core::prelude::*;

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

pub struct ExpectedError { line: uint, kind: ~str, msg: ~str }

Expand All @@ -31,15 +30,15 @@ pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {
fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
let error_tag = ~"//~";
let mut idx;
match str::find_str(line, error_tag) {
match line.find_str(error_tag) {
None => return ~[],
Some(nn) => { idx = (nn as uint) + str::len(error_tag); }
Some(nn) => { idx = (nn as uint) + error_tag.len(); }
}

// "//~^^^ kind msg" denotes a message expected
// three lines above current line:
let mut adjust_line = 0u;
let len = str::len(line);
let len = line.len();
while idx < len && line[idx] == ('^' as u8) {
adjust_line += 1u;
idx += 1u;
Expand All @@ -52,12 +51,12 @@ fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {

// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
let kind = str::slice(line, start_kind, idx);
let kind = line.slice(start_kind, idx);
let kind = kind.to_ascii().to_lower().to_str_ascii();

// Extract msg:
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
let msg = str::slice(line, idx, len).to_owned();
let msg = line.slice(idx, len).to_owned();

debug!("line=%u kind=%s msg=%s", line_num - adjust_line, kind, msg);

Expand Down
16 changes: 8 additions & 8 deletions src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use core::prelude::*;
use common::config;
use common;

use core::iterator::IteratorUtil;
use core::io;
use core::os;
use core::str;

pub struct TestProps {
// Lines that should be expected, in order, on standard out
Expand Down Expand Up @@ -111,7 +111,7 @@ fn iter_header(testfile: &Path, it: &fn(~str) -> bool) -> bool {
// Assume that any directives will be found before the first
// module or function. This doesn't seem to be an optimization
// with a warm page cache. Maybe with a cold one.
if str::starts_with(ln, "fn") || str::starts_with(ln, "mod") {
if ln.starts_with("fn") || ln.starts_with("mod") {
return false;
} else { if !(it(ln)) { return false; } }
}
Expand Down Expand Up @@ -141,8 +141,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 mut strs = ~[];
for str::each_splitn_char(*nv, '=', 1u) |s| { strs.push(s.to_owned()); }
let mut strs: ~[~str] = nv.splitn_iter('=', 1).transform(|s| s.to_owned()).collect();

match strs.len() {
1u => (strs.pop(), ~""),
2u => {
Expand All @@ -168,16 +168,16 @@ fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
}

fn parse_name_directive(line: &str, directive: &str) -> bool {
str::contains(line, directive)
line.contains(directive)
}

fn parse_name_value_directive(line: &str,
directive: ~str) -> Option<~str> {
let keycolon = directive + ":";
match str::find_str(line, keycolon) {
match line.find_str(keycolon) {
Some(colon) => {
let value = str::slice(line, colon + str::len(keycolon),
str::len(line)).to_owned();
let value = line.slice(colon + keycolon.len(),
line.len()).to_owned();
debug!("%s: %s", directive, value);
Some(value)
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] {
if k == ~"PATH" { (~"PATH", v + ";" + lib_path + ";" + aux_path) }
else { (k,v) }
};
if str::ends_with(prog, "rustc.exe") {
if prog.ends_with("rustc.exe") {
env.push((~"RUST_THREADS", ~"1"));
}
return env;
Expand Down
43 changes: 19 additions & 24 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
None => copy *config
};
let config = &mut config;
let cmds = str::connect(props.debugger_cmds, "\n");
let cmds = props.debugger_cmds.connect("\n");
let check_lines = copy props.check_lines;

// compile test file (it shoud have 'compile-flags:-g' in the header)
Expand Down Expand Up @@ -278,7 +278,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::each_line(ProcRes.stdout) |line| {
for ProcRes.stdout.line_iter().advance |line| {
if check_lines[i].trim() == line.trim() {
i += 1u;
}
Expand Down Expand Up @@ -308,8 +308,8 @@ 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::each_line(ProcRes.stderr) |line| {
if str::contains(line, *next_err_pat) {
for ProcRes.stderr.line_iter().advance |line| {
if line.contains(*next_err_pat) {
debug!("found error pattern %s", *next_err_pat);
next_err_idx += 1u;
if next_err_idx == props.error_patterns.len() {
Expand Down Expand Up @@ -358,15 +358,15 @@ 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::each_line(ProcRes.stderr) |line| {
for ProcRes.stderr.line_iter().advance |line| {
let mut was_expected = false;
for vec::eachi(expected_errors) |i, ee| {
if !found_flags[i] {
debug!("prefix=%s ee.kind=%s ee.msg=%s line=%s",
prefixes[i], ee.kind, ee.msg, line);
if (str::starts_with(line, prefixes[i]) &&
str::contains(line, ee.kind) &&
str::contains(line, ee.msg)) {
if (line.starts_with(prefixes[i]) &&
line.contains(ee.kind) &&
line.contains(ee.msg)) {
found_flags[i] = true;
was_expected = true;
break;
Expand All @@ -375,7 +375,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
}

// ignore this msg which gets printed at the end
if str::contains(line, "aborting due to") {
if line.contains("aborting due to") {
was_expected = true;
}

Expand Down Expand Up @@ -417,7 +417,7 @@ fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
if *idx >= haystack.len() {
return false;
}
let opt = str::find_char_from(haystack, needle, *idx);
let opt = haystack.slice_from(*idx).find(needle);
if opt.is_none() {
return false;
}
Expand All @@ -429,7 +429,7 @@ fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
if *idx >= haystack.len() {
return false;
}
let range = str::char_range_at(haystack, *idx);
let range = haystack.char_range_at(*idx);
if range.ch != needle {
return false;
}
Expand All @@ -440,7 +440,7 @@ fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
fn scan_integer(haystack: &str, idx: &mut uint) -> bool {
let mut i = *idx;
while i < haystack.len() {
let range = str::char_range_at(haystack, i);
let range = haystack.char_range_at(i);
if range.ch < '0' || '9' < range.ch {
break;
}
Expand All @@ -460,7 +460,7 @@ fn scan_string(haystack: &str, needle: &str, idx: &mut uint) -> bool {
if haystack_i >= haystack.len() {
return false;
}
let range = str::char_range_at(haystack, haystack_i);
let range = haystack.char_range_at(haystack_i);
haystack_i = range.next;
if !scan_char(needle, range.ch, &mut needle_i) {
return false;
Expand Down Expand Up @@ -612,15 +612,11 @@ fn make_run_args(config: &config, _props: &TestProps, testfile: &Path) ->
}

fn split_maybe_args(argstr: &Option<~str>) -> ~[~str] {
fn rm_whitespace(v: ~[~str]) -> ~[~str] {
v.filtered(|s| !str::is_whitespace(*s))
}

match *argstr {
Some(ref s) => {
let mut ss = ~[];
for str::each_split_char(*s, ' ') |s| { ss.push(s.to_owned()) }
rm_whitespace(ss)
s.split_iter(' ')
.filter_map(|s| if s.is_whitespace() {None} else {Some(s.to_owned())})
.collect()
}
None => ~[]
}
Expand Down Expand Up @@ -649,13 +645,13 @@ fn program_output(config: &config, testfile: &Path, lib_path: &str, prog: ~str,
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn make_cmdline(_libpath: &str, prog: &str, args: &[~str]) -> ~str {
fmt!("%s %s", prog, str::connect(args, " "))
fmt!("%s %s", prog, args.connect(" "))
}

#[cfg(target_os = "win32")]
fn make_cmdline(libpath: &str, prog: &str, args: &[~str]) -> ~str {
fmt!("%s %s %s", lib_path_cmd_prefix(libpath), prog,
str::connect(args, " "))
args.connect(" "))
}

// Build the LD_LIBRARY_PATH variable as it would be seen on the command line
Expand Down Expand Up @@ -739,8 +735,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
let cmdline = make_cmdline("", args.prog, args.args);

// get bare program string
let mut tvec = ~[];
for str::each_split_char(args.prog, '/') |ts| { tvec.push(ts.to_owned()) }
let mut tvec: ~[~str] = args.prog.split_iter('/').transform(|ts| ts.to_owned()).collect();
let prog_short = tvec.pop();

// copy to target
Expand Down
1 change: 0 additions & 1 deletion src/libextra/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use sync::{Mutex, mutex_with_condvars, RWlock, rwlock_with_condvars};

use core::cast;
use core::unstable::sync::UnsafeAtomicRcBox;
use core::ptr;
use core::task;
use core::borrow;

Expand Down
26 changes: 13 additions & 13 deletions src/libextra/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'self> ToBase64 for &'self [u8] {
fn to_base64(&self) -> ~str {
let mut s = ~"";
let len = self.len();
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
s.reserve(((len + 3u) / 4u) * 3u);

let mut i = 0u;

Expand All @@ -59,10 +59,10 @@ impl<'self> ToBase64 for &'self [u8] {
(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]);
s.push_char(CHARS[(n >> 18u) & 63u]);
s.push_char(CHARS[(n >> 12u) & 63u]);
s.push_char(CHARS[(n >> 6u) & 63u]);
s.push_char(CHARS[n & 63u]);

i += 3u;
}
Expand All @@ -73,18 +73,18 @@ impl<'self> ToBase64 for &'self [u8] {
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, '=');
str::push_char(&mut s, '=');
s.push_char(CHARS[(n >> 18u) & 63u]);
s.push_char(CHARS[(n >> 12u) & 63u]);
s.push_char('=');
s.push_char('=');
}
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, '=');
s.push_char(CHARS[(n >> 18u) & 63u]);
s.push_char(CHARS[(n >> 12u) & 63u]);
s.push_char(CHARS[(n >> 6u) & 63u]);
s.push_char('=');
}
_ => fail!("Algebra is broken, please alert the math police")
}
Expand Down
5 changes: 1 addition & 4 deletions src/libextra/fileinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ mod test {
use core::iterator::IteratorUtil;
use core::io;
use core::str;
use core::uint;
use core::vec;
Expand Down Expand Up @@ -527,9 +526,7 @@ mod test {
}

for input_vec_state(filenames) |line, state| {
let nums = do vec::build |p| {
for str::each_split_char(line, ' ') |s| { p(s.to_owned()); }
};
let nums: ~[&str] = line.split_iter(' ').collect();
let file_num = uint::from_str(nums[0]).get();
let line_num = uint::from_str(nums[1]).get();
assert_eq!(line_num, state.line_num_file);
Expand Down
Loading

0 comments on commit 1310212

Please sign in to comment.