From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/2] string: add delimiter output parameter to strsep_unescaped
Date: Wed, 28 May 2025 07:58:13 +0200 [thread overview]
Message-ID: <20250528055814.1368888-1-a.fatoum@pengutronix.de> (raw)
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
next reply other threads:[~2025-05-28 5:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-28 5:58 Ahmad Fatoum [this message]
2025-05-28 5:58 ` [PATCH 2/2] test: self: string: add test cases for strsep_unescaped Ahmad Fatoum
2025-06-02 11:47 ` [PATCH 1/2] string: add delimiter output parameter to strsep_unescaped Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250528055814.1368888-1-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox