Skip to content

Commit 28e3340

Browse files
committed
std: migrate path::unix to using Vec internally.
1 parent ecc774f commit 28e3340

File tree

3 files changed

+72
-64
lines changed

3 files changed

+72
-64
lines changed

src/libstd/path/mod.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ use fmt;
7070
use iter::Iterator;
7171
use option::{Option, None, Some};
7272
use str;
73-
use str::{MaybeOwned, OwnedStr, Str, StrSlice, from_utf8_lossy};
74-
use slice;
75-
use slice::{CloneableVector, OwnedCloneableVector, OwnedVector, Vector};
73+
use str::{MaybeOwned, Str, StrSlice, from_utf8_lossy};
74+
use slice::{OwnedCloneableVector, OwnedVector, Vector};
7675
use slice::{ImmutableEqVector, ImmutableVector};
76+
use vec::Vec;
7777

7878
/// Typedef for POSIX file paths.
7979
/// See `posix::Path` for more info.
@@ -184,7 +184,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
184184
fn as_vec<'a>(&'a self) -> &'a [u8];
185185

186186
/// Converts the Path into an owned byte vector
187-
fn into_vec(self) -> ~[u8];
187+
fn into_vec(self) -> Vec<u8>;
188188

189189
/// Returns an object that implements `Show` for printing paths
190190
///
@@ -293,15 +293,15 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
293293
let extlen = extension.container_as_bytes().len();
294294
match (name.rposition_elem(&dot), extlen) {
295295
(None, 0) | (Some(0), 0) => None,
296-
(Some(idx), 0) => Some(name.slice_to(idx).to_owned()),
296+
(Some(idx), 0) => Some(Vec::from_slice(name.slice_to(idx))),
297297
(idx, extlen) => {
298298
let idx = match idx {
299299
None | Some(0) => name.len(),
300300
Some(val) => val
301301
};
302302

303303
let mut v;
304-
v = slice::with_capacity(idx + extlen + 1);
304+
v = Vec::with_capacity(idx + extlen + 1);
305305
v.push_all(name.slice_to(idx));
306306
v.push(dot);
307307
v.push_all(extension.container_as_bytes());
@@ -441,10 +441,10 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
441441
pub trait BytesContainer {
442442
/// Returns a &[u8] representing the receiver
443443
fn container_as_bytes<'a>(&'a self) -> &'a [u8];
444-
/// Consumes the receiver and converts it into ~[u8]
444+
/// Consumes the receiver and converts it into Vec<u8>
445445
#[inline]
446-
fn container_into_owned_bytes(self) -> ~[u8] {
447-
self.container_as_bytes().to_owned()
446+
fn container_into_owned_bytes(self) -> Vec<u8> {
447+
Vec::from_slice(self.container_as_bytes())
448448
}
449449
/// Returns the receiver interpreted as a utf-8 string, if possible
450450
#[inline]
@@ -522,10 +522,6 @@ impl BytesContainer for ~str {
522522
self.as_bytes()
523523
}
524524
#[inline]
525-
fn container_into_owned_bytes(self) -> ~[u8] {
526-
self.into_bytes()
527-
}
528-
#[inline]
529525
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
530526
Some(self.as_slice())
531527
}
@@ -545,8 +541,15 @@ impl BytesContainer for ~[u8] {
545541
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
546542
self.as_slice()
547543
}
544+
}
545+
546+
impl BytesContainer for Vec<u8> {
547+
#[inline]
548+
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
549+
self.as_slice()
550+
}
548551
#[inline]
549-
fn container_into_owned_bytes(self) -> ~[u8] {
552+
fn container_into_owned_bytes(self) -> Vec<u8> {
550553
self
551554
}
552555
}
@@ -564,10 +567,6 @@ impl<'a> BytesContainer for str::MaybeOwned<'a> {
564567
self.as_slice().as_bytes()
565568
}
566569
#[inline]
567-
fn container_into_owned_bytes(self) -> ~[u8] {
568-
self.into_owned().into_bytes()
569-
}
570-
#[inline]
571570
fn container_as_str<'b>(&'b self) -> Option<&'b str> {
572571
Some(self.as_slice())
573572
}

src/libstd/path/posix.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ use iter::{AdditiveIterator, Extendable, Iterator, Map};
2020
use option::{Option, None, Some};
2121
use str;
2222
use str::Str;
23-
use slice;
2423
use slice::{CloneableVector, RevSplits, Splits, Vector, VectorVector,
2524
ImmutableEqVector, OwnedVector, ImmutableVector, OwnedCloneableVector};
25+
use vec::Vec;
26+
2627
use super::{BytesContainer, GenericPath, GenericPathUnsafe};
2728

2829
/// Iterator that yields successive components of a Path as &[u8]
@@ -40,7 +41,7 @@ pub type RevStrComponents<'a> = Map<'a, &'a [u8], Option<&'a str>,
4041
/// Represents a POSIX file path
4142
#[deriving(Clone)]
4243
pub struct Path {
43-
repr: ~[u8], // assumed to never be empty or contain NULs
44+
repr: Vec<u8>, // assumed to never be empty or contain NULs
4445
sepidx: Option<uint> // index of the final separator in repr
4546
}
4647

