From patchwork Mon Dec 12 15:23:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 61796 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 44036384F975 for ; Mon, 12 Dec 2022 15:24:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 44036384F975 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670858669; bh=DGKsvubXZxAiJqM6OfcMpHTljeWXNgzB2gbG0im4qqw=; 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=QdatNIisPBp6JZVHoRlQN3t93u6a+N+8M1gCf23X5HyuY0UW7wk0GmjI4IofEsOXm dbDJt95JZNHZQDDfF1x+MWvvrGUc/YUCZGEQvlLXVllJ+mIp0A0dTy2YS4f+Jq+f+6 MLIqP7axUPwp+QDBh7EnMYwVIVqT9V9Q2D0NwiG0= 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 86589385C673 for ; Mon, 12 Dec 2022 15:23:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 86589385C673 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-387-UhQSSRclO36p9OwgxFkw8Q-1; Mon, 12 Dec 2022 10:23:16 -0500 X-MC-Unique: UhQSSRclO36p9OwgxFkw8Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E183D29DD9A2 for ; Mon, 12 Dec 2022 15:23:15 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.81]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 479DC53A0 for ; Mon, 12 Dec 2022 15:23:15 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v5 04/11] stdio-common: Add __translated_number_width In-Reply-To: References: X-From-Line: 8566149e25485ca6e0cafa0c3e7366d3f7efe532 Mon Sep 17 00:00:00 2001 Message-Id: <8566149e25485ca6e0cafa0c3e7366d3f7efe532.1670858473.git.fweimer@redhat.com> Date: Mon, 12 Dec 2022 16:23:11 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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_NONE, RCVD_IN_MSPIKE_H2, 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: 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. Reviewed-by: Adhemerval Zanella --- 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 6e6da091b1..3e0c574ca5 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; + } +}