Linux 4.9.251
[linux/fpc-iii.git] / lib / string.c
blobd099762a9bd602988f67c07226c0919005ec5949
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 #include <asm/byteorder.h>
31 #include <asm/word-at-a-time.h>
32 #include <asm/page.h>
34 #ifndef __HAVE_ARCH_STRNCASECMP
35 /**
36 * strncasecmp - Case insensitive, length-limited string comparison
37 * @s1: One string
38 * @s2: The other string
39 * @len: the maximum number of characters to compare
41 int strncasecmp(const char *s1, const char *s2, size_t len)
43 /* Yes, Virginia, it had better be unsigned */
44 unsigned char c1, c2;
46 if (!len)
47 return 0;
49 do {
50 c1 = *s1++;
51 c2 = *s2++;
52 if (!c1 || !c2)
53 break;
54 if (c1 == c2)
55 continue;
56 c1 = tolower(c1);
57 c2 = tolower(c2);
58 if (c1 != c2)
59 break;
60 } while (--len);
61 return (int)c1 - (int)c2;
63 EXPORT_SYMBOL(strncasecmp);
64 #endif
66 #ifndef __HAVE_ARCH_STRCASECMP
67 int strcasecmp(const char *s1, const char *s2)
69 int c1, c2;
71 do {
72 c1 = tolower(*s1++);
73 c2 = tolower(*s2++);
74 } while (c1 == c2 && c1 != 0);
75 return c1 - c2;
77 EXPORT_SYMBOL(strcasecmp);
78 #endif
80 #ifndef __HAVE_ARCH_STRCPY
81 /**
82 * strcpy - Copy a %NUL terminated string
83 * @dest: Where to copy the string to
84 * @src: Where to copy the string from
86 #undef strcpy
87 char *strcpy(char *dest, const char *src)
89 char *tmp = dest;
91 while ((*dest++ = *src++) != '\0')
92 /* nothing */;
93 return tmp;
95 EXPORT_SYMBOL(strcpy);
96 #endif
98 #ifndef __HAVE_ARCH_STRNCPY
99 /**
100 * strncpy - Copy a length-limited, C-string
101 * @dest: Where to copy the string to
102 * @src: Where to copy the string from
103 * @count: The maximum number of bytes to copy
105 * The result is not %NUL-terminated if the source exceeds
106 * @count bytes.
108 * In the case where the length of @src is less than that of
109 * count, the remainder of @dest will be padded with %NUL.
112 char *strncpy(char *dest, const char *src, size_t count)
114 char *tmp = dest;
116 while (count) {
117 if ((*tmp = *src) != 0)
118 src++;
119 tmp++;
120 count--;
122 return dest;
124 EXPORT_SYMBOL(strncpy);
125 #endif
127 #ifndef __HAVE_ARCH_STRLCPY
129 * strlcpy - Copy a C-string into a sized buffer
130 * @dest: Where to copy the string to
131 * @src: Where to copy the string from
132 * @size: size of destination buffer
134 * Compatible with *BSD: the result is always a valid
135 * NUL-terminated string that fits in the buffer (unless,
136 * of course, the buffer size is zero). It does not pad
137 * out the result like strncpy() does.
139 size_t strlcpy(char *dest, const char *src, size_t size)
141 size_t ret = strlen(src);
143 if (size) {
144 size_t len = (ret >= size) ? size - 1 : ret;
145 memcpy(dest, src, len);
146 dest[len] = '\0';
148 return ret;
150 EXPORT_SYMBOL(strlcpy);
151 #endif
153 #ifndef __HAVE_ARCH_STRSCPY
155 * strscpy - Copy a C-string into a sized buffer
156 * @dest: Where to copy the string to
157 * @src: Where to copy the string from
158 * @count: Size of destination buffer
160 * Copy the string, or as much of it as fits, into the dest buffer.
161 * The routine returns the number of characters copied (not including
162 * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
163 * The behavior is undefined if the string buffers overlap.
164 * The destination buffer is always NUL terminated, unless it's zero-sized.
166 * Preferred to strlcpy() since the API doesn't require reading memory
167 * from the src string beyond the specified "count" bytes, and since
168 * the return value is easier to error-check than strlcpy()'s.
169 * In addition, the implementation is robust to the string changing out
170 * from underneath it, unlike the current strlcpy() implementation.
172 * Preferred to strncpy() since it always returns a valid string, and
173 * doesn't unnecessarily force the tail of the destination buffer to be
174 * zeroed. If the zeroing is desired, it's likely cleaner to use strscpy()
175 * with an overflow test, then just memset() the tail of the dest buffer.
177 ssize_t strscpy(char *dest, const char *src, size_t count)
179 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
180 size_t max = count;
181 long res = 0;
183 if (count == 0)
184 return -E2BIG;
186 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
188 * If src is unaligned, don't cross a page boundary,
189 * since we don't know if the next page is mapped.
191 if ((long)src & (sizeof(long) - 1)) {
192 size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
193 if (limit < max)
194 max = limit;
196 #else
197 /* If src or dest is unaligned, don't do word-at-a-time. */
198 if (((long) dest | (long) src) & (sizeof(long) - 1))
199 max = 0;
200 #endif
202 while (max >= sizeof(unsigned long)) {
203 unsigned long c, data;
205 c = read_word_at_a_time(src+res);
206 if (has_zero(c, &data, &constants)) {
207 data = prep_zero_mask(c, data, &constants);
208 data = create_zero_mask(data);
209 *(unsigned long *)(dest+res) = c & zero_bytemask(data);
210 return res + find_zero(data);
212 *(unsigned long *)(dest+res) = c;
213 res += sizeof(unsigned long);
214 count -= sizeof(unsigned long);
215 max -= sizeof(unsigned long);
218 while (count) {
219 char c;
221 c = src[res];
222 dest[res] = c;
223 if (!c)
224 return res;
225 res++;
226 count--;
229 /* Hit buffer length without finding a NUL; force NUL-termination. */
230 if (res)
231 dest[res-1] = '\0';
233 return -E2BIG;
235 EXPORT_SYMBOL(strscpy);
236 #endif
239 * stpcpy - copy a string from src to dest returning a pointer to the new end
240 * of dest, including src's %NUL-terminator. May overrun dest.
241 * @dest: pointer to end of string being copied into. Must be large enough
242 * to receive copy.
243 * @src: pointer to the beginning of string being copied from. Must not overlap
244 * dest.
246 * stpcpy differs from strcpy in a key way: the return value is a pointer
247 * to the new %NUL-terminating character in @dest. (For strcpy, the return
248 * value is a pointer to the start of @dest). This interface is considered
249 * unsafe as it doesn't perform bounds checking of the inputs. As such it's
250 * not recommended for usage. Instead, its definition is provided in case
251 * the compiler lowers other libcalls to stpcpy.
253 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
254 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
256 while ((*dest++ = *src++) != '\0')
257 /* nothing */;
258 return --dest;
260 EXPORT_SYMBOL(stpcpy);
262 #ifndef __HAVE_ARCH_STRCAT
264 * strcat - Append one %NUL-terminated string to another
265 * @dest: The string to be appended to
266 * @src: The string to append to it
268 #undef strcat
269 char *strcat(char *dest, const char *src)
271 char *tmp = dest;
273 while (*dest)
274 dest++;
275 while ((*dest++ = *src++) != '\0')
277 return tmp;
279 EXPORT_SYMBOL(strcat);
280 #endif
282 #ifndef __HAVE_ARCH_STRNCAT
284 * strncat - Append a length-limited, C-string to another
285 * @dest: The string to be appended to
286 * @src: The string to append to it
287 * @count: The maximum numbers of bytes to copy
289 * Note that in contrast to strncpy(), strncat() ensures the result is
290 * terminated.
292 char *strncat(char *dest, const char *src, size_t count)
294 char *tmp = dest;
296 if (count) {
297 while (*dest)
298 dest++;
299 while ((*dest++ = *src++) != 0) {
300 if (--count == 0) {
301 *dest = '\0';
302 break;
306 return tmp;
308 EXPORT_SYMBOL(strncat);
309 #endif
311 #ifndef __HAVE_ARCH_STRLCAT
313 * strlcat - Append a length-limited, C-string to another
314 * @dest: The string to be appended to
315 * @src: The string to append to it
316 * @count: The size of the destination buffer.
318 size_t strlcat(char *dest, const char *src, size_t count)
320 size_t dsize = strlen(dest);
321 size_t len = strlen(src);
322 size_t res = dsize + len;
324 /* This would be a bug */
325 BUG_ON(dsize >= count);
327 dest += dsize;
328 count -= dsize;
329 if (len >= count)
330 len = count-1;
331 memcpy(dest, src, len);
332 dest[len] = 0;
333 return res;
335 EXPORT_SYMBOL(strlcat);
336 #endif
338 #ifndef __HAVE_ARCH_STRCMP
340 * strcmp - Compare two strings
341 * @cs: One string
342 * @ct: Another string
344 #undef strcmp
345 int strcmp(const char *cs, const char *ct)
347 unsigned char c1, c2;
349 while (1) {
350 c1 = *cs++;
351 c2 = *ct++;
352 if (c1 != c2)
353 return c1 < c2 ? -1 : 1;
354 if (!c1)
355 break;
357 return 0;
359 EXPORT_SYMBOL(strcmp);
360 #endif
362 #ifndef __HAVE_ARCH_STRNCMP
364 * strncmp - Compare two length-limited strings
365 * @cs: One string
366 * @ct: Another string
367 * @count: The maximum number of bytes to compare
369 int strncmp(const char *cs, const char *ct, size_t count)
371 unsigned char c1, c2;
373 while (count) {
374 c1 = *cs++;
375 c2 = *ct++;
376 if (c1 != c2)
377 return c1 < c2 ? -1 : 1;
378 if (!c1)
379 break;
380 count--;
382 return 0;
384 EXPORT_SYMBOL(strncmp);
385 #endif
387 #ifndef __HAVE_ARCH_STRCHR
389 * strchr - Find the first occurrence of a character in a string
390 * @s: The string to be searched
391 * @c: The character to search for
393 char *strchr(const char *s, int c)
395 for (; *s != (char)c; ++s)
396 if (*s == '\0')
397 return NULL;
398 return (char *)s;
400 EXPORT_SYMBOL(strchr);
401 #endif
403 #ifndef __HAVE_ARCH_STRCHRNUL
405 * strchrnul - Find and return a character in a string, or end of string
406 * @s: The string to be searched
407 * @c: The character to search for
409 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
410 * return a pointer to the null byte at the end of s.
412 char *strchrnul(const char *s, int c)
414 while (*s && *s != (char)c)
415 s++;
416 return (char *)s;
418 EXPORT_SYMBOL(strchrnul);
419 #endif
421 #ifndef __HAVE_ARCH_STRRCHR
423 * strrchr - Find the last occurrence of a character in a string
424 * @s: The string to be searched
425 * @c: The character to search for
427 char *strrchr(const char *s, int c)
429 const char *last = NULL;
430 do {
431 if (*s == (char)c)
432 last = s;
433 } while (*s++);
434 return (char *)last;
436 EXPORT_SYMBOL(strrchr);
437 #endif
439 #ifndef __HAVE_ARCH_STRNCHR
441 * strnchr - Find a character in a length limited string
442 * @s: The string to be searched
443 * @count: The number of characters to be searched
444 * @c: The character to search for
446 char *strnchr(const char *s, size_t count, int c)
448 for (; count-- && *s != '\0'; ++s)
449 if (*s == (char)c)
450 return (char *)s;
451 return NULL;
453 EXPORT_SYMBOL(strnchr);
454 #endif
457 * skip_spaces - Removes leading whitespace from @str.
458 * @str: The string to be stripped.
460 * Returns a pointer to the first non-whitespace character in @str.
462 char *skip_spaces(const char *str)
464 while (isspace(*str))
465 ++str;
466 return (char *)str;
468 EXPORT_SYMBOL(skip_spaces);
471 * strim - Removes leading and trailing whitespace from @s.
472 * @s: The string to be stripped.
474 * Note that the first trailing whitespace is replaced with a %NUL-terminator
475 * in the given string @s. Returns a pointer to the first non-whitespace
476 * character in @s.
478 char *strim(char *s)
480 size_t size;
481 char *end;
483 size = strlen(s);
484 if (!size)
485 return s;
487 end = s + size - 1;
488 while (end >= s && isspace(*end))
489 end--;
490 *(end + 1) = '\0';
492 return skip_spaces(s);
494 EXPORT_SYMBOL(strim);
496 #ifndef __HAVE_ARCH_STRLEN
498 * strlen - Find the length of a string
499 * @s: The string to be sized
501 size_t strlen(const char *s)
503 const char *sc;
505 for (sc = s; *sc != '\0'; ++sc)
506 /* nothing */;
507 return sc - s;
509 EXPORT_SYMBOL(strlen);
510 #endif
512 #ifndef __HAVE_ARCH_STRNLEN
514 * strnlen - Find the length of a length-limited string
515 * @s: The string to be sized
516 * @count: The maximum number of bytes to search
518 size_t strnlen(const char *s, size_t count)
520 const char *sc;
522 for (sc = s; count-- && *sc != '\0'; ++sc)
523 /* nothing */;
524 return sc - s;
526 EXPORT_SYMBOL(strnlen);
527 #endif
529 #ifndef __HAVE_ARCH_STRSPN
531 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
532 * @s: The string to be searched
533 * @accept: The string to search for
535 size_t strspn(const char *s, const char *accept)
537 const char *p;
538 const char *a;
539 size_t count = 0;
541 for (p = s; *p != '\0'; ++p) {
542 for (a = accept; *a != '\0'; ++a) {
543 if (*p == *a)
544 break;
546 if (*a == '\0')
547 return count;
548 ++count;
550 return count;
553 EXPORT_SYMBOL(strspn);
554 #endif
556 #ifndef __HAVE_ARCH_STRCSPN
558 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
559 * @s: The string to be searched
560 * @reject: The string to avoid
562 size_t strcspn(const char *s, const char *reject)
564 const char *p;
565 const char *r;
566 size_t count = 0;
568 for (p = s; *p != '\0'; ++p) {
569 for (r = reject; *r != '\0'; ++r) {
570 if (*p == *r)
571 return count;
573 ++count;
575 return count;
577 EXPORT_SYMBOL(strcspn);
578 #endif
580 #ifndef __HAVE_ARCH_STRPBRK
582 * strpbrk - Find the first occurrence of a set of characters
583 * @cs: The string to be searched
584 * @ct: The characters to search for
586 char *strpbrk(const char *cs, const char *ct)
588 const char *sc1, *sc2;
590 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
591 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
592 if (*sc1 == *sc2)
593 return (char *)sc1;
596 return NULL;
598 EXPORT_SYMBOL(strpbrk);
599 #endif
601 #ifndef __HAVE_ARCH_STRSEP
603 * strsep - Split a string into tokens
604 * @s: The string to be searched
605 * @ct: The characters to search for
607 * strsep() updates @s to point after the token, ready for the next call.
609 * It returns empty tokens, too, behaving exactly like the libc function
610 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
611 * Same semantics, slimmer shape. ;)
613 char *strsep(char **s, const char *ct)
615 char *sbegin = *s;
616 char *end;
618 if (sbegin == NULL)
619 return NULL;
621 end = strpbrk(sbegin, ct);
622 if (end)
623 *end++ = '\0';
624 *s = end;
625 return sbegin;
627 EXPORT_SYMBOL(strsep);
628 #endif
631 * sysfs_streq - return true if strings are equal, modulo trailing newline
632 * @s1: one string
633 * @s2: another string
635 * This routine returns true iff two strings are equal, treating both
636 * NUL and newline-then-NUL as equivalent string terminations. It's
637 * geared for use with sysfs input strings, which generally terminate
638 * with newlines but are compared against values without newlines.
640 bool sysfs_streq(const char *s1, const char *s2)
642 while (*s1 && *s1 == *s2) {
643 s1++;
644 s2++;
647 if (*s1 == *s2)
648 return true;
649 if (!*s1 && *s2 == '\n' && !s2[1])
650 return true;
651 if (*s1 == '\n' && !s1[1] && !*s2)
652 return true;
653 return false;
655 EXPORT_SYMBOL(sysfs_streq);
658 * match_string - matches given string in an array
659 * @array: array of strings
660 * @n: number of strings in the array or -1 for NULL terminated arrays
661 * @string: string to match with
663 * Return:
664 * index of a @string in the @array if matches, or %-EINVAL otherwise.
666 int match_string(const char * const *array, size_t n, const char *string)
668 int index;
669 const char *item;
671 for (index = 0; index < n; index++) {
672 item = array[index];
673 if (!item)
674 break;
675 if (!strcmp(item, string))
676 return index;
679 return -EINVAL;
681 EXPORT_SYMBOL(match_string);
683 #ifndef __HAVE_ARCH_MEMSET
685 * memset - Fill a region of memory with the given value
686 * @s: Pointer to the start of the area.
687 * @c: The byte to fill the area with
688 * @count: The size of the area.
690 * Do not use memset() to access IO space, use memset_io() instead.
692 void *memset(void *s, int c, size_t count)
694 char *xs = s;
696 while (count--)
697 *xs++ = c;
698 return s;
700 EXPORT_SYMBOL(memset);
701 #endif
704 * memzero_explicit - Fill a region of memory (e.g. sensitive
705 * keying data) with 0s.
706 * @s: Pointer to the start of the area.
707 * @count: The size of the area.
709 * Note: usually using memset() is just fine (!), but in cases
710 * where clearing out _local_ data at the end of a scope is
711 * necessary, memzero_explicit() should be used instead in
712 * order to prevent the compiler from optimising away zeroing.
714 * memzero_explicit() doesn't need an arch-specific version as
715 * it just invokes the one of memset() implicitly.
717 void memzero_explicit(void *s, size_t count)
719 memset(s, 0, count);
720 barrier_data(s);
722 EXPORT_SYMBOL(memzero_explicit);
724 #ifndef __HAVE_ARCH_MEMCPY
726 * memcpy - Copy one area of memory to another
727 * @dest: Where to copy to
728 * @src: Where to copy from
729 * @count: The size of the area.
731 * You should not use this function to access IO space, use memcpy_toio()
732 * or memcpy_fromio() instead.
734 void *memcpy(void *dest, const void *src, size_t count)
736 char *tmp = dest;
737 const char *s = src;
739 while (count--)
740 *tmp++ = *s++;
741 return dest;
743 EXPORT_SYMBOL(memcpy);
744 #endif
746 #ifndef __HAVE_ARCH_MEMMOVE
748 * memmove - Copy one area of memory to another
749 * @dest: Where to copy to
750 * @src: Where to copy from
751 * @count: The size of the area.
753 * Unlike memcpy(), memmove() copes with overlapping areas.
755 void *memmove(void *dest, const void *src, size_t count)
757 char *tmp;
758 const char *s;
760 if (dest <= src) {
761 tmp = dest;
762 s = src;
763 while (count--)
764 *tmp++ = *s++;
765 } else {
766 tmp = dest;
767 tmp += count;
768 s = src;
769 s += count;
770 while (count--)
771 *--tmp = *--s;
773 return dest;
775 EXPORT_SYMBOL(memmove);
776 #endif
778 #ifndef __HAVE_ARCH_MEMCMP
780 * memcmp - Compare two areas of memory
781 * @cs: One area of memory
782 * @ct: Another area of memory
783 * @count: The size of the area.
785 #undef memcmp
786 __visible int memcmp(const void *cs, const void *ct, size_t count)
788 const unsigned char *su1, *su2;
789 int res = 0;
791 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
792 if ((res = *su1 - *su2) != 0)
793 break;
794 return res;
796 EXPORT_SYMBOL(memcmp);
797 #endif
799 #ifndef __HAVE_ARCH_BCMP
801 * bcmp - returns 0 if and only if the buffers have identical contents.
802 * @a: pointer to first buffer.
803 * @b: pointer to second buffer.
804 * @len: size of buffers.
806 * The sign or magnitude of a non-zero return value has no particular
807 * meaning, and architectures may implement their own more efficient bcmp(). So
808 * while this particular implementation is a simple (tail) call to memcmp, do
809 * not rely on anything but whether the return value is zero or non-zero.
811 #undef bcmp
812 int bcmp(const void *a, const void *b, size_t len)
814 return memcmp(a, b, len);
816 EXPORT_SYMBOL(bcmp);
817 #endif
819 #ifndef __HAVE_ARCH_MEMSCAN
821 * memscan - Find a character in an area of memory.
822 * @addr: The memory area
823 * @c: The byte to search for
824 * @size: The size of the area.
826 * returns the address of the first occurrence of @c, or 1 byte past
827 * the area if @c is not found
829 void *memscan(void *addr, int c, size_t size)
831 unsigned char *p = addr;
833 while (size) {
834 if (*p == c)
835 return (void *)p;
836 p++;
837 size--;
839 return (void *)p;
841 EXPORT_SYMBOL(memscan);
842 #endif
844 #ifndef __HAVE_ARCH_STRSTR
846 * strstr - Find the first substring in a %NUL terminated string
847 * @s1: The string to be searched
848 * @s2: The string to search for
850 char *strstr(const char *s1, const char *s2)
852 size_t l1, l2;
854 l2 = strlen(s2);
855 if (!l2)
856 return (char *)s1;
857 l1 = strlen(s1);
858 while (l1 >= l2) {
859 l1--;
860 if (!memcmp(s1, s2, l2))
861 return (char *)s1;
862 s1++;
864 return NULL;
866 EXPORT_SYMBOL(strstr);
867 #endif
869 #ifndef __HAVE_ARCH_STRNSTR
871 * strnstr - Find the first substring in a length-limited string
872 * @s1: The string to be searched
873 * @s2: The string to search for
874 * @len: the maximum number of characters to search
876 char *strnstr(const char *s1, const char *s2, size_t len)
878 size_t l2;
880 l2 = strlen(s2);
881 if (!l2)
882 return (char *)s1;
883 while (len >= l2) {
884 len--;
885 if (!memcmp(s1, s2, l2))
886 return (char *)s1;
887 s1++;
889 return NULL;
891 EXPORT_SYMBOL(strnstr);
892 #endif
894 #ifndef __HAVE_ARCH_MEMCHR
896 * memchr - Find a character in an area of memory.
897 * @s: The memory area
898 * @c: The byte to search for
899 * @n: The size of the area.
901 * returns the address of the first occurrence of @c, or %NULL
902 * if @c is not found
904 void *memchr(const void *s, int c, size_t n)
906 const unsigned char *p = s;
907 while (n-- != 0) {
908 if ((unsigned char)c == *p++) {
909 return (void *)(p - 1);
912 return NULL;
914 EXPORT_SYMBOL(memchr);
915 #endif
917 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
919 while (bytes) {
920 if (*start != value)
921 return (void *)start;
922 start++;
923 bytes--;
925 return NULL;
929 * memchr_inv - Find an unmatching character in an area of memory.
930 * @start: The memory area
931 * @c: Find a character other than c
932 * @bytes: The size of the area.
934 * returns the address of the first character other than @c, or %NULL
935 * if the whole buffer contains just @c.
937 void *memchr_inv(const void *start, int c, size_t bytes)
939 u8 value = c;
940 u64 value64;
941 unsigned int words, prefix;
943 if (bytes <= 16)
944 return check_bytes8(start, value, bytes);
946 value64 = value;
947 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
948 value64 *= 0x0101010101010101ULL;
949 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
950 value64 *= 0x01010101;
951 value64 |= value64 << 32;
952 #else
953 value64 |= value64 << 8;
954 value64 |= value64 << 16;
955 value64 |= value64 << 32;
956 #endif
958 prefix = (unsigned long)start % 8;
959 if (prefix) {
960 u8 *r;
962 prefix = 8 - prefix;
963 r = check_bytes8(start, value, prefix);
964 if (r)
965 return r;
966 start += prefix;
967 bytes -= prefix;
970 words = bytes / 8;
972 while (words) {
973 if (*(u64 *)start != value64)
974 return check_bytes8(start, value, 8);
975 start += 8;
976 words--;
979 return check_bytes8(start, value, bytes % 8);
981 EXPORT_SYMBOL(memchr_inv);
984 * strreplace - Replace all occurrences of character in string.
985 * @s: The string to operate on.
986 * @old: The character being replaced.
987 * @new: The character @old is replaced with.
989 * Returns pointer to the nul byte at the end of @s.
991 char *strreplace(char *s, char old, char new)
993 for (; *s; ++s)
994 if (*s == old)
995 *s = new;
996 return s;
998 EXPORT_SYMBOL(strreplace);