Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109771 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 40766 invoked from network); 22 Apr 2020 14:53:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Apr 2020 14:53:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 180F41801FD for ; Wed, 22 Apr 2020 06:25:02 -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_H2,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-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (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 06:25:01 -0700 (PDT) Received: by mail-lj1-f174.google.com with SMTP id z26so2224781ljz.11 for ; Wed, 22 Apr 2020 06:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=u4la/qNjourRLwgbgplJIkPUMD2eO9+88Wq8VNsDS0U=; b=Tdh8fw5nxR3d/syZVeMBFsB+Motdrke530R10UB3WsYX9XONF6p/gJX+8b4/gE2M3n 6OC7eyqbVXFDfU4WhrVrXk3aKhRLyOaNa1qFisLT1e8ZeSdWscZXywjGR4ecqtZYEe3d HOYwTQtzwK4a19eAW1ucyVeytD/YiXEp1S8zxMUXUU2iULBpRWf21urizmTQpcAMGwVr smB3cr9bE0TtRtVb9VzFwcchwJHsZ2qcZkkpv3wyX20B1kVraW3sbTXGjWcynteadPhl 2xFGSt6hI/sz66qCOJAnpE5iHQs8TCzZhOFEQ2hNRYJLk8n0QbdS6A1B+pzkT83LQvy1 Aniw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=u4la/qNjourRLwgbgplJIkPUMD2eO9+88Wq8VNsDS0U=; b=ZqlgWbLRWDJYz/tvat7CxP3RcH37c5DpId/7/xgaHLB2Mi+TwznsemVPk/ezlhqqyA G86If/ZhlT4ituVXPRyyvQrzcifPjhPQOCc/jNKnSSTtuZVGyrQU/xbBRFLwNWxZAP5p yekqjsRoG0kzX+kr9DcaAdZGYQvyOU3l2eUJFdD54VQ+fDD6IdegcuyaHn0MDW7xN/3x AzrbFhoYAaa9qoP5wruCHzHWCCymy5Dsfl4hsJLtZbBKF22AUuigim9MA75y3a/y/KM5 mx1w4NChct6Vp0+G8KADl65od39z1FHC0TZ0VHyK0i7/xDDuh1HGsZ4Yn0neOb6JhhWQ 9NnQ== X-Gm-Message-State: AGi0PuadavitgfxmWowCFnTRmx5lwV+/SFEanPCBZ51mWHmZnlA7osfk p2z5aGnT81oFiS1eymFBw9fxq1s87w4M0g6qPoVw9YUYSNvSXA== X-Google-Smtp-Source: APiQypL2IGko2eDAENZl/4l4TeOgWO0DYMb5VdODRTJWnk2jVWkd6cniQYTf3bJKdTQEzfd4i8oMExEkXm4cv8UMQm8= X-Received: by 2002:a2e:81d5:: with SMTP id s21mr10433544ljg.258.1587561899708; Wed, 22 Apr 2020 06:24:59 -0700 (PDT) MIME-Version: 1.0 Date: Wed, 22 Apr 2020 15:24:43 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000001754b205a3e10f0a" Subject: printf() improvements From: nikita.ppv@gmail.com (Nikita Popov) --0000000000001754b205a3e10f0a Content-Type: text/plain; charset="UTF-8" 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 --0000000000001754b205a3e10f0a--