History

string: factorize skip_spaces and export it to be generally available

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 most common case (non-null string). Fixed const return as noticed by Jan Engelhardt and James Bottomley. Fixed unnecessary extra cast on strstrip() as noticed by Jan Engelhardt.

André Goddard Rosa committed 16 years ago

Commit f6533981 parent 4e62b093 files · +17 −4Browse files

Unified · Split

3 files
  1. include/linux/ctype.h +1 −0
  2. include/linux/string.h +1 −0
  3. lib/string.c +15 −4
include/linux/ctype.h+1−0
@@ -27,6 +27,7 @@ extern const unsigned char _ctype[]; · 26 unmodified lines
2727 #define islower(c) ((__ismask(c)&(_L)) != 0)
2828 #define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
2929 #define ispunct(c) ((__ismask(c)&(_P)) != 0)
30+/* Note: isspace() must return false for %NUL-terminator */
3031 #define isspace(c) ((__ismask(c)&(_S)) != 0)
3132 #define isupper(c) ((__ismask(c)&(_U)) != 0)
3233 #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
include/linux/string.h+1−0
@@ -62,6 +62,7 @@ extern char * strnchr(const char *, size_t, int); · 61 unmodified lines
6262 #ifndef __HAVE_ARCH_STRRCHR
6363 extern char * strrchr(const char *,int);
6464 #endif
65+extern char * __must_check skip_spaces(const char *);
6566 extern char * __must_check strstrip(char *);
6667 #ifndef __HAVE_ARCH_STRSTR
6768 extern char * strstr(const char *,const char *);
lib/string.c+15−4
@@ -337,6 +337,20 @@ char *strnchr(const char *s, size_t count, int c) · 336 unmodified lines
337337 EXPORT_SYMBOL(strnchr);
338338 #endif
339339
340+/**
341+ * skip_spaces - Removes leading whitespace from @s.
342+ * @s: The string to be stripped.
343+ *
344+ * Returns a pointer to the first non-whitespace character in @s.
345+ */
346+char *skip_spaces(const char *str)
347+{
348+ while (isspace(*str))
349+ ++str;
350+ return (char *)str;
351+}
352+EXPORT_SYMBOL(skip_spaces);
353+
340354 /**
341355 * strstrip - Removes leading and trailing whitespace from @s.
342356 * @s: The string to be stripped.
@@ -360,10 +374,7 @@ char *strstrip(char *s) · 17 unmodified lines
360374 end--;
361375 *(end + 1) = '\0';
362376
363− while (*s && isspace(*s))
364− s++;
365−
366− return s;
377+ return skip_spaces(s);
367378 }
368379 EXPORT_SYMBOL(strstrip);
369380