From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6570 invoked by alias); 16 Oct 2019 12:39:18 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 6511 invoked by uid 89); 16 Oct 2019 12:39:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3 autolearn=ham version=3.3.1 spammy= X-HELO: mx1.osci.io Received: from polly.osci.io (HELO mx1.osci.io) (8.43.85.229) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 16 Oct 2019 12:39:16 +0000 Received: by mx1.osci.io (Postfix, from userid 994) id C0EC2208FF; Wed, 16 Oct 2019 08:39:13 -0400 (EDT) Received: from gnutoolchain-gerrit.osci.io (gnutoolchain-gerrit.osci.io [IPv6:2620:52:3:1:5054:ff:fe06:16ca]) by mx1.osci.io (Postfix) with ESMTP id 5CFA9208B6; Wed, 16 Oct 2019 08:39:08 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by gnutoolchain-gerrit.osci.io (Postfix) with ESMTP id 3EB8021926; Wed, 16 Oct 2019 08:39:08 -0400 (EDT) X-Gerrit-PatchSet: 1 Date: Wed, 16 Oct 2019 12:39:00 -0000 From: "Christian Biesinger (Code Review)" To: gdb-patches@sourceware.org Cc: Christian Biesinger Message-ID: Auto-Submitted: auto-generated X-Gerrit-MessageType: newchange Subject: [review] Add a string_view version of startswith X-Gerrit-Change-Id: I5389855de2fd70e7065a789a79374b0693651b71 X-Gerrit-Change-Number: 126 X-Gerrit-ChangeURL: X-Gerrit-Commit: 4f19efa87b8c6a33090da56fd122bfaae9731f12 References: Reply-To: cbiesinger@google.com, cbiesinger@google.com, gdb-patches@sourceware.org MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline User-Agent: Gerrit/3.0.3 Content-Type: text/plain; charset=UTF-8 X-SW-Source: 2019-10/txt/msg00473.txt.bz2 Christian Biesinger has uploaded a new change for review. Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/126 ...................................................................... Add a string_view version of startswith 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. Change-Id: I5389855de2fd70e7065a789a79374b0693651b71 --- M gdb/gdbsupport/common-utils.h 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gdb/gdbsupport/common-utils.h b/gdb/gdbsupport/common-utils.h index a5312cb..e76b5b8 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 @@ 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