Browse Source

lib/strutils: add ul_strchr_escaped()

Like strchr(), but ignores escaped chars.

Signed-off-by: Karel Zak <kzak@redhat.com>
pull/1675/head
Karel Zak 4 months ago
parent
commit
63bb04509e
  1. 2
      include/strutils.h
  2. 35
      lib/strutils.c

2
include/strutils.h

@ -389,6 +389,8 @@ extern int strappend(char **a, const char *b);
extern const char *split(const char **state, size_t *l, const char *separator, int quoted);
extern char *ul_strchr_escaped(const char *s, int c);
extern int skip_fline(FILE *fp);
extern int ul_stralnumcmp(const char *p1, const char *p2);

35
lib/strutils.c

@ -998,6 +998,37 @@ static size_t strcspn_escaped(const char *s, const char *reject)
return n - escaped;
}
/*
* Like strchr() but ignores @c if escaped by '\', '\\' is interpreted like '\'.
*
* For example for @c='X':
*
* "abcdXefgXh" --> "XefgXh"
* "abcd\XefgXh" --> "Xh"
* "abcd\\XefgXh" --> "XefgXh"
* "abcd\\\XefgXh" --> "Xh"
* "abcd\Xefg\Xh" --> (null)
*
* "abcd\\XefgXh" --> "\XefgXh" for @c='\\'
*/
char *ul_strchr_escaped(const char *s, int c)
{
char *p;
int esc = 0;
for (p = (char *) s; p && *p; p++) {
if (!esc && *p == '\\') {
esc = 1;
continue;
}
if (*p == c && (!esc || c == '\\'))
return p;
esc = 0;
}
return NULL;
}
/* Split a string into words. */
const char *split(const char **state, size_t *l, const char *separator, int quoted)
{
@ -1295,6 +1326,10 @@ int main(int argc, char *argv[])
printf("'%s'-->%hu\n", argv[2], strtou16_or_err(argv[2], "strtou16 failed"));
return EXIT_SUCCESS;
} else if (argc == 4 && strcmp(argv[1], "--strchr-escaped") == 0) {
printf("\"%s\" --> \"%s\"\n", argv[2], ul_strchr_escaped(argv[2], *argv[3]));
return EXIT_SUCCESS;
} else {
fprintf(stderr, "usage: %1$s --size <number>[suffix]\n"
" %1$s --cmp-paths <path> <path>\n"

Loading…
Cancel
Save