Repository navigation
sed: treat v as a no-op after a successful version check - #671
emirmrkaljevic wants to merge 2 commits into
Conversation
The compiler already accepted v, but the processor panicked with invalid command code once there was input. Execution is now a no-op. The version argument stops at ;, #, or }, addresses are accepted, and the comparison follows GNU sed 4.9 (two components, so v4.9 succeeds and v4.9.0 does not).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #671 +/- ##
==========================================
+ Coverage 95.31% 95.41% +0.10%
==========================================
Files 14 14
Lines 7512 7658 +146
Branches 443 457 +14
==========================================
+ Hits 7160 7307 +147
+ Misses 351 350 -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:
|
Merging this PR will not alter performance1 warning
Comparing |
| _ => return compilation_error(lines, line, "invalid version of sed"), | ||
| let c = line.current(); | ||
| // The version argument ends at a command separator; leave it in place. | ||
| if matches!(c, ';' | '#' | '}') { |
There was a problem hiding this comment.
v4.9 ;p or 1{v4.9 } still fail with invalid version, GNU stops at blanks too.
could you please eat_spaces() and reuse parse_command_ending like the other commands instead of a custom separator check?
There was a problem hiding this comment.
Thanks, done. After the version I now call eat_spaces() and parse_command_ending, so v4.9 ;p and 1{v4.9 } work. A following # is left in place so the comment can be parsed.
| /// Return true if `requested` is newer than the implementation version. | ||
| /// Equal shared components with extra requested components count as newer, | ||
| /// matching GNU sed 4.9 (`v4.9` succeeds, `v4.9.0` does not). | ||
| fn version_is_newer_than(requested: &[u32], current: &[u32]) -> bool { |
There was a problem hiding this comment.
slices already compare lexicographically, a longer one counting as greater, so components.as_slice() > &GNU_VERSION[..] should do it. do we need this function?
There was a problem hiding this comment.
You're right, the helper wasn't needed. I now compare with components.as_slice() > &GNU_VERSION[..].
| if c == '.' { | ||
| if current.is_empty() { | ||
| // Leading or consecutive dots are empty components (GNU: `v.9`). | ||
| components.push(0); |
There was a problem hiding this comment.
the parse + push logic shows up 3 times, and push(0) twice. splitting the argument on '.' would be simpler, no?
There was a problem hiding this comment.
Agreed, that's simpler. I now collect the argument and split it on ..
| } | ||
|
|
||
| #[test] | ||
| fn test_version_stops_at_semicolon() { |
There was a problem hiding this comment.
please also add tests for } and # after the version (e.g. 1{v4.9}), a blank before ;, and a component that overflows u32 (codecov flags those error branches)
There was a problem hiding this comment.
Added tests for 1{v4.9}, 1{v4.9 }, v4.9#c, a blank before ;, and a component that overflows u32.
Stop the version argument at whitespace or a command ending, split it on '.', and compare components as slices against the advertised 4.9 version. Also cover '}', '#', a blank before ';', and a u32 overflow.
Fixes #669.
The compiler already accepted the GNU
vversion assertion and rejected versions newer than 4.9, but a successful check still panicked at runtime withinvalid command codeas soon as there was input. Tests that ransed vwith no stdin never reached the processor, so they stayed green.vis now a no-op at execution time. I also matched a few related GNU sed 4.9 details I hit while reproducing this with behave:;,#, or}, sov;s/a/A/runs the substitute.v3.99andv4.8.99succeed,v4.9succeeds, andv4.9.0is newer and is rejected.Added tests that pipe input for the success cases, plus tests for the semicolon, addresses, older components, and
v4.9.0. The existingv999test now expectsexpected newer version of sedinstead of a parse error.cargo test --test tests version,cargo fmt --check, andcargo clippy --all-targets --all-features -- -D warningspass.