Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: André Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

André Goddard Rosa and committed by
Linus Torvalds
f653398c 4e62b093

+17 -4
+1
include/linux/ctype.h
··· 27 27 #define islower(c) ((__ismask(c)&(_L)) != 0) 28 28 #define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0) 29 29 #define ispunct(c) ((__ismask(c)&(_P)) != 0) 30 + /* Note: isspace() must return false for %NUL-terminator */ 30 31 #define isspace(c) ((__ismask(c)&(_S)) != 0) 31 32 #define isupper(c) ((__ismask(c)&(_U)) != 0) 32 33 #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
+1
include/linux/string.h
··· 62 62 #ifndef __HAVE_ARCH_STRRCHR 63 63 extern char * strrchr(const char *,int); 64 64 #endif 65 + extern char * __must_check skip_spaces(const char *); 65 66 extern char * __must_check strstrip(char *); 66 67 #ifndef __HAVE_ARCH_STRSTR 67 68 extern char * strstr(const char *,const char *);
+15 -4
lib/string.c
··· 338 338 #endif 339 339 340 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 + 354 + /** 341 355 * strstrip - Removes leading and trailing whitespace from @s. 342 356 * @s: The string to be stripped. 343 357 * ··· 374 360 end--; 375 361 *(end + 1) = '\0'; 376 362 377 - while (*s && isspace(*s)) 378 - s++; 379 - 380 - return s; 363 + return skip_spaces(s); 381 364 } 382 365 EXPORT_SYMBOL(strstrip); 383 366