Skip to contentSkip to conversation

string: Group str_has_prefix() and strstarts()

PATCH Applied Andy Shevchenko 3 participants 5 messages Jul 11, 2025 – Jul 17, 2025 Generic string library

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 string: Group str_has_prefix() and strstarts()

The two str_has_prefix() and strstarts() are about the same with a slight difference on what they return. Group them in the header.

---
 include/linux/string.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
include/linux/string.h +10 -10
index 01621ad0f598..fdd3442c6bcb 100644
@@ -345,16 +345,6 @@ extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
 
 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
 
-/**
- * strstarts - does @str start with @prefix?
- * @str: string to examine
- * @prefix: prefix to look for.
- */
-static inline bool strstarts(const char *str, const char *prefix)
-{
-	return strncmp(str, prefix, strlen(prefix)) == 0;
-}
-
 size_t memweight(const void *ptr, size_t bytes);
 
 /**
@@ -562,4 +552,14 @@ static __always_inline size_t str_has_prefix(const char *str, const char *prefix
 	return strncmp(str, prefix, len) == 0 ? len : 0;
 }
 
+/**
+ * strstarts - does @str start with @prefix?
+ * @str: string to examine
+ * @prefix: prefix to look for.
+ */
+static inline bool strstarts(const char *str, const char *prefix)
+{
+	return strncmp(str, prefix, strlen(prefix)) == 0;
+}
+
 #endif /* _LINUX_STRING_H_ */
Signature · 1 line
2.47.2
maintainer
#1 lore raw
Quoted 3 lines · Andy Shevchenko

The two str_has_prefix() and strstarts() are about the same with a slight difference on what they return. Group them in the header.

It seems more like strstarts should just be a macro around str_has_prefix() != 0?

-Kees

Quoted 45 lines
---
 include/linux/string.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
include/linux/string.h +10 -10
index 01621ad0f598..fdd3442c6bcb 100644
@@ -345,16 +345,6 @@ extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
 
 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
 
-/**
- * strstarts - does @str start with @prefix?
- * @str: string to examine
- * @prefix: prefix to look for.
- */
-static inline bool strstarts(const char *str, const char *prefix)
-{
-	return strncmp(str, prefix, strlen(prefix)) == 0;
-}
-
 size_t memweight(const void *ptr, size_t bytes);
 
 /**
@@ -562,4 +552,14 @@ static __always_inline size_t str_has_prefix(const char *str, const char *prefix
 	return strncmp(str, prefix, len) == 0 ? len : 0;
 }
 
+/**
+ * strstarts - does @str start with @prefix?
+ * @str: string to examine
+ * @prefix: prefix to look for.
+ */
+static inline bool strstarts(const char *str, const char *prefix)
+{
+	return strncmp(str, prefix, strlen(prefix)) == 0;
+}
+
 #endif /* _LINUX_STRING_H_ */
Signature · 1 line
2.47.2
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 8 lines · Kees Cook
Quoted 3 lines · Andy Shevchenko

The two str_has_prefix() and strstarts() are about the same with a slight difference on what they return. Group them in the header.

It seems more like strstarts should just be a macro around str_has_prefix() != 0?

This change is only about grouping, but if you think it worth modifying, I am not going to object. Just that I am about to have my vacation and I leave this as is for now. Up to you how to proceed.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Kees Cook
Quoted 3 lines · Andy Shevchenko

The two str_has_prefix() and strstarts() are about the same with a slight difference on what they return. Group them in the header.

It seems more like strstarts should just be a macro around str_has_prefix() != 0?

Actually this won't work for len == 0, would it?

Quoted 3 lines

This change is only about grouping, but if you think it worth modifying, I am not going to object. Just that I am about to have my vacation and I leave this as is for now. Up to you how to proceed.

Signature · 2 lines
With Best Regards,
Andy Shevchenko