ASoC: max98090: Replace w->codec snd_soc_dapm_to_codec(w->dapm)
[linux-2.6/btrfs-unstable.git] / lib / string.c
blob2fc20aa06f848fcebd8aa30d238942f5ec399690
1 /*
2 * linux/lib/string.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * stupid library routines.. The optimized versions should generally be found
9 * as inline code in <asm-xx/string.h>
11 * These are buggy as well..
13 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
14 * - Added strsep() which will replace strtok() soon (because strsep() is
15 * reentrant and should be faster). Use only strsep() in new code, please.
17 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
18 * Matthew Hawkins <matt@mh.dropbear.id.au>
19 * - Kissed strtok() goodbye
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/ctype.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
27 #include <linux/bug.h>
28 #include <linux/errno.h>
30 #ifndef __HAVE_ARCH_STRNCASECMP
31 /**
32 * strncasecmp - Case insensitive, length-limited string comparison
33 * @s1: One string
34 * @s2: The other string
35 * @len: the maximum number of characters to compare
37 int strncasecmp(const char *s1, const char *s2, size_t len)
39 /* Yes, Virginia, it had better be unsigned */
40 unsigned char c1, c2;
42 if (!len)
43 return 0;
45 do {
46 c1 = *s1++;
47 c2 = *s2++;
48 if (!c1 || !c2)
49 break;
50 if (c1 == c2)
51 continue;
52 c1 = tolower(c1);
53 c2 = tolower(c2);
54 if (c1 != c2)
55 break;
56 } while (--len);
57 return (int)c1 - (int)c2;
59 EXPORT_SYMBOL(strncasecmp);
60 #endif
61 #ifndef __HAVE_ARCH_STRNICMP
62 #undef strnicmp
63 int strnicmp(const char *s1, const char *s2, size_t len)
65 return strncasecmp(s1, s2, len);
67 EXPORT_SYMBOL(strnicmp);
68 #endif
70 #ifndef __HAVE_ARCH_STRCASECMP
71 int strcasecmp(const char *s1, const char *s2)
73 int c1, c2;
75 do {
76 c1 = tolower(*s1++);
77 c2 = tolower(*s2++);
78 } while (c1 == c2 && c1 != 0);
79 return c1 - c2;
81 EXPORT_SYMBOL(strcasecmp);
82 #endif
84 #ifndef __HAVE_ARCH_STRCPY
85 /**
86 * strcpy - Copy a %NUL terminated string
87 * @dest: Where to copy the string to
88 * @src: Where to copy the string from
90 #undef strcpy
91 char *strcpy(char *dest, const char *src)
93 char *tmp = dest;
95 while ((*dest++ = *src++) != '\0')
96 /* nothing */;
97 return tmp;
99 EXPORT_SYMBOL(strcpy);
100 #endif
102 #ifndef __HAVE_ARCH_STRNCPY
104 * strncpy - Copy a length-limited, C-string
105 * @dest: Where to copy the string to
106 * @src: Where to copy the string from
107 * @count: The maximum number of bytes to copy
109 * The result is not %NUL-terminated if the source exceeds
110 * @count bytes.
112 * In the case where the length of @src is less than that of
113 * count, the remainder of @dest will be padded with %NUL.
116 char *strncpy(char *dest, const char *src, size_t count)
118 char *tmp = dest;
120 while (count) {
121 if ((*tmp = *src) != 0)
122 src++;
123 tmp++;
124 count--;
126 return dest;
128 EXPORT_SYMBOL(strncpy);
129 #endif
131 #ifndef __HAVE_ARCH_STRLCPY
133 * strlcpy - Copy a C-string into a sized buffer
134 * @dest: Where to copy the string to
135 * @src: Where to copy the string from
136 * @size: size of destination buffer
138 * Compatible with *BSD: the result is always a valid
139 * NUL-terminated string that fits in the buffer (unless,
140 * of course, the buffer size is zero). It does not pad
141 * out the result like strncpy() does.
143 size_t strlcpy(char *dest, const char *src, size_t size)
145 size_t ret = strlen(src);
147 if (size) {
148 size_t len = (ret >= size) ? size - 1 : ret;
149 memcpy(dest, src, len);
150 dest[len] = '\0';
152 return ret;
154 EXPORT_SYMBOL(strlcpy);
155 #endif
157 #ifndef __HAVE_ARCH_STRCAT
159 * strcat - Append one %NUL-terminated string to another
160 * @dest: The string to be appended to
161 * @src: The string to append to it
163 #undef strcat
164 char *strcat(char *dest, const char *src)
166 char *tmp = dest;
168 while (*dest)
169 dest++;
170 while ((*dest++ = *src++) != '\0')
172 return tmp;
174 EXPORT_SYMBOL(strcat);
175 #endif
177 #ifndef __HAVE_ARCH_STRNCAT
179 * strncat - Append a length-limited, C-string to another
180 * @dest: The string to be appended to
181 * @src: The string to append to it
182 * @count: The maximum numbers of bytes to copy
184 * Note that in contrast to strncpy(), strncat() ensures the result is
185 * terminated.
187 char *strncat(char *dest, const char *src, size_t count)
189 char *tmp = dest;
191 if (count) {
192 while (*dest)
193 dest++;
194 while ((*dest++ = *src++) != 0) {
195 if (--count == 0) {
196 *dest = '\0';
197 break;
201 return tmp;
203 EXPORT_SYMBOL(strncat);
204 #endif
206 #ifndef __HAVE_ARCH_STRLCAT
208 * strlcat - Append a length-limited, C-string to another
209 * @dest: The string to be appended to
210 * @src: The string to append to it
211 * @count: The size of the destination buffer.
213 size_t strlcat(char *dest, const char *src, size_t count)
215 size_t dsize = strlen(dest);
216 size_t len = strlen(src);
217 size_t res = dsize + len;
219 /* This would be a bug */
220 BUG_ON(dsize >= count);
222 dest += dsize;
223 count -= dsize;
224 if (len >= count)
225 len = count-1;
226 memcpy(dest, src, len);
227 dest[len] = 0;
228 return res;
230 EXPORT_SYMBOL(strlcat);
231 #endif
233 #ifndef __HAVE_ARCH_STRCMP
235 * strcmp - Compare two strings
236 * @cs: One string
237 * @ct: Another string
239 #undef strcmp
240 int strcmp(const char *cs, const char *ct)
242 unsigned char c1, c2;
244 while (1) {
245 c1 = *cs++;
246 c2 = *ct++;
247 if (c1 != c2)
248 return c1 < c2 ? -1 : 1;
249 if (!c1)
250 break;
252 return 0;
254 EXPORT_SYMBOL(strcmp);
255 #endif
257 #ifndef __HAVE_ARCH_STRNCMP
259 * strncmp - Compare two length-limited strings
260 * @cs: One string
261 * @ct: Another string
262 * @count: The maximum number of bytes to compare
264 int strncmp(const char *cs, const char *ct, size_t count)
266 unsigned char c1, c2;
268 while (count) {
269 c1 = *cs++;
270 c2 = *ct++;
271 if (c1 != c2)
272 return c1 < c2 ? -1 : 1;
273 if (!c1)
274 break;
275 count--;
277 return 0;
279 EXPORT_SYMBOL(strncmp);
280 #endif
282 #ifndef __HAVE_ARCH_STRCHR
284 * strchr - Find the first occurrence of a character in a string
285 * @s: The string to be searched
286 * @c: The character to search for
288 char *strchr(const char *s, int c)
290 for (; *s != (char)c; ++s)
291 if (*s == '\0')
292 return NULL;
293 return (char *)s;
295 EXPORT_SYMBOL(strchr);
296 #endif
298 #ifndef __HAVE_ARCH_STRCHRNUL
300 * strchrnul - Find and return a character in a string, or end of string
301 * @s: The string to be searched
302 * @c: The character to search for
304 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
305 * return a pointer to the null byte at the end of s.
307 char *strchrnul(const char *s, int c)
309 while (*s && *s != (char)c)
310 s++;
311 return (char *)s;
313 EXPORT_SYMBOL(strchrnul);
314 #endif
316 #ifndef __HAVE_ARCH_STRRCHR
318 * strrchr - Find the last occurrence of a character in a string
319 * @s: The string to be searched
320 * @c: The character to search for
322 char *strrchr(const char *s, int c)
324 const char *p = s + strlen(s);
325 do {
326 if (*p == (char)c)
327 return (char *)p;
328 } while (--p >= s);
329 return NULL;
331 EXPORT_SYMBOL(strrchr);
332 #endif
334 #ifndef __HAVE_ARCH_STRNCHR
336 * strnchr - Find a character in a length limited string
337 * @s: The string to be searched
338 * @count: The number of characters to be searched
339 * @c: The character to search for
341 char *strnchr(const char *s, size_t count, int c)
343 for (; count-- && *s != '\0'; ++s)
344 if (*s == (char)c)
345 return (char *)s;
346 return NULL;
348 EXPORT_SYMBOL(strnchr);
349 #endif
352 * skip_spaces - Removes leading whitespace from @str.
353 * @str: The string to be stripped.
355 * Returns a pointer to the first non-whitespace character in @str.
357 char *skip_spaces(const char *str)
359 while (isspace(*str))
360 ++str;
361 return (char *)str;
363 EXPORT_SYMBOL(skip_spaces);
366 * strim - Removes leading and trailing whitespace from @s.
367 * @s: The string to be stripped.
369 * Note that the first trailing whitespace is replaced with a %NUL-terminator
370 * in the given string @s. Returns a pointer to the first non-whitespace
371 * character in @s.
373 char *strim(char *s)
375 size_t size;
376 char *end;
378 size = strlen(s);
379 if (!size)
380 return s;
382 end = s + size - 1;
383 while (end >= s && isspace(*end))
384 end--;
385 *(end + 1) = '\0';
387 return skip_spaces(s);
389 EXPORT_SYMBOL(strim);
391 #ifndef __HAVE_ARCH_STRLEN
393 * strlen - Find the length of a string
394 * @s: The string to be sized
396 size_t strlen(const char *s)
398 const char *sc;
400 for (sc = s; *sc != '\0'; ++sc)
401 /* nothing */;
402 return sc - s;
404 EXPORT_SYMBOL(strlen);
405 #endif
407 #ifndef __HAVE_ARCH_STRNLEN
409 * strnlen - Find the length of a length-limited string
410 * @s: The string to be sized
411 * @count: The maximum number of bytes to search
413 size_t strnlen(const char *s, size_t count)
415 const char *sc;
417 for (sc = s; count-- && *sc != '\0'; ++sc)
418 /* nothing */;
419 return sc - s;
421 EXPORT_SYMBOL(strnlen);
422 #endif
424 #ifndef __HAVE_ARCH_STRSPN
426 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
427 * @s: The string to be searched
428 * @accept: The string to search for
430 size_t strspn(const char *s, const char *accept)
432 const char *p;
433 const char *a;
434 size_t count = 0;
436 for (p = s; *p != '\0'; ++p) {
437 for (a = accept; *a != '\0'; ++a) {
438 if (*p == *a)
439 break;
441 if (*a == '\0')
442 return count;
443 ++count;
445 return count;
448 EXPORT_SYMBOL(strspn);
449 #endif
451 #ifndef __HAVE_ARCH_STRCSPN
453 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
454 * @s: The string to be searched
455 * @reject: The string to avoid
457 size_t strcspn(const char *s, const char *reject)
459 const char *p;
460 const char *r;
461 size_t count = 0;
463 for (p = s; *p != '\0'; ++p) {
464 for (r = reject; *r != '\0'; ++r) {
465 if (*p == *r)
466 return count;
468 ++count;
470 return count;
472 EXPORT_SYMBOL(strcspn);
473 #endif
475 #ifndef __HAVE_ARCH_STRPBRK
477 * strpbrk - Find the first occurrence of a set of characters
478 * @cs: The string to be searched
479 * @ct: The characters to search for
481 char *strpbrk(const char *cs, const char *ct)
483 const char *sc1, *sc2;
485 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
486 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
487 if (*sc1 == *sc2)
488 return (char *)sc1;
491 return NULL;
493 EXPORT_SYMBOL(strpbrk);
494 #endif
496 #ifndef __HAVE_ARCH_STRSEP
498 * strsep - Split a string into tokens
499 * @s: The string to be searched
500 * @ct: The characters to search for
502 * strsep() updates @s to point after the token, ready for the next call.
504 * It returns empty tokens, too, behaving exactly like the libc function
505 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
506 * Same semantics, slimmer shape. ;)
508 char *strsep(char **s, const char *ct)
510 char *sbegin = *s;
511 char *end;
513 if (sbegin == NULL)
514 return NULL;
516 end = strpbrk(sbegin, ct);
517 if (end)
518 *end++ = '\0';
519 *s = end;
520 return sbegin;
522 EXPORT_SYMBOL(strsep);
523 #endif
526 * sysfs_streq - return true if strings are equal, modulo trailing newline
527 * @s1: one string
528 * @s2: another string
530 * This routine returns true iff two strings are equal, treating both
531 * NUL and newline-then-NUL as equivalent string terminations. It's
532 * geared for use with sysfs input strings, which generally terminate
533 * with newlines but are compared against values without newlines.
535 bool sysfs_streq(const char *s1, const char *s2)
537 while (*s1 && *s1 == *s2) {
538 s1++;
539 s2++;
542 if (*s1 == *s2)
543 return true;
544 if (!*s1 && *s2 == '\n' && !s2[1])
545 return true;
546 if (*s1 == '\n' && !s1[1] && !*s2)
547 return true;
548 return false;
550 EXPORT_SYMBOL(sysfs_streq);
553 * strtobool - convert common user inputs into boolean values
554 * @s: input string
555 * @res: result
557 * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
558 * Otherwise it will return -EINVAL. Value pointed to by res is
559 * updated upon finding a match.
561 int strtobool(const char *s, bool *res)
563 switch (s[0]) {
564 case 'y':
565 case 'Y':
566 case '1':
567 *res = true;
568 break;
569 case 'n':
570 case 'N':
571 case '0':
572 *res = false;
573 break;
574 default:
575 return -EINVAL;
577 return 0;
579 EXPORT_SYMBOL(strtobool);
581 #ifndef __HAVE_ARCH_MEMSET
583 * memset - Fill a region of memory with the given value
584 * @s: Pointer to the start of the area.
585 * @c: The byte to fill the area with
586 * @count: The size of the area.
588 * Do not use memset() to access IO space, use memset_io() instead.
590 void *memset(void *s, int c, size_t count)
592 char *xs = s;
594 while (count--)
595 *xs++ = c;
596 return s;
598 EXPORT_SYMBOL(memset);
599 #endif
601 #ifndef __HAVE_ARCH_MEMCPY
603 * memcpy - Copy one area of memory to another
604 * @dest: Where to copy to
605 * @src: Where to copy from
606 * @count: The size of the area.
608 * You should not use this function to access IO space, use memcpy_toio()
609 * or memcpy_fromio() instead.
611 void *memcpy(void *dest, const void *src, size_t count)
613 char *tmp = dest;
614 const char *s = src;
616 while (count--)
617 *tmp++ = *s++;
618 return dest;
620 EXPORT_SYMBOL(memcpy);
621 #endif
623 #ifndef __HAVE_ARCH_MEMMOVE
625 * memmove - Copy one area of memory to another
626 * @dest: Where to copy to
627 * @src: Where to copy from
628 * @count: The size of the area.
630 * Unlike memcpy(), memmove() copes with overlapping areas.
632 void *memmove(void *dest, const void *src, size_t count)
634 char *tmp;
635 const char *s;
637 if (dest <= src) {
638 tmp = dest;
639 s = src;
640 while (count--)
641 *tmp++ = *s++;
642 } else {
643 tmp = dest;
644 tmp += count;
645 s = src;
646 s += count;
647 while (count--)
648 *--tmp = *--s;
650 return dest;
652 EXPORT_SYMBOL(memmove);
653 #endif
655 #ifndef __HAVE_ARCH_MEMCMP
657 * memcmp - Compare two areas of memory
658 * @cs: One area of memory
659 * @ct: Another area of memory
660 * @count: The size of the area.
662 #undef memcmp
663 __visible int memcmp(const void *cs, const void *ct, size_t count)
665 const unsigned char *su1, *su2;
666 int res = 0;
668 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
669 if ((res = *su1 - *su2) != 0)
670 break;
671 return res;
673 EXPORT_SYMBOL(memcmp);
674 #endif
676 #ifndef __HAVE_ARCH_MEMSCAN
678 * memscan - Find a character in an area of memory.
679 * @addr: The memory area
680 * @c: The byte to search for
681 * @size: The size of the area.
683 * returns the address of the first occurrence of @c, or 1 byte past
684 * the area if @c is not found
686 void *memscan(void *addr, int c, size_t size)
688 unsigned char *p = addr;
690 while (size) {
691 if (*p == c)
692 return (void *)p;
693 p++;
694 size--;
696 return (void *)p;
698 EXPORT_SYMBOL(memscan);
699 #endif
701 #ifndef __HAVE_ARCH_STRSTR
703 * strstr - Find the first substring in a %NUL terminated string
704 * @s1: The string to be searched
705 * @s2: The string to search for
707 char *strstr(const char *s1, const char *s2)
709 size_t l1, l2;
711 l2 = strlen(s2);
712 if (!l2)
713 return (char *)s1;
714 l1 = strlen(s1);
715 while (l1 >= l2) {
716 l1--;
717 if (!memcmp(s1, s2, l2))
718 return (char *)s1;
719 s1++;
721 return NULL;
723 EXPORT_SYMBOL(strstr);
724 #endif
726 #ifndef __HAVE_ARCH_STRNSTR
728 * strnstr - Find the first substring in a length-limited string
729 * @s1: The string to be searched
730 * @s2: The string to search for
731 * @len: the maximum number of characters to search
733 char *strnstr(const char *s1, const char *s2, size_t len)
735 size_t l2;
737 l2 = strlen(s2);
738 if (!l2)
739 return (char *)s1;
740 while (len >= l2) {
741 len--;
742 if (!memcmp(s1, s2, l2))
743 return (char *)s1;
744 s1++;
746 return NULL;
748 EXPORT_SYMBOL(strnstr);
749 #endif
751 #ifndef __HAVE_ARCH_MEMCHR
753 * memchr - Find a character in an area of memory.
754 * @s: The memory area
755 * @c: The byte to search for
756 * @n: The size of the area.
758 * returns the address of the first occurrence of @c, or %NULL
759 * if @c is not found
761 void *memchr(const void *s, int c, size_t n)
763 const unsigned char *p = s;
764 while (n-- != 0) {
765 if ((unsigned char)c == *p++) {
766 return (void *)(p - 1);
769 return NULL;
771 EXPORT_SYMBOL(memchr);
772 #endif
774 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
776 while (bytes) {
777 if (*start != value)
778 return (void *)start;
779 start++;
780 bytes--;
782 return NULL;
786 * memchr_inv - Find an unmatching character in an area of memory.
787 * @start: The memory area
788 * @c: Find a character other than c
789 * @bytes: The size of the area.
791 * returns the address of the first character other than @c, or %NULL
792 * if the whole buffer contains just @c.
794 void *memchr_inv(const void *start, int c, size_t bytes)
796 u8 value = c;
797 u64 value64;
798 unsigned int words, prefix;
800 if (bytes <= 16)
801 return check_bytes8(start, value, bytes);
803 value64 = value;
804 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
805 value64 *= 0x0101010101010101;
806 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
807 value64 *= 0x01010101;
808 value64 |= value64 << 32;
809 #else
810 value64 |= value64 << 8;
811 value64 |= value64 << 16;
812 value64 |= value64 << 32;
813 #endif
815 prefix = (unsigned long)start % 8;
816 if (prefix) {
817 u8 *r;
819 prefix = 8 - prefix;
820 r = check_bytes8(start, value, prefix);
821 if (r)
822 return r;
823 start += prefix;
824 bytes -= prefix;
827 words = bytes / 8;
829 while (words) {
830 if (*(u64 *)start != value64)
831 return check_bytes8(start, value, 8);
832 start += 8;
833 words--;
836 return check_bytes8(start, value, bytes % 8);
838 EXPORT_SYMBOL(memchr_inv);