Search by

eugene-erg / printf-format-parser

EugeneErg

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

Package info

github.com/EugeneErg/printf-format-parser

pkg:composer/eugene-erg/printf-format-parser

Statistics

Installs: 229

Dependents: 2

Suggesters: 0

Stars: 1

Open Issues: 0

1.1.0 2026-12-03 16:28 UTC

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.

  • %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

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

License

MIT