Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109800 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 36538 invoked from network); 23 Apr 2020 07:58:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Apr 2020 07:58:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 96C921804C2 for ; Wed, 22 Apr 2020 23:30:11 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 22 Apr 2020 23:30:10 -0700 (PDT) Received: by mail-ej1-f53.google.com with SMTP id nv1so3876501ejb.0 for ; Wed, 22 Apr 2020 23:30:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Q4i3hMxnwJOe8K/p3YlpZIF125B+w15sEOqabM1FeF0=; b=e9bmrMj6L7GoVZ9yEoQmMQzTx9/cx6OEoNxAWtxtE8kJMYNsNW+1gs2lgGwwLw2c3O AfDAgrSCxGjaVc6jkmBYwrKdQ1mWzUYqHpOt+/Jj7CnsZYTRm/w+1spkpTIalzC8iylU 44YBabAqYwSw1kuabrKKjbvswDPw//wFn8ppHMC08B2CJQGGhistGEWrljhmpXs/rofK f+7AxetFg3v7OeJ27q6E7kjyzmDaFGn/pOx3G5GI8JKFXu1bfvVTCUr9cni0+VMDKL5G 90ZSszkiz6qNqcQdL3ouluI2R8y/ypgBjY3eBeVwyjyLiZs2guvjoQJNqMikLIclxXnM mnvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Q4i3hMxnwJOe8K/p3YlpZIF125B+w15sEOqabM1FeF0=; b=cHeap7yOwZAtSBXL8eeJet43u7IXSzTwYcODhTVBUAobSYxgIXx1stP5vOCMXTxwEE PJ6dJYJxnFJqfMSVucy0kjoqWNm8Wn+VZ9KI/ZPIFLzXz04xpGeAyPv1ZdxupGgbWITw A2kKj71aNmTvxX0K7N8XhjrlyZzZhRer1OIMt/lYPoeTmwzaZ7FinuQtBiPybrtE4xzb lpL7oCg+OqMMsxXAEsxgJcsJ2XSSD1W2ffa2W2mvEFg8as3Lqlob9ye1lXSi5ozth8XY LxpbCZjuVPeOts4+EspwPAfb0tcnI2TZoNUpf6uc7ISCBTgLax8ON4t0NVjq/kU/Fa4b o4/Q== X-Gm-Message-State: AGi0PubzC+h5Za8iSwQ0X1wGCYzyAL1qfxKpLHCShlCrXOOcxzjN7On5 2CB0HqYHFh9X2XW1wNi09j2TC3y+L8d4RQjwyzM= X-Google-Smtp-Source: APiQypK2DTubV9SxM0TAT31O0WSXa78m3Kl7kwwMnsEA6WV3lDBWBs8j8XV+jw7x4G1IBlzUPhtSMat6hjSgVIXowmw= X-Received: by 2002:a17:906:4310:: with SMTP id j16mr1519835ejm.102.1587623408702; Wed, 22 Apr 2020 23:30:08 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 23 Apr 2020 08:29:57 +0200 Message-ID: To: Nikita Popov Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000050195b05a3ef61c4" Subject: Re: [PHP-DEV] printf() improvements From: george.banyard@gmail.com ("G. P. B.") --00000000000050195b05a3ef61c4 Content-Type: text/plain; charset="UTF-8" On Wed, 22 Apr 2020 at 15:25, Nikita Popov wrote: > Hi internals, > > I'd like to make two improvements to the printf() functionality exposed by > PHP (also affecting other variations like sprintf, vprintf, etc.) These > improvements are motivated by > https://wiki.php.net/rfc/locale_independent_float_to_string, which will > make float to string casts locale insensitive, but not change the behavior > of printf() specifiers. > > The proposed improvements are: > > 1. Support for the %h and %H specifiers, which behave the same as %g and > %G, but are locale insensitive. These specifiers are already supported > internally in PHP (though %h goes by the name of %k for reasons that are > not relevant to userland) and is used for formatting floating-point numbers > where case-sensitivity is not desired, such as for var_export(). > > 2. Support for * width and precision, in which case the width/precision is > provided as an argument to printf(). This is a feature of printf() in C. > > The combination of these two features allows us to easily print floating > point numbers exactly as PHP would print them: > > // Locale-sensitive using precision ini setting. > sprintf("%.*G", ini_get('precision'), $float); > > // Locale-insensitive using serialize_precision ini setting. > sprintf("%.*H", ini_get('serialize_precision'), $float); > > Notably, this also supports precision -1 (the default serialize_precision), > which will pick the shortest accurate representation of the float. > > Without these features, it is actually quite hard to replicate PHP's exact > behavior. The best approximation I've found is to print with %G at multiple > precisions, pick out the shorted one and replace commas with dots on the > assumption that comma is the only locale-specific decimal separator. It > would be good to expose what PHP can already do directly. > > Implementations for the two features are available at > https://github.com/php/php-src/pull/5432 and > https://github.com/php/php-src/pull/5436. > > Regards, > Nikita > Seems like a no-brainer to add on my side. Best regards George P. Banyard --00000000000050195b05a3ef61c4--