From patchwork Wed Nov 9 02:10:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Boeckel X-Patchwork-Id: 17306 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp91719wru; Tue, 8 Nov 2022 18:12:48 -0800 (PST) X-Google-Smtp-Source: AMsMyM5Tyw64roI8GPva6fSA0OFcScw58QqveUfEtBYxUcGjoQ9cMoDMSIPe4HjgMh1n0P3O2dXK X-Received: by 2002:a05:6402:194d:b0:461:8982:52b8 with SMTP id f13-20020a056402194d00b00461898252b8mr58197301edz.49.1667959968600; Tue, 08 Nov 2022 18:12:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667959968; cv=none; d=google.com; s=arc-20160816; b=obFvkeGZnlI8O2DQKSP9n/qgfRwkfvwaFqxVsuTFObJXkxm/Lnt/KZU+YnOxaeU2mM vXVKeH33au8zfPA24k5C2Pa8BhlYFOAUcbKnMo4qqofJF4U4ViL4W/nXDGR2JSHgsGyM VW7UmYONRujG7ZzsmLb1YIxo7dqr5vJKMCyNeo0uFDfyCZp2do2A3NhnI9VpHdJETPSu 79qtvsfzvS8ewa5LdO3JqT6ARAGzyinpiXgMsebubBCBXO+WQAcxMKy2cdMT6LydS5+P K5l2nLesQmAGuzOfVPkclFfsdkWawtyUVkFrPHFgMh7hdCCEUCs3x92X4uKPLUy8p9dx 2jdg== 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=kp0PKF9mAvgM+v+KxF74DdB+Nsm0o/+XbhWVumRCo2Y=; b=RheDxDrbBgtRvKl2MGRYXYgv+SNXAbJO+YpXUPQhGXfBDDvGHDuKKepb0B33feRe8Q suvNv/DSwDWfqF2uX86AsvvDzJgnOEHc+Whv1NAfNap0XzSe6pfUyxh6aIwUDZn5nUWg Eez2yGThW/sUPdv7oRqVCTsGCOZL3A/Am4SKS0WtKvCC0/28n6jlTRFg9bbG40J5Wv+c NsxXR5LSUgayK8eTG6hPhRg8HE8/Cg3BUrDTzjT7EscluQ8dLUovNPTQ5ODmcYzIRfhT RcFYlT9IbH/6A2WV1c0FOdfa2Xyob5E58X4A8W4L2gX9BGd5ZWSTF2REhwlmMaint3o5 NKdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=mufuWXf1; 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 o21-20020a170906975500b007adf6f0f962si16930194ejy.291.2022.11.08.18.12.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 18:12:48 -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=mufuWXf1; 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 2510D3856955 for ; Wed, 9 Nov 2022 02:11:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2510D3856955 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667959919; bh=kp0PKF9mAvgM+v+KxF74DdB+Nsm0o/+XbhWVumRCo2Y=; 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=mufuWXf1Fo+bvZUavw2MrNKnDZc01Y2Wj9KPvQpjIotaHqfGPqwizn1U25Lav104N OP30WqveUuhvgM000WlFrWBazVf9TuO8fSnjh68/Edsx35QFLt1PNTXmkHnc3M82h5 Izk4ka8tGK3gnTr0uLkuIWS5NNrx63OKAQrEYmA0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by sourceware.org (Postfix) with ESMTPS id 74A5538582AE for ; Wed, 9 Nov 2022 02:11:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 74A5538582AE Received: by mail-qk1-x72c.google.com with SMTP id s20so10234001qkg.5 for ; Tue, 08 Nov 2022 18:11:03 -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=kp0PKF9mAvgM+v+KxF74DdB+Nsm0o/+XbhWVumRCo2Y=; b=DEM0ulZSbv23gzWfFOCQrytvrmP8RVzWIrGhjGWK25XD/Br1jukXhD4vOuWDe3LlI8 9WlvqrnpaLBR6pIhrmUMvPV134tJnefi9Lb8nDbxCkZtruOEQU867GuYWbZ1ao4pu98w 5Ck/ZaLTo9+Q3WefbaPiF/dJWWu+/yN5cbclfntHePoch64Yp00gCIxmGQ2Bo9pAPtIb X81jzPjUVQbGAWwfJNZNZ6AeSGgWUo0aD4+JngUvCl70PzQK2VvSk1dcMZJZtajJsIaS vn/YwacJCjmy24MRKjKij7zlP/yk+HJCxa9qhEh9za1q3iGoLpjETr0/yrufQdKsxGhy f3nQ== X-Gm-Message-State: ACrzQf1FpQ+B2uJI2q3mwgtpBHSs+je7wyJ9ofZpbyOQ+GWnT7dShTZJ MaiEFMeG5QtCmmutSKePXhwnYz/QR7weyw== X-Received: by 2002:a05:620a:208d:b0:6fa:b41e:8a02 with SMTP id e13-20020a05620a208d00b006fab41e8a02mr13149376qka.540.1667959862956; Tue, 08 Nov 2022 18:11:02 -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 q37-20020a05620a2a6500b006ee7923c187sm2127687qkp.42.2022.11.08.18.11.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 18:11:02 -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 v3 2/3] libcpp: add a function to determine UTF-8 validity of a C string Date: Tue, 8 Nov 2022 21:10:47 -0500 Message-Id: <20221109021048.2123704-3-ben.boeckel@kitware.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221109021048.2123704-1-ben.boeckel@kitware.com> References: <20221109021048.2123704-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?1748982791682916208?= X-GMAIL-MSGID: =?utf-8?q?1748982791682916208?= 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..e130bc01f48 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,