diff options
Diffstat (limited to 'lib/string.c')
-rw-r--r-- | lib/string.c | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/lib/string.c b/lib/string.c index e96421ab9a9a..f71bead1be3e 100644 --- a/lib/string.c +++ b/lib/string.c | |||
@@ -36,25 +36,21 @@ int strnicmp(const char *s1, const char *s2, size_t len) | |||
36 | /* Yes, Virginia, it had better be unsigned */ | 36 | /* Yes, Virginia, it had better be unsigned */ |
37 | unsigned char c1, c2; | 37 | unsigned char c1, c2; |
38 | 38 | ||
39 | c1 = c2 = 0; | 39 | if (!len) |
40 | if (len) { | 40 | return 0; |
41 | do { | 41 | |
42 | c1 = *s1; | 42 | do { |
43 | c2 = *s2; | 43 | c1 = *s1++; |
44 | s1++; | 44 | c2 = *s2++; |
45 | s2++; | 45 | if (!c1 || !c2) |
46 | if (!c1) | 46 | break; |
47 | break; | 47 | if (c1 == c2) |
48 | if (!c2) | 48 | continue; |
49 | break; | 49 | c1 = tolower(c1); |
50 | if (c1 == c2) | 50 | c2 = tolower(c2); |
51 | continue; | 51 | if (c1 != c2) |
52 | c1 = tolower(c1); | 52 | break; |
53 | c2 = tolower(c2); | 53 | } while (--len); |
54 | if (c1 != c2) | ||
55 | break; | ||
56 | } while (--len); | ||
57 | } | ||
58 | return (int)c1 - (int)c2; | 54 | return (int)c1 - (int)c2; |
59 | } | 55 | } |
60 | EXPORT_SYMBOL(strnicmp); | 56 | EXPORT_SYMBOL(strnicmp); |
@@ -338,20 +334,34 @@ EXPORT_SYMBOL(strnchr); | |||
338 | #endif | 334 | #endif |
339 | 335 | ||
340 | /** | 336 | /** |
341 | * strstrip - Removes leading and trailing whitespace from @s. | 337 | * skip_spaces - Removes leading whitespace from @str. |
338 | * @str: The string to be stripped. | ||
339 | * | ||
340 | * Returns a pointer to the first non-whitespace character in @str. | ||
341 | */ | ||
342 | char *skip_spaces(const char *str) | ||
343 | { | ||
344 | while (isspace(*str)) | ||
345 | ++str; | ||
346 | return (char *)str; | ||
347 | } | ||
348 | EXPORT_SYMBOL(skip_spaces); | ||
349 | |||
350 | /** | ||
351 | * strim - Removes leading and trailing whitespace from @s. | ||
342 | * @s: The string to be stripped. | 352 | * @s: The string to be stripped. |
343 | * | 353 | * |
344 | * Note that the first trailing whitespace is replaced with a %NUL-terminator | 354 | * Note that the first trailing whitespace is replaced with a %NUL-terminator |
345 | * in the given string @s. Returns a pointer to the first non-whitespace | 355 | * in the given string @s. Returns a pointer to the first non-whitespace |
346 | * character in @s. | 356 | * character in @s. |
347 | */ | 357 | */ |
348 | char *strstrip(char *s) | 358 | char *strim(char *s) |
349 | { | 359 | { |
350 | size_t size; | 360 | size_t size; |
351 | char *end; | 361 | char *end; |
352 | 362 | ||
363 | s = skip_spaces(s); | ||
353 | size = strlen(s); | 364 | size = strlen(s); |
354 | |||
355 | if (!size) | 365 | if (!size) |
356 | return s; | 366 | return s; |
357 | 367 | ||
@@ -360,12 +370,9 @@ char *strstrip(char *s) | |||
360 | end--; | 370 | end--; |
361 | *(end + 1) = '\0'; | 371 | *(end + 1) = '\0'; |
362 | 372 | ||
363 | while (*s && isspace(*s)) | ||
364 | s++; | ||
365 | |||
366 | return s; | 373 | return s; |
367 | } | 374 | } |
368 | EXPORT_SYMBOL(strstrip); | 375 | EXPORT_SYMBOL(strim); |
369 | 376 | ||
370 | #ifndef __HAVE_ARCH_STRLEN | 377 | #ifndef __HAVE_ARCH_STRLEN |
371 | /** | 378 | /** |
@@ -656,7 +663,7 @@ EXPORT_SYMBOL(memscan); | |||
656 | */ | 663 | */ |
657 | char *strstr(const char *s1, const char *s2) | 664 | char *strstr(const char *s1, const char *s2) |
658 | { | 665 | { |
659 | int l1, l2; | 666 | size_t l1, l2; |
660 | 667 | ||
661 | l2 = strlen(s2); | 668 | l2 = strlen(s2); |
662 | if (!l2) | 669 | if (!l2) |
@@ -673,6 +680,31 @@ char *strstr(const char *s1, const char *s2) | |||
673 | EXPORT_SYMBOL(strstr); | 680 | EXPORT_SYMBOL(strstr); |
674 | #endif | 681 | #endif |
675 | 682 | ||
683 | #ifndef __HAVE_ARCH_STRNSTR | ||
684 | /** | ||
685 | * strnstr - Find the first substring in a length-limited string | ||
686 | * @s1: The string to be searched | ||
687 | * @s2: The string to search for | ||
688 | * @len: the maximum number of characters to search | ||
689 | */ | ||
690 | char *strnstr(const char *s1, const char *s2, size_t len) | ||
691 | { | ||
692 | size_t l2; | ||
693 | |||
694 | l2 = strlen(s2); | ||
695 | if (!l2) | ||
696 | return (char *)s1; | ||
697 | while (len >= l2) { | ||
698 | len--; | ||
699 | if (!memcmp(s1, s2, l2)) | ||
700 | return (char *)s1; | ||
701 | s1++; | ||
702 | } | ||
703 | return NULL; | ||
704 | } | ||
705 | EXPORT_SYMBOL(strnstr); | ||
706 | #endif | ||
707 | |||
676 | #ifndef __HAVE_ARCH_MEMCHR | 708 | #ifndef __HAVE_ARCH_MEMCHR |
677 | /** | 709 | /** |
678 | * memchr - Find a character in an area of memory. | 710 | * memchr - Find a character in an area of memory. |