From patchwork Tue Oct 1 19:17:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 34771 Received: (qmail 2436 invoked by alias); 1 Oct 2019 19:17:52 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 2428 invoked by uid 89); 1 Oct 2019 19:17:52 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-qt1-f202.google.com Received: from mail-qt1-f202.google.com (HELO mail-qt1-f202.google.com) (209.85.160.202) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 01 Oct 2019 19:17:50 +0000 Received: by mail-qt1-f202.google.com with SMTP id 59so18609777qtc.5 for ; Tue, 01 Oct 2019 12:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=5G91OftbyYWICWKaqCsrmhMocebWifdh2xH6QOYNfOE=; b=do5cs0P49Y+4Y9HbADGixt0+V+ZfWwROyUh5mXt5g1YitN+9OIbqTYXhRvlwt4iPEy l+H5GcCmefgejSbK+7FFNXmE/mmYvDoQX+idG2JJDmlRFc6Wr/wnCVKHa/QfAAeE7X0x 7FwLDYawuBfv3N3K/d0h3vPULLGUSHR6UjYWsNl6c5JfXifmMakjdZHmlpMcFNQXWF38 sxj5/ruIfG0HZxz9a4RiEgY9B7Zc1EvrexOjA/TBQA4Bc+CfxjEL97EWa7dBdoR15//5 BUyJStYYNDqMcPv6gQ5Y+23CCxtZAz8l8RzWzlngiqfylR3DVHRFYapmBtzz5eiNa4VK 2uFg== Date: Tue, 1 Oct 2019 14:17:43 -0500 In-Reply-To: Message-Id: <20191001191743.239208-1-cbiesinger@google.com> Mime-Version: 1.0 References: Subject: [PATCH] Add a string_view version of startswith X-Patchwork-Original-From: "Christian Biesinger via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger X-IsSubscribed: yes [Thanks Pedro, both comments are fixed now. Note that this patch depends on https://sourceware.org/ml/gdb-patches/2019-10/msg00032.html] Makes sure that the string is longer than prefix, so that strncmp will do the right thing even if the string is not null-terminated. For use in my string_view conversion patch: https://sourceware.org/ml/gdb-patches/2019-10/msg00030.html gdb/ChangeLog: 2019-10-01 Christian Biesinger * gdbsupport/common-utils.h (startswith): Add an overloaded version that takes gdb::string_view arguments. --- gdb/gdbsupport/common-utils.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gdb/gdbsupport/common-utils.h b/gdb/gdbsupport/common-utils.h index a5312cb0c4..4a20a665cc 100644 --- a/gdb/gdbsupport/common-utils.h +++ b/gdb/gdbsupport/common-utils.h @@ -23,6 +23,7 @@ #include #include +#include "gdb_string_view.h" #include "poison.h" /* If possible, define FUNCTION_NAME, a macro containing the name of @@ -110,15 +111,25 @@ std::string extract_string_maybe_quoted (const char **arg); extern char *safe_strerror (int); -/* Return non-zero if the start of STRING matches PATTERN, zero +/* Return true if the start of STRING matches PATTERN, false otherwise. */ -static inline int +static inline bool startswith (const char *string, const char *pattern) { return strncmp (string, pattern, strlen (pattern)) == 0; } +/* Version of startswith that takes string_view arguments. See comment + above. */ + +static inline bool +startswith (gdb::string_view string, gdb::string_view pattern) +{ + return (string.length() >= pattern.length () + && strncmp (string.data (), pattern.data (), pattern.length ()) == 0); +} + ULONGEST strtoulst (const char *num, const char **trailer, int base); /* Skip leading whitespace characters in INP, returning an updated