[PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available

From: Andrà Goddard Rosa
Date: Sun Nov 08 2009 - 11:25:44 EST


On the following sentence:
while (*s && isspace(*s))
s++;

If *s == 0, isspace() evaluates to ((_ctype[*s] & 0x20) != 0), which
evaluates to ((0x08 & 0x20) != 0) which equals to 0 as well.
If *s == 1, we depend on isspace() result anyway.

In other words, "a char equals zero is never a space". So remove this check.
Also, *s != 0 is by far the most common case (non-empty string).

Fixed const return as noticed by Jan Engelhardt and James Bottomley

Signed-off-by: Andrà Goddard Rosa <andre.goddard@xxxxxxxxx>
---
include/linux/string.h | 1 +
lib/string.c | 19 +++++++++++++++----
2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index b850886..3bba9ee 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -62,6 +62,7 @@ extern char * strnchr(const char *, size_t, int);
#ifndef __HAVE_ARCH_STRRCHR
extern char * strrchr(const char *,int);
#endif
+extern char * __must_check skip_spaces(const char *);
extern char * __must_check strstrip(char *);
#ifndef __HAVE_ARCH_STRSTR
extern char * strstr(const char *,const char *);
diff --git a/lib/string.c b/lib/string.c
index b19b87a..d9a51d5 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -330,6 +330,20 @@ EXPORT_SYMBOL(strnchr);
#endif

/**
+ * skip_spaces - Removes leading whitespace from @s.
+ * @s: The string to be stripped.
+ *
+ * Returns a pointer to the first non-whitespace character in @s.
+ */
+char *skip_spaces(const char *str)
+{
+ while (isspace(*str))
+ ++str;
+ return str;
+}
+EXPORT_SYMBOL(skip_spaces);
+
+/**
* strstrip - Removes leading and trailing whitespace from @s.
* @s: The string to be stripped.
*
@@ -352,10 +366,7 @@ char *strstrip(char *s)
end--;
*(end + 1) = '\0';

- while (*s && isspace(*s))
- s++;
-
- return s;
+ return (char *)skip_spaces(s);
}
EXPORT_SYMBOL(strstrip);

--
1.6.5.2.153.g6e31f.dirty

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/