Skip to content

Commit 21990cd

Browse files
committed
auto merge of #10622 : Kimundi/rust/str_de_iter, r=alexcrichton
This PR removes almost all `_iter` suffixes in various APIs of the codebase that return Iterators, as discussed in #9440. As a summarize for the intend behind this PR: - Iterators are the recommended way to provide a potentially lazy list of values, no need to name them painfully verbose. If anything, functions that return a specific container type should have more verbose names. - We have a static type system, so no need to encode the return value of a constructor function into its name. Following is a possibly incomplete list of all renamings I performed in the codebase. For a few of them I'm a bit unsure whether the new name still properly expresses their functionality, so feedback would be welcome: ~~~ &str : word_iter() -> words() line_iter() -> lines() any_line_iter() -> lines_any() iter() -> chars() char_offset_iter() -> char_indices() byte_iter() -> bytes() split_iter() -> split() splitn_iter() -> splitn() split_str_iter() -> split_str() split_terminator_iter() -> split_terminator() matches_index_iter() -> match_indices() nfd_iter() -> nfd_chars() nfkd_iter() -> nfkd_chars() &[T] : split_iter() -> split() splitn_iter() -> splitn() window_iter() -> windows() chunk_iter() -> chunks() permutations_iter() -> permutations() extra:bitv::Bitv : rev_liter() -> rev_iter() common_iter() -> commons() outlier_iter() -> outliers() extra::treemap::{...} : lower_bound_iter() -> lower_bound() upper_bound_iter() -> upper_bound() std::trie::{...} : bound_iter() -> bound() lower_bound_iter() -> lower_bound() upper_bound_iter() -> upper_bound() rustpkg::package_id::{...} : prefixes_iter() -> prefixes() std::hashmap::{...} : difference_iter() -> difference() symmetric_difference_iter() -> symmetric_difference() intersection_iter() -> intersection() union_iter() -> union() std::path::{posix, windows} : component_iter() -> components() str_component_iter() -> str_components() ... not showing all identical renamings for reverse versions ~~~ --- I'm also planning a few more changes, like removing all unnecessary `_rev` constructors (#9391), or reducing the `split` variants on `&str` to a more versatile and concise system.
2 parents b42c438 + 24b316a commit 21990cd

Some content is hidden

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

63 files changed

+473
-469
lines changed

