提交 b29cd22b 编写于 作者: H Huon Wilson

std: replace str::all/any fns and methods with iterators

上级 1e8982bd
......@@ -13,6 +13,7 @@
use common::config;
use common;
use core::iterator::IteratorUtil;
use core::io;
use core::os;
use core::str;
......
......@@ -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
......
......@@ -567,7 +567,7 @@ fn parse_whitespace(&mut self) {
}
fn parse_ident(&mut self, ident: &str, value: Json) -> Result<Json, Error> {
if str::all(ident, |c| c == self.next_char()) {
if ident.iter().all(|c| c == self.next_char()) {
self.bump();
Ok(value)
} else {
......
......@@ -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)
......
......@@ -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 == ' ' {
......
......@@ -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)
}
......
......@@ -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('"');
}
......
......@@ -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<Sep: StrCharSplitSeparator>(&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";
......
......@@ -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]
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册