From e93388417c1ecc6d69b155045cabb994cb8687fd Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 19 Mar 2021 17:39:24 +1030 Subject: [PATCH] Provide an inline startswith function in bfd.h bfd/ * bfd-in.h (startswith): New inline. (CONST_STRNEQ): Use startswith. * bfd-in2.h: Regenerate. gdbsupport/ * common-utils.h (startswith): Delete version now supplied by bfd.h. libctf/ * ctf-impl.h: Include string.h. --- bfd/ChangeLog | 6 ++++++ bfd/bfd-in.h | 10 +++++++++- bfd/bfd-in2.h | 10 +++++++++- gdbsupport/ChangeLog | 4 ++++ gdbsupport/common-utils.h | 12 ++---------- libctf/ChangeLog | 4 ++++ libctf/ctf-impl.h | 1 + 7 files changed, 35 insertions(+), 12 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 90ec96bacbf..bb273426eb3 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,9 @@ +2021-03-21 Alan Modra + + * bfd-in.h (startswith): New inline. + (CONST_STRNEQ): Use startswith. + * bfd-in2.h: Regenerate. + 2021-03-20 Alan Modra PR 27590 diff --git a/bfd/bfd-in.h b/bfd/bfd-in.h index c9a76731475..453ac48e262 100644 --- a/bfd/bfd-in.h +++ b/bfd/bfd-in.h @@ -65,7 +65,6 @@ extern "C" { definition of strncmp is provided here. Note - these macros do NOT work if STR2 is not a constant string. */ -#define CONST_STRNEQ(STR1,STR2) (strncmp ((STR1), (STR2), sizeof (STR2) - 1) == 0) /* strcpy() can have a similar problem, but since we know we are copying a constant string, we can use memcpy which will be faster since there is no need to check for a NUL byte inside STR. We @@ -564,3 +563,12 @@ struct ecoff_debug_swap; struct ecoff_extr; struct bfd_link_info; struct bfd_link_hash_entry; + +/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise. */ + +static inline bfd_boolean +startswith (const char *str, const char *prefix) +{ + return strncmp (str, prefix, strlen (prefix)) == 0; +} +#define CONST_STRNEQ(STR1,STR2) startswith (STR1, STR2) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 54c1c9a6b64..c8bf687ba2d 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -72,7 +72,6 @@ extern "C" { definition of strncmp is provided here. Note - these macros do NOT work if STR2 is not a constant string. */ -#define CONST_STRNEQ(STR1,STR2) (strncmp ((STR1), (STR2), sizeof (STR2) - 1) == 0) /* strcpy() can have a similar problem, but since we know we are copying a constant string, we can use memcpy which will be faster since there is no need to check for a NUL byte inside STR. We @@ -571,6 +570,15 @@ struct ecoff_debug_swap; struct ecoff_extr; struct bfd_link_info; struct bfd_link_hash_entry; + +/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise. */ + +static inline bfd_boolean +startswith (const char *str, const char *prefix) +{ + return strncmp (str, prefix, strlen (prefix)) == 0; +} +#define CONST_STRNEQ(STR1,STR2) startswith (STR1, STR2) /* Extracted from init.c. */ unsigned int bfd_init (void); diff --git a/gdbsupport/ChangeLog b/gdbsupport/ChangeLog index c8fa9e73148..a838cfc9991 100644 --- a/gdbsupport/ChangeLog +++ b/gdbsupport/ChangeLog @@ -1,3 +1,7 @@ +2021-03-21 Alan Modra + + * common-utils.h (startswith): Delete version now supplied by bfd.h. + 2021-02-05 Paul E. Murphy * common-defs.h (ATTRIBUTE_PRINTF): Rename diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h index 28c08ee9766..1de747f186d 100644 --- a/gdbsupport/common-utils.h +++ b/gdbsupport/common-utils.h @@ -116,16 +116,8 @@ std::string extract_string_maybe_quoted (const char **arg); extern const char *safe_strerror (int); -/* Return true if the start of STRING matches PATTERN, false otherwise. */ - -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. */ +/* Version of startswith that takes string_view arguments. Return + true if the start of STRING matches PATTERN, false otherwise. */ static inline bool startswith (gdb::string_view string, gdb::string_view pattern) diff --git a/libctf/ChangeLog b/libctf/ChangeLog index d19327ee58a..59f6fb042a0 100644 --- a/libctf/ChangeLog +++ b/libctf/ChangeLog @@ -1,3 +1,7 @@ +2021-03-21 Alan Modra + + * ctf-impl.h: Include string.h. + 2021-03-18 Nick Alcock * ctf-types.c (ctf_type_encoding): Support, after a fashion, for enums. diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h index ad4af32e7ee..342d2ff23e2 100644 --- a/libctf/ctf-impl.h +++ b/libctf/ctf-impl.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include -- 2.30.2