]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: add normalize_whitespace()
authorKarel Zak <kzak@redhat.com>
Tue, 21 Jul 2020 10:16:40 +0000 (12:16 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 13 Nov 2020 08:19:02 +0000 (09:19 +0100)
This function removes extra whitespace.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c

index 4c5d18b4892514e22af5763350813ebf7604a4e8..09cc35e25345bc8840aa3760e4e0e17abc29135d 100644 (file)
@@ -310,6 +310,33 @@ static inline size_t ltrim_whitespace(unsigned char *str)
        return len;
 }
 
+/* Removes left-hand, right-hand and repeating whitespaces.
+ */
+static inline size_t normalize_whitespace(unsigned char *str)
+{
+       size_t i, x, sz = strlen((char *) str);
+       int nsp = 0, intext = 0;
+
+       if (!sz)
+               return 0;
+
+       for (i = 0, x = 0; i < sz; ) {
+               if (isspace(str[i]))
+                       nsp++;
+               else
+                       nsp = 0, intext = 1;
+
+               if (nsp > 1 || (nsp && !intext))
+                       i++;
+               else
+                       str[x++] = str[i++];
+       }
+       if (nsp)                /* tailing space */
+               x--;
+       str[x] = '\0';
+       return x;
+}
+
 static inline void strrep(char *s, int find, int replace)
 {
        while (s && *s && (s = strchr(s, find)) != NULL)
index 304f314076f5ab6c8f996c19b20d6fe9c0182b40..eec4cd5fa4d630b5704c60aac8002b6b2e1d5b32 100644 (file)
@@ -1114,6 +1114,23 @@ static int test_strutils_cmp_paths(int argc, char *argv[])
        return EXIT_SUCCESS;
 }
 
+static int test_strutils_normalize(int argc, char *argv[])
+{
+       unsigned char *str;
+       size_t sz;
+
+       if (argc < 2)
+               return EXIT_FAILURE;
+
+       str = (unsigned char *) strdup(argv[1]);
+       sz = normalize_whitespace(str);
+
+       printf("'%s' --> '%s' [sz=%zu]\n", argv[1], str, sz);
+       free(str);
+
+       return EXIT_SUCCESS;
+}
+
 int main(int argc, char *argv[])
 {
        if (argc == 3 && strcmp(argv[1], "--size") == 0)
@@ -1125,10 +1142,17 @@ int main(int argc, char *argv[])
        if (argc == 4 && strcmp(argv[1], "--strdup-member") == 0)
                return test_strdup_to_member(argc - 1, argv + 1);
 
-       fprintf(stderr, "usage: %1$s --size <number>[suffix]\n"
-                       "       %1$s --cmp-paths <path> <path>\n"
-                       "       %1$s --strdup-member <str> <str>\n",
-                       argv[0]);
+       else if (argc == 3 && strcmp(argv[1], "--normalize") == 0)
+               return test_strutils_normalize(argc - 1, argv + 1);
+
+       else {
+               fprintf(stderr, "usage: %1$s --size <number>[suffix]\n"
+                               "       %1$s --cmp-paths <path> <path>\n"
+                               "       %1$s --strdup-member <str> <str>\n"
+                               "       %1$s --normalize <str>\n",
+                               argv[0]);
+               exit(EXIT_FAILURE);
+       }
 
        return EXIT_FAILURE;
 }