@@ -20,9 +20,10 @@ use iter::{AdditiveIterator, Extendable, Iterator, Map};
20
20
use option:: { Option , None , Some } ;
21
21
use str;
22
22
use str:: Str ;
23
- use slice;
24
23
use slice:: { CloneableVector , RevSplits , Splits , Vector , VectorVector ,
25
24
ImmutableEqVector , OwnedVector , ImmutableVector , OwnedCloneableVector } ;
25
+ use vec:: Vec ;
26
+
26
27
use super :: { BytesContainer , GenericPath , GenericPathUnsafe } ;
27
28
28
29
/// 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>,
40
41
/// Represents a POSIX file path
41
42
#[ deriving( Clone ) ]
42
43
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
44
45
sepidx : Option < uint > // index of the final separator in repr
45
46
}
46
47
@@ -103,7 +104,7 @@ impl BytesContainer for Path {
103
104
self . as_vec ( )
104
105
}
105
106
#[ inline]
106
- fn container_into_owned_bytes ( self ) -> ~ [ u8 ] {
107
+ fn container_into_owned_bytes ( self ) -> Vec < u8 > {
107
108
self . into_vec ( )
108
109
}
109
110
}
@@ -119,38 +120,41 @@ impl GenericPathUnsafe for Path {
119
120
unsafe fn new_unchecked < T : BytesContainer > ( path : T ) -> Path {
120
121
let path = Path :: normalize ( path. container_as_bytes ( ) ) ;
121
122
assert ! ( !path. is_empty( ) ) ;
122
- let idx = path. rposition_elem ( & SEP_BYTE ) ;
123
+ let idx = path. as_slice ( ) . rposition_elem ( & SEP_BYTE ) ;
123
124
Path { repr : path, sepidx : idx }
124
125
}
125
126
126
127
unsafe fn set_filename_unchecked < T : BytesContainer > ( & mut self , filename : T ) {
127
128
let filename = filename. container_as_bytes ( ) ;
128
129
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 ( ) ) ;
131
132
v. push_all ( dot_dot_static) ;
132
133
v. push ( SEP_BYTE ) ;
133
134
v. push_all ( filename) ;
134
- self . repr = Path :: normalize ( v) ;
135
+ // FIXME: this is slow
136
+ self . repr = Path :: normalize ( v. as_slice ( ) ) ;
135
137
}
136
138
None => {
137
139
self . repr = Path :: normalize ( filename) ;
138
140
}
139
141
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 ( ) ) ;
142
144
v. push ( SEP_BYTE ) ;
143
145
v. push_all ( filename) ;
144
- self . repr = Path :: normalize ( v) ;
146
+ // FIXME: this is slow
147
+ self . repr = Path :: normalize ( v. as_slice ( ) ) ;
145
148
}
146
149
Some ( idx) => {
147
- let mut v = slice :: with_capacity ( idx + 1 + filename. len ( ) ) ;
150
+ let mut v = Vec :: with_capacity ( idx + 1 + filename. len ( ) ) ;
148
151
v. push_all ( self . repr . slice_to ( idx+1 ) ) ;
149
152
v. push_all ( filename) ;
150
- self . repr = Path :: normalize ( v) ;
153
+ // FIXME: this is slow
154
+ self . repr = Path :: normalize ( v. as_slice ( ) ) ;
151
155
}
152
156
}
153
- self . sepidx = self . repr . rposition_elem ( & SEP_BYTE ) ;
157
+ self . sepidx = self . repr . as_slice ( ) . rposition_elem ( & SEP_BYTE ) ;
154
158
}
155
159
156
160
unsafe fn push_unchecked < T : BytesContainer > ( & mut self , path : T ) {
@@ -159,13 +163,14 @@ impl GenericPathUnsafe for Path {
159
163
if path[ 0 ] == SEP_BYTE {
160
164
self . repr = Path :: normalize ( path) ;
161
165
} 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 ( ) ) ;
164
168
v. push ( SEP_BYTE ) ;
165
169
v. push_all ( path) ;
166
- self . repr = Path :: normalize ( v) ;
170
+ // FIXME: this is slow
171
+ self . repr = Path :: normalize ( v. as_slice ( ) ) ;
167
172
}
168
- self . sepidx = self . repr . rposition_elem ( & SEP_BYTE ) ;
173
+ self . sepidx = self . repr . as_slice ( ) . rposition_elem ( & SEP_BYTE ) ;
169
174
}
170
175
}
171
176
}
@@ -176,13 +181,13 @@ impl GenericPath for Path {
176
181
self . repr . as_slice ( )
177
182
}
178
183
179
- fn into_vec ( self ) -> ~ [ u8 ] {
184
+ fn into_vec ( self ) -> Vec < u8 > {
180
185
self . repr
181
186
}
182
187
183
188
fn dirname < ' a > ( & ' a self ) -> & ' a [ u8 ] {
184
189
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 ( ) ,
186
191
None => dot_static,
187
192
Some ( 0 ) => self . repr . slice_to ( 1 ) ,
188
193
Some ( idx) if self . repr . slice_from ( idx+1 ) == bytes ! ( ".." ) => self . repr . as_slice ( ) ,
@@ -192,7 +197,8 @@ impl GenericPath for Path {
192
197
193
198
fn filename < ' a > ( & ' a self ) -> Option < & ' a [ u8 ] > {
194
199
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 ,
196
202
None => Some ( self . repr . as_slice ( ) ) ,
197
203
Some ( idx) if self . repr . slice_from ( idx+1 ) == bytes ! ( ".." ) => None ,
198
204
Some ( 0 ) if self . repr . slice_from ( 1 ) . is_empty ( ) => None ,
@@ -202,20 +208,20 @@ impl GenericPath for Path {
202
208
203
209
fn pop ( & mut self ) -> bool {
204
210
match self . sepidx {
205
- None if bytes ! ( "." ) == self . repr => false ,
211
+ None if bytes ! ( "." ) == self . repr . as_slice ( ) => false ,
206
212
None => {
207
- self . repr = ~ [ '.' as u8 ] ;
213
+ self . repr = vec ! [ '.' as u8 ] ;
208
214
self . sepidx = None ;
209
215
true
210
216
}
211
- Some ( 0 ) if bytes ! ( "/" ) == self . repr => false ,
217
+ Some ( 0 ) if bytes ! ( "/" ) == self . repr . as_slice ( ) => false ,
212
218
Some ( idx) => {
213
219
if idx == 0 {
214
220
self . repr . truncate ( idx+1 ) ;
215
221
} else {
216
222
self . repr . truncate ( idx) ;
217
223
}
218
- self . sepidx = self . repr . rposition_elem ( & SEP_BYTE ) ;
224
+ self . sepidx = self . repr . as_slice ( ) . rposition_elem ( & SEP_BYTE ) ;
219
225
true
220
226
}
221
227
}
@@ -231,7 +237,7 @@ impl GenericPath for Path {
231
237
232
238
#[ inline]
233
239
fn is_absolute ( & self ) -> bool {
234
- self . repr [ 0 ] == SEP_BYTE
240
+ * self . repr . get ( 0 ) == SEP_BYTE
235
241
}
236
242
237
243
fn is_ancestor_of ( & self , other : & Path ) -> bool {
@@ -240,7 +246,7 @@ impl GenericPath for Path {
240
246
} else {
241
247
let mut ita = self . components ( ) ;
242
248
let mut itb = other. components ( ) ;
243
- if bytes ! ( "." ) == self . repr {
249
+ if bytes ! ( "." ) == self . repr . as_slice ( ) {
244
250
return match itb. next ( ) {
245
251
None => true ,
246
252
Some ( b) => b != bytes ! ( ".." )
@@ -261,6 +267,7 @@ impl GenericPath for Path {
261
267
}
262
268
}
263
269
270
+ #[ allow( deprecated_owned_vector) ]
264
271
fn path_relative_from ( & self , base : & Path ) -> Option < Path > {
265
272
if self . is_absolute ( ) != base. is_absolute ( ) {
266
273
if self . is_absolute ( ) {
@@ -271,7 +278,7 @@ impl GenericPath for Path {
271
278
} else {
272
279
let mut ita = self . components ( ) ;
273
280
let mut itb = base. components ( ) ;
274
- let mut comps = ~ [ ] ;
281
+ let mut comps = vec ! [ ] ;
275
282
loop {
276
283
match ( ita. next ( ) , itb. next ( ) ) {
277
284
( None , None ) => break ,
@@ -295,7 +302,7 @@ impl GenericPath for Path {
295
302
}
296
303
}
297
304
}
298
- Some ( Path :: new ( comps. connect_vec ( & SEP_BYTE ) ) )
305
+ Some ( Path :: new ( comps. as_slice ( ) . connect_vec ( & SEP_BYTE ) ) )
299
306
}
300
307
}
301
308
@@ -334,7 +341,7 @@ impl Path {
334
341
335
342
/// Returns a normalized byte vector representation of a path, by removing all empty
336
343
/// 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 > {
338
345
// borrowck is being very picky
339
346
let val = {
340
347
let is_abs = !v. as_slice ( ) . is_empty ( ) && v. as_slice ( ) [ 0 ] == SEP_BYTE ;
@@ -344,11 +351,11 @@ impl Path {
344
351
None => None ,
345
352
Some ( comps) => {
346
353
if is_abs && comps. is_empty ( ) {
347
- Some ( ~ [ SEP_BYTE ] )
354
+ Some ( vec ! [ SEP_BYTE ] )
348
355
} else {
349
356
let n = if is_abs { comps. len ( ) } else { comps. len ( ) - 1 } +
350
357
comps. iter ( ) . map ( |v| v. len ( ) ) . sum ( ) ;
351
- let mut v = slice :: with_capacity ( n) ;
358
+ let mut v = Vec :: with_capacity ( n) ;
352
359
let mut it = comps. move_iter ( ) ;
353
360
if !is_abs {
354
361
match it. next ( ) {
@@ -366,7 +373,7 @@ impl Path {
366
373
}
367
374
} ;
368
375
match val {
369
- None => v . into_owned ( ) ,
376
+ None => Vec :: from_slice ( v . as_slice ( ) ) ,
370
377
Some ( val) => val
371
378
}
372
379
}
@@ -376,7 +383,7 @@ impl Path {
376
383
/// /a/b/c and a/b/c yield the same set of components.
377
384
/// A path of "/" yields no components. A path of "." yields one component.
378
385
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 {
380
387
self . repr . slice_from ( 1 )
381
388
} else { self . repr . as_slice ( ) } ;
382
389
let mut ret = v. split ( is_sep_byte) ;
@@ -390,7 +397,7 @@ impl Path {
390
397
/// Returns an iterator that yields each component of the path in reverse.
391
398
/// See components() for details.
392
399
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 {
394
401
self . repr . slice_from ( 1 )
395
402
} else { self . repr . as_slice ( ) } ;
396
403
let mut ret = v. rsplit ( is_sep_byte) ;
@@ -415,11 +422,11 @@ impl Path {
415
422
}
416
423
417
424
// 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 ] > > {
419
426
if is_abs && v. as_slice ( ) . is_empty ( ) {
420
427
return None ;
421
428
}
422
- let mut comps: ~ [ & ' a [ u8 ] ] = ~ [ ] ;
429
+ let mut comps: Vec < & ' a [ u8 ] > = vec ! [ ] ;
423
430
let mut n_up = 0 u;
424
431
let mut changed = false ;
425
432
for comp in v. split ( is_sep_byte) {
0 commit comments