@@ -8,8 +8,9 @@ use extname;
8
8
use plus;
9
9
use relative_path_from;
10
10
use debug;
11
- use helpers:: TryFrom ;
11
+ use helpers:: { TryFrom , to_str } ;
12
12
use pathname_sys:: null_byte_check;
13
+ use path_parsing:: { SEP , find_last_non_sep_pos} ;
13
14
14
15
use ruru;
15
16
use ruru:: {
@@ -84,11 +85,11 @@ impl TryFrom<AnyObject> for Pathname {
84
85
type Error = debug:: RubyDebugInfo ;
85
86
fn try_from ( obj : AnyObject ) -> Result < Pathname , Self :: Error > {
86
87
if Class :: from_existing ( "String" ) . case_equals ( & obj) {
87
- Ok ( Pathname :: new ( & RString :: from ( obj. value ( ) ) . to_string ( ) ) )
88
+ Ok ( Pathname :: new ( & RString :: from ( obj. value ( ) ) . to_str ( ) ) )
88
89
} else if Class :: from_existing ( "Pathname" ) . case_equals ( & obj) {
89
90
Ok ( Pathname :: from ( obj. value ( ) ) )
90
91
} else if obj. respond_to ( "to_path" ) {
91
- Ok ( Pathname :: new ( & RString :: from ( obj. send ( "to_path" , None ) . value ( ) ) . to_string ( ) ) )
92
+ Ok ( Pathname :: new ( & RString :: from ( obj. send ( "to_path" , None ) . value ( ) ) . to_str ( ) ) )
92
93
} else {
93
94
Err ( Self :: Error :: from ( obj) )
94
95
}
@@ -113,15 +114,8 @@ impl VerifiedObject for Pathname {
113
114
}
114
115
}
115
116
116
- fn to_str ( maybe_string : & MaybeString ) -> & str {
117
- match maybe_string {
118
- & Ok ( ref ruru_string) => ruru_string. to_str ( ) ,
119
- & Err ( _) => "" ,
120
- }
121
- }
122
-
123
117
pub fn pn_add_trailing_separator ( pth : MaybeString ) -> RString {
124
- let p = pth. ok ( ) . unwrap ( ) ;
118
+ let p = pth. unwrap ( ) ;
125
119
let x = format ! ( "{}{}" , p. to_str( ) , "a" ) ;
126
120
match x. rsplit_terminator ( MAIN_SEPARATOR ) . next ( ) {
127
121
Some ( "a" ) => p,
@@ -130,10 +124,7 @@ pub fn pn_add_trailing_separator(pth: MaybeString) -> RString {
130
124
}
131
125
132
126
pub fn pn_is_absolute ( pth : MaybeString ) -> Boolean {
133
- Boolean :: new ( match to_str ( & pth) . chars ( ) . next ( ) {
134
- Some ( c) => c == MAIN_SEPARATOR ,
135
- None => false
136
- } )
127
+ Boolean :: new ( to_str ( & pth) . as_bytes ( ) . get ( 0 ) == Some ( & SEP ) )
137
128
}
138
129
139
130
// pub fn pn_ascend(){}
@@ -143,16 +134,16 @@ pub fn pn_basename(pth: MaybeString, ext: MaybeString) -> RString {
143
134
}
144
135
145
136
pub fn pn_children ( pth : MaybeString , with_dir : MaybeBoolean ) -> Result < AnyObject , Exception > {
146
- let val = pth. ok ( ) . unwrap_or ( RString :: new ( "." ) ) ;
147
- let val = val . to_str ( ) ;
137
+ let path = pth. unwrap_or ( RString :: new ( "." ) ) ;
138
+ let path = path . to_str ( ) ;
148
139
149
- if let Ok ( entries) = fs:: read_dir ( val ) {
150
- let mut with_directory = with_dir. ok ( ) . unwrap_or ( Boolean :: new ( true ) ) . to_bool ( ) ;
151
- if val == "." {
140
+ if let Ok ( entries) = fs:: read_dir ( path ) {
141
+ let mut with_directory = with_dir. unwrap_or ( Boolean :: new ( true ) ) . to_bool ( ) ;
142
+ if path == "." {
152
143
with_directory = false ;
153
144
}
154
145
155
- let mut arr = Array :: new ( ) ;
146
+ let mut arr = Array :: with_capacity ( entries . size_hint ( ) . 1 . unwrap_or ( 0 ) ) ;
156
147
for entry in entries {
157
148
if with_directory {
158
149
match entry {
@@ -169,21 +160,21 @@ pub fn pn_children(pth: MaybeString, with_dir: MaybeBoolean) -> Result<AnyObject
169
160
170
161
Ok ( arr. to_any_object ( ) )
171
162
} else {
172
- let msg = format ! ( "No such file or directory @ dir_initialize - {}" , val ) ;
163
+ let msg = format ! ( "No such file or directory @ dir_initialize - {}" , path ) ;
173
164
Err ( Exception :: new ( "Errno::NOENT" , Some ( & msg) ) )
174
165
}
175
166
}
176
167
177
168
pub fn pn_children_compat ( pth : MaybeString , with_dir : MaybeBoolean ) -> Result < AnyObject , Exception > {
178
- let val = to_str ( & pth) ;
169
+ let path = to_str ( & pth) ;
179
170
180
- if let Ok ( entries) = fs:: read_dir ( val ) {
181
- let mut with_directory = with_dir. ok ( ) . unwrap_or ( Boolean :: new ( true ) ) . to_bool ( ) ;
182
- if val == "." {
171
+ if let Ok ( entries) = fs:: read_dir ( path ) {
172
+ let mut with_directory = with_dir. unwrap_or ( Boolean :: new ( true ) ) . to_bool ( ) ;
173
+ if path == "." {
183
174
with_directory = false ;
184
175
}
185
176
186
- let mut arr = Array :: new ( ) ;
177
+ let mut arr = Array :: with_capacity ( entries . size_hint ( ) . 1 . unwrap_or ( 0 ) ) ;
187
178
for entry in entries {
188
179
if with_directory {
189
180
if let Ok ( v) = entry {
@@ -198,18 +189,17 @@ pub fn pn_children_compat(pth: MaybeString, with_dir: MaybeBoolean) -> Result<An
198
189
199
190
Ok ( arr. to_any_object ( ) )
200
191
} else {
201
- let msg = format ! ( "No such file or directory @ dir_initialize - {}" , val ) ;
192
+ let msg = format ! ( "No such file or directory @ dir_initialize - {}" , path ) ;
202
193
Err ( Exception :: new ( "Errno::NOENT" , Some ( & msg) ) )
203
194
}
204
195
}
205
196
206
197
pub fn pn_chop_basename ( pth : MaybeString ) -> AnyObject {
207
- let mut arr = Array :: with_capacity ( 2 ) ;
208
- let results = chop_basename:: chop_basename ( to_str ( & pth) ) ;
209
- match results {
198
+ match chop_basename:: chop_basename ( to_str ( & pth) ) {
210
199
Some ( ( dirname, basename) ) => {
211
- arr. push ( RString :: new ( & dirname[ ..] ) ) ;
212
- arr. push ( RString :: new ( & basename[ ..] ) ) ;
200
+ let mut arr = Array :: with_capacity ( 2 ) ;
201
+ arr. push ( RString :: new ( & dirname) ) ;
202
+ arr. push ( RString :: new ( & basename) ) ;
213
203
arr. to_any_object ( )
214
204
} ,
215
205
None => NilClass :: new ( ) . to_any_object ( )
@@ -227,22 +217,19 @@ pub fn pn_cleanpath_conservative(pth: MaybeString) -> RString {
227
217
}
228
218
229
219
pub fn pn_del_trailing_separator ( pth : MaybeString ) -> RString {
230
- if let & Ok ( ref path ) = & pth {
231
- let path = path . to_str ( ) ;
232
-
233
- if !path . is_empty ( ) {
234
- let path = path . trim_right_matches ( '/' ) ;
235
-
236
- if path . is_empty ( ) {
237
- return RString :: new ( "/" ) ;
238
- } else {
239
- return RString :: new ( path) ;
240
- }
220
+ {
221
+ let path = to_str ( & pth ) ;
222
+ if path . is_empty ( ) {
223
+ return RString :: new ( "/" ) ;
224
+ }
225
+ let pos = match find_last_non_sep_pos ( path . as_bytes ( ) ) {
226
+ Some ( pos ) => pos ,
227
+ None => return RString :: new ( "/" ) ,
228
+ } ;
229
+ if pos != path. len ( ) - 1 {
230
+ return RString :: new ( & path [ ..pos + 1 ] ) ;
241
231
}
242
- } else {
243
- return RString :: new ( "" ) ;
244
232
}
245
-
246
233
pth. unwrap ( )
247
234
}
248
235
@@ -263,41 +250,39 @@ pub fn pn_dirname(pth: MaybeString) -> RString {
263
250
// }
264
251
265
252
pub fn pn_entries ( pth : MaybeString ) -> Result < AnyObject , Exception > {
266
- if let Ok ( files) = fs:: read_dir ( to_str ( & pth) ) {
267
- let mut arr = Array :: new ( ) ;
253
+ let path = to_str ( & pth) ;
254
+ if let Ok ( files) = fs:: read_dir ( path) {
255
+ let mut arr = Array :: with_capacity ( files. size_hint ( ) . 1 . unwrap_or ( 0 ) + 2 ) ;
268
256
269
257
arr. push ( RString :: new ( "." ) ) ;
270
258
arr. push ( RString :: new ( ".." ) ) ;
271
259
272
260
for file in files {
273
- let file_name_str = file. unwrap ( ) . file_name ( ) . into_string ( ) . unwrap ( ) ;
274
- arr. push ( RString :: new ( & file_name_str[ ..] ) ) ;
261
+ arr. push ( RString :: new ( file. unwrap ( ) . file_name ( ) . to_str ( ) . unwrap ( ) ) ) ;
275
262
}
276
263
277
264
Ok ( arr. to_any_object ( ) )
278
265
} else {
279
- let val = pth. unwrap_or ( RString :: new ( "" ) ) ;
280
- let msg = format ! ( "No such file or directory @ dir_initialize - {}" , val. to_str( ) ) ;
266
+ let msg = format ! ( "No such file or directory @ dir_initialize - {}" , path) ;
281
267
Err ( Exception :: new ( "Errno::NOENT" , Some ( & msg) ) )
282
268
}
283
269
}
284
270
285
271
pub fn pn_entries_compat ( pth : MaybeString ) -> Result < AnyObject , Exception > {
286
- if let Ok ( files) = fs:: read_dir ( to_str ( & pth) ) {
287
- let mut arr = Array :: new ( ) ;
272
+ let path = to_str ( & pth) ;
273
+ if let Ok ( files) = fs:: read_dir ( path) {
274
+ let mut arr = Array :: with_capacity ( files. size_hint ( ) . 1 . unwrap_or ( 0 ) + 2 ) ;
288
275
289
276
arr. push ( Pathname :: new ( "." ) ) ;
290
277
arr. push ( Pathname :: new ( ".." ) ) ;
291
278
292
279
for file in files {
293
- let file_name_str = file. unwrap ( ) . file_name ( ) . into_string ( ) . unwrap ( ) ;
294
- arr. push ( Pathname :: new ( & file_name_str) ) ;
280
+ arr. push ( Pathname :: new ( file. unwrap ( ) . file_name ( ) . to_str ( ) . unwrap ( ) ) ) ;
295
281
}
296
282
297
283
Ok ( arr. to_any_object ( ) )
298
284
} else {
299
- let val = pth. unwrap_or ( RString :: new ( "" ) ) ;
300
- let msg = format ! ( "No such file or directory @ dir_initialize - {}" , val. to_str( ) ) ;
285
+ let msg = format ! ( "No such file or directory @ dir_initialize - {}" , path) ;
301
286
Err ( Exception :: new ( "Errno::NOENT" , Some ( & msg) ) )
302
287
}
303
288
}
@@ -309,11 +294,9 @@ pub fn pn_extname(pth: MaybeString) -> RString {
309
294
// pub fn pn_find(pth: MaybeString, ignore_error: Boolean){}
310
295
311
296
pub fn pn_has_trailing_separator ( pth : MaybeString ) -> Boolean {
312
- let v = pth. ok ( ) . unwrap_or ( RString :: new ( "" ) ) ;
313
- match chop_basename:: chop_basename ( v. to_str ( ) ) {
314
- Some ( ( a, b) ) => {
315
- Boolean :: new ( a. len ( ) + b. len ( ) < v. to_str ( ) . len ( ) )
316
- } ,
297
+ let v = to_str ( & pth) ;
298
+ match chop_basename:: chop_basename ( v) {
299
+ Some ( ( a, b) ) => Boolean :: new ( a. len ( ) + b. len ( ) < v. len ( ) ) ,
317
300
_ => Boolean :: new ( false )
318
301
}
319
302
}
@@ -333,7 +316,7 @@ pub fn pn_join(args: MaybeArray) -> AnyObject {
333
316
334
317
let item = args. pop ( ) ;
335
318
result = plus:: plus_paths ( & anyobject_to_string ( item) . unwrap ( ) , & result) ;
336
- if result. chars ( ) . next ( ) == Some ( MAIN_SEPARATOR ) {
319
+ if result. as_bytes ( ) . get ( 0 ) == Some ( & SEP ) {
337
320
return Pathname :: new ( & result) . to_any_object ( )
338
321
}
339
322
@@ -354,23 +337,17 @@ pub fn pn_join(args: MaybeArray) -> AnyObject {
354
337
// pub fn pn_parent(pth: MaybeString){}
355
338
356
339
pub fn pn_plus ( pth1 : MaybeString , pth2 : MaybeString ) -> RString {
357
- RString :: new (
358
- & plus:: plus_paths (
359
- pth1. ok ( ) . unwrap_or ( RString :: new ( "" ) ) . to_str ( ) ,
360
- pth2. ok ( ) . unwrap_or ( RString :: new ( "" ) ) . to_str ( )
361
- ) [ ..]
362
- )
340
+ RString :: new ( & plus:: plus_paths ( to_str ( & pth1) , to_str ( & pth2) ) )
363
341
}
364
342
365
343
// pub fn pn_prepend_prefix(prefix: MaybeString, relpath: MaybeString){}
366
344
367
345
pub fn pn_is_relative ( pth : MaybeString ) -> Boolean {
368
- Boolean :: new (
369
- match pth. ok ( ) . unwrap_or ( RString :: new ( & MAIN_SEPARATOR . to_string ( ) [ ..] ) ) . to_str ( ) . chars ( ) . next ( ) {
370
- Some ( c) => c != MAIN_SEPARATOR ,
371
- None => true
372
- }
373
- )
346
+ let path = match & pth {
347
+ & Ok ( ref ruru_string) => ruru_string. to_str ( ) ,
348
+ & Err ( _) => return Boolean :: new ( false ) ,
349
+ } ;
350
+ Boolean :: new ( path. as_bytes ( ) . get ( 0 ) != Some ( & SEP ) )
374
351
}
375
352
376
353
// pub fn pn_root(pth: MaybeString){}
0 commit comments