aboutsummaryrefslogtreecommitdiffstats
path: root/lib/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/string.c')
-rw-r--r--lib/string.c106
1 files changed, 73 insertions, 33 deletions
diff --git a/lib/string.c b/lib/string.c
index b19b87af65a3..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}
60EXPORT_SYMBOL(strnicmp); 56EXPORT_SYMBOL(strnicmp);
@@ -246,13 +242,17 @@ EXPORT_SYMBOL(strlcat);
246#undef strcmp 242#undef strcmp
247int strcmp(const char *cs, const char *ct) 243int strcmp(const char *cs, const char *ct)
248{ 244{
249 signed char __res; 245 unsigned char c1, c2;
250 246
251 while (1) { 247 while (1) {
252 if ((__res = *cs - *ct++) != 0 || !*cs++) 248 c1 = *cs++;
249 c2 = *ct++;
250 if (c1 != c2)
251 return c1 < c2 ? -1 : 1;
252 if (!c1)
253 break; 253 break;
254 } 254 }
255 return __res; 255 return 0;
256} 256}
257EXPORT_SYMBOL(strcmp); 257EXPORT_SYMBOL(strcmp);
258#endif 258#endif
@@ -266,14 +266,18 @@ EXPORT_SYMBOL(strcmp);
266 */ 266 */
267int strncmp(const char *cs, const char *ct, size_t count) 267int strncmp(const char *cs, const char *ct, size_t count)
268{ 268{
269 signed char __res = 0; 269 unsigned char c1, c2;
270 270
271 while (count) { 271 while (count) {
272 if ((__res = *cs - *ct++) != 0 || !*cs++) 272 c1 = *cs++;
273 c2 = *ct++;
274 if (c1 != c2)
275 return c1 < c2 ? -1 : 1;
276 if (!c1)
273 break; 277 break;
274 count--; 278 count--;
275 } 279 }
276 return __res; 280 return 0;
277} 281}
278EXPORT_SYMBOL(strncmp); 282EXPORT_SYMBOL(strncmp);
279#endif 283#endif
@@ -330,20 +334,34 @@ EXPORT_SYMBOL(strnchr);
330#endif 334#endif
331 335
332/** 336/**
333 * 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 */
342char *skip_spaces(const char *str)
343{
344 while (isspace(*str))
345 ++str;
346 return (char *)str;
347}
348EXPORT_SYMBOL(skip_spaces);
349
350/**
351 * strim - Removes leading and trailing whitespace from @s.
334 * @s: The string to be stripped. 352 * @s: The string to be stripped.
335 * 353 *
336 * 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
337 * 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
338 * character in @s. 356 * character in @s.
339 */ 357 */
340char *strstrip(char *s) 358char *strim(char *s)
341{ 359{
342 size_t size; 360 size_t size;
343 char *end; 361 char *end;
344 362
363 s = skip_spaces(s);
345 size = strlen(s); 364 size = strlen(s);
346
347 if (!size) 365 if (!size)
348 return s; 366 return s;
349 367
@@ -352,12 +370,9 @@ char *strstrip(char *s)
352 end--; 370 end--;
353 *(end + 1) = '\0'; 371 *(end + 1) = '\0';
354 372
355 while (*s && isspace(*s))
356 s++;
357
358 return s; 373 return s;
359} 374}
360EXPORT_SYMBOL(strstrip); 375EXPORT_SYMBOL(strim);
361 376
362#ifndef __HAVE_ARCH_STRLEN 377#ifndef __HAVE_ARCH_STRLEN
363/** 378/**
@@ -648,7 +663,7 @@ EXPORT_SYMBOL(memscan);
648 */ 663 */
649char *strstr(const char *s1, const char *s2) 664char *strstr(const char *s1, const char *s2)
650{ 665{
651 int l1, l2; 666 size_t l1, l2;
652 667
653 l2 = strlen(s2); 668 l2 = strlen(s2);
654 if (!l2) 669 if (!l2)
@@ -665,6 +680,31 @@ char *strstr(const char *s1, const char *s2)
665EXPORT_SYMBOL(strstr); 680EXPORT_SYMBOL(strstr);
666#endif 681#endif
667 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 */
690char *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}
705EXPORT_SYMBOL(strnstr);
706#endif
707
668#ifndef __HAVE_ARCH_MEMCHR 708#ifndef __HAVE_ARCH_MEMCHR
669/** 709/**
670 * memchr - Find a character in an area of memory. 710 * memchr - Find a character in an area of memory.