Skip to content

Commit 1310212

Browse files
committed
auto merge of #7032 : huonw/rust/each-fn-kill, r=thestinger
Continuation of #7015, and #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.)
2 parents 2ff6b29 + e8782ee commit 1310212

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1533
-2356
lines changed

doc/rust.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -803,19 +803,14 @@ An example of `use` declarations:
803803

804804
~~~~
805805
use std::float::sin;
806-
use std::str::{slice, contains};
807-
use std::option::Some;
806+
use std::option::{Some, None};
808807
809808
fn main() {
810809
// Equivalent to 'info!(std::float::sin(1.0));'
811810
info!(sin(1.0));
812811
813-
// Equivalent to 'info!(std::option::Some(1.0));'
814-
info!(Some(1.0));
815-
816-
// Equivalent to
817-
// 'info!(std::str::contains(std::str::slice("foo", 0, 1), "oo"));'
818-
info!(contains(slice("foo", 0, 1), "oo"));
812+
// Equivalent to 'info!(~[std::option::Some(1.0), std::option::None]);'
813+
info!(~[Some(1.0), None]);
819814
}
820815
~~~~
821816

src/compiletest/compiletest.rc

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ pub fn is_test(config: &config, testfile: &Path) -> bool {
231231
let mut valid = false;
232232

233233
for valid_extensions.each |ext| {
234-
if str::ends_with(name, *ext) { valid = true; }
234+
if name.ends_with(*ext) { valid = true; }
235235
}
236236

237237
for invalid_prefixes.each |pre| {
238-
if str::starts_with(name, *pre) { valid = false; }
238+
if name.starts_with(*pre) { valid = false; }
239239
}
240240

241241
return valid;

src/compiletest/errors.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use core::prelude::*;
1212

1313
use core::io;
14-
use core::str;
1514

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

@@ -31,15 +30,15 @@ pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {
3130
fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
3231
let error_tag = ~"//~";
3332
let mut idx;
34-
match str::find_str(line, error_tag) {
33+
match line.find_str(error_tag) {
3534
None => return ~[],
36-
Some(nn) => { idx = (nn as uint) + str::len(error_tag); }
35+
Some(nn) => { idx = (nn as uint) + error_tag.len(); }
3736
}
3837

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

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

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

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

src/compiletest/header.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use core::prelude::*;
1313
use common::config;
1414
use common;
1515

16+
use core::iterator::IteratorUtil;
1617
use core::io;
1718
use core::os;
18-
use core::str;
1919

2020
pub struct TestProps {
2121
// Lines that should be expected, in order, on standard out
@@ -111,7 +111,7 @@ fn iter_header(testfile: &Path, it: &fn(~str) -> bool) -> bool {
111111
// Assume that any directives will be found before the first
112112
// module or function. This doesn't seem to be an optimization
113113
// with a warm page cache. Maybe with a cold one.
114-
if str::starts_with(ln, "fn") || str::starts_with(ln, "mod") {
114+
if ln.starts_with("fn") || ln.starts_with("mod") {
115115
return false;
116116
} else { if !(it(ln)) { return false; } }
117117
}
@@ -141,8 +141,8 @@ fn parse_check_line(line: &str) -> Option<~str> {
141141
fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
142142
do parse_name_value_directive(line, ~"exec-env").map |nv| {
143143
// nv is either FOO or FOO=BAR
144-
let mut strs = ~[];
145-
for str::each_splitn_char(*nv, '=', 1u) |s| { strs.push(s.to_owned()); }
144+
let mut strs: ~[~str] = nv.splitn_iter('=', 1).transform(|s| s.to_owned()).collect();
145+
146146
match strs.len() {
147147
1u => (strs.pop(), ~""),
148148
2u => {
@@ -168,16 +168,16 @@ fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
168168
}
169169

170170
fn parse_name_directive(line: &str, directive: &str) -> bool {
171-
str::contains(line, directive)
171+
line.contains(directive)
172172
}
173173

174174
fn parse_name_value_directive(line: &str,
175175
directive: ~str) -> Option<~str> {
176176
let keycolon = directive + ":";
177-
match str::find_str(line, keycolon) {
177+
match line.find_str(keycolon) {
178178
Some(colon) => {
179-
let value = str::slice(line, colon + str::len(keycolon),
180-
str::len(line)).to_owned();
179+
let value = line.slice(colon + keycolon.len(),
180+
line.len()).to_owned();
181181
debug!("%s: %s", directive, value);
182182
Some(value)
183183
}

src/compiletest/procsrv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] {
2828
if k == ~"PATH" { (~"PATH", v + ";" + lib_path + ";" + aux_path) }
2929
else { (k,v) }
3030
};
31-
if str::ends_with(prog, "rustc.exe") {
31+
if prog.ends_with("rustc.exe") {
3232
env.push((~"RUST_THREADS", ~"1"));
3333
}
3434
return env;

src/compiletest/runtest.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
244244
None => copy *config
245245
};
246246
let config = &mut config;
247-
let cmds = str::connect(props.debugger_cmds, "\n");
247+
let cmds = props.debugger_cmds.connect("\n");
248248
let check_lines = copy props.check_lines;
249249

250250
// compile test file (it shoud have 'compile-flags:-g' in the header)
@@ -278,7 +278,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
278278
// check if each line in props.check_lines appears in the
279279
// output (in order)
280280
let mut i = 0u;
281-
for str::each_line(ProcRes.stdout) |line| {
281+
for ProcRes.stdout.line_iter().advance |line| {
282282
if check_lines[i].trim() == line.trim() {
283283
i += 1u;
284284
}
@@ -308,8 +308,8 @@ fn check_error_patterns(props: &TestProps,
308308
let mut next_err_idx = 0u;
309309
let mut next_err_pat = &props.error_patterns[next_err_idx];
310310
let mut done = false;
311-
for str::each_line(ProcRes.stderr) |line| {
312-
if str::contains(line, *next_err_pat) {
311+
for ProcRes.stderr.line_iter().advance |line| {
312+
if line.contains(*next_err_pat) {
313313
debug!("found error pattern %s", *next_err_pat);
314314
next_err_idx += 1u;
315315
if next_err_idx == props.error_patterns.len() {
@@ -358,15 +358,15 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
358358
// filename:line1:col1: line2:col2: *warning:* msg
359359
// where line1:col1: is the starting point, line2:col2:
360360
// is the ending point, and * represents ANSI color codes.
361-
for str::each_line(ProcRes.stderr) |line| {
361+
for ProcRes.stderr.line_iter().advance |line| {
362362
let mut was_expected = false;
363363
for vec::eachi(expected_errors) |i, ee| {
364364
if !found_flags[i] {
365365
debug!("prefix=%s ee.kind=%s ee.msg=%s line=%s",
366366
prefixes[i], ee.kind, ee.msg, line);
367-
if (str::starts_with(line, prefixes[i]) &&
368-
str::contains(line, ee.kind) &&
369-
str::contains(line, ee.msg)) {
367+
if (line.starts_with(prefixes[i]) &&
368+
line.contains(ee.kind) &&
369+
line.contains(ee.msg)) {
370370
found_flags[i] = true;
371371
was_expected = true;
372372
break;
@@ -375,7 +375,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
375375
}
376376

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

@@ -417,7 +417,7 @@ fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
417417
if *idx >= haystack.len() {
418418
return false;
419419
}
420-
let opt = str::find_char_from(haystack, needle, *idx);
420+
let opt = haystack.slice_from(*idx).find(needle);
421421
if opt.is_none() {
422422
return false;
423423
}
@@ -429,7 +429,7 @@ fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
429429
if *idx >= haystack.len() {
430430
return false;
431431
}
432-
let range = str::char_range_at(haystack, *idx);
432+
let range = haystack.char_range_at(*idx);
433433
if range.ch != needle {
434434
return false;
435435
}
@@ -440,7 +440,7 @@ fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
440440
fn scan_integer(haystack: &str, idx: &mut uint) -> bool {
441441
let mut i = *idx;
442442
while i < haystack.len() {
443-
let range = str::char_range_at(haystack, i);
443+
let range = haystack.char_range_at(i);
444444
if range.ch < '0' || '9' < range.ch {
445445
break;
446446
}
@@ -460,7 +460,7 @@ fn scan_string(haystack: &str, needle: &str, idx: &mut uint) -> bool {
460460
if haystack_i >= haystack.len() {
461461
return false;
462462
}
463-
let range = str::char_range_at(haystack, haystack_i);
463+
let range = haystack.char_range_at(haystack_i);
464464
haystack_i = range.next;
465465
if !scan_char(needle, range.ch, &mut needle_i) {
466466
return false;
@@ -612,15 +612,11 @@ fn make_run_args(config: &config, _props: &TestProps, testfile: &Path) ->
612612
}
613613

614614
fn split_maybe_args(argstr: &Option<~str>) -> ~[~str] {
615-
fn rm_whitespace(v: ~[~str]) -> ~[~str] {
616-
v.filtered(|s| !str::is_whitespace(*s))
617-
}
618-
619615
match *argstr {
620616
Some(ref s) => {
621-
let mut ss = ~[];
622-
for str::each_split_char(*s, ' ') |s| { ss.push(s.to_owned()) }
623-
rm_whitespace(ss)
617+
s.split_iter(' ')
618+
.filter_map(|s| if s.is_whitespace() {None} else {Some(s.to_owned())})
619+
.collect()
624620
}
625621
None => ~[]
626622
}
@@ -649,13 +645,13 @@ fn program_output(config: &config, testfile: &Path, lib_path: &str, prog: ~str,
649645
#[cfg(target_os = "macos")]
650646
#[cfg(target_os = "freebsd")]
651647
fn make_cmdline(_libpath: &str, prog: &str, args: &[~str]) -> ~str {
652-
fmt!("%s %s", prog, str::connect(args, " "))
648+
fmt!("%s %s", prog, args.connect(" "))
653649
}
654650

655651
#[cfg(target_os = "win32")]
656652
fn make_cmdline(libpath: &str, prog: &str, args: &[~str]) -> ~str {
657653
fmt!("%s %s %s", lib_path_cmd_prefix(libpath), prog,
658-
str::connect(args, " "))
654+
args.connect(" "))
659655
}
660656

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

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

746741
// copy to target

src/libextra/arc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use sync::{Mutex, mutex_with_condvars, RWlock, rwlock_with_condvars};
4646

4747
use core::cast;
4848
use core::unstable::sync::UnsafeAtomicRcBox;
49-
use core::ptr;
5049
use core::task;
5150
use core::borrow;
5251

src/libextra/base64.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'self> ToBase64 for &'self [u8] {
4949
fn to_base64(&self) -> ~str {
5050
let mut s = ~"";
5151
let len = self.len();
52-
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
52+
s.reserve(((len + 3u) / 4u) * 3u);
5353

5454
let mut i = 0u;
5555

@@ -59,10 +59,10 @@ impl<'self> ToBase64 for &'self [u8] {
5959
(self[i + 2u] as uint);
6060

6161
// This 24-bit number gets separated into four 6-bit numbers.
62-
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
63-
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
64-
str::push_char(&mut s, CHARS[(n >> 6u) & 63u]);
65-
str::push_char(&mut s, CHARS[n & 63u]);
62+
s.push_char(CHARS[(n >> 18u) & 63u]);
63+
s.push_char(CHARS[(n >> 12u) & 63u]);
64+
s.push_char(CHARS[(n >> 6u) & 63u]);
65+
s.push_char(CHARS[n & 63u]);
6666

6767
i += 3u;
6868
}
@@ -73,18 +73,18 @@ impl<'self> ToBase64 for &'self [u8] {
7373
0 => (),
7474
1 => {
7575
let n = (self[i] as uint) << 16u;
76-
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
77-
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
78-
str::push_char(&mut s, '=');
79-
str::push_char(&mut s, '=');
76+
s.push_char(CHARS[(n >> 18u) & 63u]);
77+
s.push_char(CHARS[(n >> 12u) & 63u]);
78+
s.push_char('=');
79+
s.push_char('=');
8080
}
8181
2 => {
8282
let n = (self[i] as uint) << 16u |
8383
(self[i + 1u] as uint) << 8u;
84-
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
85-
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
86-
str::push_char(&mut s, CHARS[(n >> 6u) & 63u]);
87-
str::push_char(&mut s, '=');
84+
s.push_char(CHARS[(n >> 18u) & 63u]);
85+
s.push_char(CHARS[(n >> 12u) & 63u]);
86+
s.push_char(CHARS[(n >> 6u) & 63u]);
87+
s.push_char('=');
8888
}
8989
_ => fail!("Algebra is broken, please alert the math police")
9090
}

src/libextra/fileinput.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@ mod test {
416416
417417
use core::iterator::IteratorUtil;
418418
use core::io;
419-
use core::str;
420419
use core::uint;
421420
use core::vec;
422421
@@ -527,9 +526,7 @@ mod test {
527526
}
528527

529528
for input_vec_state(filenames) |line, state| {
530-
let nums = do vec::build |p| {
531-
for str::each_split_char(line, ' ') |s| { p(s.to_owned()); }
532-
};
529+
let nums: ~[&str] = line.split_iter(' ').collect();
533530
let file_num = uint::from_str(nums[0]).get();
534531
let line_num = uint::from_str(nums[1]).get();
535532
assert_eq!(line_num, state.line_num_file);

0 commit comments

Comments
 (0)