mirror of
https://github.com/u-boot/u-boot.git
synced 2025-04-11 07:24:46 +00:00
lib: implement strnstr()
Implement library function strnstr(). Implement strstr() using strnstr(). Sort the includes. Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com> Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org> [jf: replace <stdint.h> by <limits.h>, folded from next patch] Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
This commit is contained in:
parent
10917df17f
commit
7c7361b98d
2 changed files with 45 additions and 23 deletions
|
@ -72,6 +72,9 @@ extern char * strrchr(const char *,int);
|
|||
#ifndef __HAVE_ARCH_STRSTR
|
||||
extern char * strstr(const char *,const char *);
|
||||
#endif
|
||||
#ifndef __HAVE_ARCH_STRNSTR
|
||||
extern char *strnstr(const char *, const char *, size_t);
|
||||
#endif
|
||||
#ifndef __HAVE_ARCH_STRLEN
|
||||
extern __kernel_size_t strlen(const char *);
|
||||
#endif
|
||||
|
|
65
lib/string.c
65
lib/string.c
|
@ -15,13 +15,14 @@
|
|||
* reentrant and should be faster). Use only strsep() in new code, please.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
#include <linux/compiler.h>
|
||||
#include <linux/types.h>
|
||||
#include <linux/string.h>
|
||||
#include <linux/ctype.h>
|
||||
#include <malloc.h>
|
||||
#include <asm/sections.h>
|
||||
#include <config.h>
|
||||
#include <limits.h>
|
||||
#include <linux/compiler.h>
|
||||
#include <linux/ctype.h>
|
||||
#include <linux/string.h>
|
||||
#include <linux/types.h>
|
||||
#include <malloc.h>
|
||||
|
||||
/**
|
||||
* strncasecmp - Case insensitive, length-limited string comparison
|
||||
|
@ -679,27 +680,45 @@ char *memdup(const void *src, size_t len)
|
|||
return p;
|
||||
}
|
||||
|
||||
#ifndef __HAVE_ARCH_STRNSTR
|
||||
/**
|
||||
* strnstr() - find the first substring occurrence in a NUL terminated string
|
||||
*
|
||||
* @s1: string to be searched
|
||||
* @s2: string to search for
|
||||
* @len: maximum number of characters in s2 to consider
|
||||
*
|
||||
* Return: pointer to the first occurrence or NULL
|
||||
*/
|
||||
char *strnstr(const char *s1, const char *s2, size_t len)
|
||||
{
|
||||
size_t l1, l2;
|
||||
|
||||
l1 = strnlen(s1, len);
|
||||
l2 = strlen(s2);
|
||||
|
||||
for (; l1 >= l2; --l1, ++s1) {
|
||||
if (!memcmp(s1, s2, l2))
|
||||
return (char *) s1;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef __HAVE_ARCH_STRSTR
|
||||
/**
|
||||
* strstr - Find the first substring in a %NUL terminated string
|
||||
* @s1: The string to be searched
|
||||
* @s2: The string to search for
|
||||
* strstr() - find the first substring occurrence in a NUL terminated string
|
||||
*
|
||||
* @s1: string to be searched
|
||||
* @s2: string to search for
|
||||
* @len: maximum number of characters in s2 to consider
|
||||
*
|
||||
* Return: pointer to the first occurrence or NULL
|
||||
*/
|
||||
char * strstr(const char * s1,const char * s2)
|
||||
char *strstr(const char *s1, const char *s2)
|
||||
{
|
||||
int l1, l2;
|
||||
|
||||
l2 = strlen(s2);
|
||||
if (!l2)
|
||||
return (char *) s1;
|
||||
l1 = strlen(s1);
|
||||
while (l1 >= l2) {
|
||||
l1--;
|
||||
if (!memcmp(s1,s2,l2))
|
||||
return (char *) s1;
|
||||
s1++;
|
||||
}
|
||||
return NULL;
|
||||
return strnstr(s1, s2, SIZE_MAX);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue