From patchwork Thu Sep 12 20:49:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1984890 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hQDqmg+9; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X4V714MDcz1y1C for ; Fri, 13 Sep 2024 06:54:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6FCDC385B50C for ; Thu, 12 Sep 2024 20:54:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 5D5B43858D26 for ; Thu, 12 Sep 2024 20:54:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D5B43858D26 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5D5B43858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726174455; cv=none; b=fMAQKYivuDHLeh4iaOeZbCuVPJNy51JYWVNxecOK+O8Svgl+fIsCtLbCzeOceHxkUdLbG+dTP/tC2xCQ/03ii2Mz2mihAEdbctn1IdPKqeUcyRdxfKhIyzV06EPoi+JRvOgiCLLi8SMZ2URjBIbl9a4VrCYypnfJyNHDt/QSL7w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726174455; c=relaxed/simple; bh=SW1K/IywqdXxLcpXU6lBdQlOchuUcmY84V1Y29Ncwss=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=czPTEpVVhN1qUGjDkmux6ixXxyKrnildnzm3FDxUi7uuFXioZnCk9pS3W1JjGgZ3rZkB45jt3Zf4L9YYd+ddPntYTLOzrMNRSbdCiSnbzsr6fk3xREbQUl8JlOubuhb4m1iVvHyzYf+sMc7TOSyxotKqGZr5ym8uB6IZPaItMnc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726174454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2/ywYQAJytgte32+YfqPDH1w3Z3uoO4E7B0/qHSJXgg=; b=hQDqmg+9zZAFAq9jsMSKhmQu7POiY7Z5MSFOa/X9aMGrKPiAruqotNTIrS79rmmZupLmPf ELHzIuofq522YesWOs/MOm7jcYCffs95yaokgD9rYov2kJxWuEyKWmC4tpMQv0VSLtkaGb NmmLL+FutU8J/FHK7bZKmvwRRwmejUI= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-278-vs3fbkH6MDiQOVu9swcL8A-1; Thu, 12 Sep 2024 16:54:12 -0400 X-MC-Unique: vs3fbkH6MDiQOVu9swcL8A-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8EF501955F41; Thu, 12 Sep 2024 20:54:11 +0000 (UTC) Received: from localhost (unknown [10.42.28.101]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B1293195609F; Thu, 12 Sep 2024 20:54:10 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Tweak localized formatting for floating-point types Date: Thu, 12 Sep 2024 21:49:07 +0100 Message-ID: <20240912205409.495976-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This adds some comments to explain what this rather subtle code is doing. It also replaces string::copy with using char_traits::copy directly, because the bounds checks and length adjustments that string::copy does are redundant here - we already ensure the lengths are correct. Tested x86_64-linux. -- >8 -- libstdc++-v3/ChangeLog: * include/std/format (__formatter_fp::_M_localize): Add comments and micro-optimize string copy. --- libstdc++-v3/include/std/format | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 52243eb5479..e963d7f79b3 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -1886,25 +1886,28 @@ namespace __format if (__grp.empty() && __point == __dot) return __lstr; // Locale uses '.' and no grouping. - size_t __d = __str.find(__dot); - size_t __e = min(__d, __str.find(__exp)); + size_t __d = __str.find(__dot); // Index of radix character (if any). + size_t __e = min(__d, __str.find(__exp)); // First of radix or exponent if (__e == __str.npos) __e = __str.size(); - const size_t __r = __str.size() - __e; + const size_t __r = __str.size() - __e; // Length of remainder. auto __overwrite = [&](_CharT* __p, size_t) { + // Apply grouping to the digits before the radix or exponent. auto __end = std::__add_grouping(__p, __np.thousands_sep(), __grp.data(), __grp.size(), __str.data(), __str.data() + __e); - if (__r) + if (__r) // If there's a fractional part or exponent { if (__d != __str.npos) { - *__end = __point; + *__end = __point; // Add the locale's radix character. ++__end; ++__e; } - if (__r > 1) - __end += __str.copy(__end, __str.npos, __e); + const size_t __rlen = __str.size() - __e; + // Append fractional digits and/or exponent: + char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen); + __end += __rlen; } return (__end - __p); };