[PATCH 091/112] lib: string: import Linux strreplace helper

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jan 3 10:12:51 PST 2024


This helper replaces all occurrences of a given character in a string.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 32ce56939699..0d046f783280 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -115,6 +115,7 @@ extern char * skip_spaces(const char *);
 extern char *strim(char *);
 
 void *memchr_inv(const void *start, int c, size_t bytes);
+char *strreplace(char *str, char old, char new);
 
 /**
  * memzero_explicit - Fill a region of memory (e.g. sensitive
diff --git a/lib/string.c b/lib/string.c
index 695e50bc8fc1..374f326143a7 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -1028,3 +1028,24 @@ char *strjoin(const char *separator, char **arr, size_t arrlen)
 	return buf;
 }
 EXPORT_SYMBOL(strjoin);
+
+/**
+ * strreplace - Replace all occurrences of character in string.
+ * @str: The string to operate on.
+ * @old: The character being replaced.
+ * @new: The character @old is replaced with.
+ *
+ * Replaces the each @old character with a @new one in the given string @str.
+ *
+ * Return: pointer to the string @str itself.
+ */
+char *strreplace(char *str, char old, char new)
+{
+	char *s = str;
+
+	for (; *s; ++s)
+		if (*s == old)
+			*s = new;
+	return str;
+}
+EXPORT_SYMBOL(strreplace);
-- 
2.39.2




More information about the barebox mailing list