diff options
Diffstat (limited to 'lib/string.c')
-rw-r--r-- | lib/string.c | 72 |
1 files changed, 58 insertions, 14 deletions
diff --git a/lib/string.c b/lib/string.c index b19b87af65a3..a1cdcfcc42d0 100644 --- a/lib/string.c +++ b/lib/string.c | |||
@@ -246,13 +246,17 @@ EXPORT_SYMBOL(strlcat); | |||
246 | #undef strcmp | 246 | #undef strcmp |
247 | int strcmp(const char *cs, const char *ct) | 247 | int strcmp(const char *cs, const char *ct) |
248 | { | 248 | { |
249 | signed char __res; | 249 | unsigned char c1, c2; |
250 | 250 | ||
251 | while (1) { | 251 | while (1) { |
252 | if ((__res = *cs - *ct++) != 0 || !*cs++) | 252 | c1 = *cs++; |
253 | c2 = *ct++; | ||
254 | if (c1 != c2) | ||
255 | return c1 < c2 ? -1 : 1; | ||
256 | if (!c1) | ||
253 | break; | 257 | break; |
254 | } | 258 | } |
255 | return __res; | 259 | return 0; |
256 | } | 260 | } |
257 | EXPORT_SYMBOL(strcmp); | 261 | EXPORT_SYMBOL(strcmp); |
258 | #endif | 262 | #endif |
@@ -266,14 +270,18 @@ EXPORT_SYMBOL(strcmp); | |||
266 | */ | 270 | */ |
267 | int strncmp(const char *cs, const char *ct, size_t count) | 271 | int strncmp(const char *cs, const char *ct, size_t count) |
268 | { | 272 | { |
269 | signed char __res = 0; | 273 | unsigned char c1, c2; |
270 | 274 | ||
271 | while (count) { | 275 | while (count) { |
272 | if ((__res = *cs - *ct++) != 0 || !*cs++) | 276 | c1 = *cs++; |
277 | c2 = *ct++; | ||
278 | if (c1 != c2) | ||
279 | return c1 < c2 ? -1 : 1; | ||
280 | if (!c1) | ||
273 | break; | 281 | break; |
274 | count--; | 282 | count--; |
275 | } | 283 | } |
276 | return __res; | 284 | return 0; |
277 | } | 285 | } |
278 | EXPORT_SYMBOL(strncmp); | 286 | EXPORT_SYMBOL(strncmp); |
279 | #endif | 287 | #endif |
@@ -330,20 +338,34 @@ EXPORT_SYMBOL(strnchr); | |||
330 | #endif | 338 | #endif |
331 | 339 | ||
332 | /** | 340 | /** |
333 | * strstrip - Removes leading and trailing whitespace from @s. | 341 | * skip_spaces - Removes leading whitespace from @str. |
342 | * @str: The string to be stripped. | ||
343 | * | ||
344 | * Returns a pointer to the first non-whitespace character in @str. | ||
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 | /** | ||
355 | * strim - Removes leading and trailing whitespace from @s. | ||
334 | * @s: The string to be stripped. | 356 | * @s: The string to be stripped. |
335 | * | 357 | * |
336 | * Note that the first trailing whitespace is replaced with a %NUL-terminator | 358 | * Note that the first trailing whitespace is replaced with a %NUL-terminator |
337 | * in the given string @s. Returns a pointer to the first non-whitespace | 359 | * in the given string @s. Returns a pointer to the first non-whitespace |
338 | * character in @s. | 360 | * character in @s. |
339 | */ | 361 | */ |
340 | char *strstrip(char *s) | 362 | char *strim(char *s) |
341 | { | 363 | { |
342 | size_t size; | 364 | size_t size; |
343 | char *end; | 365 | char *end; |
344 | 366 | ||
367 | s = skip_spaces(s); | ||
345 | size = strlen(s); | 368 | size = strlen(s); |
346 | |||
347 | if (!size) | 369 | if (!size) |
348 | return s; | 370 | return s; |
349 | 371 | ||
@@ -352,12 +374,9 @@ char *strstrip(char *s) | |||
352 | end--; | 374 | end--; |
353 | *(end + 1) = '\0'; | 375 | *(end + 1) = '\0'; |
354 | 376 | ||
355 | while (*s && isspace(*s)) | ||
356 | s++; | ||
357 | |||
358 | return s; | 377 | return s; |
359 | } | 378 | } |
360 | EXPORT_SYMBOL(strstrip); | 379 | EXPORT_SYMBOL(strim); |
361 | 380 | ||
362 | #ifndef __HAVE_ARCH_STRLEN | 381 | #ifndef __HAVE_ARCH_STRLEN |
363 | /** | 382 | /** |
@@ -648,7 +667,7 @@ EXPORT_SYMBOL(memscan); | |||
648 | */ | 667 | */ |
649 | char *strstr(const char *s1, const char *s2) | 668 | char *strstr(const char *s1, const char *s2) |
650 | { | 669 | { |
651 | int l1, l2; | 670 | size_t l1, l2; |
652 | 671 | ||
653 | l2 = strlen(s2); | 672 | l2 = strlen(s2); |
654 | if (!l2) | 673 | if (!l2) |
@@ -665,6 +684,31 @@ char *strstr(const char *s1, const char *s2) | |||
665 | EXPORT_SYMBOL(strstr); | 684 | EXPORT_SYMBOL(strstr); |
666 | #endif | 685 | #endif |
667 | 686 | ||
687 | #ifndef __HAVE_ARCH_STRNSTR | ||
688 | /** | ||
689 | * strnstr - Find the first substring in a length-limited string | ||
690 | * @s1: The string to be searched | ||
691 | * @s2: The string to search for | ||
692 | * @len: the maximum number of characters to search | ||
693 | */ | ||
694 | char *strnstr(const char *s1, const char *s2, size_t len) | ||
695 | { | ||
696 | size_t l1 = len, l2; | ||
697 | |||
698 | l2 = strlen(s2); | ||
699 | if (!l2) | ||
700 | return (char *)s1; | ||
701 | while (l1 >= l2) { | ||
702 | l1--; | ||
703 | if (!memcmp(s1, s2, l2)) | ||
704 | return (char *)s1; | ||
705 | s1++; | ||
706 | } | ||
707 | return NULL; | ||
708 | } | ||
709 | EXPORT_SYMBOL(strnstr); | ||
710 | #endif | ||
711 | |||
668 | #ifndef __HAVE_ARCH_MEMCHR | 712 | #ifndef __HAVE_ARCH_MEMCHR |
669 | /** | 713 | /** |
670 | * memchr - Find a character in an area of memory. | 714 | * memchr - Find a character in an area of memory. |