doc/tutorial-conditions.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn read_int_pairs() -> ~[(int,int)] {
8282
let line = fi.read_line();
8383
8484
// 2. Split the line into fields ("words").
85-
let fields = line.word_iter().to_owned_vec();
85+
let fields = line.words().to_owned_vec();
8686
8787
// 3. Match the vector of fields against a vector pattern.
8888
match fields {
@@ -295,7 +295,7 @@ fn read_int_pairs() -> ~[(int,int)] {
295295
let fi = FileInput::from_args();
296296
while ! fi.eof() {
297297
let line = fi.read_line();
298-
let fields = line.word_iter().to_owned_vec();
298+
let fields = line.words().to_owned_vec();
299299
match fields {
300300
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
301301
from_str::<int>(b).unwrap())),
@@ -396,7 +396,7 @@ fn read_int_pairs() -> ~[(int,int)] {
396396
let fi = FileInput::from_args();
397397
while ! fi.eof() {
398398
let line = fi.read_line();
399-
let fields = line.word_iter().to_owned_vec();
399+
let fields = line.words().to_owned_vec();
400400
match fields {
401401
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
402402
from_str::<int>(b).unwrap())),
@@ -473,7 +473,7 @@ fn read_int_pairs() -> ~[(int,int)] {
473473
let fi = FileInput::from_args();
474474
while ! fi.eof() {
475475
let line = fi.read_line();
476-
let fields = line.word_iter().to_owned_vec();
476+
let fields = line.words().to_owned_vec();
477477
match fields {
478478
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
479479
from_str::<int>(b).unwrap())),
@@ -551,7 +551,7 @@ fn read_int_pairs() -> ~[(int,int)] {
551551
let fi = FileInput::from_args();
552552
while ! fi.eof() {
553553
let line = fi.read_line();
554-
let fields = line.word_iter().to_owned_vec();
554+
let fields = line.words().to_owned_vec();
555555
match fields {
556556
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
557557
from_str::<int>(b).unwrap())),
@@ -647,7 +647,7 @@ fn read_int_pairs() -> ~[(int,int)] {
647647
let fi = FileInput::from_args();
648648
while ! fi.eof() {
649649
let line = fi.read_line();
650-
let fields = line.word_iter().to_owned_vec();
650+
let fields = line.words().to_owned_vec();
651651
match fields {
652652
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
653653
from_str::<int>(b).unwrap())),
@@ -787,7 +787,7 @@ fn read_int_pairs() -> ~[(int,int)] {
787787
let fi = FileInput::from_args();
788788
while ! fi.eof() {
789789
let line = fi.read_line();
790-
let fields = line.word_iter().to_owned_vec();
790+
let fields = line.words().to_owned_vec();
791791
match fields {
792792
793793
// Delegate parsing ints to helper function that will

src/compiletest/header.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn parse_check_line(line: &str) -> Option<~str> {
145145
fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
146146
do parse_name_value_directive(line, ~"exec-env").map |nv| {
147147
// nv is either FOO or FOO=BAR
148-
let mut strs: ~[~str] = nv.splitn_iter('=', 1).map(|s| s.to_owned()).collect();
148+
let mut strs: ~[~str] = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
149149

150150
match strs.len() {
151151
1u => (strs.pop(), ~""),

src/compiletest/runtest.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,11 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
383383
if num_check_lines > 0 {
384384
// Allow check lines to leave parts unspecified (e.g., uninitialized
385385
// bits in the wrong case of an enum) with the notation "[...]".
386-
let check_fragments: ~[~[&str]] = check_lines.map(|s| s.split_str_iter("[...]").collect());
386+
let check_fragments: ~[~[&str]] = check_lines.map(|s| s.split_str("[...]").collect());
387387
// check if each line in props.check_lines appears in the
388388
// output (in order)
389389
let mut i = 0u;
390-
for line in ProcRes.stdout.line_iter() {
390+
for line in ProcRes.stdout.lines() {
391391
let mut rest = line.trim();
392392
let mut first = true;
393393
let mut failed = false;
@@ -439,7 +439,7 @@ fn check_error_patterns(props: &TestProps,
439439
let mut next_err_idx = 0u;
440440
let mut next_err_pat = &props.error_patterns[next_err_idx];
441441
let mut done = false;
442-
for line in ProcRes.stderr.line_iter() {
442+
for line in ProcRes.stderr.lines() {
443443
if line.contains(*next_err_pat) {
444444
debug!("found error pattern {}", *next_err_pat);
445445
next_err_idx += 1u;
@@ -483,7 +483,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
483483
}).collect::<~[~str]>();
484484
485485
fn to_lower( s : &str ) -> ~str {
486-
let i = s.iter();
486+
let i = s.chars();
487487
let c : ~[char] = i.map( |c| {
488488
if c.is_ascii() {
489489
c.to_ascii().to_lower().to_char()
@@ -512,7 +512,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
512512
// filename:line1:col1: line2:col2: *warning:* msg
513513
// where line1:col1: is the starting point, line2:col2:
514514
// is the ending point, and * represents ANSI color codes.
515-
for line in ProcRes.stderr.line_iter() {
515+
for line in ProcRes.stderr.lines() {
516516
let mut was_expected = false;
517517
for (i, ee) in expected_errors.iter().enumerate() {
518518
if !found_flags[i] {
@@ -777,7 +777,7 @@ fn make_run_args(config: &config, _props: &TestProps, testfile: &Path) ->
777777
fn split_maybe_args(argstr: &Option<~str>) -> ~[~str] {
778778
match *argstr {
779779
Some(ref s) => {
780-
s.split_iter(' ')
780+
s.split(' ')
781781
.filter_map(|s| if s.is_whitespace() {None} else {Some(s.to_owned())})
782782
.collect()
783783
}
@@ -896,7 +896,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
896896
let cmdline = make_cmdline("", args.prog, args.args);
897897

898898
// get bare program string
899-
let mut tvec: ~[~str] = args.prog.split_iter('/').map(|ts| ts.to_owned()).collect();
899+
let mut tvec: ~[~str] = args.prog.split('/').map(|ts| ts.to_owned()).collect();
900900
let prog_short = tvec.pop();
901901

902902
// copy to target
@@ -939,7 +939,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
939939
Some(~""));
940940
941941
let mut exitcode : int = 0;
942-
for c in exitcode_out.iter() {
942+
for c in exitcode_out.chars() {
943943
if !c.is_digit() { break; }
944944
exitcode = exitcode * 10 + match c {
945945
'0' .. '9' => c as int - ('0' as int),
@@ -1089,7 +1089,7 @@ fn disassemble_extract(config: &config, _props: &TestProps,
10891089
fn count_extracted_lines(p: &Path) -> uint {
10901090
let x = File::open(&p.with_extension("ll")).read_to_end();
10911091
let x = str::from_utf8_owned(x);
1092-
x.line_iter().len()
1092+
x.lines().len()
10931093
}
10941094
10951095

src/libextra/base64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<'self> FromBase64 for &'self str {
193193
let mut buf: u32 = 0;
194194
let mut modulus = 0;
195195

196-
let mut it = self.byte_iter().enumerate();
196+
let mut it = self.bytes().enumerate();
197197
for (idx, byte) in it {
198198
let val = byte as u32;
199199

src/libextra/bitv.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl Bitv {
413413
}
414414

415415
#[inline]
416-
pub fn rev_liter<'a>(&'a self) -> Invert<BitvIterator<'a>> {
416+
pub fn rev_iter<'a>(&'a self) -> Invert<BitvIterator<'a>> {
417417
self.iter().invert()
418418
}
419419

@@ -723,38 +723,38 @@ impl BitvSet {
723723
}
724724

725725
pub fn difference(&self, other: &BitvSet, f: |&uint| -> bool) -> bool {
726-
for (i, w1, w2) in self.common_iter(other) {
726+
for (i, w1, w2) in self.commons(other) {
727727
if !iterate_bits(i, w1 & !w2, |b| f(&b)) {
728728
return false
729729
}
730730
};
731731
/* everything we have that they don't also shows up */
732-
self.outlier_iter(other).advance(|(mine, i, w)|
732+
self.outliers(other).advance(|(mine, i, w)|
733733
!mine || iterate_bits(i, w, |b| f(&b))
734734
)
735735
}
736736

737737
pub fn symmetric_difference(&self, other: &BitvSet, f: |&uint| -> bool)
738738
-> bool {
739-
for (i, w1, w2) in self.common_iter(other) {
739+
for (i, w1, w2) in self.commons(other) {
740740
if !iterate_bits(i, w1 ^ w2, |b| f(&b)) {
741741
return false
742742
}
743743
};
744-
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
744+
self.outliers(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
745745
}
746746

747747
pub fn intersection(&self, other: &BitvSet, f: |&uint| -> bool) -> bool {
748-
self.common_iter(other).advance(|(i, w1, w2)| iterate_bits(i, w1 & w2, |b| f(&b)))
748+
self.commons(other).advance(|(i, w1, w2)| iterate_bits(i, w1 & w2, |b| f(&b)))
749749
}
750750

751751
pub fn union(&self, other: &BitvSet, f: |&uint| -> bool) -> bool {
752-
for (i, w1, w2) in self.common_iter(other) {
752+
for (i, w1, w2) in self.commons(other) {
753753
if !iterate_bits(i, w1 | w2, |b| f(&b)) {
754754
return false
755755
}
756756
};
757-
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
757+
self.outliers(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
758758
}
759759
}
760760

@@ -763,12 +763,12 @@ impl cmp::Eq for BitvSet {
763763
if self.size != other.size {
764764
return false;
765765
}
766-
for (_, w1, w2) in self.common_iter(other) {
766+
for (_, w1, w2) in self.commons(other) {
767767
if w1 != w2 {
768768
return false;
769769
}
770770
}
771-
for (_, _, w) in self.outlier_iter(other) {
771+
for (_, _, w) in self.outliers(other) {
772772
if w != 0 {
773773
return false;
774774
}
@@ -803,15 +803,15 @@ impl Set<uint> for BitvSet {
803803
}
804804

805805
fn is_subset(&self, other: &BitvSet) -> bool {
806-
for (_, w1, w2) in self.common_iter(other) {
806+
for (_, w1, w2) in self.commons(other) {
807807
if w1 & w2 != w1 {
808808
return false;
809809
}
810810
}
811811
/* If anything is not ours, then everything is not ours so we're
812812
definitely a subset in that case. Otherwise if there's any stray
813813
ones that 'other' doesn't have, we're not a subset. */
814-
for (mine, _, w) in self.outlier_iter(other) {
814+
for (mine, _, w) in self.outliers(other) {
815815
if !mine {
816816
return true;
817817
} else if w != 0 {
@@ -865,7 +865,7 @@ impl BitvSet {
865865
/// both have in common. The three yielded arguments are (bit location,
866866
/// w1, w2) where the bit location is the number of bits offset so far,
867867
/// and w1/w2 are the words coming from the two vectors self, other.
868-
fn common_iter<'a>(&'a self, other: &'a BitvSet)
868+
fn commons<'a>(&'a self, other: &'a BitvSet)
869869
-> Map<'static, ((uint, &'a uint), &'a ~[uint]), (uint, uint, uint),
870870
Zip<Enumerate<vec::VecIterator<'a, uint>>, Repeat<&'a ~[uint]>>> {
871871
let min = num::min(self.bitv.storage.len(), other.bitv.storage.len());
@@ -881,7 +881,7 @@ impl BitvSet {
881881
/// The yielded arguments are a `bool`, the bit offset, and a word. The `bool`
882882
/// is true if the word comes from `self`, and `false` if it comes from
883883
/// `other`.
884-
fn outlier_iter<'a>(&'a self, other: &'a BitvSet)
884+
fn outliers<'a>(&'a self, other: &'a BitvSet)
885885
-> Map<'static, ((uint, &'a uint), uint), (bool, uint, uint),
886886
Zip<Enumerate<vec::VecIterator<'a, uint>>, Repeat<uint>>> {
887887
let slen = self.bitv.storage.len();

src/libextra/getopts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
413413
let mut i_arg = None;
414414
if cur[1] == '-' as u8 {
415415
let tail = cur.slice(2, curlen);
416-
let tail_eq: ~[&str] = tail.split_iter('=').collect();
416+
let tail_eq: ~[&str] = tail.split('=').collect();
417417
if tail_eq.len() <= 1 {
418418
names = ~[Long(tail.to_owned())];
419419
} else {
@@ -735,7 +735,7 @@ pub mod groups {
735735

736736
// Normalize desc to contain words separated by one space character
737737
let mut desc_normalized_whitespace = ~"";
738-
for word in desc.word_iter() {
738+
for word in desc.words() {
739739
desc_normalized_whitespace.push_str(word);
740740
desc_normalized_whitespace.push_char(' ');
741741
}
@@ -826,7 +826,7 @@ pub mod groups {
826826
cont
827827
};
828828

829-
ss.char_offset_iter().advance(|x| machine(x));
829+
ss.char_indices().advance(|x| machine(x));
830830

831831
// Let the automaton 'run out' by supplying trailing whitespace
832832
while cont && match state { B | C => true, A => false } {

src/libextra/glob.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> GlobIterator {
102102

103103
let root_len = pat_root.map_default(0u, |p| p.as_vec().len());
104104
let dir_patterns = pattern.slice_from(root_len.min(&pattern.len()))
105-
.split_terminator_iter(is_sep).map(|s| Pattern::new(s)).to_owned_vec();
105+
.split_terminator(is_sep).map(|s| Pattern::new(s)).to_owned_vec();
106106

107107
let todo = list_dir_sorted(&root).move_iter().map(|x|(x,0u)).to_owned_vec();
108108

@@ -209,7 +209,7 @@ impl Pattern {
209209
*/
210210
pub fn new(pattern: &str) -> Pattern {
211211

212-
let chars = pattern.iter().to_owned_vec();
212+
let chars = pattern.chars().to_owned_vec();
213213
let mut tokens = ~[];
214214
let mut i = 0;
215215

@@ -272,7 +272,7 @@ impl Pattern {
272272
*/
273273
pub fn escape(s: &str) -> ~str {
274274
let mut escaped = ~"";
275-
for c in s.iter() {
275+
for c in s.chars() {
276276
match c {
277277
// note that ! does not need escaping because it is only special inside brackets
278278
'?' | '*' | '[' | ']' => {
@@ -586,10 +586,10 @@ mod test {
586586
let pats = ["[a-z123]", "[1a-z23]", "[123a-z]"];
587587
for &p in pats.iter() {
588588
let pat = Pattern::new(p);
589-
for c in "abcdefghijklmnopqrstuvwxyz".iter() {
589+
for c in "abcdefghijklmnopqrstuvwxyz".chars() {
590590
assert!(pat.matches(c.to_str()));
591591
}
592-
for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".iter() {
592+
for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars() {
593593
let options = MatchOptions {case_sensitive: false, .. MatchOptions::new()};
594594
assert!(pat.matches_with(c.to_str(), options));
595595
}

src/libextra/hex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'self> FromHex for &'self str {
9191
let mut modulus = 0;
9292
let mut buf = 0u8;
9393

94-
for (idx, byte) in self.byte_iter().enumerate() {
94+
for (idx, byte) in self.bytes().enumerate() {
9595
buf <<= 4;
9696

9797
match byte as char {

src/libextra/json.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct Error {
5959

6060
fn escape_str(s: &str) -> ~str {
6161
let mut escaped = ~"\"";
62-
for c in s.iter() {
62+
for c in s.chars() {
6363
match c {
6464
'"' => escaped.push_str("\\\""),
6565
'\\' => escaped.push_str("\\\\"),
@@ -559,7 +559,7 @@ impl<T : Iterator<char>> Parser<T> {
559559
}
560560

561561
fn parse_ident(&mut self, ident: &str, value: Json) -> Result<Json, Error> {
562-
if ident.iter().all(|c| c == self.next_char()) {
562+
if ident.chars().all(|c| c == self.next_char()) {
563563
self.bump();
564564
Ok(value)
565565
} else {
@@ -844,13 +844,13 @@ impl<T : Iterator<char>> Parser<T> {
844844
/// Decodes a json value from an `&mut io::Reader`
845845
pub fn from_reader(rdr: &mut io::Reader) -> Result<Json, Error> {
846846
let s = str::from_utf8(rdr.read_to_end());
847-
let mut parser = Parser(~s.iter());
847+
let mut parser = Parser(~s.chars());
848848
parser.parse()
849849
}
850850

851851
/// Decodes a json value from a string
852852
pub fn from_str(s: &str) -> Result<Json, Error> {
853-
let mut parser = Parser(~s.iter());
853+
let mut parser = Parser(~s.chars());
854854
parser.parse()
855855
}
856856

@@ -930,7 +930,7 @@ impl serialize::Decoder for Decoder {
930930
fn read_char(&mut self) -> char {
931931
let s = self.read_str();
932932
{
933-
let mut it = s.iter();
933+
let mut it = s.chars();
934934
match (it.next(), it.next()) {
935935
// exactly one character
936936
(Some(c), None) => return c,

0 commit comments

Comments
 (0)