Skip to content

Commit

Permalink
libc: minimal: add strspn and strcspn support
Browse files Browse the repository at this point in the history
These functions are useful for determining prefixes, as with file system
paths.  They are required by littlefs.

Signed-off-by: Peter A. Bigot <[email protected]>
  • Loading branch information
pabigot authored and andrewboie committed Jul 31, 2019
1 parent b7db90d commit 8420f43
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/libc/minimal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ zephyr_library_sources(
source/string/strncasecmp.c
source/string/strstr.c
source/string/string.c
source/string/strspn.c
source/stdout/prf.c
source/stdout/stdout_console.c
source/stdout/sprintf.c
Expand Down
3 changes: 3 additions & 0 deletions lib/libc/minimal/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ extern char *strncat(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s,
size_t n);
extern char *strstr(const char *s, const char *find);

extern size_t strspn(const char *s, const char *accept);
extern size_t strcspn(const char *s, const char *reject);

extern int memcmp(const void *m1, const void *m2, size_t n);
extern void *memmove(void *d, const void *s, size_t n);
extern void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s,
Expand Down
32 changes: 32 additions & 0 deletions lib/libc/minimal/source/string/strspn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>
#include <string.h>

size_t strspn(const char *s,
const char *accept)
{
const char *ins = s;

while ((*s != '\0') && (strchr(accept, *s) != NULL)) {
++s;
}

return s - ins;
}

size_t strcspn(const char *s,
const char *reject)
{
const char *ins = s;

while ((*s != '\0') && (strchr(reject, *s) == NULL)) {
++s;
}

return s - ins;
}
25 changes: 25 additions & 0 deletions tests/lib/c_lib/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ void test_strchr(void)

}

/**
*
* @brief Test string prefix match functions
*
*/

void test_strxspn(void)
{
const char *empty = "";
const char *cset = "abc";

zassert_true(strspn("", empty) == 0U, "strspn empty empty");
zassert_true(strcspn("", empty) == 0U, "strcspn empty empty");

zassert_true(strspn("abde", cset) == 2U, "strspn match");
zassert_true(strcspn("abde", cset) == 0U, "strcspn nomatch");

zassert_true(strspn("da", cset) == 0U, "strspn nomatch");
zassert_true(strcspn("da", cset) == 1U, "strcspn match");

zassert_true(strspn("abac", cset) == 4U, "strspn all");
zassert_true(strcspn("defg", cset) == 4U, "strcspn all");
}

/**
*
* @brief Test memory comparison function
Expand Down Expand Up @@ -310,6 +334,7 @@ void test_main(void)
ztest_unit_test(test_memset),
ztest_unit_test(test_strlen),
ztest_unit_test(test_strcmp),
ztest_unit_test(test_strxspn),
ztest_unit_test(test_bsearch)
);
ztest_run_test_suite(test_c_lib);
Expand Down

0 comments on commit 8420f43

Please sign in to comment.