mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] string: implement str_has_prefix
@ 2022-08-08  6:20 Ahmad Fatoum
  2022-08-08 11:20 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2022-08-08  6:20 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

In addition to strstarts which we already have, Linux also defines
str_has_prefix(), which returns the length of the prefix on success and
zero otherwise. This allows writing straight-forward code to skip over
a prefix in case it exists. Port it over.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/linux/string.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index ae5e5bca8d24..0c79d3e5cf3e 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -155,4 +155,25 @@ static inline bool strstarts(const char *str, const char *prefix)
 	return strncmp(str, prefix, strlen(prefix)) == 0;
 }
 
+/**
+ * str_has_prefix - Test if a string has a given prefix
+ * @str: The string to test
+ * @prefix: The string to see if @str starts with
+ *
+ * A common way to test a prefix of a string is to do:
+ *  strncmp(str, prefix, sizeof(prefix) - 1)
+ *
+ * But this can lead to bugs due to typos, or if prefix is a pointer
+ * and not a constant. Instead use str_has_prefix().
+ *
+ * Returns:
+ * * strlen(@prefix) if @str starts with @prefix
+ * * 0 if @str does not start with @prefix
+ */
+static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
+{
+	size_t len = strlen(prefix);
+	return strncmp(str, prefix, len) == 0 ? len : 0;
+}
+
 #endif /* _LINUX_STRING_H_ */
-- 
2.30.2




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

* Re: [PATCH] string: implement str_has_prefix
  2022-08-08  6:20 [PATCH] string: implement str_has_prefix Ahmad Fatoum
@ 2022-08-08 11:20 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-08-08 11:20 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Aug 08, 2022 at 08:20:28AM +0200, Ahmad Fatoum wrote:
> In addition to strstarts which we already have, Linux also defines
> str_has_prefix(), which returns the length of the prefix on success and
> zero otherwise. This allows writing straight-forward code to skip over
> a prefix in case it exists. Port it over.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  include/linux/string.h | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)

Applied, thanks

Sascha

> 
> diff --git a/include/linux/string.h b/include/linux/string.h
> index ae5e5bca8d24..0c79d3e5cf3e 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -155,4 +155,25 @@ static inline bool strstarts(const char *str, const char *prefix)
>  	return strncmp(str, prefix, strlen(prefix)) == 0;
>  }
>  
> +/**
> + * str_has_prefix - Test if a string has a given prefix
> + * @str: The string to test
> + * @prefix: The string to see if @str starts with
> + *
> + * A common way to test a prefix of a string is to do:
> + *  strncmp(str, prefix, sizeof(prefix) - 1)
> + *
> + * But this can lead to bugs due to typos, or if prefix is a pointer
> + * and not a constant. Instead use str_has_prefix().
> + *
> + * Returns:
> + * * strlen(@prefix) if @str starts with @prefix
> + * * 0 if @str does not start with @prefix
> + */
> +static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
> +{
> +	size_t len = strlen(prefix);
> +	return strncmp(str, prefix, len) == 0 ? len : 0;
> +}
> +
>  #endif /* _LINUX_STRING_H_ */
> -- 
> 2.30.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



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

end of thread, other threads:[~2022-08-08 11:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-08  6:20 [PATCH] string: implement str_has_prefix Ahmad Fatoum
2022-08-08 11:20 ` Sascha Hauer

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