1
Fork 0

std: replace str::each_split* with an iterator

This commit is contained in:
Huon Wilson 2013-06-09 23:10:50 +10:00
parent 2ff6b298c5
commit 1e8982bdb2
19 changed files with 270 additions and 516 deletions

View file

@ -18,6 +18,7 @@ Cross-platform file path handling
use container::Container;
use cmp::Eq;
use iterator::IteratorUtil;
use libc;
use option::{None, Option, Some};
use str;
@ -449,10 +450,9 @@ impl ToStr for PosixPath {
// PosixPath and WindowsPath, most of their methods are common.
impl GenericPath for PosixPath {
fn from_str(s: &str) -> PosixPath {
let mut components = ~[];
for str::each_split_nonempty(s, |c| c == '/') |s| {
components.push(s.to_owned())
}
let components = s.split_iter('/')
.filter_map(|s| if s.is_empty() {None} else {Some(s.to_owned())})
.collect();
let is_absolute = (s.len() != 0 && s[0] == '/' as u8);
PosixPath {
is_absolute: is_absolute,
@ -508,7 +508,7 @@ impl GenericPath for PosixPath {
}
fn with_filename(&self, f: &str) -> PosixPath {
assert!(! str::any(f, |c| windows::is_sep(c as u8)));
assert!(! str::any(f, |c| windows::is_sep(c)));
self.dir_path().push(f)
}
@ -569,11 +569,11 @@ impl GenericPath for PosixPath {
fn push_many(&self, cs: &[~str]) -> PosixPath {
let mut v = copy self.components;
for cs.each |e| {
let mut ss = ~[];
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
for e.split_iter(windows::is_sep).advance |s| {
if !s.is_empty() {
v.push(s.to_owned())
}
}
v.push_all_move(ss);
}
PosixPath {
is_absolute: self.is_absolute,
@ -583,11 +583,11 @@ impl GenericPath for PosixPath {
fn push(&self, s: &str) -> PosixPath {
let mut v = copy self.components;
let mut ss = ~[];
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
for s.split_iter(windows::is_sep).advance |s| {
if !s.is_empty() {
v.push(s.to_owned())
}
}
v.push_all_move(ss);
PosixPath { components: v, ..copy *self }
}
@ -661,11 +661,11 @@ impl GenericPath for WindowsPath {
}
}
let mut components = ~[];
for str::each_split_nonempty(rest, |c| windows::is_sep(c as u8)) |s| {
components.push(s.to_owned())
}
let is_absolute = (rest.len() != 0 && windows::is_sep(rest[0]));
let components = rest.split_iter(windows::is_sep)
.filter_map(|s| if s.is_empty() {None} else {Some(s.to_owned())})
.collect();
let is_absolute = (rest.len() != 0 && windows::is_sep(rest[0] as char));
WindowsPath {
host: host,
device: device,
@ -722,7 +722,7 @@ impl GenericPath for WindowsPath {
}
fn with_filename(&self, f: &str) -> WindowsPath {
assert!(! str::any(f, |c| windows::is_sep(c as u8)));
assert!(! str::any(f, |c| windows::is_sep(c)));
self.dir_path().push(f)
}
@ -826,11 +826,11 @@ impl GenericPath for WindowsPath {
fn push_many(&self, cs: &[~str]) -> WindowsPath {
let mut v = copy self.components;
for cs.each |e| {
let mut ss = ~[];
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
for e.split_iter(windows::is_sep).advance |s| {
if !s.is_empty() {
v.push(s.to_owned())
}
}
v.push_all_move(ss);
}
// tedious, but as-is, we can't use ..self
WindowsPath {
@ -843,11 +843,11 @@ impl GenericPath for WindowsPath {
fn push(&self, s: &str) -> WindowsPath {
let mut v = copy self.components;
let mut ss = ~[];
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
for s.split_iter(windows::is_sep).advance |s| {
if !s.is_empty() {
v.push(s.to_owned())
}
}
v.push_all_move(ss);
WindowsPath { components: v, ..copy *self }
}
@ -905,8 +905,8 @@ pub mod windows {
use option::{None, Option, Some};
#[inline(always)]
pub fn is_sep(u: u8) -> bool {
u == '/' as u8 || u == '\\' as u8
pub fn is_sep(u: char) -> bool {
u == '/' || u == '\\'
}
pub fn extract_unc_prefix(s: &str) -> Option<(~str,~str)> {
@ -915,7 +915,7 @@ pub mod windows {
s[0] == s[1]) {
let mut i = 2;
while i < s.len() {
if is_sep(s[i]) {
if is_sep(s[i] as char) {
let pre = s.slice(2, i).to_owned();
let rest = s.slice(i, s.len()).to_owned();
return Some((pre, rest));