feat(printf): support '*' field width and precision; fix %q precision - #677
Merged
Conversation
Implement the `*' field width and precision bash's printf takes from the argument list (#675). The conversion scan now admits `*', and the flags/width/precision text is resolved into ordinary digits before the spec is built, fetching values in bash's order: width argument, then precision, then the conversion's own argument. Semantics follow bash's getint/printstr: - a negative width means left-justify; a negative precision is treated as if the precision were missing - a leading ' or " yields the next character's code - a value outside int range diagnoses `NNN: Result too large' and substitutes 0 (width) / missing (precision); a malformed number diagnoses `S: invalid number' but keeps the parsed prefix (`12abc' is width 12); both set status 1 while processing continues - a missing argument is width/precision 0 and consumes nothing The %q/%Q branch now parses the resolved text and matches printstr: %q's precision truncates the QUOTED string (`%.2q "a b"' -> `a\'), while %Q keeps pre-truncating the raw argument; a literal width or precision past INT_MAX diagnoses `Result too large' instead of passing through to snprintf; and padding honours only the `-' flag, always with spaces (`%08.2q' space-pads), as printstr does. Verified byte-identical with bash 5.3.15 on 40+ cases; the bash-suite scoreboard now scores printf.tests 0 (was a 388-line diff against the repo snapshot), with builtins/errors/test/varenv/quote/new-exp/assoc/ array all still 0. Closes #675
Owner
Author
|
Correction to the verification note: "the bash-suite scoreboard now scores printf.tests 0" was a false zero — the scoreboard's |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the
*field width and precision for the printf builtin (#675), and fixes%qprecision semantics uncovered along the way.What changed
*; flags/width/precision are resolved into ordinary digits before the spec is built, fetching*values in bash's order (width argument, precision argument, then the conversion's own argument).getintsemantics: negative width → left-justify; negative precision → missing; leading'/"→ character code; out-of-int-range →NNN: Result too largewith 0/missing substituted; malformed number →S: invalid numberkeeping the parsed prefix (12abcis width 12); both diagnose with status 1 while processing continues; a missing argument is 0 and consumes nothing.%q/%Qnow match bash's printstr path:%qprecision truncates the quoted string (printf '%.2q' 'a b'→a\),%Qkeeps pre-truncating the raw argument; literal widths/precisions past INT_MAX diagnoseResult too largeinstead of passing through to snprintf; padding honours only the-flag and always pads with spaces (%08.2qspace-pads).%*ls/%.*lswork through the same resolved text.Verification
Byte-identical stdout/stderr/exit vs reference bash 5.3.15 on 40+ cases (
%*d,%-*d, negative/overflow/invalid/character-code widths,%*.*f,%.*s,%.*q,%.*Q,%*ls, format reuse with*, missing arguments). The bash-suite scoreboard now scores printf.tests 0 — full byte parity with bash — with builtins/errors/test/varenv/quote/new-exp/assoc/array suites all still 0.smoke.testspasses.Known remaining printf gaps (pre-existing, out of scope): width/precision on
%b/%c/%(fmt)T, ANSI-C$'...'quoting in%q, and option-parsing/usage messages.Closes #675
🤖 Generated with Claude Code