Commit f653398c authored by André Goddard Rosa's avatar André Goddard Rosa Committed by Linus Torvalds

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.
Signed-off-by: default avatarAndré Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 4e62b093
...@@ -27,6 +27,7 @@ extern const unsigned char _ctype[]; ...@@ -27,6 +27,7 @@ extern const unsigned char _ctype[];
#define islower(c) ((__ismask(c)&(_L)) != 0) #define islower(c) ((__ismask(c)&(_L)) != 0)
#define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0) #define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
#define ispunct(c) ((__ismask(c)&(_P)) != 0) #define ispunct(c) ((__ismask(c)&(_P)) != 0)
/* Note: isspace() must return false for %NUL-terminator */
#define isspace(c) ((__ismask(c)&(_S)) != 0) #define isspace(c) ((__ismask(c)&(_S)) != 0)
#define isupper(c) ((__ismask(c)&(_U)) != 0) #define isupper(c) ((__ismask(c)&(_U)) != 0)
#define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0) #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
......
...@@ -62,6 +62,7 @@ extern char * strnchr(const char *, size_t, int); ...@@ -62,6 +62,7 @@ extern char * strnchr(const char *, size_t, int);
#ifndef __HAVE_ARCH_STRRCHR #ifndef __HAVE_ARCH_STRRCHR
extern char * strrchr(const char *,int); extern char * strrchr(const char *,int);
#endif #endif
extern char * __must_check skip_spaces(const char *);
extern char * __must_check strstrip(char *); extern char * __must_check strstrip(char *);
#ifndef __HAVE_ARCH_STRSTR #ifndef __HAVE_ARCH_STRSTR
extern char * strstr(const char *,const char *); extern char * strstr(const char *,const char *);
......
...@@ -337,6 +337,20 @@ char *strnchr(const char *s, size_t count, int c) ...@@ -337,6 +337,20 @@ char *strnchr(const char *s, size_t count, int c)
EXPORT_SYMBOL(strnchr); EXPORT_SYMBOL(strnchr);
#endif #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 (char *)str;
}
EXPORT_SYMBOL(skip_spaces);
/** /**
* strstrip - Removes leading and trailing whitespace from @s. * strstrip - Removes leading and trailing whitespace from @s.
* @s: The string to be stripped. * @s: The string to be stripped.
...@@ -360,10 +374,7 @@ char *strstrip(char *s) ...@@ -360,10 +374,7 @@ char *strstrip(char *s)
end--; end--;
*(end + 1) = '\0'; *(end + 1) = '\0';
while (*s && isspace(*s)) return skip_spaces(s);
s++;
return s;
} }
EXPORT_SYMBOL(strstrip); EXPORT_SYMBOL(strstrip);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment