Add a string_view version of startswith
authorChristian Biesinger <cbiesinger@google.com>
Tue, 1 Oct 2019 18:41:58 +0000 (13:41 -0500)
committerChristian Biesinger <cbiesinger@google.com>
Mon, 28 Oct 2019 17:20:13 +0000 (12:20 -0500)
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

gdb/ChangeLog
gdb/gdbsupport/common-utils.h

index edf7c34f43cb695f783271f05f6fc6c9afdec795..7059623788e7713547f3fa167ac2a4e19b1753bd 100644 (file)
@@ -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.
index e96fc21e3f20ad20ac5a68d0356624f9cb033c88..23bf354a8bd1bedff5c6855ec0759acb430173bf 100644 (file)
@@ -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
This page took 0.031716 seconds and 4 git commands to generate.