diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2024-03-04 20:00:17 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2024-03-05 16:28:06 +0100 |
commit | b823168b76ff9b2ae12730197dd73a06ade728d6 (patch) | |
tree | b536fea9ecfd3d8b5a958bd304a12ea5b72676e7 | |
parent | 402dae62ce82283bab70da32c06fff4e012e45c9 (diff) | |
download | barebox-b823168b76ff.tar.gz barebox-b823168b76ff.tar.xz |
lib: string: import Linux strreplace helper
This helper replaces all occurrences of a given character in a string.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20240304190038.3486881-93-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r-- | include/linux/string.h | 1 | ||||
-rw-r--r-- | lib/string.c | 21 |
2 files changed, 22 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h index 32ce569396..0d046f7832 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 695e50bc8f..374f326143 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); |