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

* [PATCH 2/2] test: self: string: add test cases for strsep_unescaped
  2025-05-28  5:58 [PATCH 1/2] string: add delimiter output parameter to strsep_unescaped Ahmad Fatoum
@ 2025-05-28  5:58 ` Ahmad Fatoum
  0 siblings, 0 replies; 2+ messages in thread
From: Ahmad Fatoum @ 2025-05-28  5:58 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Just to make sure the function works as intended, add some unit tests.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/string.c | 138 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 136 insertions(+), 2 deletions(-)

diff --git a/test/self/string.c b/test/self/string.c
index d3d17cdc096f..f8da83c72a71 100644
--- a/test/self/string.c
+++ b/test/self/string.c
@@ -172,9 +172,9 @@ static void __expect_streq(const char *func, int line,
 			   char *is, const char *expect, bool free_is)
 {
 	total_tests++;
-	if (strcmp(is, expect)) {
+	if (!streq_ptr(is, expect)) {
 		failed_tests++;
-		printf("%s:%d: got %s, but %s expected\n", func, line, is, expect);
+		printf("%s:%d: got '%s', but '%s' expected\n", func, line, is, expect);
 	}
 
 	if (free_is)
@@ -183,6 +183,13 @@ static void __expect_streq(const char *func, int line,
 
 #define expect_dynstreq(args...) \
 	__expect_streq(__func__, __LINE__, args, true)
+#define expect_streq(args...) \
+	__expect_streq(__func__, __LINE__, args, false)
+#define expect_chreq(ch1, ch2) do { \
+	char s1[2] = { ch1, '\0' }; \
+	char s2[2] = { ch2, '\0' }; \
+	__expect_streq(__func__, __LINE__, s1, s2, false); \
+} while (0);
 
 static void test_strjoin(void)
 {
@@ -195,9 +202,136 @@ static void test_strjoin(void)
 	expect_dynstreq(strjoin(" ",   NULL, 0),                "");
 }
 
+static void test_strsep_unescaped_basic(void)
+{
+	char str[] = "ayy,bee,cee", *s = str, delim;
+
+	expect_streq(strsep_unescaped(&s, ",", NULL), "ayy");
+	expect_streq(strsep_unescaped(&s, ",", &delim), "bee");
+	expect_chreq(delim, ',');
+	expect_streq(strsep_unescaped(&s, ",", NULL), "cee");
+	delim = '!';
+	expect_streq(strsep_unescaped(&s, ",", &delim), NULL);
+	expect_chreq(delim, '!');
+}
+
+static void test_strsep_unescaped_with_escape(void)
+{
+	char str[] = "ayy\\,bee,cee", *s = str;
+
+	expect_streq(strsep_unescaped(&s, ",", NULL), "ayy,bee");
+	expect_streq(strsep_unescaped(&s, ",", NULL), "cee");
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_double_backslash(void)
+{
+	char str[] = "ayy\\\\,bee", *s = str;
+
+	expect_streq(strsep_unescaped(&s, ",", NULL), "ayy\\");
+	expect_streq(strsep_unescaped(&s, ",", NULL), "bee");
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_trailing_escape(void)
+{
+	char str[] = "ayy\\", *s = str;
+
+	expect_streq(strsep_unescaped(&s, ",", NULL), "ayy");
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_multiple_escaped(void)
+{
+	char str[] = "ayy\\,\\,bee\\,cee,dee", *s = str;
+
+	expect_streq(strsep_unescaped(&s, ",", NULL), "ayy,,bee,cee");
+	expect_streq(strsep_unescaped(&s, ",", NULL), "dee");
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_multiple_delims(void)
+{
+	char str[] = "ayy\\ \\:bee:cee dee", *s = str;
+
+	expect_streq(strsep_unescaped(&s, ": ", NULL), "ayy :bee");
+	expect_streq(strsep_unescaped(&s, ": ", NULL), "cee");
+	expect_streq(strsep_unescaped(&s, ": ", NULL), "dee");
+	expect_streq(strsep_unescaped(&s, ": ", NULL), NULL);
+}
+
+static void test_strsep_unescaped_multiple_delims_empty(void)
+{
+	char str[] = "ayy\\ \\:bee::cee  dee", *s = str, delim;
+
+	expect_streq(strsep_unescaped(&s, ": ", &delim), "ayy :bee");
+	expect_chreq(delim, ':');
+	expect_streq(strsep_unescaped(&s, ": ", &delim), "");
+	expect_chreq(delim, ':');
+	expect_streq(strsep_unescaped(&s, ": ", &delim), "cee");
+	expect_chreq(delim, ' ');
+	expect_streq(strsep_unescaped(&s, ": ", &delim), "");
+	expect_chreq(delim, ' ');
+	expect_streq(strsep_unescaped(&s, ": ", &delim), "dee");
+	expect_chreq(delim, '\0');
+	expect_streq(strsep_unescaped(&s, ": ", NULL), NULL);
+}
+
+static void test_strsep_unescaped_no_delim(void)
+{
+	char str[] = "abc", *s = str, delim;
+
+	expect_streq(strsep_unescaped(&s, ",", &delim), "abc");
+	expect_chreq(delim, '\0');
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_null_string(void)
+{
+	char *s = NULL;
+
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_empty_string(void)
+{
+	char str[] = "", *s = str, delim;
+
+	expect_streq(strsep_unescaped(&s, ",", &delim), "");
+	expect_chreq(delim, '\0');
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_only_delimiters(void)
+{
+	char str[] = ",,,", *s = str;
+
+	expect_streq(strsep_unescaped(&s, ",", NULL), "");
+	expect_streq(strsep_unescaped(&s, ",", NULL), "");
+	expect_streq(strsep_unescaped(&s, ",", NULL), "");
+	expect_streq(strsep_unescaped(&s, ",", NULL), "");
+	expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped(void)
+{
+	test_strsep_unescaped_basic();
+	test_strsep_unescaped_with_escape();
+	test_strsep_unescaped_double_backslash();
+	test_strsep_unescaped_trailing_escape();
+	test_strsep_unescaped_multiple_escaped();
+	test_strsep_unescaped_multiple_delims();
+	test_strsep_unescaped_multiple_delims_empty();
+	test_strsep_unescaped_no_delim();
+	test_strsep_unescaped_null_string();
+	test_strsep_unescaped_empty_string();
+	test_strsep_unescaped_only_delimiters();
+}
+
 static void test_string(void)
 {
 	test_strverscmp();
 	test_strjoin();
+	test_strsep_unescaped();
 }
 bselftest(parser, test_string);
-- 
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