From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by sourceware.org (Postfix) with ESMTPS id CA17B3858C2D for ; Wed, 25 Jan 2023 21:06:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA17B3858C2D Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=kitware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kitware.com Received: by mail-qt1-x82d.google.com with SMTP id o5so17170565qtr.11 for ; Wed, 25 Jan 2023 13:06:55 -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=UJHIaYM0U5u0SEst0IBU/9LMwwy3p3yBJTTN7TdQwgI=; b=P5DA87llARzYx94XErFeCxeNfhOlUFGGjRtBnrbzIGrfzwnoeo23V3MadQkqk/jWbf 3TK96wiOLqAFSLKUfvPWEzpKDm92UJwO3Sx6PPBcGWyrnuWBrRu+9oWZuwIIUdjn/Si+ eWP8fHQ0/oVbP4CalJTV2VhFlo31OxBENGUb0= 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=UJHIaYM0U5u0SEst0IBU/9LMwwy3p3yBJTTN7TdQwgI=; b=owYwTE2/jEXgX/tZmpLgIkce6j2IRGUtb3/P4rBETm8AZJNcr+FhRV2AqYTj5I1YOt FlBW4Kf9AfK7M596uVwk673k4yXT+4tx3vQjA/gahg9vCfJWplCjn4nOGO1Lx5t8/Zs/ KzAgPFJvADMWbYWi1gR3YMetD3AwKMtY979uezcJvqmJCGjDEcG9TI38OM3wmWyNj6Fc yy+rWtW2Gy8stIg/uutY8GOzOXsIO9h0saR1DOJIOl4ISbOazxsTIgdsHHRrbReo/Tf2 dupDy6OkQVQTsuubMYwpiNIrS5hXrQMdTuzam4kLEpZpkbhLY+IGHU+d3qMUWChuTf8i zyHQ== X-Gm-Message-State: AO0yUKWWg+8SLEKRm5vJbi2h0llScPuuE4ZAhGz1IH/1stBrskG/UXqx zBtLcn1NGKHTOsdXP+fiaMHHHQ== X-Google-Smtp-Source: AK7set//ESpZkkUIhgm7Cw640FS2xdAEV3JCla0iPTYuGLxpKmEnol619Ij7CQCYmgm5LuQnzemB/g== X-Received: by 2002:ac8:4e94:0:b0:3b7:faaf:f6da with SMTP id 20-20020ac84e94000000b003b7faaff6damr9785164qtp.50.1674680815158; Wed, 25 Jan 2023 13:06:55 -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 72-20020a37074b000000b00706c1fc62desm4152724qkh.112.2023.01.25.13.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 13:06:54 -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 v5 2/5] libcpp: add a function to determine UTF-8 validity of a C string Date: Wed, 25 Jan 2023 16:06:33 -0500 Message-Id: <20230125210636.2960049-3-ben.boeckel@kitware.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230125210636.2960049-1-ben.boeckel@kitware.com> References: <20230125210636.2960049-1-ben.boeckel@kitware.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.3 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 f7ae12ea5a2..616be9d02ee 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 9724676a8cd..48520901b2d 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.39.0