public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Add a string_view version of startswith
@ 2019-10-28 17:21 Christian Biesinger
  0 siblings, 0 replies; only message in thread
From: Christian Biesinger @ 2019-10-28 17:21 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=87f34879e5339be458505ca85f70cff346295140

commit 87f34879e5339be458505ca85f70cff346295140
Author: Christian Biesinger <cbiesinger@google.com>
Date:   Tue Oct 1 13:41:58 2019 -0500

    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
    https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/125
    
    gdb/ChangeLog:
    
    2019-10-28  Christian Biesinger  <cbiesinger@google.com>
    
    	* gdbsupport/common-utils.h (startswith): Add an overloaded version
    	that takes gdb::string_view arguments.
    
    Change-Id: I5389855de2fd70e7065a789a79374b0693651b71

Diff:
---
 gdb/ChangeLog                 |  5 +++++
 gdb/gdbsupport/common-utils.h | 12 ++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index edf7c34..7059623 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2019-10-28  Christian Biesinger  <cbiesinger@google.com>
+
+	* gdbsupport/common-utils.h (startswith): Add an overloaded version
+	that takes gdb::string_view arguments.
+
 2019-10-26  Tom de Vries  <tdevries@suse.de>
 
 	* aarch64-linux-tdep.c: Fix typos in comments.
diff --git a/gdb/gdbsupport/common-utils.h b/gdb/gdbsupport/common-utils.h
index e96fc21..23bf354 100644
--- a/gdb/gdbsupport/common-utils.h
+++ b/gdb/gdbsupport/common-utils.h
@@ -43,6 +43,8 @@
 #endif
 #endif
 
+#include "gdb_string_view.h"
+
 /* xmalloc(), xrealloc() and xcalloc() have already been declared in
    "libiberty.h". */
 
@@ -118,6 +120,16 @@ 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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-10-28 17:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-28 17:21 [binutils-gdb] Add a string_view version of startswith Christian Biesinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).