fn make_command_line(prog: &OsStr, args: &[Arg], force_quotes: bool) -> io::Result<Vec<u16>> {
    // Encode the command and arguments in a command line string such
    // that the spawned process may recover them using CommandLineToArgvW.
    let mut cmd: Vec<u16> = Vec::new();
    // Always quote the program name so CreateProcess doesn't interpret args as
    // part of the name if the binary wasn't found first time.
    append_arg(&mut cmd, prog, Quote::Always)?;
    for arg in args {
        cmd.push(' ' as u16);
        let (arg, quote) = match arg {
            Arg::Regular(arg) => (arg, if force_quotes { Quote::Always } else { Quote::Auto }),
            Arg::Raw(arg) => (arg, Quote::Never),
        };
        append_arg(&mut cmd, arg, quote)?;
    }
    return Ok(cmd);

    fn append_arg(cmd: &mut Vec<u16>, arg: &OsStr, quote: Quote) -> io::Result<()> {
        // If an argument has 0 characters then we need to quote it to ensure
        // that it actually gets passed through on the command line or otherwise
        // it will be dropped entirely when parsed on the other end.
        ensure_no_nuls(arg)?;
        let arg_bytes = &arg.as_inner().inner.as_inner();
        let (quote, escape) = match quote {
            Quote::Always => (true, true),
            Quote::Auto => {
                (arg_bytes.iter().any(|c| *c == b' ' || *c == b'\t') || arg_bytes.is_empty(), true)
            }
            Quote::Never => (false, false),
        };
        if quote {
            cmd.push('"' as u16);
        }

        let mut backslashes: usize = 0;
        for x in arg.encode_wide() {
            if escape {
                if x == '\\' as u16 {
                    backslashes += 1;
                } else {
                    if x == '"' as u16 {
                        // Add n+1 backslashes to total 2n+1 before internal '"'.
                        cmd.extend((0..=backslashes).map(|_| '\\' as u16));
                    }
                    backslashes = 0;
                }
            }
            cmd.push(x);
        }

        if quote {
            // Add n backslashes to total 2n before ending '"'.
            cmd.extend((0..backslashes).map(|_| '\\' as u16));
            cmd.push('"' as u16);
        }
        Ok(())
    }
}
// Produces a wide string *without terminating null*; returns an error if
// `prog` or any of the `args` contain a nul.
Produces a wide string *without terminating null*; returns an error if
`prog` or any of the `args` contain a nul.
Produces a wide string * without terminating null * ; returns an error if ` prog ` or any of the ` args ` contain a nul .
fn make_command_line(prog: &OsStr, args: &[Arg], force_quotes: bool) -> io::Result<Vec<u16>> {
    // Encode the command and arguments in a command line string such
    // that the spawned process may recover them using CommandLineToArgvW.
    let mut cmd: Vec<u16> = Vec::new();
    // Always quote the program name so CreateProcess doesn't interpret args as
    // part of the name if the binary wasn't found first time.
    append_arg(&mut cmd, prog, Quote::Always)?;
    for arg in args {
        cmd.push(' ' as u16);
        let (arg, quote) = match arg {
            Arg::Regular(arg) => (arg, if force_quotes { Quote::Always } else { Quote::Auto }),
            Arg::Raw(arg) => (arg, Quote::Never),
        };
        append_arg(&mut cmd, arg, quote)?;
    }
    return Ok(cmd);
    fn append_arg(cmd: &mut Vec<u16>, arg: &OsStr, quote: Quote) -> io::Result<()> {
        // If an argument has 0 characters then we need to quote it to ensure
        // that it actually gets passed through on the command line or otherwise
        // it will be dropped entirely when parsed on the other end.
        ensure_no_nuls(arg)?;
        let arg_bytes = &arg.as_inner().inner.as_inner();
        let (quote, escape) = match quote {
            Quote::Always => (true, true),
            Quote::Auto => {
                (arg_bytes.iter().any(|c| *c == b' ' || *c == b'\t') || arg_bytes.is_empty(), true)
            }
            Quote::Never => (false, false),
        };
        if quote {
            cmd.push('"' as u16);
        }
        let mut backslashes: usize = 0;
        for x in arg.encode_wide() {
            if escape {
                if x == '\\' as u16 {
                    backslashes += 1;
                } else {
                    if x == '"' as u16 {
                        // Add n+1 backslashes to total 2n+1 before internal '"'.
                        cmd.extend((0..=backslashes).map(|_| '\\' as u16));
                    }
                    backslashes = 0;
                }
            }
            cmd.push(x);
        }
        if quote {
            // Add n backslashes to total 2n before ending '"'.
            cmd.extend((0..backslashes).map(|_| '\\' as u16));
            cmd.push('"' as u16);
        }
        Ok(())
    }
}
fn make_command_line ( prog : & OsStr , args : & [ Arg ] , force_quotes : bool ) -> io :: Result < Vec < u16 > > { // Encode the command and arguments in a command line string such // that the spawned process may recover them using CommandLineToArgvW. let mut cmd : Vec < u16 > = Vec :: new ( ) ; // Always quote the program name so CreateProcess doesn't interpret args as // part of the name if the binary wasn't found first time. append_arg ( & mut cmd , prog , Quote :: Always ) ? ; for arg in args { cmd . push ( ' ' as u16 ) ; let ( arg , quote ) = match arg { Arg :: Regular ( arg ) => ( arg , if force_quotes { Quote :: Always } else { Quote :: Auto } ) , Arg :: Raw ( arg ) => ( arg , Quote :: Never ) , } ; append_arg ( & mut cmd , arg , quote ) ? ; } return Ok ( cmd ) ; fn append_arg ( cmd : & mut Vec < u16 > , arg : & OsStr , quote : Quote ) -> io :: Result < ( ) > { // If an argument has 0 characters then we need to quote it to ensure // that it actually gets passed through on the command line or otherwise // it will be dropped entirely when parsed on the other end. ensure_no_nuls ( arg ) ? ; let arg_bytes = & arg . as_inner ( ) . inner . as_inner ( ) ; let ( quote , escape ) = match quote { Quote :: Always => ( true , true ) , Quote :: Auto => { ( arg_bytes . iter ( ) . any ( | c | * c == b' ' || * c == b'\t' ) || arg_bytes . is_empty ( ) , true ) } Quote :: Never => ( false , false ) , } ; if quote { cmd . push ( '"' as u16 ) ; } let mut backslashes : usize = 0 ; for x in arg . encode_wide ( ) { if escape { if x == '\\' as u16 { backslashes += 1 ; } else { if x == '"' as u16 { // Add n+1 backslashes to total 2n+1 before internal '"'. cmd . extend ( ( 0 ..= backslashes ) . map ( | _ | '\\' as u16 ) ) ; } backslashes = 0 ; } } cmd . push ( x ) ; } if quote { // Add n backslashes to total 2n before ending '"'. cmd . extend ( ( 0 .. backslashes ) . map ( | _ | '\\' as u16 ) ) ; cmd . push ( '"' as u16 ) ; } Ok ( ( ) ) } }
Produces a wide string *without terminating null*; returns an error if
`prog` or any of the `args` contain a nul.
Produces a wide string * without terminating null * ; returns an error if ` prog ` or any of the ` args ` contain a nul .
prog
&OsStr
args
&[Arg]
force_quotes
bool