Skip to content

Commit 127c0d4

Browse files
committed
Teach path to collapse double-slashes in components. Close #3430.
1 parent c5347b4 commit 127c0d4

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/libcore/path.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,19 @@ impl PosixPath : GenericPath {
197197
}
198198

199199
pure fn push_many(cs: &[~str]) -> PosixPath {
200-
PosixPath { components: (copy self.components) + copy cs, ..self }
200+
let mut v = copy self.components;
201+
for cs.each |e| {
202+
let mut ss = str::split_nonempty(e, |c| windows::is_sep(c as u8));
203+
unchecked { vec::push_all_move(v, move ss); }
204+
}
205+
PosixPath { components: move v, ..self }
201206
}
202207

203208
pure fn push(s: &str) -> PosixPath {
204-
let mut cs = copy self.components;
205-
unchecked { vec::push(cs, move str::from_slice(s)); }
206-
return PosixPath { components: move cs,
207-
..self }
209+
let mut v = copy self.components;
210+
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
211+
unchecked { vec::push_all_move(v, move ss); }
212+
PosixPath { components: move v, ..self }
208213
}
209214

210215
pure fn pop() -> PosixPath {
@@ -385,15 +390,19 @@ impl WindowsPath : GenericPath {
385390
}
386391

387392
pure fn push_many(cs: &[~str]) -> WindowsPath {
388-
return WindowsPath { components: (copy self.components) + (copy cs),
389-
..self }
393+
let mut v = copy self.components;
394+
for cs.each |e| {
395+
let mut ss = str::split_nonempty(e, |c| windows::is_sep(c as u8));
396+
unchecked { vec::push_all_move(v, move ss); }
397+
}
398+
return WindowsPath { components: move v, ..self }
390399
}
391400

392401
pure fn push(s: &str) -> WindowsPath {
393-
let mut cs = copy self.components;
394-
unchecked { vec::push(cs, move str::from_slice(s)); }
395-
return WindowsPath { components: move cs,
396-
..self }
402+
let mut v = copy self.components;
403+
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
404+
unchecked { vec::push_all_move(v, move ss); }
405+
return WindowsPath { components: move v, ..self }
397406
}
398407

399408
pure fn pop() -> WindowsPath {
@@ -419,6 +428,7 @@ pure fn normalize(components: &[~str]) -> ~[~str] {
419428
for components.each |c| {
420429
unchecked {
421430
if c == ~"." && components.len() > 1 { loop; }
431+
if c == ~"" { loop; }
422432
if c == ~".." && cs.len() != 0 {
423433
vec::pop(cs);
424434
loop;
@@ -430,6 +440,20 @@ pure fn normalize(components: &[~str]) -> ~[~str] {
430440
move cs
431441
}
432442
443+
#[test]
444+
fn test_double_slash_collapsing()
445+
{
446+
let path = from_str::<PosixPath>("tmp/");
447+
let path = path.push("/hmm");
448+
let path = path.normalize();
449+
assert ~"tmp/hmm" == path.to_str();
450+
451+
let path = from_str::<WindowsPath>("tmp/");
452+
let path = path.push("/hmm");
453+
let path = path.normalize();
454+
assert ~"tmp\\hmm" == path.to_str();
455+
}
456+
433457
mod posix {
434458

435459
#[cfg(test)]

0 commit comments

Comments
 (0)