Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [review] Add a string_view version of startswith
@ 2019-10-16 12:39 Christian Biesinger (Code Review)
  2019-10-22 19:12 ` [review v2] " Christian Biesinger (Code Review)
                   ` (11 more replies)
  0 siblings, 12 replies; 14+ messages in thread
From: Christian Biesinger (Code Review) @ 2019-10-16 12:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Christian Biesinger

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  <cbiesinger@google.com>

	* 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 <string>
 #include <vector>
 
+#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


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2019-10-28 17:22 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-16 12:39 [review] Add a string_view version of startswith Christian Biesinger (Code Review)
2019-10-22 19:12 ` [review v2] " Christian Biesinger (Code Review)
2019-10-22 22:14 ` [review v3] " Christian Biesinger (Code Review)
2019-10-25 17:35 ` Tom Tromey (Code Review)
2019-10-26 23:11 ` Tom Tromey (Code Review)
2019-10-27  1:46   ` Simon Marchi
2019-10-27  1:46 ` Simon Marchi (2) (Code Review)
2019-10-27  1:49 ` Simon Marchi (Code Review)
2019-10-27 18:23 ` Christian Biesinger (Code Review)
2019-10-27 21:44 ` [review v4] " Tom Tromey (Code Review)
2019-10-27 22:10 ` Simon Marchi (Code Review)
2019-10-28 17:20 ` Christian Biesinger (Code Review)
2019-10-28 17:22 ` [pushed] " Sourceware to Gerrit sync (Code Review)
2019-10-28 17:22 ` Sourceware to Gerrit sync (Code Review)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox