basic/extract-word: add EXTRACT_RETAIN_SEPARATORS flag

Makes the helpers avoid skipping over the separator(s) in the
input string
This commit is contained in:
Luca Boccassi
2021-06-24 13:41:17 +01:00
parent b80ef40caf
commit 70b6ee6110
3 changed files with 65 additions and 9 deletions
+12 -9
View File
@@ -51,7 +51,8 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
goto finish_force_terminate;
else if (strchr(separators, c)) {
if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
(*p)++;
if (!(flags & EXTRACT_RETAIN_SEPARATORS))
(*p)++;
goto finish_force_next;
}
} else {
@@ -153,16 +154,18 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
break;
} else if (strchr(separators, c)) {
if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
(*p)++;
if (!(flags & EXTRACT_RETAIN_SEPARATORS))
(*p)++;
goto finish_force_next;
}
/* Skip additional coalesced separators. */
for (;; (*p)++, c = **p) {
if (c == 0)
goto finish_force_terminate;
if (!strchr(separators, c))
break;
}
if (!(flags & EXTRACT_RETAIN_SEPARATORS))
/* Skip additional coalesced separators. */
for (;; (*p)++, c = **p) {
if (c == 0)
goto finish_force_terminate;
if (!strchr(separators, c))
break;
}
goto finish;
}
+1
View File
@@ -12,6 +12,7 @@ typedef enum ExtractFlags {
EXTRACT_UNQUOTE = 1 << 5, /* Ignore separators in quoting with "" and '', and remove the quotes. */
EXTRACT_DONT_COALESCE_SEPARATORS = 1 << 6, /* Don't treat multiple adjacent separators as one */
EXTRACT_RETAIN_ESCAPE = 1 << 7, /* Treat escape character '\' as any other character without special meaning */
EXTRACT_RETAIN_SEPARATORS = 1 << 8, /* Do not advance the original string pointer past the separator(s) */
/* Note that if no flags are specified, escaped escape characters will be silently stripped. */
} ExtractFlags;