From b29cd22bce6325a60788ab84f989bd2e82fcaaf4 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Mon, 10 Jun 2013 00:34:23 +1000 Subject: [PATCH] std: replace str::all/any fns and methods with iterators --- src/compiletest/header.rs | 1 + src/compiletest/runtest.rs | 2 +- src/libextra/json.rs | 2 +- src/libextra/semver.rs | 3 +- src/librustdoc/unindent_pass.rs | 2 +- src/libstd/path.rs | 4 +-- src/libstd/run.rs | 2 +- src/libstd/str.rs | 52 ++------------------------------- src/libstd/str/ascii.rs | 4 +-- src/libsyntax/parse/lexer.rs | 3 +- 10 files changed, 15 insertions(+), 60 deletions(-) diff --git a/src/compiletest/header.rs b/src/compiletest/header.rs index 782571fc679..153a8de8029 100644 --- a/src/compiletest/header.rs +++ b/src/compiletest/header.rs @@ -13,6 +13,7 @@ use common::config; use common; +use core::iterator::IteratorUtil; use core::io; use core::os; use core::str; diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index 444f4c8d539..6812f6e4455 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -735,7 +735,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps, let cmdline = make_cmdline("", args.prog, args.args); // get bare program string - let tvec: ~[~str] = args.prog.split_iter('/').transform(|ts| ts.to_owned()).collect(); + let mut tvec: ~[~str] = args.prog.split_iter('/').transform(|ts| ts.to_owned()).collect(); let prog_short = tvec.pop(); // copy to target diff --git a/src/libextra/json.rs b/src/libextra/json.rs index fc1597ffed4..30a2b5f8627 100644 --- a/src/libextra/json.rs +++ b/src/libextra/json.rs @@ -567,7 +567,7 @@ fn parse_whitespace(&mut self) { } fn parse_ident(&mut self, ident: &str, value: Json) -> Result { - if str::all(ident, |c| c == self.next_char()) { + if ident.iter().all(|c| c == self.next_char()) { self.bump(); Ok(value) } else { diff --git a/src/libextra/semver.rs b/src/libextra/semver.rs index 494f0c8ea81..36ebd10ae17 100644 --- a/src/libextra/semver.rs +++ b/src/libextra/semver.rs @@ -14,6 +14,7 @@ use core::prelude::*; +use core::iterator::IteratorUtil; use core::char; use core::cmp; use core::io::{ReaderUtil}; @@ -168,7 +169,7 @@ fn take_num(rdr: @io::Reader, ch: char) -> (uint, char) { fn take_ident(rdr: @io::Reader, ch: char) -> (Identifier, char) { let (s,ch) = take_nonempty_prefix(rdr, ch, char::is_alphanumeric); - if s.all(char::is_digit) { + if s.iter().all(char::is_digit) { match uint::from_str(s) { None => { bad_parse::cond.raise(()); (Numeric(0), ch) }, Some(i) => (Numeric(i), ch) diff --git a/src/librustdoc/unindent_pass.rs b/src/librustdoc/unindent_pass.rs index b6753f385df..6bb5b2e0cfc 100644 --- a/src/librustdoc/unindent_pass.rs +++ b/src/librustdoc/unindent_pass.rs @@ -63,7 +63,7 @@ fn unindent(s: &str) -> ~str { } else { saw_first_line = true; let mut spaces = 0; - do str::all(*line) |char| { + do line.iter().all |char| { // Only comparing against space because I wouldn't // know what to do with mixed whitespace chars if char == ' ' { diff --git a/src/libstd/path.rs b/src/libstd/path.rs index b2f25d41157..eb78120c6be 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -508,7 +508,7 @@ fn with_dirname(&self, d: &str) -> PosixPath { } fn with_filename(&self, f: &str) -> PosixPath { - assert!(! str::any(f, |c| windows::is_sep(c))); + assert!(! f.iter().all(windows::is_sep)); self.dir_path().push(f) } @@ -722,7 +722,7 @@ fn with_dirname(&self, d: &str) -> WindowsPath { } fn with_filename(&self, f: &str) -> WindowsPath { - assert!(! str::any(f, |c| windows::is_sep(c))); + assert!(! f.iter().all(windows::is_sep)); self.dir_path().push(f) } diff --git a/src/libstd/run.rs b/src/libstd/run.rs index 3b17067feba..29598bc48fa 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -588,7 +588,7 @@ pub fn make_command_line(prog: &str, args: &[~str]) -> ~str { return cmd; fn append_arg(cmd: &mut ~str, arg: &str) { - let quote = arg.any(|c| c == ' ' || c == '\t'); + let quote = arg.iter().any(|c| c == ' ' || c == '\t'); if quote { cmd.push_char('"'); } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 930026fa4f7..09ea6a5dfa9 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1175,22 +1175,6 @@ fn equiv(&self, other: &~str) -> bool { eq_slice(*self, *other) } Section: Iterating through strings */ -/** - * Return true if a predicate matches all characters or if the string - * contains no characters - */ -pub fn all(s: &str, it: &fn(char) -> bool) -> bool { - all_between(s, 0u, len(s), it) -} - -/** - * Return true if a predicate matches any character (and false if it - * matches none or there are no characters) - */ -pub fn any(ss: &str, pred: &fn(char) -> bool) -> bool { - !all(ss, |cc| !pred(cc)) -} - /// Apply a function to each character pub fn map(ss: &str, ff: &fn(char) -> char) -> ~str { let mut result = ~""; @@ -1675,7 +1659,7 @@ pub fn is_empty(s: &str) -> bool { len(s) == 0u } * Whitespace characters are determined by `char::is_whitespace` */ pub fn is_whitespace(s: &str) -> bool { - return all(s, char::is_whitespace); + s.iter().all(char::is_whitespace) } /** @@ -1684,7 +1668,7 @@ pub fn is_whitespace(s: &str) -> bool { * Alphanumeric characters are determined by `char::is_alphanumeric` */ fn is_alphanumeric(s: &str) -> bool { - return all(s, char::is_alphanumeric); + s.iter().all(char::is_alphanumeric) } /// Returns the string length/size in bytes not counting the null terminator @@ -2467,8 +2451,6 @@ pub mod traits {} #[allow(missing_doc)] pub trait StrSlice<'self> { - fn all(&self, it: &fn(char) -> bool) -> bool; - fn any(&self, it: &fn(char) -> bool) -> bool; fn contains<'a>(&self, needle: &'a str) -> bool; fn contains_char(&self, needle: char) -> bool; fn iter(&self) -> StrCharIterator<'self>; @@ -2514,18 +2496,6 @@ fn split_options_iter(&self, sep: Sep, /// Extension methods for strings impl<'self> StrSlice<'self> for &'self str { - /** - * Return true if a predicate matches all characters or if the string - * contains no characters - */ - #[inline] - fn all(&self, it: &fn(char) -> bool) -> bool { all(*self, it) } - /** - * Return true if a predicate matches any character (and false if it - * matches none or there are no characters) - */ - #[inline] - fn any(&self, it: &fn(char) -> bool) -> bool { any(*self, it) } /// Returns true if one string contains another #[inline] fn contains<'a>(&self, needle: &'a str) -> bool { @@ -3522,24 +3492,6 @@ fn test_map() { assert_eq!(~"YMCA", map("ymca", |c| unsafe {libc::toupper(c as c_char)} as char)); } - #[test] - fn test_all() { - assert_eq!(true, all("", char::is_uppercase)); - assert_eq!(false, all("ymca", char::is_uppercase)); - assert_eq!(true, all("YMCA", char::is_uppercase)); - assert_eq!(false, all("yMCA", char::is_uppercase)); - assert_eq!(false, all("YMCy", char::is_uppercase)); - } - - #[test] - fn test_any() { - assert_eq!(false, any("", char::is_uppercase)); - assert_eq!(false, any("ymca", char::is_uppercase)); - assert_eq!(true, any("YMCA", char::is_uppercase)); - assert_eq!(true, any("yMCA", char::is_uppercase)); - assert_eq!(true, any("Ymcy", char::is_uppercase)); - } - #[test] fn test_chars() { let ss = ~"ศไทย中华Việt Nam"; diff --git a/src/libstd/str/ascii.rs b/src/libstd/str/ascii.rs index e288d605714..a4a1b7a171d 100644 --- a/src/libstd/str/ascii.rs +++ b/src/libstd/str/ascii.rs @@ -226,8 +226,8 @@ fn test_ascii() { assert_eq!('`'.to_ascii().to_upper().to_char(), '`'); assert_eq!('{'.to_ascii().to_upper().to_char(), '{'); - assert!(str::all("banana", |c| c.is_ascii())); - assert!(! str::all("ประเทศไทย中华Việt Nam", |c| c.is_ascii())); + assert!("banana".iter().all(|c| c.is_ascii())); + assert!(!"ประเทศไทย中华Việt Nam".iter().all(|c| c.is_ascii())); } #[test] diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 8ee0a976c8b..f615f1321df 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -19,6 +19,7 @@ use parse::token; use parse::token::{str_to_ident}; +use core::iterator::IteratorUtil; use core::char; use core::either; use core::str; @@ -245,7 +246,7 @@ fn consume_whitespace_and_comments(rdr: @mut StringReader) pub fn is_line_non_doc_comment(s: &str) -> bool { let s = s.trim_right(); - s.len() > 3 && s.all(|ch| ch == '/') + s.len() > 3 && s.iter().all(|ch| ch == '/') } // PRECONDITION: rdr.curr is not whitespace -- GitLab