@@ -103,7 +104,7 @@ impl BytesContainer for Path {
103104
self.as_vec()
104105
}
105106
#[inline]
106-
fn container_into_owned_bytes(self) -> ~[u8] {
107+
fn container_into_owned_bytes(self) -> Vec<u8> {
107108
self.into_vec()
108109
}
109110
}
@@ -119,38 +120,41 @@ impl GenericPathUnsafe for Path {
119120
unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
120121
let path = Path::normalize(path.container_as_bytes());
121122
assert!(!path.is_empty());
122-
let idx = path.rposition_elem(&SEP_BYTE);
123+
let idx = path.as_slice().rposition_elem(&SEP_BYTE);
123124
Path{ repr: path, sepidx: idx }
124125
}
125126

126127
unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
127128
let filename = filename.container_as_bytes();
128129
match self.sepidx {
129-
None if bytes!("..") == self.repr => {
130-
let mut v = slice::with_capacity(3 + filename.len());
130+
None if bytes!("..") == self.repr.as_slice() => {
131+
let mut v = Vec::with_capacity(3 + filename.len());
131132
v.push_all(dot_dot_static);
132133
v.push(SEP_BYTE);
133134
v.push_all(filename);
134-
self.repr = Path::normalize(v);
135+
// FIXME: this is slow
136+
self.repr = Path::normalize(v.as_slice());
135137
}
136138
None => {
137139
self.repr = Path::normalize(filename);
138140
}
139141
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => {
140-
let mut v = slice::with_capacity(self.repr.len() + 1 + filename.len());
141-
v.push_all(self.repr);
142+
let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
143+
v.push_all(self.repr.as_slice());
142144
v.push(SEP_BYTE);
143145
v.push_all(filename);
144-
self.repr = Path::normalize(v);
146+
// FIXME: this is slow
147+
self.repr = Path::normalize(v.as_slice());
145148
}
146149
Some(idx) => {
147-
let mut v = slice::with_capacity(idx + 1 + filename.len());
150+
let mut v = Vec::with_capacity(idx + 1 + filename.len());
148151
v.push_all(self.repr.slice_to(idx+1));
149152
v.push_all(filename);
150-
self.repr = Path::normalize(v);
153+
// FIXME: this is slow
154+
self.repr = Path::normalize(v.as_slice());
151155
}
152156
}
153-
self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
157+
self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
154158
}
155159

156160
unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
@@ -159,13 +163,14 @@ impl GenericPathUnsafe for Path {
159163
if path[0] == SEP_BYTE {
160164
self.repr = Path::normalize(path);
161165
} else {
162-
let mut v = slice::with_capacity(self.repr.len() + path.len() + 1);
163-
v.push_all(self.repr);
166+
let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
167+
v.push_all(self.repr.as_slice());
164168
v.push(SEP_BYTE);
165169
v.push_all(path);
166-
self.repr = Path::normalize(v);
170+
// FIXME: this is slow
171+
self.repr = Path::normalize(v.as_slice());
167172
}
168-
self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
173+
self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
169174
}
170175
}
171176
}
@@ -176,13 +181,13 @@ impl GenericPath for Path {
176181
self.repr.as_slice()
177182
}
178183

