From 64b42e0042c980201d3782e2fbf2330b3dc85b38 Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Mon, 28 Oct 2019 03:00:35 +0100 Subject: [PATCH] * str: added str_delim(); fixed escape sequence output --- str/str.h | 19 +++++++++++++++++++ str/str_test.c | 16 ++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/str/str.h b/str/str.h index 2f5150f..db350ec 100644 --- a/str/str.h +++ b/str/str.h @@ -9,6 +9,8 @@ * str_icmp, * str_nicmp - compare two strings ignoring case * + * str_delim - locate the first occurrence of a delimiter + * * str_istr - locate a substring ignoring case * * mem_mem, @@ -64,6 +66,23 @@ static inline int str_nicmp(const char *s1, const char *s2, size_t n) { } +/* + * str_delim - locate the first occurrence of a delimiter + * + * The str_delim() function finds the first occurrence in the string s + * of one of the characters in the string delim. + * + * The function returns a pointer to the first occurrence of one of the + * specified delimiters, or a pointer to the terminating null character + * at the end of s, if no such occurrence was found. + */ +static inline char *str_delim(const char *s, const char *delim) { + while (!strchr(delim, *s)) + ++s; + return (char *)s; +} + + /* * str_istr - locate a substring ignoring case * diff --git a/str/str_test.c b/str/str_test.c index bc8a1cf..f58e6bc 100644 --- a/str/str_test.c +++ b/str/str_test.c @@ -24,8 +24,8 @@ for (int i = 0; i < (int)sizeof(s_); ++i) { \ if (s_[i] == 0) \ fprintf(stderr, "\\0"); \ - else if (s_[i] < ' ' || s_[i] > 127) \ - fprintf(stderr, "\\x%02"PRIX8, (uint8_t)(s_[i])); \ + else if (!isprint(s_[i])) \ + fprintf(stderr, "\\x%02"PRIx8, (uint8_t)(s_[i])); \ else \ fputc(s_[i], stderr); \ } \ @@ -69,6 +69,18 @@ int main(void) { T(str_nicmp("まほaSdf", "まほasdF", 1) == 0); T(str_nicmp("asdf", "asdF", 1) == 0); + H("str_delim"); + char s1[] = "abc:def,\r\n\tghi"; + D(s1); + T(str_delim(s1, "") - s1 == (int)strlen(s1)); + T(str_delim(s1, "*") - s1 == (int)strlen(s1)); + T(str_delim(s1, "i") - s1 == (int)strlen(s1)-1); + T(str_delim(s1, ":") - s1 == 3); + T(str_delim(s1, "-_:") - s1 == 3); + T(str_delim(s1, "e,") - s1 == 5); + T(str_delim(s1, ",#'") - s1 == 7); + T(str_delim(s1, "\n\r") - s1 == 8); + H("str_istr"); char h1[] = "asdfま\0ほyxc"; char h2[] = ""; -- 2.30.2