]> granicus.if.org Git - php/commit
Add support for * width and precision in printf()
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 21 Apr 2020 14:54:22 +0000 (16:54 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 27 May 2020 08:42:25 +0000 (10:42 +0200)
commit0221b8b2ab8cd53845a03cedb31add36e58e390f
tree8fa2ee16ccf477a0211fac9ad4e86ad9406be974
parent2423288f0f6244988e85f35b2e5a4c90a4e557a7
Add support for * width and precision in printf()

If * is used for width/precision in printf, then the width/precision
is provided by a printf argument instead of being part of the format
string. Semantics generally match those of printf in C.

This can be used to easily reproduce PHP's float printing behavior:

    // Locale-sensitive using precision ini setting.
    // Used prior to PHP 8.0.
    sprintf("%.*G", (int) ini_get('precision'), $float);

    // Locale-insensitive using precision ini setting.
    // Used since to PHP 8.0.
    sprintf("%.*H", (int) ini_get('precision'), $float);

    // Locale-insensitive using serialize_precision ini setting.
    // Used in serialize(), json_encode() etc.
    sprintf("%.*H", (int) ini_get('serialize_precision'), $float);

Closes GH-5432.
UPGRADING
ext/standard/formatted_print.c
ext/standard/tests/strings/sprintf_star.phpt [new file with mode: 0644]