Skip to content

Commit

Permalink
De-duplicate the code to skip rest of word
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Apr 17, 2024
1 parent 87d4485 commit bea34b6
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,20 @@ fn lookup_long(name: &str) -> FlagConstructor {
pub(crate) fn parse(f: &mut RustFlags) -> Option<Flag> {
const SEPARATOR: char = '\x1F';

let mut skip = false;

while f.pos < f.encoded.len() {
if skip {
match f.encoded[f.pos..].find(SEPARATOR) {
// `nonflag` ...
Some(i) => f.pos += i + 1,
// `nonflag`$
None => f.pos = f.encoded.len(),
}
skip = false;
continue;
}

let (constructor, arg) = if let Some((constructor, len)) = f.repeat.take() {
let arg = &f.encoded[f.pos..f.pos + len];
f.pos += len;
Expand All @@ -347,13 +360,8 @@ pub(crate) fn parse(f: &mut RustFlags) -> Option<Flag> {
FlagConstructor::Opt(f) => ConstructorFn::Opt(f),
FlagConstructor::Repeated(f) => ConstructorFn::Repeated(f),
FlagConstructor::Unrecognized => {
// Skip rest of word.
if let Some(i) = f.encoded[f.pos..].find(SEPARATOR) {
f.pos += i + 1;
} else {
f.pos = f.encoded.len();
}
f.short = false;
skip = true;
continue;
}
};
Expand Down Expand Up @@ -442,12 +450,7 @@ pub(crate) fn parse(f: &mut RustFlags) -> Option<Flag> {
}
}
} else {
match f.encoded[f.pos..].find(SEPARATOR) {
// `nonflag` ...
Some(i) => f.pos += i + 1,
// `nonflag`$
None => f.pos = f.encoded.len(),
}
skip = true;
continue;
};

Expand Down

0 comments on commit bea34b6

Please sign in to comment.