]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/string.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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.
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
26 * strncasecmp - Case insensitive, length-limited string comparison
28 * @s2: The other string
29 * @len: the maximum number of characters to compare
31 int strncasecmp(const char *s1
, const char *s2
, size_t len
)
33 /* Yes, Virginia, it had better be unsigned */
53 return (int)c1
- (int)c2
;
57 * strcasecmp - Case insensitive string comparison
59 * @s2: The other string
61 int strcasecmp(const char *s1
, const char *s2
)
63 return strncasecmp(s1
, s2
, -1U);
68 #ifndef __HAVE_ARCH_STRCPY
70 * strcpy - Copy a %NUL terminated string
71 * @dest: Where to copy the string to
72 * @src: Where to copy the string from
74 char * strcpy(char * dest
,const char *src
)
78 while ((*dest
++ = *src
++) != '\0')
84 #ifndef __HAVE_ARCH_STRNCPY
86 * strncpy - Copy a length-limited, %NUL-terminated string
87 * @dest: Where to copy the string to
88 * @src: Where to copy the string from
89 * @count: The maximum number of bytes to copy
91 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
92 * However, the result is not %NUL-terminated if the source exceeds
95 char * strncpy(char * dest
,const char *src
,size_t count
)
99 while (count
-- && (*dest
++ = *src
++) != '\0')
106 #ifndef __HAVE_ARCH_STRLCPY
108 * strlcpy - Copy a C-string into a sized buffer
109 * @dest: Where to copy the string to
110 * @src: Where to copy the string from
111 * @size: size of destination buffer
113 * Compatible with *BSD: the result is always a valid
114 * NUL-terminated string that fits in the buffer (unless,
115 * of course, the buffer size is zero). It does not pad
116 * out the result like strncpy() does.
118 size_t strlcpy(char *dest
, const char *src
, size_t size
)
120 size_t ret
= strlen(src
);
123 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
124 memcpy(dest
, src
, len
);
131 #ifndef __HAVE_ARCH_STRCAT
133 * strcat - Append one %NUL-terminated string to another
134 * @dest: The string to be appended to
135 * @src: The string to append to it
137 char * strcat(char * dest
, const char * src
)
143 while ((*dest
++ = *src
++) != '\0')
150 #ifndef __HAVE_ARCH_STRNCAT
152 * strncat - Append a length-limited, %NUL-terminated string to another
153 * @dest: The string to be appended to
154 * @src: The string to append to it
155 * @count: The maximum numbers of bytes to copy
157 * Note that in contrast to strncpy, strncat ensures the result is
160 char * strncat(char *dest
, const char *src
, size_t count
)
167 while ((*dest
++ = *src
++)) {
179 #ifndef __HAVE_ARCH_STRCMP
181 * strcmp - Compare two strings
183 * @ct: Another string
185 int strcmp(const char * cs
,const char * ct
)
187 register signed char __res
;
190 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
198 #ifndef __HAVE_ARCH_STRNCMP
200 * strncmp - Compare two length-limited strings
202 * @ct: Another string
203 * @count: The maximum number of bytes to compare
205 int strncmp(const char * cs
,const char * ct
,size_t count
)
207 register signed char __res
= 0;
210 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
219 #ifndef __HAVE_ARCH_STRCHR
221 * strchr - Find the first occurrence of a character in a string
222 * @s: The string to be searched
223 * @c: The character to search for
225 char * strchr(const char * s
, int c
)
227 for(; *s
!= (char) c
; ++s
)
234 const char *strchrnul(const char *s
, int c
)
236 for (; *s
!= (char)c
; ++s
)
242 #ifndef __HAVE_ARCH_STRRCHR
244 * strrchr - Find the last occurrence of a character in a string
245 * @s: The string to be searched
246 * @c: The character to search for
248 char * strrchr(const char * s
, int c
)
250 const char *p
= s
+ strlen(s
);
259 #ifndef __HAVE_ARCH_STRLEN
261 * strlen - Find the length of a string
262 * @s: The string to be sized
264 size_t strlen(const char * s
)
268 for (sc
= s
; *sc
!= '\0'; ++sc
)
274 #ifndef __HAVE_ARCH_STRNLEN
276 * strnlen - Find the length of a length-limited string
277 * @s: The string to be sized
278 * @count: The maximum number of bytes to search
280 size_t strnlen(const char * s
, size_t count
)
284 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
290 #ifndef __HAVE_ARCH_STRCSPN
292 * strcspn - Calculate the length of the initial substring of @s which does
293 * not contain letters in @reject
294 * @s: The string to be searched
295 * @reject: The string to avoid
297 size_t strcspn(const char *s
, const char *reject
)
303 for (p
= s
; *p
!= '\0'; ++p
) {
304 for (r
= reject
; *r
!= '\0'; ++r
) {
314 #ifndef __HAVE_ARCH_STRDUP
315 char * strdup(const char *s
)
320 ((new = malloc (strlen(s
) + 1)) == NULL
) ) {
329 #ifndef __HAVE_ARCH_STRSPN
331 * strspn - Calculate the length of the initial substring of @s which only
332 * contain letters in @accept
333 * @s: The string to be searched
334 * @accept: The string to search for
336 size_t strspn(const char *s
, const char *accept
)
342 for (p
= s
; *p
!= '\0'; ++p
) {
343 for (a
= accept
; *a
!= '\0'; ++a
) {
356 #ifndef __HAVE_ARCH_STRPBRK
358 * strpbrk - Find the first occurrence of a set of characters
359 * @cs: The string to be searched
360 * @ct: The characters to search for
362 char * strpbrk(const char * cs
,const char * ct
)
364 const char *sc1
,*sc2
;
366 for( sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
367 for( sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
376 #ifndef __HAVE_ARCH_STRTOK
378 * strtok - Split a string into tokens
379 * @s: The string to be searched
380 * @ct: The characters to search for
382 * WARNING: strtok is deprecated, use strsep instead.
384 char * strtok(char * s
,const char * ct
)
388 sbegin
= s
? s
: ___strtok
;
392 sbegin
+= strspn(sbegin
,ct
);
393 if (*sbegin
== '\0') {
397 send
= strpbrk( sbegin
, ct
);
398 if (send
&& *send
!= '\0')
405 #ifndef __HAVE_ARCH_STRSEP
407 * strsep - Split a string into tokens
408 * @s: The string to be searched
409 * @ct: The characters to search for
411 * strsep() updates @s to point after the token, ready for the next call.
413 * It returns empty tokens, too, behaving exactly like the libc function
414 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
415 * Same semantics, slimmer shape. ;)
417 char * strsep(char **s
, const char *ct
)
419 char *sbegin
= *s
, *end
;
424 end
= strpbrk(sbegin
, ct
);
433 #ifndef __HAVE_ARCH_STRSWAB
435 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
436 * s: address of the string
438 * returns the address of the swapped string or NULL on error. If
439 * string length is odd, last byte is untouched.
441 char *strswab(const char *s
)
445 if ((NULL
== s
) || ('\0' == *s
)) {
449 for (p
=(char *)s
, q
=p
+1; (*p
!= '\0') && (*q
!= '\0'); p
+=2, q
+=2) {
461 #ifndef __HAVE_ARCH_MEMSET
463 * memset - Fill a region of memory with the given value
464 * @s: Pointer to the start of the area.
465 * @c: The byte to fill the area with
466 * @count: The size of the area.
468 * Do not use memset() to access IO space, use memset_io() instead.
470 void * memset(void * s
,int c
,size_t count
)
472 unsigned long *sl
= (unsigned long *) s
;
475 #if !CONFIG_IS_ENABLED(TINY_MEMSET)
476 unsigned long cl
= 0;
479 /* do it one word at a time (32 bits or 64 bits) while possible */
480 if ( ((ulong
)s
& (sizeof(*sl
) - 1)) == 0) {
481 for (i
= 0; i
< sizeof(*sl
); i
++) {
485 while (count
>= sizeof(*sl
)) {
487 count
-= sizeof(*sl
);
490 #endif /* fill 8 bits at a time */
499 #ifndef __HAVE_ARCH_MEMCPY
501 * memcpy - Copy one area of memory to another
502 * @dest: Where to copy to
503 * @src: Where to copy from
504 * @count: The size of the area.
506 * You should not use this function to access IO space, use memcpy_toio()
507 * or memcpy_fromio() instead.
509 void * memcpy(void *dest
, const void *src
, size_t count
)
511 unsigned long *dl
= (unsigned long *)dest
, *sl
= (unsigned long *)src
;
517 /* while all data is aligned (common case), copy a word at a time */
518 if ( (((ulong
)dest
| (ulong
)src
) & (sizeof(*dl
) - 1)) == 0) {
519 while (count
>= sizeof(*dl
)) {
521 count
-= sizeof(*dl
);
524 /* copy the reset one byte at a time */
534 #ifndef __HAVE_ARCH_MEMMOVE
536 * memmove - Copy one area of memory to another
537 * @dest: Where to copy to
538 * @src: Where to copy from
539 * @count: The size of the area.
541 * Unlike memcpy(), memmove() copes with overlapping areas.
543 void * memmove(void * dest
,const void *src
,size_t count
)
548 memcpy(dest
, src
, count
);
550 tmp
= (char *) dest
+ count
;
551 s
= (char *) src
+ count
;
560 #ifndef __HAVE_ARCH_MEMCMP
562 * memcmp - Compare two areas of memory
563 * @cs: One area of memory
564 * @ct: Another area of memory
565 * @count: The size of the area.
567 int memcmp(const void * cs
,const void * ct
,size_t count
)
569 const unsigned char *su1
, *su2
;
572 for( su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
573 if ((res
= *su1
- *su2
) != 0)
579 #ifndef __HAVE_ARCH_MEMSCAN
581 * memscan - Find a character in an area of memory.
582 * @addr: The memory area
583 * @c: The byte to search for
584 * @size: The size of the area.
586 * returns the address of the first occurrence of @c, or 1 byte past
587 * the area if @c is not found
589 void * memscan(void * addr
, int c
, size_t size
)
591 unsigned char * p
= (unsigned char *) addr
;
603 #ifndef __HAVE_ARCH_STRSTR
605 * strstr - Find the first substring in a %NUL terminated string
606 * @s1: The string to be searched
607 * @s2: The string to search for
609 char * strstr(const char * s1
,const char * s2
)
619 if (!memcmp(s1
,s2
,l2
))
627 #ifndef __HAVE_ARCH_MEMCHR
629 * memchr - Find a character in an area of memory.
630 * @s: The memory area
631 * @c: The byte to search for
632 * @n: The size of the area.
634 * returns the address of the first occurrence of @c, or %NULL
637 void *memchr(const void *s
, int c
, size_t n
)
639 const unsigned char *p
= s
;
641 if ((unsigned char)c
== *p
++) {
642 return (void *)(p
-1);
649 #ifndef __HAVE_ARCH_MEMCHR_INV
650 static void *check_bytes8(const u8
*start
, u8 value
, unsigned int bytes
)
654 return (void *)start
;
661 * memchr_inv - Find an unmatching character in an area of memory.
662 * @start: The memory area
663 * @c: Find a character other than c
664 * @bytes: The size of the area.
666 * returns the address of the first character other than @c, or %NULL
667 * if the whole buffer contains just @c.
669 void *memchr_inv(const void *start
, int c
, size_t bytes
)
673 unsigned int words
, prefix
;
676 return check_bytes8(start
, value
, bytes
);
679 value64
|= value64
<< 8;
680 value64
|= value64
<< 16;
681 value64
|= value64
<< 32;
683 prefix
= (unsigned long)start
% 8;
688 r
= check_bytes8(start
, value
, prefix
);
698 if (*(u64
*)start
!= value64
)
699 return check_bytes8(start
, value
, 8);
704 return check_bytes8(start
, value
, bytes
% 8);