eugene-erg / printf-format-parser
printf format strings in the Java (Android), C (gettext) and Objective-C (iOS) dialects: exact parsing and rebuilding, and conversion to and from ICU MessageFormat
Requires
- php: ^8.2
- eugene-erg/icu-message-format-parser: ^3.0
Requires (Dev)
- ext-intl: *
- laravel/pint: ^1.32
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.2
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.1
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 16:29:13 UTC
README
printf format strings in three dialects, parsed exactly and converted to and from ICU MessageFormat — where both print the same.
| Dialect | Used by | Specific |
|---|---|---|
Dialect::Java |
Android string resources (java.util.Formatter) |
%n, %b, %,d, %(d; strict flag rules |
Dialect::C |
gettext c-format, POSIX printf |
%ld, %zu, %u, %'d |
Dialect::ObjC |
iOS / macOS NSString formats |
C plus %@ |
Installation
composer require eugene-erg/printf-format-parser
Parsing
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
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.
%dhas no grouping (1234567),%,d(%'din C) has it.%.2fkeeps its digits:3.10, so the skeleton is.00, not.##.- Java rounds HALF_UP (
-0.005→-0.01), so its fractions carryrounding-mode-half-up; C ones carry ICU's defaultrounding-mode-half-even. The rounding mode also marks a fraction:%.0fcomes 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% offstays50% 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
composer check # pint --test, phpstan (level max, strict rules) and phpunit
composer coverage
License
MIT