Skip to content

printf format parser ​

printf format strings in three dialects, parsed exactly and converted to and from ICU MessageFormat — where both print the same.

DialectUsed bySpecific
Dialect::JavaAndroid string resources (java.util.Formatter)%n, %b, %,d, %(d; strict flag rules
Dialect::Cgettext c-format, POSIX printf%ld, %zu, %u, %'d
Dialect::ObjCiOS / macOS NSString formatsC plus %@

Installation ​

bash
composer require eugene-erg/printf-format-parser

Parsing ​

php
use EugeneErg\PrintfFormatParser\Dialect;
use EugeneErg\PrintfFormatParser\PrintfParser;

$parser = new PrintfParser(Dialect::Java);

$parts = $parser->parse('Hi %1$s, %,d new');   // ['Hi ', Specifier(s, 1), ', ', Specifier(d, flags: ','), ' new']
$parser->build($parts);                        // 'Hi %1$s, %,d new' — always the same string back

$parser->isFormat('50% off');                  // false: "% o" is not a valid Java conversion

The Java dialect follows java.util.Formatter: a flag or precision a conversion does not accept makes the text not a format (%.2d, %-s, %,o), so a percent sign in ordinary text is recognised as text.

ICU ​

php
use EugeneErg\PrintfFormatParser\Icu\IcuConverter;

$icu = new IcuConverter(Dialect::Java);

$icu->toIcu('%1$s has %2$.2f%%');   // '{0} has {1, number, ::.00 group-off rounding-mode-half-up}%'
$icu->fromIcu('{0} has {1, number, ::.00 group-off rounding-mode-half-up}%');   // '%1$s has %2$.2f%%'

// named variables need their printf position
$icu->fromIcu('{count, number, ::precision-integer group-off} files', ['count' => 1]);   // '%1$d files'

The printf argument N is the ICU variable {N-1}. Every conversion is checked against ICU itself: the ICU message prints what printf prints.

  • %d has no grouping (1234567), %,d (%'d in C) has it.
  • %.2f keeps its digits: 3.10, so the skeleton is .00, not .##.
  • Java rounds HALF_UP (-0.005 → -0.01), so its fractions carry rounding-mode-half-up; C ones carry ICU's default rounding-mode-half-even. The rounding mode also marks a fraction: %.0f comes back as %.0f, not as %d, which would fail with a double.
  • %05d → integer-width/*00000, %+d → sign-always, Java %(d → sign-accounting.
  • Without arguments a message is plain text: 50% off stays 50% off, with arguments % becomes %%.

What has no exact counterpart throws UnsupportedFormatException instead of changing the output: padding with spaces (%5d, %-10s), %x, %o, %e, %c, %b, %S; on the ICU side plurals, selects, dates, named variables without a position, {n, number} without a skeleton (ICU groups and shows up to 3 digits, printf has no such format).

Known differences, not hidden by the converter: a negative zero-padded number (%05d of -42 is -0042, ICU prints -00042), and C rounds the binary value of a double (%.2f of 2.675 is 2.67), ICU the decimal one.

Development ​

bash
composer check    # pint --test, phpstan (level max, strict rules) and phpunit
composer coverage

License ​

MIT


From the README.md of EugeneErg/printf-format-parser · composer require eugene-erg/printf-format-parser