mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] string: add delimiter output parameter to strsep_unescaped
@ 2025-05-28  5:58 Ahmad Fatoum
  2025-05-28  5:58 ` [PATCH 2/2] test: self: string: add test cases for strsep_unescaped Ahmad Fatoum
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2025-05-28  5:58 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

strsep overwrites the found delimiter with '\0' making it cumbersome for
callers that match on multiple delimiters to find out, which delimiter
actually matched.

Parsers that split on multiple delimiters are likely to want support
escaping them too, so let's add an extra output parameter to
strsep_unescaped to make it possible to retrieve the delimiter that was
ultimately overwritten.

The intention behind this change is to allow retrofitting existing
space-separated strings with a colon separator that has special
semantics for zero-size strings:

  "ayy  cee" -> old behavior: multiple spaces are concatenated
  "ayy::cee" -> empty string expands to some default value

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/sandbox/os/common.c | 10 +++++-----
 drivers/of/overlay.c     |  6 +++---
 include/string.h         |  2 +-
 lib/string.c             | 11 ++++++++++-
 test/self/string.c       |  2 +-
 5 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index c5043160b1f9..ef39f5336d60 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -305,7 +305,7 @@ int linux_watchdog_set_timeout(unsigned int timeout)
 extern void start_barebox(void);
 extern void mem_malloc_init(void *start, void *end);
 
-extern char * strsep_unescaped(char **s, const char *ct);
+extern char * strsep_unescaped(char **s, const char *ct, char *delim);
 
 static int add_image(const char *_str, char *devname_template, int *devname_number)
 {
@@ -320,8 +320,8 @@ static int add_image(const char *_str, char *devname_template, int *devname_numb
 
 	str = strdup(_str);
 
-	filename = strsep_unescaped(&str, ",");
-	while ((opt = strsep_unescaped(&str, ","))) {
+	filename = strsep_unescaped(&str, ",", NULL);
+	while ((opt = strsep_unescaped(&str, ",", NULL))) {
 		if (!strcmp(opt, "ro"))
 			hf->is_readonly = 1;
 		if (!strcmp(opt, "cdev"))
@@ -331,8 +331,8 @@ static int add_image(const char *_str, char *devname_template, int *devname_numb
 	}
 
 	/* parses: "devname=filename" */
-	devname = strsep_unescaped(&filename, "=");
-	filename = strsep_unescaped(&filename, "=");
+	devname = strsep_unescaped(&filename, "=", NULL);
+	filename = strsep_unescaped(&filename, "=", NULL);
 	if (!filename) {
 		filename = devname;
 		snprintf(tmp, sizeof(tmp),
diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
index 7b3936bd1c4a..6944dd4a744d 100644
--- a/drivers/of/overlay.c
+++ b/drivers/of/overlay.c
@@ -242,7 +242,7 @@ static bool of_overlay_matches_filter(const char *filename, struct device_node *
 
 	p = path = strdup(of_overlay_filter);
 
-	while ((n = strsep_unescaped(&p, " "))) {
+	while ((n = strsep_unescaped(&p, " ", NULL))) {
 		int score = 0;
 
 		if (!*n)
@@ -524,7 +524,7 @@ static bool of_overlay_filter_filename(struct of_overlay_filter *f,
 
 	p = path = strdup(of_overlay_filepattern);
 
-	while ((n = strsep_unescaped(&p, " "))) {
+	while ((n = strsep_unescaped(&p, " ", NULL))) {
 		if (!*n)
 			continue;
 
@@ -575,7 +575,7 @@ static bool of_overlay_filter_compatible(struct of_overlay_filter *f,
 
 	p = compatibles = xstrdup(of_overlay_compatible);
 
-	while ((n = strsep_unescaped(&p, " "))) {
+	while ((n = strsep_unescaped(&p, " ", NULL))) {
 		if (!*n)
 			continue;
 
diff --git a/include/string.h b/include/string.h
index 986ccd83dd73..db9e3406bfa5 100644
--- a/include/string.h
+++ b/include/string.h
@@ -7,7 +7,7 @@
 
 void *mempcpy(void *dest, const void *src, size_t count);
 int strtobool(const char *str, int *val);
-char *strsep_unescaped(char **, const char *);
+char *strsep_unescaped(char **, const char *, char *);
 char *stpcpy(char *dest, const char *src);
 bool strends(const char *str, const char *postfix);
 
diff --git a/lib/string.c b/lib/string.c
index f2272be37e76..03ee7917f40e 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -541,12 +541,17 @@ EXPORT_SYMBOL(strsep);
  * strsep_unescaped - Split a string into tokens, while ignoring escaped delimiters
  * @s: The string to be searched
  * @ct: The delimiter characters to search for
+ * @delim: optional pointer to store found delimiter into
  *
  * strsep_unescaped() behaves like strsep unless it meets an escaped delimiter.
  * In that case, it shifts the string back in memory to overwrite the escape's
  * backslash then continues the search until an unescaped delimiter is found.
+ *
+ * On end of string, this function returns NULL. As long as a non-NULL
+ * value is returned and @delim is not NULL, the found delimiter will
+ * be stored into *@delim.
  */
-char *strsep_unescaped(char **s, const char *ct)
+char *strsep_unescaped(char **s, const char *ct, char *delim)
 {
         char *sbegin = *s, *hay;
         const char *needle;
@@ -571,9 +576,13 @@ char *strsep_unescaped(char **s, const char *ct)
         }
 
         *s = NULL;
+	if (delim)
+		*delim = '\0';
         return sbegin;
 
 match:
+	if (delim)
+		*delim = *hay;
         *hay = '\0';
         *s = &hay[shift + 1];
 
diff --git a/test/self/string.c b/test/self/string.c
index 542277a09797..d3d17cdc096f 100644
--- a/test/self/string.c
+++ b/test/self/string.c
@@ -41,7 +41,7 @@ static int __strverscmp_assert(char *expr)
 	int expect = -42;
 	int i = 0;
 
-	while ((token = strsep_unescaped(&expr, " "))) {
+	while ((token = strsep_unescaped(&expr, " ", NULL))) {
 		if (i == 3) {
 			pr_err("invalid expression\n");
 			return -EILSEQ;
-- 
2.39.5




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

end of thread, other threads:[~2025-05-28  5:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-28  5:58 [PATCH 1/2] string: add delimiter output parameter to strsep_unescaped Ahmad Fatoum
2025-05-28  5:58 ` [PATCH 2/2] test: self: string: add test cases for strsep_unescaped Ahmad Fatoum

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