Skip to content

Fix padding bug#48

Merged
donatj merged 5 commits into
masterfrom
fix/padding-count-bug
Apr 17, 2026
Merged

Fix padding bug#48
donatj merged 5 commits into
masterfrom
fix/padding-count-bug

Conversation

@donatj

@donatj donatj commented Apr 17, 2026

Copy link
Copy Markdown
Owner

Fixes a bug where padding would not be calculated correctly in cases where a padding character was not provided

Fixes a bug where padding would not be calculated correctly in cases where a padding character was not provided
@donatj donatj force-pushed the fix/padding-count-bug branch from 9044f98 to 9f587c8 Compare April 17, 2026 13:42
@donatj donatj marked this pull request as ready for review April 17, 2026 13:51
Copilot AI review requested due to automatic review settings April 17, 2026 13:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes parsing of printf padding width when no padding character flag is provided, and extends integration coverage around validity/invalid lexemes.

Changes:

  • Parse a leading integer after % as a pad width when it is not a positional argument ($).
  • Always attempt to read a pad width after flag parsing (instead of only when a pad char flag was set).
  • Expand ParserTest data provider to include validity expectations and additional format cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Parser.php Adjusts width parsing logic so widths are recognized even when pad char isn’t specified.
test/Integration/ParserTest.php Adds validity assertions and new/updated integration cases for padding/width scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/Integration/ParserTest.php Outdated
Comment on lines +42 to +45
$this->assertSame(
($invalid === null),
!($valid === false),
sprintf(

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$valid is documented as bool|string and the provider supplies canonical strings in a couple cases, but the test only treats it as a boolean validity flag ($valid === false). This makes the data provider contract misleading and leaves the canonical string values unused.

Either assert the canonical string when $valid is a string (if canonicalization is a requirement), or change the provider/doc/type expectations to a simple boolean validity indicator.

Copilot uses AI. Check for mistakes.
Comment thread src/Parser.php Outdated
Comment on lines +139 to +146
$lexer->rewind();
$peek = $lexer->peek();
if( ctype_digit($peek->getString()) ) {
$padWidth = $this->eatInt($lexer);
}

$next = $lexer->next();

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lexSprintf rewinds and re-parses a width unconditionally. If the format string ends immediately after an initial width (e.g. "%10"), $next is EOF, rewind() moves back onto the last digit, and eatInt() overwrites $padWidth with only that trailing digit (10 becomes 0). This makes the parsed pad width incorrect for EOF-after-width cases.

Consider only rewinding/parsing width when the current $next is a digit (e.g. ctype_digit($next->getString())), and skip the rewind/width parse when $next is EOF or a non-digit.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 93a467e. The unconditional rewind() is replaced with a check on $next (the token that broke the flag loop) — only rewinding and re-parsing the pad width when that token is itself a digit. EOF has length()=0 so never advances the position, meaning the old rewind() would step back onto the last digit of an already-parsed width and overwrite $padWidth with just that trailing digit. A test case for %10 (no flags, EOF after width) is also added.

Comment thread test/Integration/ParserTest.php
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@donatj donatj merged commit fedd05a into master Apr 17, 2026
17 checks passed
@donatj donatj deleted the fix/padding-count-bug branch April 17, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants