]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: move string trim function to strutils.h
authorKarel Zak <kzak@redhat.com>
Mon, 25 Aug 2014 13:31:53 +0000 (15:31 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 25 Aug 2014 13:31:53 +0000 (15:31 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
libblkid/src/probe.c

index 3883b4288f4aedac8d43c4e440323b67a02affba..cfe8a95603bdc838adb3c0fbd2487c9646c59aba 100644 (file)
@@ -161,4 +161,41 @@ static inline const char *skip_blank(const char *p)
        return p;
 }
 
+
+/* Removes whitespace from the right-hand side of a string (trailing
+ * whitespace).
+ *
+ * Returns size of the new string (without \0).
+ */
+static inline size_t rtrim_whitespace(unsigned char *str)
+{
+       size_t i = strlen((char *) str);
+
+       while (i--) {
+               if (!isspace(str[i]))
+                       break;
+       }
+       str[++i] = '\0';
+       return i;
+}
+
+/* Removes whitespace from the left-hand side of a string.
+ *
+ * Returns size of the new string (without \0).
+ */
+static inline size_t ltrim_whitespace(unsigned char *str)
+{
+       size_t len;
+       unsigned char *p;
+
+       for (p = str; p && isspace(*p); p++);
+
+       len = strlen((char *) p);
+
+       if (len && p > str)
+               memmove(str, p, len + 1);
+
+       return len;
+}
+
 #endif
index b1eddd628b011de86e2f9a6db4520cdb15d32826..4fea3ccb4ac720c854dc684370a01368a8392aa0 100644 (file)
 #include "blkidP.h"
 #include "all-io.h"
 #include "sysfs.h"
+#include "strutils.h"
 
 /* chains */
 extern const struct blkid_chaindrv superblocks_drv;
@@ -1717,14 +1718,7 @@ int blkid_uuid_is_empty(const unsigned char *buf, size_t len)
  */
 size_t blkid_rtrim_whitespace(unsigned char *str)
 {
-       size_t i = strlen((char *) str);
-
-       while (i--) {
-               if (!isspace(str[i]))
-                       break;
-       }
-       str[++i] = '\0';
-       return i;
+       return rtrim_whitespace(str);
 }
 
 /* Removes whitespace from the left-hand side of a string.
@@ -1733,18 +1727,9 @@ size_t blkid_rtrim_whitespace(unsigned char *str)
  */
 size_t blkid_ltrim_whitespace(unsigned char *str)
 {
-       size_t len;
-       unsigned char *p;
-
-       for (p = str; p && isspace(*p); p++);
-
-       len = strlen((char *) p);
-
-       if (len && p > str)
-               memmove(str, p, len + 1);
-
-       return len;
+       return ltrim_whitespace(str);
 }
+
 /*
  * Some mkfs-like utils wipe some parts (usually begin) of the device.
  * For example LVM (pvcreate) or mkswap(8). This information could be used