Skip to content

Commit a7d0021

Browse files
committed
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>
1 parent 6b45ba4 commit a7d0021

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

include/linux/string.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ extern __kernel_size_t strlen(const char *);
7676
#ifndef __HAVE_ARCH_STRNLEN
7777
extern __kernel_size_t strnlen(const char *,__kernel_size_t);
7878
#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+
7994
#ifndef __HAVE_ARCH_STRDUP
8095
extern char * strdup(const char *);
8196
#endif

lib/string.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,30 @@ size_t strnlen(const char * s, size_t count)
286286
}
287287
#endif
288288

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+
289313
#ifndef __HAVE_ARCH_STRDUP
290314
char * strdup(const char *s)
291315
{

0 commit comments

Comments
 (0)