Repository navigation
Conversation
-F was parsed but never used. Set FS from it before any rule runs, decoding escapes as in a string literal, as POSIX defines -F sepstring to be the same as -v FS=sepstring.
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.
| // `-F sepstring` is the same as `-v FS=sepstring`, so escapes such as | ||
| // `\t` are decoded as in a string literal. | ||
| if let Some(fs) = &args.field_separator { | ||
| let fs = lexer::unescape(fs.as_encoded_bytes(), args.posix); |
There was a problem hiding this comment.
mode is Posix for --traditional too, should it be args.posix || args.traditional here?
There was a problem hiding this comment.
I think args.posix alone is right here. That flag only controls the escapes that are GNU extensions (\x, \u), and gawk still decodes those with --traditional:
$ printf 'xAy\n' | gawk --traditional -F '\x41' '{ print $2 }'
y
$ printf 'xAy\n' | gawk --posix -F '\x41' '{ print $2 }'
It is also what the program lexer does: string literals in the program decode \x41 under --traditional. I added that case as a test in 9f09f1e, so it will fail if someone switches it to posix || traditional.
There was a problem hiding this comment.
--traditional is weird. It implies POSIX for basically everything but some regex and string parsing shenanigans. It doesn't help that documentation mentions both as "compatibility" modes...
| ("|", "a|b\n", "b\n"), | ||
| (".", "a.b\n", "b\n"), | ||
| ("\\t", "a b\tc\n", "c\n"), | ||
| ("[0-9]", "a1b2c\n", "b\n"), |
There was a problem hiding this comment.
could you please add the -F '\' and -F '' cases from the description too?
There was a problem hiding this comment.
Added in 9f09f1e: -F '\' (splits a\b c into a and b c), -F '' (one field per character), and --posix -F '' (the whole record is one field). All three match gawk and mawk.
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.
Alonely0
left a comment
There was a problem hiding this comment.
Thanks for your contribution! No comments from my end, looks great.
|
Please fix the conflicts once you have a moment :) |
…ption # Conflicts: # tests/by-util/test_awk.rs
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.
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.
Alonely0
left a comment
There was a problem hiding this comment.
Looks great! Only two nits from my end.
| // `\t` are decoded as in a string literal. | ||
| if let Some(fs) = &args.field_separator { | ||
| let fs = lexer::unescape(fs.as_encoded_bytes(), args.posix); | ||
| intrp.set_field_separator(rt_arena.alloc_slice_copy(&fs)); |
There was a problem hiding this comment.
we don't have to copy this to the arena to make lifetimes work; just take the buffer by-value and create it as a Value::new_string in the interpreter instead of a Value::new_str. Although copying to the runtime arena is not a big deal because it rarely grows after everything is created, it makes our life a bit inconvenient since it boycotts the optimization of growing the last allocation (useful for growing the register array on function calls).
| let encoding = LocaleEncoding::detect(); | ||
| let mut out = Vec::with_capacity(bytes.len()); | ||
| let mut start = 0; | ||
| while let Some(rel_i) = memchr(b'\\', &bytes[start..]) { |
There was a problem hiding this comment.
A big chunk of the FS values that will appear here consist of only one escape, like \t. We could add a fast path that checks whether the first byte is a \, parses the escape sequence, and returns directly if the input is exhausted. In both other cases we fall through to the parsing loop, tho with different start values.
-Fwas parsed intoArgs::field_separatorbut never used, so the option had no effect and fields were always split on blanks:POSIX defines
-F sepstringas the same as-v FS=sepstring. This sets FS before any rule runs, and decodes escape sequences the way a string literal does, so-F '\t'splits on tabs. ABEGINassignment to FS still overrides it.The change:
lexer::unescapedecodes the escapes in a command-line value, reusingparse_escape. A trailing lone backslash is kept as is, so-F '\'splits on backslashes like gawk and mawk. This can also be used for-vassignments later; that's still atodo!()inmain.rsand I left it alone here.Interpreter::set_field_separatorswitches the record splitter and stores FS, the same way an assignment to FS in the program does.main.rscalls it before handing the interpreter to the event loop.I checked
-F :,,,|,.,\t,[0-9],t,\and' 'againstgawk --posixand mawk, and all now match.-F ''behaves like mawk and gawk without--posix(one field per character).The new test
field_separator_optionfails on main and passes with this change.cargo test --workspacepasses (273 tests), andcargo fmtandcargo clippy --workspace --all-targets -- -D warningsare clean.I used Claude Code (Opus 5.5) to help investigate and draft this change; I reproduced the bug and checked the fix myself.