From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by sourceware.org (Postfix) with ESMTPS id 90A2138582B6 for ; Wed, 9 Nov 2022 02:11:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 90A2138582B6 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-qk1-x72d.google.com with SMTP id i9so10211705qki.10 for ; Tue, 08 Nov 2022 18:11:03 -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=kp0PKF9mAvgM+v+KxF74DdB+Nsm0o/+XbhWVumRCo2Y=; b=BisVWkz+jIwfpiwE1hWze9Gdz9uZ8uCQU/p5iwuI0Q/MGD7B3Z/LfKd5aFGvz3zG4x GpXEUg/3+TkWwR75YI5IRb8yVezEVvDivkQJg5Dl6lLsr30OPxSt85kUArmbXpzUWk+l +xzorQOvF/ykxcFw3KBJ5EoGndKOHjZt9onGw= 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=ncNvQSAh0Queaji2NekiUl14ugEAI32o9IInmOc/otroxn5ZVyDch+Xv2oA8nK8Gc4 TNNk3JpfddJSUdNBsQk0a2Z44bIsuX8U26qidYCv9xQnekfPQJoVvV9tomzK7QNyYALE ZMbGW8lVVDPKHohu6dfK4kh+AwBaQGcBY9VqnIHIL56J1rywDuqlICkQzgrBfU74uJjX lQC/7Yh73SAZsmgk5bblKU/phyaEYBSHLQM/GifNwdboZdz2RJNG8qTIWfQOQarsR49u JTj9GI5HSvxctty9O78kyw6Gd4FUP/tPRRSjJP75dcdIiZJZNiUbf3Oys8daozAKuNWe tUVg== X-Gm-Message-State: ACrzQf10XvRv5HSCej3HHLaxwI3ZG4TWSKUshT2+tpuB4HT9bRK9nBJy w4lhBj2oOOq+WGqaV9Im4DUf6g== X-Google-Smtp-Source: AMsMyM49tEXo3cTp3jLYDg3Fvo4A9KGz2mR1hiCrP7TGBvKIaoG1zssOMhBBoolHbTcRitECEIohKg== 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) 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 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.2 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=unavailable 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..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, -- 2.38.1