Repository navigation
sed: preserve literal characters in bracket expressions - #717
luantaraschi wants to merge 1 commit into
Conversation
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.
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| 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'\\'); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
could be shorter, no? the second sentence is enough with the examples
| #[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")] |
There was a problem hiding this comment.
please add [---] and [^--x] here too, they are only covered by the unit tests
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.