From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by sourceware.org (Postfix) with ESMTPS id 0F799387054D for ; Sat, 10 Dec 2022 22:21:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0F799387054D Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=kitware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kitware.com Received: by mail-qt1-x82b.google.com with SMTP id j16so6320642qtv.4 for ; Sat, 10 Dec 2022 14:21:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kitware.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uveuOna947CSrNvr3VoiiSKHe9OMZ0xgQGHh19CSsWo=; b=rCF9w9K637WscN0AdYv00iMAlOO6m/t3PRmh3CShKVYKkp84sxAfrJq25gW6g5yTMh jto5uKVlI/pd1PUaEOYwtJb4MCevmINfWnvvtf93cuUI5ttvh/qyhE4c+cNN7gHDGP+W 2iQOp+T6rBk3Vi1LEIGpDnZZVKL7ObAz98kCg= 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=0HiQz3wOxGdwylmPVPl/DQOKaonGot+oqo2gtJo2/ecsX5X7mpTlMVcuDxzGRq0P6B Oe8j+XXdtu1IyokR/QjY6kWniy9CQ4xfhoPWP44sUOlq34sxgoiQ21+7dCL0ZpITJgkp 7QwEMs+iRoAWd1v/3dGcCQ7o1VevubD9cU+/LN7Qp7jLMZANLPYHCNfPkvmOFhLm2FPc pqFn5XMNQu5HdFBlN3lZpFt/qlXQqyM527HR10PtSjNfT5928poeDsqfszsPBltBDdt4 X69hfNdXLMa+MQY6xEpPDNdJ6Fe7KCCO7x//DIaqjXTgIkMf4tUgQG4C7+c1onb4xcvo leLA== X-Gm-Message-State: ANoB5pkbSZ2xlMbPEBSEZ1WEQOByJMgGGQ4z6KCl9iIwugoOKrqfeQa8 FtJ9ZfGKiKqq0TXKvlPtlH7NWQ== X-Google-Smtp-Source: AA0mqf6xXNunqCJWSERPAZ4EW8pOSW1EjjqHNiGrbO7b52k7akQjbvDDTI0xxlQME9chbqR4+d/RnQ== 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) From: Ben Boeckel 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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, -- 2.38.1