diff options
author | André Goddard Rosa <andre.goddard@gmail.com> | 2009-12-14 21:01:04 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-12-15 11:53:32 -0500 |
commit | f653398c86a1c104f0992bd788dd4bb065449be4 (patch) | |
tree | 69cd79aaca48c2e1bdf9a48b968772347dbd5df2 /lib | |
parent | 4e62b0930223fe2f61094ceb1bbb31b3fe4251c2 (diff) |
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/string.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/string.c b/lib/string.c index e96421ab9a9a..3a912a4e9a63 100644 --- a/lib/string.c +++ b/lib/string.c | |||
@@ -338,6 +338,20 @@ EXPORT_SYMBOL(strnchr); | |||
338 | #endif | 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 | * strstrip - Removes leading and trailing whitespace from @s. | 355 | * strstrip - Removes leading and trailing whitespace from @s. |
342 | * @s: The string to be stripped. | 356 | * @s: The string to be stripped. |
343 | * | 357 | * |
@@ -360,10 +374,7 @@ char *strstrip(char *s) | |||
360 | end--; | 374 | end--; |
361 | *(end + 1) = '\0'; | 375 | *(end + 1) = '\0'; |
362 | 376 | ||
363 | while (*s && isspace(*s)) | 377 | return skip_spaces(s); |
364 | s++; | ||
365 | |||
366 | return s; | ||
367 | } | 378 | } |
368 | EXPORT_SYMBOL(strstrip); | 379 | EXPORT_SYMBOL(strstrip); |
369 | 380 | ||