From patchwork Tue Jun 7 16:49:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 54897 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 2BF9E394243C for ; Tue, 7 Jun 2022 16:51:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2BF9E394243C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654620693; bh=fdwr+qjd36D0AQkDBRcrP6xDfTSalYBq04aMx8rq5vI=; 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=NTzqHhKjk7O6rH6fRY8yJntepbDjR6YiMmvQXqgOLgSoEJijV3UaZToSHpuvSO0f+ FSf4ANevZ4uhVngV+0uIFMbEX64v8L9aF2wSsW2nWPprI0+QrnPjRsNiV7QHWx8G92 FHxqn8X89RxZy/juY6UeRpmzDAL30bkW3It0bQXs= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 94EEC394243C for ; Tue, 7 Jun 2022 16:49:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 94EEC394243C 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-550-gPs-laoxPgmifdU30HiYqw-1; Tue, 07 Jun 2022 12:49:08 -0400 X-MC-Unique: gPs-laoxPgmifdU30HiYqw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 23DA63AF42A2 for ; Tue, 7 Jun 2022 16:49:08 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.117]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A08F11121314 for ; Tue, 7 Jun 2022 16:49:07 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v3 04/11] stdio-common: Add __translated_number_width In-Reply-To: References: X-From-Line: 1d51290cf9a28f7ecb5f4b492364f0b518192271 Mon Sep 17 00:00:00 2001 Message-Id: <1d51290cf9a28f7ecb5f4b492364f0b518192271.1654620298.git.fweimer@redhat.com> Date: Tue, 07 Jun 2022 18:49:06 +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.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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, 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; + } +}