Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Add a string_view version of startswith
@ 2019-10-01 18:44 Christian Biesinger via gdb-patches
  2019-10-01 19:10 ` Pedro Alves
  0 siblings, 1 reply; 3+ messages in thread
From: Christian Biesinger via gdb-patches @ 2019-10-01 18:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Christian Biesinger

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.
---
 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 <string>
 #include <vector>
 
+#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
-- 
2.23.0.444.g18eeb5a265-goog


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

* Re: [PATCH] Add a string_view version of startswith
  2019-10-01 18:44 [PATCH] Add a string_view version of startswith Christian Biesinger via gdb-patches
@ 2019-10-01 19:10 ` Pedro Alves
  2019-10-01 19:17   ` Christian Biesinger via gdb-patches
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Alves @ 2019-10-01 19:10 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches

On 10/1/19 7:44 PM, Christian Biesinger via gdb-patches wrote:

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

non-zero -> true
zero -> false

>  
> -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;

&& goes on next line.  And then you need to wrap the multiline expression
with (), so that emacs auto-tab-indents the && under "string".  Like this:

  return (string.length() >= pattern.length ()
          && strncmp (string.data (), pattern.data (), pattern.length ()) == 0);

Otherwise OK.

Thanks,
Pedro Alves


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

* [PATCH] Add a string_view version of startswith
  2019-10-01 19:10 ` Pedro Alves
@ 2019-10-01 19:17   ` Christian Biesinger via gdb-patches
  0 siblings, 0 replies; 3+ messages in thread
From: Christian Biesinger via gdb-patches @ 2019-10-01 19:17 UTC (permalink / raw)
  To: gdb-patches; +Cc: Christian Biesinger

[Thanks Pedro, both comments are fixed now. Note that this patch depends on
https://sourceware.org/ml/gdb-patches/2019-10/msg00032.html]

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.
---
 gdb/gdbsupport/common-utils.h | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/gdb/gdbsupport/common-utils.h b/gdb/gdbsupport/common-utils.h
index a5312cb0c4..4a20a665cc 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 @@ std::string extract_string_maybe_quoted (const char **arg);
 
 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
-- 
2.23.0.444.g18eeb5a265-goog


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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-01 18:44 [PATCH] Add a string_view version of startswith Christian Biesinger via gdb-patches
2019-10-01 19:10 ` Pedro Alves
2019-10-01 19:17   ` Christian Biesinger via gdb-patches

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