From patchwork Tue Oct 1 18:44:50 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: 34770 Received: (qmail 39106 invoked by alias); 1 Oct 2019 18:44:55 -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 39095 invoked by uid 89); 1 Oct 2019 18:44:55 -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=H*MI:google X-HELO: mail-yb1-f201.google.com Received: from mail-yb1-f201.google.com (HELO mail-yb1-f201.google.com) (209.85.219.201) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 01 Oct 2019 18:44:53 +0000 Received: by mail-yb1-f201.google.com with SMTP id 191so13069628ybr.6 for ; Tue, 01 Oct 2019 11:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=jRPHG3znhXsypcjFcLSabH7bH4ENijyUAGxQ5WTeayg=; b=rF9MU2owTklLoDwOo1ggph59yJeiAOGM+XLrYLsqSLaPIv9fHeULRgOgJ/f4D+DfSt o/GsXUgpP0Y43Wl9k5Ta7c103/uP+haKQqp+tMR6J+TH/HKxjV1qdK/7mP4yp6UfXVQb txSePznGpHHzPZX/1FO4H00h8+dtur0ymdOHkvM+NxJgOY/pWP+DPq9e53NyWAgrkake YveSoDNox8Zi65CyMkbKw+YaCL0ldnI15/g6e2K2fth0pFY5aiMTM2ytxbhz9WCZU3Xj ddvfRtC6m9Kj4yRYAxXjkHL36WCs1Twe1AklZtwURLbw1KG//DJA0IXfcM3rUBS2K6tj S2dQ== Date: Tue, 1 Oct 2019 13:44:50 -0500 Message-Id: <20191001184450.223945-1-cbiesinger@google.com> Mime-Version: 1.0 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 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 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/gdb/gdbsupport/common-utils.h b/gdb/gdbsupport/common-utils.h index a5312cb0c4..c21c5e9603 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 @@ -113,12 +114,22 @@ extern char *safe_strerror (int); /* Return non-zero if the start of STRING matches PATTERN, zero 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