string: Add strcspn()

Add an implementation of strcspn() which returns the number of initial
characters that do not match any in a rejection list.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2017-05-18 20:09:29 -06:00
parent 6b45ba45fb
commit a7d0021063
2 changed files with 39 additions and 0 deletions

View File

@ -76,6 +76,21 @@ extern __kernel_size_t strlen(const char *);
#ifndef __HAVE_ARCH_STRNLEN
extern __kernel_size_t strnlen(const char *,__kernel_size_t);
#endif
#ifndef __HAVE_ARCH_STRCSPN
/**
* strcspn() - find span of string without given characters
*
* Calculates the length of the initial segment of @s which consists entirely
* of bsytes not in reject.
*
* @s: string to search
* @reject: strings which cause the search to halt
* @return number of characters at the start of @s which are not in @reject
*/
size_t strcspn(const char *s, const char *reject);
#endif
#ifndef __HAVE_ARCH_STRDUP
extern char * strdup(const char *);
#endif

View File

@ -286,6 +286,30 @@ size_t strnlen(const char * s, size_t count)
}
#endif
#ifndef __HAVE_ARCH_STRCSPN
/**
* strcspn - Calculate the length of the initial substring of @s which does
* not contain letters in @reject
* @s: The string to be searched
* @reject: The string to avoid
*/
size_t strcspn(const char *s, const char *reject)
{
const char *p;
const char *r;
size_t count = 0;
for (p = s; *p != '\0'; ++p) {
for (r = reject; *r != '\0'; ++r) {
if (*p == *r)
return count;
}
++count;
}
return count;
}
#endif
#ifndef __HAVE_ARCH_STRDUP
char * strdup(const char *s)
{