Skip to content

Commit

Permalink
Eliminate Heap Allocation w/ !* Designator
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Nov 6, 2017
1 parent 079a236 commit 576182b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/shell/binary/designators.rs
Expand Up @@ -106,9 +106,19 @@ pub(crate) fn expand_designators<'a>(shell: &Shell, cmd: &'a str) -> Cow<'a, str

fn command<'a>(text: &'a str) -> &'a str { ArgumentSplitter::new(text).next().unwrap_or(text) }

// TODO: do this without allocating a string.
fn args(text: &str) -> String {
ArgumentSplitter::new(text).skip(1).collect::<Vec<&str>>().join(" ")
fn args(text: &str) -> &str {
let bytes = text.as_bytes();
bytes.iter()
// Obtain position of the first space character,
.position(|&x| x == b' ')
// and then obtain the arguments to the command.
.and_then(|fp| bytes[fp+1..].iter()
// Find the position of the first character in the first argument.
.position(|&x| x != b' ')
// Then slice the argument string from the original command.
.map(|sp| &text[fp+sp+1..]))
// Unwrap the arguments string if it exists, else return the original string.
.unwrap_or(text)
}

fn first_arg<'a>(text: &'a str) -> &'a str { ArgumentSplitter::new(text).nth(1).unwrap_or(text) }
Expand Down

0 comments on commit 576182b

Please sign in to comment.