From patchwork Sat Dec 10 22:20:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Boeckel X-Patchwork-Id: 32102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1516329wrr; Sat, 10 Dec 2022 14:24:27 -0800 (PST) X-Google-Smtp-Source: AA0mqf7s5d/ADHilxR6DiW4bZKIvK4wX9SIJQEOfFeEQuclVPr75nB9pMkhhNEarvdUrpWI1tr+w X-Received: by 2002:a05:6402:28c3:b0:461:5b2f:2d81 with SMTP id ef3-20020a05640228c300b004615b2f2d81mr8634658edb.33.1670711067124; Sat, 10 Dec 2022 14:24:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670711067; cv=none; d=google.com; s=arc-20160816; b=vb5eZwee8rNAFncPf+/OJV5NwxFn1KjY1z92J3H0gOxdoVDaFgchcr3HPFqSG5zXw/ fS3X4o1dGG5REoIIm7oa5y0eYYaLhCwcr2HUk6NJ6VeWr4Y7Pg3IET3O2B0xRtLGbNb9 RWkbUPPNHD1TNIN6WOkn7waVNNzkqak972a7kjk7G/dPkD4ZehbpUXNrnq/hKgWNwRWT YdfdAyija4xg5owkx9kj85BmESNG5TVopC4aCBcUn9npPNlGmZD/x5GojzNOWpNF1bXm WEYyRrQPGcmmuBkoG+sAPru9ldJQxZcR28YE/XFIV6pc5iNVe+dyzpckmgHOcN298hDM nAJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=uveuOna947CSrNvr3VoiiSKHe9OMZ0xgQGHh19CSsWo=; b=yaY/N8nUANQityvqvuPpsCRKmxb+sXikWw6TqHFn9xcpTwsejznO2tByuXuTg5RoVc qM/uI1ZUfJGfeqzKpOHAPipDt7PduyLTR73Z8qr8Ez8Vc2iBsp/ep3HI6ULoH7c00pGu Ik0UvJjYbtt7E8JyXtChB6kvonvsRh5Ro85aKr6Lp+AlbHH2KeyJwwNwf20SYrcNJ5Jb 0cm21Bcm4aU6EQHPyK1jy/B2RRb3tYt7qhr5kisfQgkUbcRhXlmmUh6oePlAtDTlbXAk gZVUaIWmKmBMslxfaVd+dd5c6VhRi/k8ApCgiK8Y6LLNhHzN0zdCuSOS5XvX1i1GrJxc 22jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ranlm6DM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w11-20020a056402268b00b00458b71488bdsi4803570edd.388.2022.12.10.14.24.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Dec 2022 14:24:27 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ranlm6DM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A9824383AB4C for ; Sat, 10 Dec 2022 22:23:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A9824383AB4C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670710982; bh=uveuOna947CSrNvr3VoiiSKHe9OMZ0xgQGHh19CSsWo=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Ranlm6DM4kGjIzoNvYnpwQDRLYM5sSNGE/WdiQWX7n2BIRtN9JAbv2hMoQD3+XTWB CrSnSNTKNKK9llBSoZn98VKIND83PL62hsspVFtZOfSc1SwvF0E6fnSXcluobJ40S+ 6LtCOLgvyOxTfD/uxEnbR+Z3GswMT5U9a4bBG/PQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by sourceware.org (Postfix) with ESMTPS id 012503845870 for ; Sat, 10 Dec 2022 22:21:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 012503845870 Received: by mail-qt1-x836.google.com with SMTP id jr11so5703110qtb.7 for ; Sat, 10 Dec 2022 14:21:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uveuOna947CSrNvr3VoiiSKHe9OMZ0xgQGHh19CSsWo=; b=Ao7hmixVHZaqARvutxLYqE/rMNTVaD9KBdnUagtYVWrJu3uxvR8sfbNtacVavU9Uqf wVHVbMi0/ZzPNme8uZwfafAl9rbthmYc5m57lkXwbL0pZ0ScKOx1vnzcsiygkwG/JLEU x9ikmuS3X4zGdp52uW2kmAYuEy15NhFZjP0AjoSoe/npNTfTxZxhpK+VXoH9p7V2hxIz 22OrljGZ8ea1FMs8m+DMp3+KL+7RM4KCu2piZfDtTlfAnHS4i2CmXbf5R1sIjU4X57IE 13VxNop7yQOwvdTWAPTpeLH/gjReMld/gMIid/CBzxSW2FYWequmm0bsORBKpDEmY4MD 3JoQ== X-Gm-Message-State: ANoB5pmry2NY6rzd0xcHwbvNctDAplpuaSqp6BsV9QCLR9hLlyI3Y5vX WvONkdC77GfS9a9YXHtwpdovNgBQQ4v/3Cdp67E= X-Received: by 2002:ac8:6b17:0:b0:3a7:f599:d7f2 with SMTP id w23-20020ac86b17000000b003a7f599d7f2mr13136683qts.51.1670710865463; Sat, 10 Dec 2022 14:21:05 -0800 (PST) Received: from localhost (cpe-142-105-146-128.nycap.res.rr.com. [142.105.146.128]) by smtp.gmail.com with ESMTPSA id y6-20020ac87086000000b0039cb59f00fcsm3343249qto.30.2022.12.10.14.21.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Dec 2022 14:21:04 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: Ben Boeckel , jason@redhat.com, nathan@acm.org, fortran@gcc.gnu.org, gcc@gcc.gnu.org, brad.king@kitware.com Subject: [PATCH v4 2/3] libcpp: add a function to determine UTF-8 validity of a C string Date: Sat, 10 Dec 2022 17:20:49 -0500 Message-Id: <20221210222050.1674457-3-ben.boeckel@kitware.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221210222050.1674457-1-ben.boeckel@kitware.com> References: <20221210222050.1674457-1-ben.boeckel@kitware.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ben Boeckel via Gcc-patches From: Ben Boeckel Reply-To: Ben Boeckel Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751867527611859302?= X-GMAIL-MSGID: =?utf-8?q?1751867527611859302?= This simplifies the interface for other UTF-8 validity detections when a simple "yes" or "no" answer is sufficient. libcpp/ * charset.cc: Add `_cpp_valid_utf8_str` which determines whether a C string is valid UTF-8 or not. * internal.h: Add prototype for `_cpp_valid_utf8_str`. Signed-off-by: Ben Boeckel --- libcpp/charset.cc | 20 ++++++++++++++++++++ libcpp/internal.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/libcpp/charset.cc b/libcpp/charset.cc index 324b5b19136..422cb52595c 100644 --- a/libcpp/charset.cc +++ b/libcpp/charset.cc @@ -1868,6 +1868,26 @@ _cpp_valid_utf8 (cpp_reader *pfile, return true; } +/* Detect whether a C-string is a valid UTF-8-encoded set of bytes. Returns + `false` if any contained byte sequence encodes an invalid Unicode codepoint + or is not a valid UTF-8 sequence. Returns `true` otherwise. */ + +extern bool +_cpp_valid_utf8_str (const char *name) +{ + const uchar* in = (const uchar*)name; + size_t len = strlen (name); + cppchar_t cp; + + while (*in) + { + if (one_utf8_to_cppchar (&in, &len, &cp)) + return false; + } + + return true; +} + /* Subroutine of convert_hex and convert_oct. N is the representation in the execution character set of a numeric escape; write it into the string buffer TBUF and update the end-of-string pointer therein. WIDE diff --git a/libcpp/internal.h b/libcpp/internal.h index badfd1b40da..4f2dd4a2f5c 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -834,6 +834,8 @@ extern bool _cpp_valid_utf8 (cpp_reader *pfile, struct normalize_state *nst, cppchar_t *cp); +extern bool _cpp_valid_utf8_str (const char *str); + extern void _cpp_destroy_iconv (cpp_reader *); extern unsigned char *_cpp_convert_input (cpp_reader *, const char *, unsigned char *, size_t, size_t,