From patchwork Thu Aug 18 08:54:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 56840 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 10D443857418 for ; Thu, 18 Aug 2022 08:55:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 10D443857418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1660812924; bh=ACIjQRIAHX6AIQWbDzN0ORNEC0O0S7d1xMfqNM05tQU=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=W7E3jYeUSo854UM+zDYmE+87GkjscMY4SuYkjeD7IMRbIy45b9kxtgG/rIi3ygCIj fb9u4sgoQA3IQp7G4yj42pYMXnuUKfJeV8c7sBItDHkujEZsBXx6iSdjdMG7lml92L E3FKXzwwyKr9jLhhGY3d3usbMpSUSsxN4TfTu9g0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 68FAA3858C53 for ; Thu, 18 Aug 2022 08:54:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 68FAA3858C53 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-665-5QvkJunxMry0Cyo8pm86Nw-1; Thu, 18 Aug 2022 04:54:43 -0400 X-MC-Unique: 5QvkJunxMry0Cyo8pm86Nw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 78E3F811E87 for ; Thu, 18 Aug 2022 08:54:42 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.88]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E5E692166B26 for ; Thu, 18 Aug 2022 08:54:41 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v4 04/11] stdio-common: Add __translated_number_width In-Reply-To: References: X-From-Line: 32b4f565ef07fff6dafbd4584f0d480fa478f5e7 Mon Sep 17 00:00:00 2001 Message-Id: <32b4f565ef07fff6dafbd4584f0d480fa478f5e7.1660812721.git.fweimer@redhat.com> Date: Thu, 18 Aug 2022 10:54:40 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This function will be used to compute the width of a number after i18n digit translation. --- include/printf.h | 13 +++++++- stdio-common/Makefile | 1 + stdio-common/translated_number_width.c | 42 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 stdio-common/translated_number_width.c diff --git a/include/printf.h b/include/printf.h index 8f064149d3..5127a45f9b 100644 --- a/include/printf.h +++ b/include/printf.h @@ -53,7 +53,18 @@ int __wprintf_function_invoke (void *, printf_function callback, #include -/* Now define the internal interfaces. */ +/* Returns the width (as for printf, in bytes) of the converted ASCII + number in the characters in the range [FIRST, LAST). The range + must only contain ASCII digits. The caller is responsible for + avoiding overflow. + + This function is used during non-wide digit translation. Wide + digit translate produces one wide character per ASCII digit, + so the width is simply LAST - FIRST. */ +int __translated_number_width (locale_t loc, + const char *first, const char *last) + attribute_hidden; + extern int __printf_fphex (FILE *, const struct printf_info *, const void *const *) attribute_hidden; extern int __printf_fp (FILE *, const struct printf_info *, diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 9195b209cd..902b8e9db3 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -85,6 +85,7 @@ routines := \ tmpfile64 \ tmpnam \ tmpnam_r \ + translated_number_width \ vfprintf \ vfprintf-internal \ vfscanf \ diff --git a/stdio-common/translated_number_width.c b/stdio-common/translated_number_width.c new file mode 100644 index 0000000000..f42d5968a1 --- /dev/null +++ b/stdio-common/translated_number_width.c @@ -0,0 +1,42 @@ +/* Compute the printf width of a sequence of ASCII digits. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +int +__translated_number_width (locale_t loc, const char *first, const char *last) +{ + struct lc_ctype_data *ctype = loc->__locales[LC_CTYPE]->private; + + if (ctype->outdigit_bytes_all_equal > 0) + return (last - first) * ctype->outdigit_bytes_all_equal; + else + { + /* Digits have varying length, so the fast path cannot be used. */ + int digits = 0; + for (const char *p = first; p < last; ++p) + { + assert ('0' <= *p && *p <= '9'); + digits += ctype->outdigit_bytes[*p - '0']; + } + return digits; + } +}