Support F (output filename) command - #527
Conversation
There was a problem hiding this comment.
Pull request overview
Adds support for GNU sed’s F command (print current input file name), including correct handling of non-ASCII file names by storing input_name as a PathBuf and emitting OS-encoded bytes for the filename.
Changes:
- Implement
Fcommand execution in the sed processor. - Store
ProcessingContext.input_nameasPathBuf(and adjust initialization / error formatting accordingly). - Add fixtures and tests for
Fwith regular files, non-ASCII file names, stdin, and--posixrejection.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/fixtures/sed/output/filename_αρχείο1 | Expected output for F with a non-ASCII input filename (interleaved with normal printing). |
| tests/fixtures/sed/output/filename_file | Expected output for F under -n (filename printed per line). |
| tests/fixtures/sed/input/αρχείο1 | Non-ASCII input fixture used to validate filename handling. |
| tests/by-util/test_sed.rs | New test coverage for F (file, non-ASCII path, stdin, and --posix behavior). |
| src/sed/processor.rs | Runtime implementation of F and PathBuf propagation for input_name. |
| src/sed/mod.rs | Initialize input_name as PathBuf for stdin (-). |
| src/sed/error_handling.rs | Quote PathBuf input names in runtime error messages. |
| src/sed/compiler.rs | Allow F only when not in --posix mode. |
| src/sed/command.rs | Update ProcessingContext.input_name type to PathBuf. |
Suppressed comments (1)
src/sed/processor.rs:744
- This builds a Vec from the path bytes and then
write_bytesimmediately clones again internally, causing an extra allocation/copy per input line. You can avoid the first allocation by writing the path bytes directly and then writing a newline separately.
let mut bytes = context.input_name.as_os_str().as_encoded_bytes().to_vec();
bytes.push(b'\n');
output.write_bytes(&bytes)?;
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| _ => panic!("invalid 'e' command data"), | ||
| }, | ||
| 'F' => { | ||
| // Ouput current input file name. |
| pub null_data: bool, | ||
|
|
||
| // Other context | ||
| /// Currently processed input file name (not script) in quoted form |
Merging this PR will degrade performance by 6.17%
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
This also requires storing input_name as PathBuf to avoid lossy conversions when outputting an OS-specific byte sequence representing the file name.