Commit a7d0021063661a65bfa47d4ec912da4e2cfa8f9a
1 parent
6b45ba45fb
Exists in
smarc_8mq_lf_v2020.04
and in
20 other branches
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>
Showing 2 changed files with 39 additions and 0 deletions Side-by-side Diff
include/linux/string.h
| ... | ... | @@ -76,6 +76,21 @@ |
| 76 | 76 | #ifndef __HAVE_ARCH_STRNLEN |
| 77 | 77 | extern __kernel_size_t strnlen(const char *,__kernel_size_t); |
| 78 | 78 | #endif |
| 79 | + | |
| 80 | +#ifndef __HAVE_ARCH_STRCSPN | |
| 81 | +/** | |
| 82 | + * strcspn() - find span of string without given characters | |
| 83 | + * | |
| 84 | + * Calculates the length of the initial segment of @s which consists entirely | |
| 85 | + * of bsytes not in reject. | |
| 86 | + * | |
| 87 | + * @s: string to search | |
| 88 | + * @reject: strings which cause the search to halt | |
| 89 | + * @return number of characters at the start of @s which are not in @reject | |
| 90 | + */ | |
| 91 | +size_t strcspn(const char *s, const char *reject); | |
| 92 | +#endif | |
| 93 | + | |
| 79 | 94 | #ifndef __HAVE_ARCH_STRDUP |
| 80 | 95 | extern char * strdup(const char *); |
| 81 | 96 | #endif |
lib/string.c
| ... | ... | @@ -286,6 +286,30 @@ |
| 286 | 286 | } |
| 287 | 287 | #endif |
| 288 | 288 | |
| 289 | +#ifndef __HAVE_ARCH_STRCSPN | |
| 290 | +/** | |
| 291 | + * strcspn - Calculate the length of the initial substring of @s which does | |
| 292 | + * not contain letters in @reject | |
| 293 | + * @s: The string to be searched | |
| 294 | + * @reject: The string to avoid | |
| 295 | + */ | |
| 296 | +size_t strcspn(const char *s, const char *reject) | |
| 297 | +{ | |
| 298 | + const char *p; | |
| 299 | + const char *r; | |
| 300 | + size_t count = 0; | |
| 301 | + | |
| 302 | + for (p = s; *p != '\0'; ++p) { | |
| 303 | + for (r = reject; *r != '\0'; ++r) { | |
| 304 | + if (*p == *r) | |
| 305 | + return count; | |
| 306 | + } | |
| 307 | + ++count; | |
| 308 | + } | |
| 309 | + return count; | |
| 310 | +} | |
| 311 | +#endif | |
| 312 | + | |
| 289 | 313 | #ifndef __HAVE_ARCH_STRDUP |
| 290 | 314 | char * strdup(const char *s) |
| 291 | 315 | { |