179-
fn into_vec(self) -> ~[u8] {
184+
fn into_vec(self) -> Vec<u8> {
180185
self.repr
181186
}
182187

183188
fn dirname<'a>(&'a self) -> &'a [u8] {
184189
match self.sepidx {
185-
None if bytes!("..") == self.repr => self.repr.as_slice(),
190+
None if bytes!("..") == self.repr.as_slice() => self.repr.as_slice(),
186191
None => dot_static,
187192
Some(0) => self.repr.slice_to(1),
188193
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => self.repr.as_slice(),
@@ -192,7 +197,8 @@ impl GenericPath for Path {
192197

193198
fn filename<'a>(&'a self) -> Option<&'a [u8]> {
194199
match self.sepidx {
195-
None if bytes!(".") == self.repr || bytes!("..") == self.repr => None,
200+
None if bytes!(".") == self.repr.as_slice() ||
201+
bytes!("..") == self.repr.as_slice() => None,
196202
None => Some(self.repr.as_slice()),
197203
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => None,
198204
Some(0) if self.repr.slice_from(1).is_empty() => None,
@@ -202,20 +208,20 @@ impl GenericPath for Path {
202208

203209
fn pop(&mut self) -> bool {
204210
match self.sepidx {
205-
None if bytes!(".") == self.repr => false,
211+
None if bytes!(".") == self.repr.as_slice() => false,
206212
None => {
207-
self.repr = ~['.' as u8];
213+
self.repr = vec!['.' as u8];
208214
self.sepidx = None;
209215
true
210216
}
211-
Some(0) if bytes!("/") == self.repr => false,
217+
Some(0) if bytes!("/") == self.repr.as_slice() => false,
212218
Some(idx) => {
213219
if idx == 0 {
214220
self.repr.truncate(idx+1);
215221
} else {
216222
self.repr.truncate(idx);
217223
}
218-
self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
224+
self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
219225
true
220226
}
221227
}
@@ -231,7 +237,7 @@ impl GenericPath for Path {
231237

232238
#[inline]
233239
fn is_absolute(&self) -> bool {
234-
self.repr[0] == SEP_BYTE
240+
*self.repr.get(0) == SEP_BYTE
235241
}
236242

237243
fn is_ancestor_of(&self, other: &Path) -> bool {
@@ -240,7 +246,7 @@ impl GenericPath for Path {
240246
} else {
241247
let mut ita = self.components();
242248
let mut itb = other.components();
243-
if bytes!(".") == self.repr {
249+
if bytes!(".") == self.repr.as_slice() {
244250
return match itb.next() {
245251
None => true,
246252
Some(b) => b != bytes!("..")
@@ -261,6 +267,7 @@ impl GenericPath for Path {
261267
}
262268
}
263269

270+
#[allow(deprecated_owned_vector)]
264271
fn path_relative_from(&self, base: &Path) -> Option<Path> {
265272
if self.is_absolute() != base.is_absolute() {
266273
if self.is_absolute() {
@@ -271,7 +278,7 @@ impl GenericPath for Path {
271278
} else {
272279
let mut ita = self.components();
273280
let mut itb = base.components();
274-
let mut comps = ~[];
281+
let mut comps = vec![];
275282
loop {
276283
match (ita.next(), itb.next()) {
277284
(None, None) => break,
@@ -295,7 +302,7 @@ impl GenericPath for Path {
295302
}
296303
}
297304
}
298-
Some(Path::new(comps.connect_vec(&SEP_BYTE)))
305+
Some(Path::new(comps.as_slice().connect_vec(&SEP_BYTE)))
299306
}
300307
}
301308

@@ -334,7 +341,7 @@ impl Path {
334341

335342
/// Returns a normalized byte vector representation of a path, by removing all empty
336343
/// components, and unnecessary . and .. components.
337-
fn normalize<V: Vector<u8>+CloneableVector<u8>>(v: V) -> ~[u8] {
344+
fn normalize<V: Vector<u8>+CloneableVector<u8>>(v: V) -> Vec<u8> {
338345
// borrowck is being very picky
339346
let val = {
340347
let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
@@ -344,11 +351,11 @@ impl Path {
344351
None => None,
345352
Some(comps) => {
346353
if is_abs && comps.is_empty() {
347-
Some(~[SEP_BYTE])
354+
Some(vec![SEP_BYTE])
348355
} else {
349356
let n = if is_abs { comps.len() } else { comps.len() - 1} +
350357
comps.iter().map(|v| v.len()).sum();
351-
let mut v = slice::with_capacity(n);
358+
let mut v = Vec::with_capacity(n);
352359
let mut it = comps.move_iter();
353360
if !is_abs {
354361
match it.next() {
@@ -366,7 +373,7 @@ impl Path {
366373
}
367374
};
368375
match val {
369-
None => v.into_owned(),
376+
None => Vec::from_slice(v.as_slice()),
370377
Some(val) => val
371378
}
372379
}
@@ -376,7 +383,7 @@ impl Path {
376383
/// /a/b/c and a/b/c yield the same set of components.
377384
/// A path of "/" yields no components. A path of "." yields one component.
378385
pub fn components<'a>(&'a self) -> Components<'a> {
379-
let v = if self.repr[0] == SEP_BYTE {
386+
let v = if *self.repr.get(0) == SEP_BYTE {
380387
self.repr.slice_from(1)
381388
} else { self.repr.as_slice() };
382389
let mut ret = v.split(is_sep_byte);
@@ -390,7 +397,7 @@ impl Path {
390397
/// Returns an iterator that yields each component of the path in reverse.
391398
/// See components() for details.
392399
pub fn rev_components<'a>(&'a self) -> RevComponents<'a> {
393-
let v = if self.repr[0] == SEP_BYTE {
400+
let v = if *self.repr.get(0) == SEP_BYTE {
394401
self.repr.slice_from(1)
395402
} else { self.repr.as_slice() };
396403
let mut ret = v.rsplit(is_sep_byte);
@@ -415,11 +422,11 @@ impl Path {
415422
}
416423

417424
// None result means the byte vector didn't need normalizing
418-
fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<~[&'a [u8]]> {
425+
fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
419426
if is_abs && v.as_slice().is_empty() {
420427
return None;
421428
}
422-
let mut comps: ~[&'a [u8]] = ~[];
429+
let mut comps: Vec<&'a [u8]> = vec![];
423430
let mut n_up = 0u;
424431
let mut changed = false;
425432
for comp in v.split(is_sep_byte) {

0 commit comments

Comments
 (0)