Skip to content

sed: preserve literal characters in bracket expressions - #717

Open
luantaraschi wants to merge 1 commit into
uutils:mainfrom
luantaraschi:fix/bracket-set-operators
Open

luantaraschi wants to merge 1 commit into
uutils:mainfrom
luantaraschi:fix/bracket-set-operators

Conversation

@luantaraschi

Copy link
Copy Markdown

Rust’s regex crate gives special meaning to &&, ~~, --, , and [ inside bracket expressions, which can change how GNU sed patterns match. For example, printf '%s\n' 'a&b' | sed 's/[a&&b]/X/g' prints XXX with GNU sed, while uutils left a&b unchanged. Likewise, printf '%s\n' 'C:\tmp' | sed 's/[\x5c]///g' prints C:/tmp with GNU sed, while uutils reported an invalid regex.

This change escapes literal class members, including characters decoded from numeric escapes, and escapes the appropriate range endpoint when two hyphens are adjacent. All 13 new regression cases fail on main and pass with the fix, and the GNU test suite results are unchanged. Reversed-range validation for [a-z--] and []--] is outside this change, and #626 adds similar escaping logic in the same parser.

The regex crate reads &&, ~~ and -- inside a class as set operations and
\ and [ as syntax. Escape them when they are ordinary members, whether
typed or written as an escape such as \x5c.
Copilot AI balanced review requested due to automatic review settings November 26, 2026 22:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.16%. Comparing base (c225cb8) to head (dd893cc).

Files with missing lines Patch % Lines
src/sed/delimited_parser.rs 96.55% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #717      +/-   ##
==========================================
+ Coverage   94.15%   94.16%   +0.01%     
==========================================
  Files          14       14              
  Lines        5745     5772      +27     
  Branches      463      466       +3     
==========================================
+ Hits         5409     5435      +26     
- Misses        335      336       +1     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.12% <96.55%> (+<0.01%) ⬆️
ubuntu_latest 96.00% <96.55%> (+<0.01%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

b'\\' | b'[' | b'&' | b'~' => result.extend_from_slice(&[b'\\', byte]),
b'-' if *raw_dash_end == Some(result.len()) => {
if result.len() == members_start + 1 {
result.insert(members_start, b'\\');

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.

could we escape a leading - when it is pushed (result.len() == members_start) instead of inserting the backslash afterwards?
that would remove the insert and the dash tracking would be simpler

Ok(decoded)
}

/// Add an ordinary member to a bracket expression. Inside a class the regex crate

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.

could be shorter, no? the second sentence is enough with the examples

Comment thread tests/by-util/test_sed.rs
#[case::double_ampersand(r"s/[a&&b]/X/g", "a&b\n", "XXX\n")]
#[case::double_tilde(r"s/[~~]/X/g", "c~d\n", "cXd\n")]
#[case::range_ending_in_dash(r"s/[!--]/X/g", "d-e&\n", "dXeX\n")]
#[case::range_starting_with_dash(r"s/[--x]/X/g", "-x.\n", "XXX\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.

please add [---] and [^--x] here too, they are only covered by the unit tests

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.

3 participants