5
5
use clap:: { Arg , ArgAction , Command } ;
6
6
use std:: io:: stdout;
7
7
use std:: ops:: ControlFlow ;
8
- #[ cfg( unix) ]
9
- use std:: os:: unix:: ffi:: { OsStrExt , OsStringExt } ;
10
- #[ cfg( windows) ]
11
- use std:: os:: windows:: ffi:: OsStrExt ;
12
8
use uucore:: error:: { UResult , UUsageError } ;
13
9
use uucore:: format:: { FormatArgument , FormatItem , parse_spec_and_escape} ;
14
- use uucore:: { format_usage, help_about, help_section, help_usage, show_warning} ;
10
+ use uucore:: { format_usage, help_about, help_section, help_usage, os_str_as_bytes , show_warning} ;
15
11
16
12
const VERSION : & str = "version" ;
17
13
const HELP : & str = "help" ;
@@ -30,33 +26,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
30
26
let format = matches
31
27
. get_one :: < std:: ffi:: OsString > ( options:: FORMAT )
32
28
. ok_or_else ( || UUsageError :: new ( 1 , "missing operand" ) ) ?;
33
-
34
- #[ cfg( unix) ]
35
- let format = format. as_bytes ( ) ;
36
-
37
- #[ cfg( windows) ]
38
- let format_vec: Vec < u8 > = format
39
- . encode_wide ( )
40
- . flat_map ( |wchar| wchar. to_le_bytes ( ) )
41
- . collect ( ) ;
42
- #[ cfg( windows) ]
43
- let format = format_vec. as_slice ( ) ;
29
+ let format = os_str_as_bytes ( format) ?;
44
30
45
31
let values: Vec < _ > = match matches. get_many :: < std:: ffi:: OsString > ( options:: ARGUMENT ) {
32
+ // FIXME: use os_str_as_bytes once FormatArgument supports Vec<u8>
46
33
Some ( s) => s
47
- . map ( |os_str| {
48
- #[ cfg( unix) ]
49
- let raw_bytes: Vec < u8 > = os_str. clone ( ) . into_vec ( ) ;
50
-
51
- #[ cfg( windows) ]
52
- let raw_bytes: Vec < u8 > = os_str
53
- . encode_wide ( )
54
- . flat_map ( |wchar| wchar. to_le_bytes ( ) )
55
- . collect ( ) ;
56
- FormatArgument :: Unparsed (
57
- String :: from_utf8 ( raw_bytes. clone ( ) )
58
- . unwrap_or_else ( |_| raw_bytes. iter ( ) . map ( |& b| b as char ) . collect ( ) ) ,
59
- )
34
+ . map ( |os_string| {
35
+ FormatArgument :: Unparsed ( std:: ffi:: OsStr :: to_string_lossy ( os_string) . to_string ( ) )
60
36
} )
61
37
. collect ( ) ,
62
38
None => vec ! [ ] ,
0 commit comments