* str: added str_delim(); fixed escape sequence output
authorUrban Wallasch <urban.wallasch@freenet.de>
Mon, 28 Oct 2019 02:00:35 +0000 (03:00 +0100)
committerUrban Wallasch <urban.wallasch@freenet.de>
Mon, 28 Oct 2019 02:00:35 +0000 (03:00 +0100)
str/str.h
str/str_test.c

index 2f5150f518f052910363a51e4db541521f527767..db350ec9a4d59594b291307b5106e4468781e9fd 100644 (file)
--- 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
  *
index bc8a1cf80d7a114c4bf3d4e2ffb2f828057e2ba0..f58e6bcf287b9f0e4e9dbcdfb1a8bb0844f4ddbc 100644 (file)
@@ -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[] = "";