From ed299af62566a9f0f285e81408aab5f7680ab4cc Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 8 Jun 2013 15:12:39 +1000 Subject: std: remove fold[lr] in favour of iterators --- src/libsyntax/attr.rs | 3 ++- src/libsyntax/ext/deriving/iter_bytes.rs | 4 ++-- src/libsyntax/ext/expand.rs | 3 ++- src/libsyntax/ext/tt/macro_parser.rs | 4 ++-- src/libsyntax/ext/tt/transcribe.rs | 10 ++++------ 5 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 2da64563159..51334772c84 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -19,6 +19,7 @@ use codemap::BytePos; use diagnostic::span_handler; use parse::comments::{doc_comment_style, strip_doc_comment_decoration}; +use core::iterator::IteratorUtil; use core::hashmap::HashSet; use core::vec; use extra; @@ -313,7 +314,7 @@ pub enum inline_attr { /// True if something like #[inline] is found in the list of attrs. pub fn find_inline_attr(attrs: &[ast::attribute]) -> inline_attr { // FIXME (#2809)---validate the usage of #[inline] and #[inline(always)] - do vec::foldl(ia_none, attrs) |ia,attr| { + do attrs.iter().fold(ia_none) |ia,attr| { match attr.node.value.node { ast::meta_word(@~"inline") => ia_hint, ast::meta_list(@~"inline", ref items) => { diff --git a/src/libsyntax/ext/deriving/iter_bytes.rs b/src/libsyntax/ext/deriving/iter_bytes.rs index 13f83b55a40..453d867fce9 100644 --- a/src/libsyntax/ext/deriving/iter_bytes.rs +++ b/src/libsyntax/ext/deriving/iter_bytes.rs @@ -16,7 +16,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -use core::vec; +use core::iterator::IteratorUtil; pub fn expand_deriving_iter_bytes(cx: @ExtCtxt, span: span, @@ -85,7 +85,7 @@ fn iter_bytes_substructure(cx: @ExtCtxt, span: span, substr: &Substructure) -> @ cx.span_bug(span, "#[deriving(IterBytes)] needs at least one field"); } - do vec::foldl(exprs[0], exprs.slice(1, exprs.len())) |prev, me| { + do exprs.slice(1, exprs.len()).iter().fold(exprs[0]) |prev, me| { cx.expr_binary(span, and, prev, *me) } } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index e1f31b83524..1630fb11626 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -27,6 +27,7 @@ use parse::token::{ident_to_str, intern, fresh_name}; use visit; use visit::{Visitor,mk_vt}; +use core::iterator::IteratorUtil; use core::vec; pub fn expand_expr(extsbox: @mut SyntaxEnv, @@ -128,7 +129,7 @@ pub fn expand_mod_items(extsbox: @mut SyntaxEnv, // decorated with "item decorators", then use that function to transform // the item into a new set of items. let new_items = do vec::flat_map(module_.items) |item| { - do vec::foldr(item.attrs, ~[*item]) |attr, items| { + do item.attrs.rev_iter().fold(~[*item]) |items, attr| { let mname = attr::get_attr_name(attr); match (*extsbox).find(&intern(*mname)) { diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 5f43452cc83..a6ec91f899c 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -129,12 +129,12 @@ pub fn copy_up(mpu: &matcher_pos_up) -> ~MatcherPos { } pub fn count_names(ms: &[matcher]) -> uint { - vec::foldl(0u, ms, |ct, m| { + do ms.iter().fold(0) |ct, m| { ct + match m.node { match_tok(_) => 0u, match_seq(ref more_ms, _, _, _, _) => count_names((*more_ms)), match_nonterminal(_,_,_) => 1u - }}) + }} } pub fn initial_matcher_pos(ms: ~[matcher], sep: Option, lo: BytePos) diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index f8a783c568a..f3bd2d4b8d1 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -19,9 +19,9 @@ use parse::token::{EOF, INTERPOLATED, IDENT, Token, nt_ident}; use parse::token::{ident_to_str}; use parse::lexer::TokenAndSpan; +use core::iterator::IteratorUtil; use core::hashmap::HashMap; use core::option; -use core::vec; ///an unzipping of `token_tree`s struct TtFrame { @@ -113,9 +113,7 @@ fn lookup_cur_matched_by_matched(r: &mut TtReader, matched_seq(ref ads, _) => ads[*idx] } } - let r = &mut *r; - let repeat_idx = &r.repeat_idx; - vec::foldl(start, *repeat_idx, red) + r.repeat_idx.iter().fold(start, red) } fn lookup_cur_matched(r: &mut TtReader, name: ident) -> @named_match { @@ -152,10 +150,10 @@ fn lockstep_iter_size(t: &token_tree, r: &mut TtReader) -> lis { } match *t { tt_delim(ref tts) | tt_seq(_, ref tts, _, _) => { - vec::foldl(lis_unconstrained, *tts, |lis, tt| { + do tts.iter().fold(lis_unconstrained) |lis, tt| { let lis2 = lockstep_iter_size(tt, r); lis_merge(lis, lis2) - }) + } } tt_tok(*) => lis_unconstrained, tt_nonterminal(_, name) => match *lookup_cur_matched(r, name) { -- cgit 1.4.1-3-g733a5 From 513d2292e5a743e630ceece06255528c1902ac01 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 8 Jun 2013 18:28:08 +1000 Subject: std: remove foldr and alli methods in vec --- doc/tutorial-tasks.md | 1 + src/librustc/middle/check_match.rs | 3 ++- src/librustc/middle/liveness.rs | 13 +++++++------ src/librustc/middle/trans/adt.rs | 3 ++- src/librustc/middle/trans/cabi_arm.rs | 1 - src/libstd/vec.rs | 16 ---------------- src/libsyntax/ast_util.rs | 5 +++-- src/libsyntax/ext/deriving/generic.rs | 8 ++++---- src/libsyntax/ext/deriving/mod.rs | 3 ++- src/test/bench/graph500-bfs.rs | 2 +- src/test/compile-fail/issue-3044.rs | 2 +- src/test/run-pass/block-arg-can-be-followed-by-binop.rs | 2 +- .../run-pass/block-arg-can-be-followed-by-block-arg.rs | 2 +- src/test/run-pass/block-arg-can-be-followed-by-call.rs | 2 +- 14 files changed, 26 insertions(+), 37 deletions(-) (limited to 'src/libsyntax') diff --git a/doc/tutorial-tasks.md b/doc/tutorial-tasks.md index 7ea6de90fb2..892908dc0a0 100644 --- a/doc/tutorial-tasks.md +++ b/doc/tutorial-tasks.md @@ -351,6 +351,7 @@ a single large vector of floats. Each task needs the full vector to perform its # use std::vec; # use std::uint; # use std::rand; +# use std::iterator::IteratorUtil; use extra::arc::ARC; fn pnorm(nums: &~[float], p: uint) -> float { diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index b50c158f37a..98f8efb72c8 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -19,6 +19,7 @@ use middle::typeck::method_map; use middle::moves; use util::ppaux::ty_to_str; +use core::iterator::IteratorUtil; use core::uint; use core::vec; use extra::sort; @@ -242,7 +243,7 @@ pub fn is_useful(cx: @MatchCheckCtxt, m: &matrix, v: &[@pat]) -> useful { not_useful } ty::ty_unboxed_vec(*) | ty::ty_evec(*) => { - let max_len = do m.foldr(0) |r, max_len| { + let max_len = do m.rev_iter().fold(0) |max_len, r| { match r[0].node { pat_vec(ref before, _, ref after) => { uint::max(before.len() + after.len(), max_len) diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index 3097be242a1..8a9a67db802 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -110,6 +110,7 @@ use middle::ty; use middle::typeck; use middle::moves; +use core::iterator::IteratorUtil; use core::cast::transmute; use core::hashmap::HashMap; use core::io; @@ -923,7 +924,7 @@ impl Liveness { pub fn propagate_through_block(&self, blk: &blk, succ: LiveNode) -> LiveNode { let succ = self.propagate_through_opt_expr(blk.node.expr, succ); - do blk.node.stmts.foldr(succ) |stmt, succ| { + do blk.node.stmts.rev_iter().fold(succ) |succ, stmt| { self.propagate_through_stmt(*stmt, succ) } } @@ -977,7 +978,7 @@ impl Liveness { pub fn propagate_through_exprs(&self, exprs: &[@expr], succ: LiveNode) -> LiveNode { - do exprs.foldr(succ) |expr, succ| { + do exprs.rev_iter().fold(succ) |succ, expr| { self.propagate_through_expr(*expr, succ) } } @@ -1021,7 +1022,7 @@ impl Liveness { // the construction of a closure itself is not important, // but we have to consider the closed over variables. let caps = self.ir.captures(expr); - do caps.foldr(succ) |cap, succ| { + do caps.rev_iter().fold(succ) |succ, cap| { self.init_from_succ(cap.ln, succ); let var = self.variable(cap.var_nid, expr.span); self.acc(cap.ln, var, ACC_READ | ACC_USE); @@ -1159,7 +1160,7 @@ impl Liveness { expr_struct(_, ref fields, with_expr) => { let succ = self.propagate_through_opt_expr(with_expr, succ); - do (*fields).foldr(succ) |field, succ| { + do fields.rev_iter().fold(succ) |succ, field| { self.propagate_through_expr(field.node.expr, succ) } } @@ -1215,10 +1216,10 @@ impl Liveness { } expr_inline_asm(ref ia) =>{ - let succ = do ia.inputs.foldr(succ) |&(_, expr), succ| { + let succ = do ia.inputs.rev_iter().fold(succ) |succ, &(_, expr)| { self.propagate_through_expr(expr, succ) }; - do ia.outputs.foldr(succ) |&(_, expr), succ| { + do ia.outputs.rev_iter().fold(succ) |succ, &(_, expr)| { self.propagate_through_expr(expr, succ) } } diff --git a/src/librustc/middle/trans/adt.rs b/src/librustc/middle/trans/adt.rs index b26f80fc355..8e1b165f408 100644 --- a/src/librustc/middle/trans/adt.rs +++ b/src/librustc/middle/trans/adt.rs @@ -44,6 +44,7 @@ * taken to it, implementing them for Rust seems difficult. */ +use core::iterator::IteratorUtil; use core::container::Map; use core::libc::c_ulonglong; use core::option::{Option, Some, None}; @@ -176,7 +177,7 @@ fn represent_type_uncached(cx: @CrateContext, t: ty::t) -> Repr { // Since there's at least one // non-empty body, explicit discriminants should have // been rejected by a checker before this point. - if !cases.alli(|i,c| c.discr == (i as int)) { + if !cases.iter().enumerate().all(|(i,c)| c.discr == (i as int)) { cx.sess.bug(fmt!("non-C-like enum %s with specified \ discriminants", ty::item_path_str(cx.tcx, def_id))) diff --git a/src/librustc/middle/trans/cabi_arm.rs b/src/librustc/middle/trans/cabi_arm.rs index 1fecdf5a338..d59635ccd76 100644 --- a/src/librustc/middle/trans/cabi_arm.rs +++ b/src/librustc/middle/trans/cabi_arm.rs @@ -20,7 +20,6 @@ use middle::trans::common::{T_array, T_ptr, T_void}; use core::iterator::IteratorUtil; use core::option::{Option, None, Some}; use core::uint; -use core::vec; fn align_up_to(off: uint, a: uint) -> uint { return (off + a - 1u) / a * a; diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 7540f54f308..bdc9fd0ccad 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1821,11 +1821,9 @@ pub trait ImmutableVector<'self, T> { fn last_opt(&self) -> Option<&'self T>; fn position(&self, f: &fn(t: &T) -> bool) -> Option; fn rposition(&self, f: &fn(t: &T) -> bool) -> Option; - fn foldr<'a, U>(&'a self, z: U, p: &fn(t: &'a T, u: U) -> U) -> U; fn map(&self, f: &fn(t: &T) -> U) -> ~[U]; fn mapi(&self, f: &fn(uint, t: &T) -> U) -> ~[U]; fn map_r(&self, f: &fn(x: &T) -> U) -> ~[U]; - fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool; fn flat_map(&self, f: &fn(t: &T) -> ~[U]) -> ~[U]; fn filter_mapped(&self, f: &fn(t: &T) -> Option) -> ~[U]; unsafe fn unsafe_ref(&self, index: uint) -> *T; @@ -1913,12 +1911,6 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { rposition(*self, f) } - /// Reduce a vector from right to left - #[inline] - fn foldr<'a, U>(&'a self, z: U, p: &fn(t: &'a T, u: U) -> U) -> U { - self.rev_iter().fold(z, |u, t| p(t, u)) - } - /// Apply a function to each element of a vector and return the results #[inline] fn map(&self, f: &fn(t: &T) -> U) -> ~[U] { map(*self, f) } @@ -1942,14 +1934,6 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { r } - /** - * Returns true if the function returns true for all elements. - * - * If the vector is empty, true is returned. - */ - fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool { - self.iter().enumerate().all(|(i, t)| f(i,t)) - } /** * Apply a function to each element of a vector and return a concatenation * of each result vector diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index d99363d7ee5..d170ca92678 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -20,6 +20,7 @@ use opt_vec; use parse::token; use visit; +use core::iterator::IteratorUtil; use core::hashmap::HashMap; use core::int; use core::option; @@ -833,7 +834,7 @@ mod test { // returning the resulting index fn unfold_test_sc(tscs : ~[TestSC], tail: SyntaxContext, table : &mut SCTable) -> SyntaxContext { - tscs.foldr(tail, |tsc : &TestSC,tail : SyntaxContext| + tscs.rev_iter().fold(tail, |tail : SyntaxContext, tsc : &TestSC| {match *tsc { M(mrk) => new_mark_internal(mrk,tail,table), R(ident,name) => new_rename_internal(ident,name,tail,table)}}) @@ -874,7 +875,7 @@ mod test { // extend a syntax context with a sequence of marks given // in a vector. v[0] will be the outermost mark. fn unfold_marks(mrks:~[Mrk],tail:SyntaxContext,table: &mut SCTable) -> SyntaxContext { - mrks.foldr(tail, |mrk:&Mrk,tail:SyntaxContext| + mrks.rev_iter().fold(tail, |tail:SyntaxContext, mrk:&Mrk| {new_mark_internal(*mrk,tail,table)}) } diff --git a/src/libsyntax/ext/deriving/generic.rs b/src/libsyntax/ext/deriving/generic.rs index b36d4496492..078fd4231ca 100644 --- a/src/libsyntax/ext/deriving/generic.rs +++ b/src/libsyntax/ext/deriving/generic.rs @@ -1025,11 +1025,11 @@ pub fn cs_fold(use_foldl: bool, match *substructure.fields { EnumMatching(_, _, ref all_fields) | Struct(ref all_fields) => { if use_foldl { - do all_fields.foldl(base) |&old, &(_, self_f, other_fs)| { + do all_fields.iter().fold(base) |old, &(_, self_f, other_fs)| { f(cx, span, old, self_f, other_fs) } } else { - do all_fields.foldr(base) |&(_, self_f, other_fs), old| { + do all_fields.rev_iter().fold(base) |old, &(_, self_f, other_fs)| { f(cx, span, old, self_f, other_fs) } } @@ -1094,11 +1094,11 @@ pub fn cs_same_method_fold(use_foldl: bool, cs_same_method( |cx, span, vals| { if use_foldl { - do vals.foldl(base) |&old, &new| { + do vals.iter().fold(base) |old, &new| { f(cx, span, old, new) } } else { - do vals.foldr(base) |&new, old| { + do vals.rev_iter().fold(base) |old, &new| { f(cx, span, old, new) } } diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index 1107f21319c..c091ab8b617 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -19,6 +19,7 @@ library. */ use core::prelude::*; +use core::iterator::IteratorUtil; use ast::{enum_def, ident, item, Generics, meta_item, struct_def}; use ext::base::ExtCtxt; @@ -74,7 +75,7 @@ pub fn expand_meta_deriving(cx: @ExtCtxt, in_items } meta_list(_, ref titems) => { - do titems.foldr(in_items) |&titem, in_items| { + do titems.rev_iter().fold(in_items) |in_items, &titem| { match titem.node { meta_name_value(tname, _) | meta_list(tname, _) | diff --git a/src/test/bench/graph500-bfs.rs b/src/test/bench/graph500-bfs.rs index 684d2ac5009..dee18c8a1b3 100644 --- a/src/test/bench/graph500-bfs.rs +++ b/src/test/bench/graph500-bfs.rs @@ -363,7 +363,7 @@ fn validate(edges: ~[(node_id, node_id)], info!(~"Verifying tree edges..."); - let status = do tree.alli() |k, parent| { + let status = do tree.iter().enumerate().all |(k, parent)| { if *parent != root && *parent != -1i64 { level[*parent] == level[k] - 1 } diff --git a/src/test/compile-fail/issue-3044.rs b/src/test/compile-fail/issue-3044.rs index 47cf9177ac2..ee96cc293eb 100644 --- a/src/test/compile-fail/issue-3044.rs +++ b/src/test/compile-fail/issue-3044.rs @@ -14,7 +14,7 @@ fn main() { let needlesArr: ~[char] = ~['a', 'f']; do needlesArr.iter().fold() |x, y| { } - //~^ ERROR 1 parameter were supplied (including the closure passed by the `do` keyword) + //~^ ERROR 1 parameter was supplied (including the closure passed by the `do` keyword) // // the first error is, um, non-ideal. } diff --git a/src/test/run-pass/block-arg-can-be-followed-by-binop.rs b/src/test/run-pass/block-arg-can-be-followed-by-binop.rs index 6a90dafa2f1..522516351d2 100644 --- a/src/test/run-pass/block-arg-can-be-followed-by-binop.rs +++ b/src/test/run-pass/block-arg-can-be-followed-by-binop.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::vec; +use std::iterator::IteratorUtil; pub fn main() { let v = ~[-1f, 0f, 1f, 2f, 3f]; diff --git a/src/test/run-pass/block-arg-can-be-followed-by-block-arg.rs b/src/test/run-pass/block-arg-can-be-followed-by-block-arg.rs index 3dc282fdffa..c6d66e07444 100644 --- a/src/test/run-pass/block-arg-can-be-followed-by-block-arg.rs +++ b/src/test/run-pass/block-arg-can-be-followed-by-block-arg.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::vec; +use std::iterator::IteratorUtil; pub fn main() { fn f(i: &fn() -> uint) -> uint { i() } diff --git a/src/test/run-pass/block-arg-can-be-followed-by-call.rs b/src/test/run-pass/block-arg-can-be-followed-by-call.rs index 0c78735a070..a205e9f8f31 100644 --- a/src/test/run-pass/block-arg-can-be-followed-by-call.rs +++ b/src/test/run-pass/block-arg-can-be-followed-by-call.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::vec; +use std::iterator::IteratorUtil; pub fn main() { fn f(i: uint) -> uint { i } -- cgit 1.4.1-3-g733a5 From 4b806b4d06a6b82771657f9b79728d3fba84ebed Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 8 Jun 2013 22:04:46 +1000 Subject: std: remove each_char* fns and methods from str, replaced by iterators. --- src/compiletest/runtest.rs | 3 +- src/libextra/fileinput.rs | 3 +- src/libextra/json.rs | 6 +- src/libextra/net_url.rs | 7 +- src/librustc/back/link.rs | 3 +- src/libstd/rand.rs | 3 +- src/libstd/repr.rs | 3 +- src/libstd/str.rs | 193 ++++++++++++---------------------------- src/libsyntax/parse/comments.rs | 5 +- src/test/run-pass/issue-2904.rs | 3 +- 10 files changed, 80 insertions(+), 149 deletions(-) (limited to 'src/libsyntax') diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index c174057aaaa..1015373d7ce 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -22,6 +22,7 @@ use procsrv; use util; use util::logv; +use core::iterator::IteratorUtil; use core::io; use core::os; use core::str; @@ -780,7 +781,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps, Some(~"")); let mut exitcode : int = 0; - for str::each_char(exitcode_out) |c| { + for exitcode_out.iter().advance |c| { if !c.is_digit() { break; } exitcode = exitcode * 10 + match c { '0' .. '9' => c as int - ('0' as int), diff --git a/src/libextra/fileinput.rs b/src/libextra/fileinput.rs index 3afa9b51c59..16082732715 100644 --- a/src/libextra/fileinput.rs +++ b/src/libextra/fileinput.rs @@ -414,6 +414,7 @@ mod test { use super::{FileInput, pathify, input_vec, input_vec_state}; + use core::iterator::IteratorUtil; use core::io; use core::str; use core::uint; @@ -455,7 +456,7 @@ mod test { let fi = FileInput::from_vec(copy filenames); - for "012".each_chari |line, c| { + for "012".iter().enumerate().advance |(line, c)| { assert_eq!(fi.read_byte(), c as int); assert_eq!(fi.state().line_num, line); assert_eq!(fi.state().line_num_file, 0); diff --git a/src/libextra/json.rs b/src/libextra/json.rs index 22abe0edbb9..fc1597ffed4 100644 --- a/src/libextra/json.rs +++ b/src/libextra/json.rs @@ -18,6 +18,7 @@ use core::prelude::*; +use core::iterator::IteratorUtil; use core::char; use core::float; use core::hashmap::HashMap; @@ -58,7 +59,7 @@ pub struct Error { fn escape_str(s: &str) -> ~str { let mut escaped = ~"\""; - for str::each_char(s) |c| { + for s.iter().advance |c| { match c { '"' => escaped += "\\\"", '\\' => escaped += "\\\\", @@ -913,7 +914,8 @@ impl serialize::Decoder for Decoder { fn read_char(&mut self) -> char { let mut v = ~[]; - for str::each_char(self.read_str()) |c| { v.push(c) } + let s = self.read_str(); + for s.iter().advance |c| { v.push(c) } if v.len() != 1 { fail!("string must have one character") } v[0] } diff --git a/src/libextra/net_url.rs b/src/libextra/net_url.rs index 08540775864..f26019d9282 100644 --- a/src/libextra/net_url.rs +++ b/src/libextra/net_url.rs @@ -14,6 +14,7 @@ use core::prelude::*; +use core::iterator::IteratorUtil; use core::cmp::Eq; use core::io::{Reader, ReaderUtil}; use core::io; @@ -358,7 +359,7 @@ pub fn query_to_str(query: &Query) -> ~str { // returns the scheme and the rest of the url, or a parsing error pub fn get_scheme(rawurl: &str) -> Result<(~str, ~str), ~str> { - for str::each_chari(rawurl) |i,c| { + for rawurl.iter().enumerate().advance |(i,c)| { match c { 'A' .. 'Z' | 'a' .. 'z' => loop, '0' .. '9' | '+' | '-' | '.' => { @@ -418,7 +419,7 @@ fn get_authority(rawurl: &str) -> let mut colon_count = 0; let mut (pos, begin, end) = (0, 2, len); - for str::each_chari(rawurl) |i,c| { + for rawurl.iter().enumerate().advance |(i,c)| { if i < 2 { loop; } // ignore the leading // // deal with input class first @@ -562,7 +563,7 @@ fn get_path(rawurl: &str, authority: bool) -> Result<(~str, ~str), ~str> { let len = str::len(rawurl); let mut end = len; - for str::each_chari(rawurl) |i,c| { + for rawurl.iter().enumerate().advance |(i,c)| { match c { 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '&' |'\'' | '(' | ')' | '.' | '@' | ':' | '%' | '/' | '+' | '!' | '*' | ',' | ';' | '=' diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index 3c10324fd3d..29e7ba0e62f 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -22,6 +22,7 @@ use middle::trans::common::CrateContext; use middle::ty; use util::ppaux; +use core::iterator::IteratorUtil; use core::char; use core::hash::Streaming; use core::hash; @@ -636,7 +637,7 @@ pub fn get_symbol_hash(ccx: @CrateContext, t: ty::t) -> @str { // gas accepts the following characters in symbols: a-z, A-Z, 0-9, ., _, $ pub fn sanitize(s: &str) -> ~str { let mut result = ~""; - for str::each_char(s) |c| { + for s.iter().advance |c| { match c { // Escape these with $ sequences '@' => result += "$SP$", diff --git a/src/libstd/rand.rs b/src/libstd/rand.rs index b763c1c2d76..7946f7e4f13 100644 --- a/src/libstd/rand.rs +++ b/src/libstd/rand.rs @@ -43,6 +43,7 @@ fn main () { use cast; use cmp; use int; +use iterator::IteratorUtil; use local_data; use prelude::*; use str; @@ -479,7 +480,7 @@ impl RngUtil for R { fn gen_char_from(&mut self, chars: &str) -> char { assert!(!chars.is_empty()); let mut cs = ~[]; - for str::each_char(chars) |c| { cs.push(c) } + for chars.iter().advance |c| { cs.push(c) } self.choose(cs) } diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index 14bec48782f..46f69d020d1 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -22,6 +22,7 @@ use intrinsic; use intrinsic::{TyDesc, TyVisitor, visit_tydesc}; use intrinsic::Opaque; use io::{Writer, WriterUtil}; +use iterator::IteratorUtil; use libc::c_void; use managed; use ptr; @@ -209,7 +210,7 @@ impl ReprVisitor { pub fn write_escaped_slice(&self, slice: &str) { self.writer.write_char('"'); - for slice.each_char |ch| { + for slice.iter().advance |ch| { self.writer.write_escaped_char(ch); } self.writer.write_char('"'); diff --git a/src/libstd/str.rs b/src/libstd/str.rs index d0345a3953e..739825ff0c5 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -25,7 +25,7 @@ use clone::Clone; use cmp::{TotalOrd, Ordering, Less, Equal, Greater}; use container::Container; use iter::Times; -use iterator::Iterator; +use iterator::{Iterator, IteratorUtil}; use libc; use option::{None, Option, Some}; use old_iter::{BaseIter, EqIter}; @@ -608,11 +608,7 @@ pub fn byte_slice_no_callback<'a>(s: &'a str) -> &'a [u8] { /// Convert a string to a unique vector of characters pub fn to_chars(s: &str) -> ~[char] { - let mut buf = ~[]; - for each_char(s) |c| { - buf.push(c); - } - buf + s.iter().collect() } /** @@ -856,12 +852,12 @@ pub fn levdistance(s: &str, t: &str) -> uint { let mut dcol = vec::from_fn(tlen + 1, |x| x); - for s.each_chari |i, sc| { + for s.iter().enumerate().advance |(i, sc)| { let mut current = i; dcol[0] = current + 1; - for t.each_chari |j, tc| { + for t.iter().enumerate().advance |(j, tc)| { let next = dcol[j + 1]; @@ -943,7 +939,7 @@ pub fn each_split_within<'a>(ss: &'a str, let mut cont = true; let slice: &fn() = || { cont = it(slice(ss, slice_start, last_end)) }; - let machine: &fn(uint, char) -> bool = |i, c| { + let machine: &fn((uint, char)) -> bool = |(i, c)| { let whitespace = if char::is_whitespace(c) { Ws } else { Cr }; let limit = if (i - slice_start + 1) <= lim { UnderLim } else { OverLim }; @@ -968,12 +964,12 @@ pub fn each_split_within<'a>(ss: &'a str, cont }; - str::each_chari(ss, machine); + ss.iter().enumerate().advance(machine); // Let the automaton 'run out' by supplying trailing whitespace let mut fake_i = ss.len(); while cont && match state { B | C => true, A => false } { - machine(fake_i, ' '); + machine((fake_i, ' ')); fake_i += 1; } return cont; @@ -1247,7 +1243,7 @@ pub fn any(ss: &str, pred: &fn(char) -> bool) -> bool { pub fn map(ss: &str, ff: &fn(char) -> char) -> ~str { let mut result = ~""; reserve(&mut result, len(ss)); - for ss.each_char |cc| { + for ss.iter().advance |cc| { str::push_char(&mut result, ff(cc)); } result @@ -1289,55 +1285,6 @@ pub fn eachi_reverse(s: &str, it: &fn(uint, u8) -> bool) -> bool { return true; } -/// Iterate over each char of a string, without allocating -#[inline(always)] -pub fn each_char(s: &str, it: &fn(char) -> bool) -> bool { - let mut i = 0; - let len = len(s); - while i < len { - let CharRange {ch, next} = char_range_at(s, i); - if !it(ch) { return false; } - i = next; - } - return true; -} - -/// Iterates over the chars in a string, with indices -#[inline(always)] -pub fn each_chari(s: &str, it: &fn(uint, char) -> bool) -> bool { - let mut pos = 0; - let mut ch_pos = 0u; - let len = s.len(); - while pos < len { - let CharRange {ch, next} = char_range_at(s, pos); - pos = next; - if !it(ch_pos, ch) { return false; } - ch_pos += 1u; - } - return true; -} - -/// Iterates over the chars in a string in reverse -#[inline(always)] -pub fn each_char_reverse(s: &str, it: &fn(char) -> bool) -> bool { - each_chari_reverse(s, |_, c| it(c)) -} - -/// Iterates over the chars in a string in reverse, with indices -#[inline(always)] -pub fn each_chari_reverse(s: &str, it: &fn(uint, char) -> bool) -> bool { - let mut pos = s.len(); - let mut ch_pos = s.char_len(); - while pos > 0 { - let CharRange {ch, next} = char_range_at_reverse(s, pos); - pos = next; - ch_pos -= 1; - - if !it(ch_pos, ch) { return false; } - } - return true; -} - /* Section: Searching */ @@ -1880,7 +1827,7 @@ pub fn is_utf16(v: &[u16]) -> bool { /// Converts to a vector of `u16` encoded as UTF-16 pub fn to_utf16(s: &str) -> ~[u16] { let mut u = ~[]; - for s.each_char |ch| { + for s.iter().advance |ch| { // Arithmetic with u32 literals is easier on the eyes than chars. let mut ch = ch as u32; @@ -2396,7 +2343,7 @@ pub fn capacity(s: &const ~str) -> uint { pub fn escape_default(s: &str) -> ~str { let mut out: ~str = ~""; reserve_at_least(&mut out, str::len(s)); - for s.each_char |c| { + for s.iter().advance |c| { push_str(&mut out, char::escape_default(c)); } out @@ -2406,7 +2353,7 @@ pub fn escape_default(s: &str) -> ~str { pub fn escape_unicode(s: &str) -> ~str { let mut out: ~str = ~""; reserve_at_least(&mut out, str::len(s)); - for s.each_char |c| { + for s.iter().advance |c| { push_str(&mut out, char::escape_unicode(c)); } out @@ -2608,15 +2555,12 @@ pub trait StrSlice<'self> { fn any(&self, it: &fn(char) -> bool) -> bool; fn contains<'a>(&self, needle: &'a str) -> bool; fn contains_char(&self, needle: char) -> bool; - fn char_iter(&self) -> StrCharIterator<'self>; + fn iter(&self) -> StrCharIterator<'self>; + fn rev_iter(&self) -> StrCharRevIterator<'self>; fn each(&self, it: &fn(u8) -> bool) -> bool; fn eachi(&self, it: &fn(uint, u8) -> bool) -> bool; fn each_reverse(&self, it: &fn(u8) -> bool) -> bool; fn eachi_reverse(&self, it: &fn(uint, u8) -> bool) -> bool; - fn each_char(&self, it: &fn(char) -> bool) -> bool; - fn each_chari(&self, it: &fn(uint, char) -> bool) -> bool; - fn each_char_reverse(&self, it: &fn(char) -> bool) -> bool; - fn each_chari_reverse(&self, it: &fn(uint, char) -> bool) -> bool; fn ends_with(&self, needle: &str) -> bool; fn is_empty(&self) -> bool; fn is_whitespace(&self) -> bool; @@ -2670,12 +2614,19 @@ impl<'self> StrSlice<'self> for &'self str { } #[inline] - fn char_iter(&self) -> StrCharIterator<'self> { + fn iter(&self) -> StrCharIterator<'self> { StrCharIterator { index: 0, string: *self } } + #[inline] + fn rev_iter(&self) -> StrCharRevIterator<'self> { + StrCharRevIterator { + index: self.len(), + string: *self + } + } /// Iterate over the bytes in a string #[inline] @@ -2691,25 +2642,6 @@ impl<'self> StrSlice<'self> for &'self str { fn eachi_reverse(&self, it: &fn(uint, u8) -> bool) -> bool { eachi_reverse(*self, it) } - /// Iterate over the chars in a string - #[inline] - fn each_char(&self, it: &fn(char) -> bool) -> bool { each_char(*self, it) } - /// Iterate over the chars in a string, with indices - #[inline] - fn each_chari(&self, it: &fn(uint, char) -> bool) -> bool { - each_chari(*self, it) - } - /// Iterate over the chars in a string in reverse - #[inline] - fn each_char_reverse(&self, it: &fn(char) -> bool) -> bool { - each_char_reverse(*self, it) - } - /// Iterate over the chars in a string in reverse, with indices from the - /// end - #[inline] - fn each_chari_reverse(&self, it: &fn(uint, char) -> bool) -> bool { - each_chari_reverse(*self, it) - } /// Returns true if one string ends with another #[inline] fn ends_with(&self, needle: &str) -> bool { @@ -2880,6 +2812,25 @@ impl<'self> Iterator for StrCharIterator<'self> { } } } +/// External iterator for a string's characters in reverse order. Use +/// with the `std::iterator` module. +pub struct StrCharRevIterator<'self> { + priv index: uint, + priv string: &'self str, +} + +impl<'self> Iterator for StrCharRevIterator<'self> { + #[inline] + fn next(&mut self) -> Option { + if self.index > 0 { + let CharRange {ch, next} = char_range_at_reverse(self.string, self.index); + self.index = next; + Some(ch) + } else { + None + } + } +} #[cfg(test)] mod tests { @@ -4067,52 +4018,6 @@ mod tests { } } - #[test] - fn test_each_char() { - let s = ~"ศไทย中华Việt Nam"; - let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; - let mut pos = 0; - for s.each_char |ch| { - assert_eq!(ch, v[pos]); - pos += 1; - } - } - - #[test] - fn test_each_chari() { - let s = ~"ศไทย中华Việt Nam"; - let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; - let mut pos = 0; - for s.each_chari |i, ch| { - assert_eq!(pos, i); - assert_eq!(ch, v[pos]); - pos += 1; - } - } - - #[test] - fn test_each_char_reverse() { - let s = ~"ศไทย中华Việt Nam"; - let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; - let mut pos = v.len(); - for s.each_char_reverse |ch| { - pos -= 1; - assert_eq!(ch, v[pos]); - } - } - - #[test] - fn test_each_chari_reverse() { - let s = ~"ศไทย中华Việt Nam"; - let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; - let mut pos = v.len(); - for s.each_chari_reverse |i, ch| { - pos -= 1; - assert_eq!(pos, i); - assert_eq!(ch, v[pos]); - } - } - #[test] fn test_escape_unicode() { assert_eq!(escape_unicode("abc"), ~"\\x61\\x62\\x63"); @@ -4168,7 +4073,23 @@ mod tests { let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; let mut pos = 0; - let mut it = s.char_iter(); + let mut it = s.iter(); + + for it.advance |c| { + assert_eq!(c, v[pos]); + pos += 1; + } + assert_eq!(pos, v.len()); + } + + #[test] + fn test_rev_iterator() { + use iterator::*; + let s = ~"ศไทย中华Việt Nam"; + let v = ~['m', 'a', 'N', ' ', 't', 'ệ','i','V','华','中','ย','ท','ไ','ศ']; + + let mut pos = 0; + let mut it = s.rev_iter(); for it.advance |c| { assert_eq!(c, v[pos]); diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 54fba29a19a..22b9e7d6c5e 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -21,6 +21,7 @@ use parse::token; use parse::token::{get_ident_interner}; use parse; +use core::iterator::IteratorUtil; use core::io; use core::str; use core::uint; @@ -78,7 +79,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str { if line.trim().is_empty() { loop; } - for line.each_chari |j, c| { + for line.iter().enumerate().advance |(j, c)| { if j >= i { break; } @@ -91,7 +92,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str { return do lines.map |line| { let mut chars = ~[]; - for str::each_char(*line) |c| { chars.push(c) } + for line.iter().advance |c| { chars.push(c) } if i > chars.len() { ~"" } else { diff --git a/src/test/run-pass/issue-2904.rs b/src/test/run-pass/issue-2904.rs index 7670a7eee7c..fcebb528c10 100644 --- a/src/test/run-pass/issue-2904.rs +++ b/src/test/run-pass/issue-2904.rs @@ -14,6 +14,7 @@ extern mod extra; +use std::iterator::IteratorUtil; use std::io::ReaderUtil; use std::io; use std::str; @@ -67,7 +68,7 @@ fn read_board_grid(in: rdr) -> ~[~[square]] { let mut grid = ~[]; for in.each_line |line| { let mut row = ~[]; - for str::each_char(line) |c| { + for line.iter().advance |c| { row.push(square_from_char(c)) } grid.push(row) -- cgit 1.4.1-3-g733a5 From 00f591680983cc19a6d9f24d8f8c0026ccf20398 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 9 Jun 2013 00:38:58 +1000 Subject: std: replace the str::each* fns/methods with byte iterators --- src/libextra/time.rs | 3 +- src/libstd/str.rs | 220 +++++++++++------------------------ src/libstd/str/ascii.rs | 6 +- src/libsyntax/ext/bytes.rs | 3 +- src/test/run-pass/linear-for-loop.rs | 4 +- src/test/run-pass/utf8.rs | 4 +- 6 files changed, 79 insertions(+), 161 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libextra/time.rs b/src/libextra/time.rs index 758181980a8..dd3e4f48c63 100644 --- a/src/libextra/time.rs +++ b/src/libextra/time.rs @@ -16,6 +16,7 @@ use core::i32; use core::int; use core::io; use core::str; +use core::iterator::IteratorUtil; static NSEC_PER_SEC: i32 = 1_000_000_000_i32; @@ -261,7 +262,7 @@ impl Tm { priv fn do_strptime(s: &str, format: &str) -> Result { fn match_str(s: &str, pos: uint, needle: &str) -> bool { let mut i = pos; - for str::each(needle) |ch| { + for needle.bytes_iter().advance |ch| { if s[i] != ch { return false; } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 739825ff0c5..f9d11164995 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -35,7 +35,7 @@ use str; use to_str::ToStr; use uint; use vec; -use vec::{OwnedVector, OwnedCopyableVector}; +use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector}; #[cfg(not(test))] use cmp::{Eq, Ord, Equiv, TotalEq}; @@ -1249,42 +1249,6 @@ pub fn map(ss: &str, ff: &fn(char) -> char) -> ~str { result } -/// Iterate over the bytes in a string -#[inline(always)] -pub fn each(s: &str, it: &fn(u8) -> bool) -> bool { - eachi(s, |_i, b| it(b)) -} - -/// Iterate over the bytes in a string, with indices -#[inline(always)] -pub fn eachi(s: &str, it: &fn(uint, u8) -> bool) -> bool { - let mut pos = 0; - let len = s.len(); - - while pos < len { - if !it(pos, s[pos]) { return false; } - pos += 1; - } - return true; -} - -/// Iterate over the bytes in a string in reverse -#[inline(always)] -pub fn each_reverse(s: &str, it: &fn(u8) -> bool) -> bool { - eachi_reverse(s, |_i, b| it(b) ) -} - -/// Iterate over the bytes in a string in reverse, with indices -#[inline(always)] -pub fn eachi_reverse(s: &str, it: &fn(uint, u8) -> bool) -> bool { - let mut pos = s.len(); - while pos > 0 { - pos -= 1; - if !it(pos, s[pos]) { return false; } - } - return true; -} - /* Section: Searching */ @@ -1604,7 +1568,7 @@ pub fn rfind_between(s: &str, start: uint, end: uint, f: &fn(char) -> bool) -> O // Utility used by various searching functions fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool { let mut i = at; - for each(needle) |c| { if haystack[i] != c { return false; } i += 1u; } + for needle.bytes_iter().advance |c| { if haystack[i] != c { return false; } i += 1u; } return true; } @@ -2557,10 +2521,8 @@ pub trait StrSlice<'self> { fn contains_char(&self, needle: char) -> bool; fn iter(&self) -> StrCharIterator<'self>; fn rev_iter(&self) -> StrCharRevIterator<'self>; - fn each(&self, it: &fn(u8) -> bool) -> bool; - fn eachi(&self, it: &fn(uint, u8) -> bool) -> bool; - fn each_reverse(&self, it: &fn(u8) -> bool) -> bool; - fn eachi_reverse(&self, it: &fn(uint, u8) -> bool) -> bool; + fn bytes_iter(&self) -> StrBytesIterator<'self>; + fn bytes_rev_iter(&self) -> StrBytesRevIterator<'self>; fn ends_with(&self, needle: &str) -> bool; fn is_empty(&self) -> bool; fn is_whitespace(&self) -> bool; @@ -2628,20 +2590,14 @@ impl<'self> StrSlice<'self> for &'self str { } } - /// Iterate over the bytes in a string - #[inline] - fn each(&self, it: &fn(u8) -> bool) -> bool { each(*self, it) } - /// Iterate over the bytes in a string, with indices - #[inline] - fn eachi(&self, it: &fn(uint, u8) -> bool) -> bool { eachi(*self, it) } - /// Iterate over the bytes in a string - #[inline] - fn each_reverse(&self, it: &fn(u8) -> bool) -> bool { each_reverse(*self, it) } - /// Iterate over the bytes in a string, with indices - #[inline] - fn eachi_reverse(&self, it: &fn(uint, u8) -> bool) -> bool { - eachi_reverse(*self, it) + fn bytes_iter(&self) -> StrBytesIterator<'self> { + StrBytesIterator { it: as_bytes_slice(*self).iter() } } + fn bytes_rev_iter(&self) -> StrBytesRevIterator<'self> { + StrBytesRevIterator { it: as_bytes_slice(*self).rev_iter() } + } + + /// Returns true if one string ends with another #[inline] fn ends_with(&self, needle: &str) -> bool { @@ -2832,6 +2788,32 @@ impl<'self> Iterator for StrCharRevIterator<'self> { } } +/// External iterator for a string's bytes. Use with the `std::iterator` +/// module. +pub struct StrBytesIterator<'self> { + priv it: vec::VecIterator<'self, u8> +} + +impl<'self> Iterator for StrBytesIterator<'self> { + #[inline] + fn next(&mut self) -> Option { + self.it.next().map_consume(|&x| x) + } +} + +/// External iterator for a string's bytes in reverse order. Use with +/// the `std::iterator` module. +pub struct StrBytesRevIterator<'self> { + priv it: vec::VecRevIterator<'self, u8> +} + +impl<'self> Iterator for StrBytesRevIterator<'self> { + #[inline] + fn next(&mut self) -> Option { + self.it.next().map_consume(|&x| x) + } +} + #[cfg(test)] mod tests { use iterator::IteratorUtil; @@ -3922,102 +3904,6 @@ mod tests { } } - #[test] - fn test_each() { - let s = ~"ศไทย中华Việt Nam"; - let v = [ - 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, - 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, - 109 - ]; - let mut pos = 0; - - for s.each |b| { - assert_eq!(b, v[pos]); - pos += 1; - } - } - - #[test] - fn test_each_empty() { - for "".each |b| { - assert_eq!(b, 0u8); - } - } - - #[test] - fn test_eachi() { - let s = ~"ศไทย中华Việt Nam"; - let v = [ - 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, - 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, - 109 - ]; - let mut pos = 0; - - for s.eachi |i, b| { - assert_eq!(pos, i); - assert_eq!(b, v[pos]); - pos += 1; - } - } - - #[test] - fn test_eachi_empty() { - for "".eachi |i, b| { - assert_eq!(i, 0); - assert_eq!(b, 0); - } - } - - #[test] - fn test_each_reverse() { - let s = ~"ศไทย中华Việt Nam"; - let v = [ - 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, - 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, - 109 - ]; - let mut pos = v.len(); - - for s.each_reverse |b| { - pos -= 1; - assert_eq!(b, v[pos]); - } - } - - #[test] - fn test_each_empty_reverse() { - for "".each_reverse |b| { - assert_eq!(b, 0u8); - } - } - - #[test] - fn test_eachi_reverse() { - let s = ~"ศไทย中华Việt Nam"; - let v = [ - 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, - 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, - 109 - ]; - let mut pos = v.len(); - - for s.eachi_reverse |i, b| { - pos -= 1; - assert_eq!(pos, i); - assert_eq!(b, v[pos]); - } - } - - #[test] - fn test_eachi_reverse_empty() { - for "".eachi_reverse |i, b| { - assert_eq!(i, 0); - assert_eq!(b, 0); - } - } - #[test] fn test_escape_unicode() { assert_eq!(escape_unicode("abc"), ~"\\x61\\x62\\x63"); @@ -4097,4 +3983,36 @@ mod tests { } assert_eq!(pos, v.len()); } + + #[test] + fn test_bytes_iterator() { + let s = ~"ศไทย中华Việt Nam"; + let v = [ + 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, + 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, + 109 + ]; + let mut pos = 0; + + for s.bytes_iter().advance |b| { + assert_eq!(b, v[pos]); + pos += 1; + } + } + + #[test] + fn test_bytes_rev_iterator() { + let s = ~"ศไทย中华Việt Nam"; + let v = [ + 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, + 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, + 109 + ]; + let mut pos = v.len(); + + for s.bytes_rev_iter().advance |b| { + pos -= 1; + assert_eq!(b, v[pos]); + } + } } diff --git a/src/libstd/str/ascii.rs b/src/libstd/str/ascii.rs index 3b31d70f7a1..e288d605714 100644 --- a/src/libstd/str/ascii.rs +++ b/src/libstd/str/ascii.rs @@ -15,6 +15,7 @@ use str; use str::StrSlice; use cast; use old_iter::BaseIter; +use iterator::IteratorUtil; use vec::{CopyableVector, ImmutableVector, OwnedVector}; /// Datatype to hold one ascii character. It is 8 bit long. @@ -101,10 +102,7 @@ impl<'self> AsciiCast<&'self[Ascii]> for &'self str { #[inline(always)] fn is_ascii(&self) -> bool { - for self.each |b| { - if !b.is_ascii() { return false; } - } - true + self.bytes_iter().all(|b| b.is_ascii()) } } diff --git a/src/libsyntax/ext/bytes.rs b/src/libsyntax/ext/bytes.rs index a046395b6f5..51fbaee7a33 100644 --- a/src/libsyntax/ext/bytes.rs +++ b/src/libsyntax/ext/bytes.rs @@ -10,6 +10,7 @@ /* The compiler code necessary to support the bytes! extension. */ +use core::iterator::IteratorUtil; use ast; use codemap::span; use ext::base::*; @@ -27,7 +28,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree]) -> bas ast::expr_lit(lit) => match lit.node { // string literal, push each byte to vector expression ast::lit_str(s) => { - for s.each |byte| { + for s.bytes_iter().advance |byte| { bytes.push(cx.expr_u8(sp, byte)); } } diff --git a/src/test/run-pass/linear-for-loop.rs b/src/test/run-pass/linear-for-loop.rs index 7ab915a9628..a42d70f5ae2 100644 --- a/src/test/run-pass/linear-for-loop.rs +++ b/src/test/run-pass/linear-for-loop.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::str; +use std::iterator::IteratorUtil; pub fn main() { let x = ~[1, 2, 3]; @@ -18,7 +18,7 @@ pub fn main() { assert_eq!(y, 6); let s = ~"hello there"; let mut i: int = 0; - for str::each(s) |c| { + for s.bytes_iter().advance |c| { if i == 0 { assert!((c == 'h' as u8)); } if i == 1 { assert!((c == 'e' as u8)); } if i == 2 { assert!((c == 'l' as u8)); } diff --git a/src/test/run-pass/utf8.rs b/src/test/run-pass/utf8.rs index fd5bc07e015..8a845439058 100644 --- a/src/test/run-pass/utf8.rs +++ b/src/test/run-pass/utf8.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::str; +use std::iterator::IteratorUtil; pub fn main() { let yen: char = '¥'; // 0xa5 @@ -43,7 +43,7 @@ pub fn main() { fn check_str_eq(a: ~str, b: ~str) { let mut i: int = 0; - for str::each(a) |ab| { + for a.bytes_iter().advance |ab| { debug!(i); debug!(ab); let bb: u8 = b[i]; -- cgit 1.4.1-3-g733a5 From 98ba91f81bea38d8fc8bd5bc0cb44ac3e173a53c Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 9 Jun 2013 02:21:11 +1000 Subject: remove unused import warnings --- src/compiletest/procsrv.rs | 6 ------ src/libextra/flate.rs | 3 +-- src/libextra/flatpipes.rs | 1 - src/libextra/sort.rs | 3 --- src/libextra/std.rc | 1 - src/libextra/sync.rs | 1 - src/libextra/tempfile.rs | 1 - src/libextra/treemap.rs | 2 -- src/libextra/uv_ll.rs | 1 - src/librustc/front/test.rs | 1 - src/librustc/middle/trans/common.rs | 1 - src/librustc/middle/trans/debuginfo.rs | 1 - src/librustc/rustc.rc | 2 -- src/librustdoc/desc_to_brief_pass.rs | 1 - src/librustdoc/pass.rs | 1 - src/librustdoc/unindent_pass.rs | 1 - src/libstd/at_vec.rs | 2 -- src/libstd/iter.rs | 1 - src/libstd/iterator.rs | 1 - src/libstd/ptr.rs | 2 -- src/libstd/sys.rs | 1 - src/libstd/trie.rs | 1 - src/libstd/vec.rs | 4 ++-- src/libsyntax/ast.rs | 2 +- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/ext/expand.rs | 18 ++++++++---------- src/libsyntax/parse/comments.rs | 1 - src/libsyntax/parse/mod.rs | 3 +-- src/libsyntax/parse/token.rs | 1 - 29 files changed, 14 insertions(+), 52 deletions(-) (limited to 'src/libsyntax') diff --git a/src/compiletest/procsrv.rs b/src/compiletest/procsrv.rs index 62f0731dab6..f86ab2c9093 100644 --- a/src/compiletest/procsrv.rs +++ b/src/compiletest/procsrv.rs @@ -10,14 +10,9 @@ use core::prelude::*; -use core::comm; -use core::io; -use core::libc::c_int; use core::os; use core::run; use core::str; -use core::task; -use core::vec; #[cfg(target_os = "win32")] fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] { @@ -74,4 +69,3 @@ pub fn run(lib_path: &str, err: str::from_bytes(output.error) } } - diff --git a/src/libextra/flate.rs b/src/libextra/flate.rs index 076126e0432..0fde03b69cb 100644 --- a/src/libextra/flate.rs +++ b/src/libextra/flate.rs @@ -16,8 +16,6 @@ Simple compression #[allow(missing_doc)]; -use core::prelude::*; - use core::libc::{c_void, size_t, c_int}; use core::libc; use core::vec; @@ -87,6 +85,7 @@ mod tests { use super::*; use core::rand; use core::rand::RngUtil; + use core::prelude::*; #[test] #[allow(non_implicitly_copyable_typarams)] diff --git a/src/libextra/flatpipes.rs b/src/libextra/flatpipes.rs index e8239b9f7fd..c0f619c1b85 100644 --- a/src/libextra/flatpipes.rs +++ b/src/libextra/flatpipes.rs @@ -654,7 +654,6 @@ mod test { use core::int; use core::io::BytesWriter; use core::result; - use core::sys; use core::task; #[test] diff --git a/src/libextra/sort.rs b/src/libextra/sort.rs index 420c63efab5..26d1e28e122 100644 --- a/src/libextra/sort.rs +++ b/src/libextra/sort.rs @@ -929,11 +929,8 @@ mod test_tim_sort { use core::prelude::*; use sort::tim_sort; - - use core::local_data; use core::rand::RngUtil; use core::rand; - use core::uint; use core::vec; struct CVal { diff --git a/src/libextra/std.rc b/src/libextra/std.rc index 4e9a547e141..83c0bb516b4 100644 --- a/src/libextra/std.rc +++ b/src/libextra/std.rc @@ -148,4 +148,3 @@ pub mod extra { pub use serialize; pub use test; } - diff --git a/src/libextra/sync.rs b/src/libextra/sync.rs index 28a5e5382be..8bbe0afa704 100644 --- a/src/libextra/sync.rs +++ b/src/libextra/sync.rs @@ -731,7 +731,6 @@ mod tests { use core::cast; use core::cell::Cell; use core::comm; - use core::ptr; use core::result; use core::task; use core::vec; diff --git a/src/libextra/tempfile.rs b/src/libextra/tempfile.rs index 6d0bd888195..98c57838072 100644 --- a/src/libextra/tempfile.rs +++ b/src/libextra/tempfile.rs @@ -34,7 +34,6 @@ mod tests { use core::prelude::*; use tempfile::mkdtemp; - use tempfile; use core::os; use core::str; diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index ebb0cdc120f..9db3d48a3b8 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -1034,8 +1034,6 @@ mod test_set { use super::*; - use core::vec; - #[test] fn test_clear() { let mut s = TreeSet::new(); diff --git a/src/libextra/uv_ll.rs b/src/libextra/uv_ll.rs index 2cb2eea8828..2522f149bf4 100644 --- a/src/libextra/uv_ll.rs +++ b/src/libextra/uv_ll.rs @@ -1234,7 +1234,6 @@ mod test { use core::comm::{SharedChan, stream, GenericChan, GenericPort}; use core::libc; - use core::result; use core::str; use core::sys; use core::task; diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index cda7d1fa937..5d0de535629 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -22,7 +22,6 @@ use syntax::codemap::{dummy_sp, span, ExpandedFrom, CallInfo, NameAndSpan}; use syntax::codemap; use syntax::ext::base::ExtCtxt; use syntax::fold; -use syntax::parse::token; use syntax::print::pprust; use syntax::{ast, ast_util}; diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index 774d2df1ca3..df5000a543c 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -53,7 +53,6 @@ use syntax::ast::ident; use syntax::ast_map::{path, path_elt}; use syntax::codemap::span; use syntax::parse::token; -use syntax::parse::token::ident_interner; use syntax::{ast, ast_map}; use syntax::abi::{X86, X86_64, Arm, Mips}; diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index eb173fb2c44..5f475f1bb9d 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -29,7 +29,6 @@ use core::str; use core::sys; use core::vec; use syntax::codemap::span; -use syntax::parse::token::ident_interner; use syntax::{ast, codemap, ast_util, ast_map}; static LLVMDebugVersion: int = (9 << 16); diff --git a/src/librustc/rustc.rc b/src/librustc/rustc.rc index baf920c04ac..0d26e4c6ef1 100644 --- a/src/librustc/rustc.rc +++ b/src/librustc/rustc.rc @@ -36,8 +36,6 @@ extern mod std(name = "std", vers = "0.7-pre"); // For bootstrapping purposes. #[cfg(stage0)] -pub use core::str; -#[cfg(stage0)] pub use core::unstable; use core::prelude::*; diff --git a/src/librustdoc/desc_to_brief_pass.rs b/src/librustdoc/desc_to_brief_pass.rs index 3066e817044..24ade927be2 100644 --- a/src/librustdoc/desc_to_brief_pass.rs +++ b/src/librustdoc/desc_to_brief_pass.rs @@ -27,7 +27,6 @@ use pass::Pass; use core::iterator::IteratorUtil; use core::str; use core::util; -use core::vec; pub fn mk_pass() -> Pass { Pass { diff --git a/src/librustdoc/pass.rs b/src/librustdoc/pass.rs index e83b7adad0e..31adb0f3b8d 100644 --- a/src/librustdoc/pass.rs +++ b/src/librustdoc/pass.rs @@ -10,7 +10,6 @@ use core::prelude::*; -use core::vec; use core::iterator::IteratorUtil; use astsrv; diff --git a/src/librustdoc/unindent_pass.rs b/src/librustdoc/unindent_pass.rs index 41a844c0b46..b6753f385df 100644 --- a/src/librustdoc/unindent_pass.rs +++ b/src/librustdoc/unindent_pass.rs @@ -24,7 +24,6 @@ use core::prelude::*; use core::iterator::IteratorUtil; use core::str; use core::uint; -use core::vec; use pass::Pass; use text_pass; diff --git a/src/libstd/at_vec.rs b/src/libstd/at_vec.rs index 23f901c23ed..a118e445fe2 100644 --- a/src/libstd/at_vec.rs +++ b/src/libstd/at_vec.rs @@ -286,8 +286,6 @@ pub mod raw { #[cfg(test)] mod test { use super::*; - use prelude::*; - use uint; #[test] diff --git a/src/libstd/iter.rs b/src/libstd/iter.rs index 8a0ec3ade4d..4886588bb94 100644 --- a/src/libstd/iter.rs +++ b/src/libstd/iter.rs @@ -42,7 +42,6 @@ much easier to implement. use cmp::Ord; use option::{Option, Some, None}; -use vec::OwnedVector; use num::{One, Zero}; use ops::{Add, Mul}; diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 309e207eaaa..8803844fdd0 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -18,7 +18,6 @@ implementing the `Iterator` trait. */ use cmp; -use iter; use iter::{FromIter, Times}; use num::{Zero, One}; use option::{Option, Some, None}; diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index c8e2f58d801..e2cbf716dd1 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -11,8 +11,6 @@ //! Unsafe pointer utility functions use cast; -#[cfg(stage0)] use libc; -#[cfg(stage0)] use libc::{c_void, size_t}; use option::{Option, Some, None}; use sys; use unstable::intrinsics; diff --git a/src/libstd/sys.rs b/src/libstd/sys.rs index 583923bc2e3..87e13e494aa 100644 --- a/src/libstd/sys.rs +++ b/src/libstd/sys.rs @@ -14,7 +14,6 @@ use option::{Some, None}; use cast; -use cmp::{Eq, Ord}; use gc; use io; use libc; diff --git a/src/libstd/trie.rs b/src/libstd/trie.rs index 7899edbfcb9..4bd3946f885 100644 --- a/src/libstd/trie.rs +++ b/src/libstd/trie.rs @@ -14,7 +14,6 @@ use prelude::*; use iterator::IteratorUtil; use uint; use util::{swap, replace}; -use vec; // FIXME: #5244: need to manually update the TrieNode constructor static SHIFT: uint = 4; diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index bdc9fd0ccad..6137b589bdb 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -3349,13 +3349,13 @@ mod tests { #[test] fn test_each_ret_len0() { - let mut a0 : [int, .. 0] = []; + let a0 : [int, .. 0] = []; assert_eq!(each(a0, |_p| fail!()), true); } #[test] fn test_each_ret_len1() { - let mut a1 = [17]; + let a1 = [17]; assert_eq!(each(a1, |_p| true), true); assert_eq!(each(a1, |_p| false), false); } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 5bbc5d4e819..f27ae3b828c 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -15,7 +15,7 @@ use core::prelude::*; use codemap::{span, spanned}; use abi::AbiSet; use opt_vec::OptVec; -use parse::token::{ident_to_str, interner_get, str_to_ident}; +use parse::token::{interner_get, str_to_ident}; use core::hashmap::HashMap; use core::option::Option; diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index d170ca92678..b040397de72 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -20,7 +20,6 @@ use opt_vec; use parse::token; use visit; -use core::iterator::IteratorUtil; use core::hashmap::HashMap; use core::int; use core::option; @@ -793,6 +792,7 @@ mod test { use ast::*; use super::*; use core::io; + use core::iterator::IteratorUtil; #[test] fn xorpush_test () { let mut s = ~[]; diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 1630fb11626..1e1f411c050 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -11,11 +11,11 @@ use core::prelude::*; use ast::{blk_, attribute_, attr_outer, meta_word}; -use ast::{crate, decl_local, expr_, expr_mac, mac_invoc_tt}; -use ast::{item_mac, local_, stmt_, stmt_decl, stmt_mac, stmt_expr, stmt_semi}; -use ast::{SCTable, illegal_ctxt}; +use ast::{crate, expr_, expr_mac, mac_invoc_tt}; +use ast::{item_mac, stmt_, stmt_mac, stmt_expr, stmt_semi}; +use ast::{illegal_ctxt}; use ast; -use ast_util::{new_rename, new_mark, resolve, get_sctable}; +use ast_util::{new_rename, new_mark, resolve}; use attr; use codemap; use codemap::{span, CallInfo, ExpandedFrom, NameAndSpan, spanned}; @@ -23,9 +23,9 @@ use ext::base::*; use fold::*; use parse; use parse::{parse_item_from_source_str}; -use parse::token::{ident_to_str, intern, fresh_name}; +use parse::token::{ident_to_str, intern}; use visit; -use visit::{Visitor,mk_vt}; +use visit::Visitor; use core::iterator::IteratorUtil; use core::vec; @@ -749,16 +749,14 @@ mod test { use super::*; use ast; use ast::{attribute_, attr_outer, meta_word, empty_ctxt}; - use ast_util::{get_sctable}; use codemap; use codemap::spanned; use parse; - use parse::token::{gensym, intern, get_ident_interner}; + use parse::token::{intern, get_ident_interner}; use print::pprust; use util::parser_testing::{string_to_item, string_to_pat, strs_to_idents}; - use visit::{mk_vt,Visitor}; + use visit::{mk_vt}; - use core::io; use core::option::{None, Some}; // make sure that fail! is present diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 22b9e7d6c5e..360ea12ec02 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -19,7 +19,6 @@ use parse::lexer::{is_line_non_doc_comment, is_block_non_doc_comment}; use parse::lexer; use parse::token; use parse::token::{get_ident_interner}; -use parse; use core::iterator::IteratorUtil; use core::io; diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index d7248204e1c..559bca34f21 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -341,10 +341,9 @@ mod test { use codemap::{span, BytePos, spanned}; use opt_vec; use ast; - use ast::{new_ident}; use abi; use parse::parser::Parser; - use parse::token::{intern, str_to_ident}; + use parse::token::{str_to_ident}; use util::parser_testing::{string_to_tts_and_sess, string_to_parser}; use util::parser_testing::{string_to_expr, string_to_item}; use util::parser_testing::{string_to_stmt, strs_to_idents}; diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index ecf83483c21..7359448a8f2 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -22,7 +22,6 @@ use core::char; use core::cmp::Equiv; use core::local_data; use core::str; -use core::hashmap::HashSet; use core::rand; use core::rand::RngUtil; use core::to_bytes; -- cgit 1.4.1-3-g733a5