Skip to content

pr: replace regex with hand-written argument parsers#11774

Open
sylvestre wants to merge 1 commit intouutils:mainfrom
sylvestre:size2
Open

pr: replace regex with hand-written argument parsers#11774
sylvestre wants to merge 1 commit intouutils:mainfrom
sylvestre:size2

Conversation

@sylvestre
Copy link
Copy Markdown
Contributor

No description provided.

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/tail-n0f (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/basenc/bounded-memory is now passing!

@github-actions
Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 151.27 MB (-1.33 MB, -0.87%)

Significant per-binary changes:
  pr     2.83 MB ->    1.50 MB  (-1.33 MB, -47.03%)

@sylvestre sylvestre marked this pull request as ready for review April 13, 2026 08:42
@sylvestre sylvestre requested a review from cakebaker April 13, 2026 08:42
}

/// Extract the `digits[:digits]` part from a `+digits[:digits]` token found anywhere in the string.
fn extract_plus_page(s: &str) -> Option<String> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can return an Option<&str>.


/// Extract the digits part from a `-digits` token found anywhere in the string.
/// Equivalent to the regex `\s*-(\d+)\s*` capture group 1.
fn extract_minus_col(s: &str) -> Option<String> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Comment on lines +461 to +465
let mut chars = s.chars();
match chars.next() {
Some('-') | None => false,
Some(_) => chars.all(|c| c.is_ascii_digit()),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use an early exit instead of a match:

Suggested change
let mut chars = s.chars();
match chars.next() {
Some('-') | None => false,
Some(_) => chars.all(|c| c.is_ascii_digit()),
}
if s.is_empty() || s.starts_with('-') {
return false;
}
s.chars().skip(1).all(|c| c.is_ascii_digit())


/// Check if string matches `^-n\s*$` (is `-n` optionally followed by whitespace).
fn is_n_flag(s: &str) -> bool {
s.trim().trim_end() == "-n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trim() is not correct if you want to ensure the string starts with -n.

Suggested change
s.trim().trim_end() == "-n"
s.trim_end() == "-n"


/// Check if string matches `^-e` (starts with `-e`).
fn is_e_flag(s: &str) -> bool {
s.trim().starts_with("-e")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Suggested change
s.trim().starts_with("-e")
s.starts_with("-e")

/// Removes -column and +page option as getopts cannot parse things like -3 etc
/// # Arguments
/// * `args` - Command line arguments
/// Removes -column and +page option as getopts cannot parse things like -3 etc.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be clap as we don't use getopts.

Suggested change
/// Removes -column and +page option as getopts cannot parse things like -3 etc.
/// Removes -column and +page option as clap cannot parse things like -3 etc.

None => 1,
};

let end_page_in_plus_option = match page_plus_match {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the logic is similar to start_page_in_plus_option it might be possible to combine them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants