pub fn substitute_commands(shell: &mut Shell, string: &str) -> Result<String, CmdSubError> {
    let mut string = string.to_string();
    // Tokenizing and capturing cmbsubs first
    lazy_static! {
        static ref CMDSUB_RE: Regex = Regex::new(
            "(\\$|@)\\([\\a-zA-Z0-9 \"-.@~/\\|<>\\[\\]\\&$()]+\\)"
        ).unwrap();
    }
    for capture in CMDSUB_RE.captures_iter(&string.clone()) {
        if let Some(capture) = capture.get(0) {
            let mut capture = capture.as_str().to_string();
            capture.pop();
            let output = execute_commands_once(shell, &capture[2..])?;
            capture.push(')');
            string = string.replacen(&capture, &output, 1);
        }
    }
    let mut stringchars = string.chars();
    let mut captures: Vec<String> = Vec::new();
    let mut rest: Vec<String> = Vec::new();
    let mut word = String::new();
    let mut in_quote = false;
    while let Some(c) = stringchars.next() {
        match c {
            '`' if !in_quote => {
                in_quote = true;
                rest.push(word.clone());
                word.clear();
            }
            '`' if in_quote => {
                in_quote = false;
                captures.push(word.clone());
                word.clear();
            }
            '\\' => {
                if let Some(c) = stringchars.next() {
                    word.push(c);
                    continue;
                }
            }
            _ => {
                word.push(c);
            }
        }
    }
    rest.push(word);
    let mut outputs = Vec::<String>::new();
    for capture in captures {
        outputs.push(execute_commands_once(shell, &capture)?);
    }
    let mut final_str = String::new();
    let mut outputs = outputs.iter();
    for string in rest {
        final_str.push_str(&string);
        if let Some(output) = outputs.next() {
            final_str.push_str(output)
        }
    }

    Ok(final_str)
}
// This command is gonna be sooo fucking slow
This command is gonna be sooo fucking slow
This command is gonna be sooo fucking slow
pub fn substitute_commands(shell: &mut Shell, string: &str) -> Result<String, CmdSubError> {
    let mut string = string.to_string();
    // Tokenizing and capturing cmbsubs first
    lazy_static! {
        static ref CMDSUB_RE: Regex = Regex::new(
            "(\\$|@)\\([\\a-zA-Z0-9 \"-.@~/\\|<>\\[\\]\\&$()]+\\)"
        ).unwrap();
    }
    for capture in CMDSUB_RE.captures_iter(&string.clone()) {
        if let Some(capture) = capture.get(0) {
            let mut capture = capture.as_str().to_string();
            capture.pop();
            let output = execute_commands_once(shell, &capture[2..])?;
            capture.push(')');
            string = string.replacen(&capture, &output, 1);
        }
    }
    let mut stringchars = string.chars();
    let mut captures: Vec<String> = Vec::new();
    let mut rest: Vec<String> = Vec::new();
    let mut word = String::new();
    let mut in_quote = false;
    while let Some(c) = stringchars.next() {
        match c {
            '`' if !in_quote => {
                in_quote = true;
                rest.push(word.clone());
                word.clear();
            }
            '`' if in_quote => {
                in_quote = false;
                captures.push(word.clone());
                word.clear();
            }
            '\\' => {
                if let Some(c) = stringchars.next() {
                    word.push(c);
                    continue;
                }
            }
            _ => {
                word.push(c);
            }
        }
    }
    rest.push(word);
    let mut outputs = Vec::<String>::new();
    for capture in captures {
        outputs.push(execute_commands_once(shell, &capture)?);
    }
    let mut final_str = String::new();
    let mut outputs = outputs.iter();
    for string in rest {
        final_str.push_str(&string);
        if let Some(output) = outputs.next() {
            final_str.push_str(output)
        }
    }
    Ok(final_str)
}
pub fn substitute_commands ( shell : & mut Shell , string : & str ) -> Result < String , CmdSubError > { let mut string = string . to_string ( ) ; // Tokenizing and capturing cmbsubs first lazy_static ! { static ref CMDSUB_RE Regex Regex new ( " \\ \\ \\ \" \\ \\ \\ \\ \\ " ) unwrap ( ) } for capture in CMDSUB_RE . captures_iter ( & string . clone ( ) ) { if let Some ( capture ) = capture . get ( 0 ) { let mut capture = capture . as_str ( ) . to_string ( ) ; capture . pop ( ) ; let output = execute_commands_once ( shell , & capture [ 2 .. ] ) ? ; capture . push ( ')' ) ; string = string . replacen ( & capture , & output , 1 ) ; } } let mut stringchars = string . chars ( ) ; let mut captures : Vec < String > = Vec :: new ( ) ; let mut rest : Vec < String > = Vec :: new ( ) ; let mut word = String :: new ( ) ; let mut in_quote = false ; while let Some ( c ) = stringchars . next ( ) { match c { '`' if ! in_quote => { in_quote = true ; rest . push ( word . clone ( ) ) ; word . clear ( ) ; } '`' if in_quote => { in_quote = false ; captures . push ( word . clone ( ) ) ; word . clear ( ) ; } '\\' => { if let Some ( c ) = stringchars . next ( ) { word . push ( c ) ; continue ; } } _ => { word . push ( c ) ; } } } rest . push ( word ) ; let mut outputs = Vec :: < String > :: new ( ) ; for capture in captures { outputs . push ( execute_commands_once ( shell , & capture ) ? ) ; } let mut final_str = String :: new ( ) ; let mut outputs = outputs . iter ( ) ; for string in rest { final_str . push_str ( & string ) ; if let Some ( output ) = outputs . next ( ) { final_str . push_str ( output ) } } Ok ( final_str ) }
This command is gonna be sooo fucking slow
This command is gonna be sooo fucking slow
shell
&mut Shell
string
&str