@@ -306,46 +306,44 @@ pub fn split_args(args: &str) -> Result<Vec<String>, String> {
306
306
let args = args. trim ( ) ;
307
307
let mut iter = args. char_indices ( ) . peekable ( ) ;
308
308
309
- while iter. peek ( ) . is_some ( ) {
310
- while let Some ( ( pos, c) ) = iter. next ( ) {
311
- if c == ' ' {
312
- out. push ( args[ start..pos] . to_string ( ) ) ;
313
- let mut found_start = false ;
314
- while let Some ( ( pos, c) ) = iter. peek ( ) {
315
- if * c != ' ' {
316
- start = * pos;
317
- found_start = true ;
318
- break ;
319
- } else {
320
- iter. next ( ) ;
321
- }
309
+ while let Some ( ( pos, c) ) = iter. next ( ) {
310
+ if c == ' ' {
311
+ out. push ( args[ start..pos] . to_string ( ) ) ;
312
+ let mut found_start = false ;
313
+ while let Some ( ( pos, c) ) = iter. peek ( ) {
314
+ if * c != ' ' {
315
+ start = * pos;
316
+ found_start = true ;
317
+ break ;
318
+ } else {
319
+ iter. next ( ) ;
322
320
}
323
- if !found_start {
324
- return Ok ( out) ;
325
- }
326
- } else if c == '"' || c == '\'' {
327
- let end = c;
328
- let mut found_end = false ;
329
- while let Some ( ( _, c) ) = iter. next ( ) {
330
- if c == end {
331
- found_end = true ;
332
- break ;
333
- } else if c == '\\' {
334
- // We skip the escaped character.
335
- iter. next ( ) ;
336
- }
337
- }
338
- if !found_end {
339
- return Err ( format ! (
340
- "Didn't find `{}` at the end of `{}`" ,
341
- end,
342
- & args[ start..]
343
- ) ) ;
321
+ }
322
+ if !found_start {
323
+ return Ok ( out) ;
324
+ }
325
+ } else if c == '"' || c == '\'' {
326
+ let end = c;
327
+ let mut found_end = false ;
328
+ while let Some ( ( _, c) ) = iter. next ( ) {
329
+ if c == end {
330
+ found_end = true ;
331
+ break ;
332
+ } else if c == '\\' {
333
+ // We skip the escaped character.
334
+ iter. next ( ) ;
344
335
}
345
- } else if c == '\\' {
346
- // We skip the escaped character.
347
- iter. next ( ) ;
348
336
}
337
+ if !found_end {
338
+ return Err ( format ! (
339
+ "Didn't find `{}` at the end of `{}`" ,
340
+ end,
341
+ & args[ start..]
342
+ ) ) ;
343
+ }
344
+ } else if c == '\\' {
345
+ // We skip the escaped character.
346
+ iter. next ( ) ;
349
347
}
350
348
}
351
349
let s = args[ start..] . trim ( ) ;
@@ -364,3 +362,26 @@ pub fn remove_file<P: AsRef<Path>>(file_path: &P) -> Result<(), String> {
364
362
)
365
363
} )
366
364
}
365
+
366
+ #[ cfg( test) ]
367
+ mod tests {
368
+ use super :: * ;
369
+
370
+ #[ test]
371
+ fn test_split_args ( ) {
372
+ // Missing `"` at the end.
373
+ assert ! ( split_args( "\" tada" ) . is_err( ) ) ;
374
+ // Missing `'` at the end.
375
+ assert ! ( split_args( "\' tada" ) . is_err( ) ) ;
376
+
377
+ assert_eq ! (
378
+ split_args( "a \" b\" c" ) ,
379
+ Ok ( vec![ "a" . to_string( ) , "\" b\" " . to_string( ) , "c" . to_string( ) ] )
380
+ ) ;
381
+ // Trailing whitespace characters.
382
+ assert_eq ! (
383
+ split_args( " a \" b\" c " ) ,
384
+ Ok ( vec![ "a" . to_string( ) , "\" b\" " . to_string( ) , "c" . to_string( ) ] )
385
+ ) ;
386
+ }
387
+ }
0 commit comments