From ba8a50523606b90ca58b521620e87891db5b119c Mon Sep 17 00:00:00 2001 From: Tomasz Gorochowik Date: Mon, 13 May 2019 14:36:36 +0200 Subject: [PATCH 1/2] libc: add strcspn() to minimal libc From IEEE Std 1003.1-2017: The strcspn() function shall compute the length (in bytes) of the maximum initial segment of the string pointed to by s1 which consists entirely of bytes not from the string pointed to by s2. Signed-off-by: Tomasz Gorochowik --- lib/libc/minimal/include/string.h | 1 + lib/libc/minimal/source/string/string.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/libc/minimal/include/string.h b/lib/libc/minimal/include/string.h index 8ff65b78de814..74c7bd5f37648 100644 --- a/lib/libc/minimal/include/string.h +++ b/lib/libc/minimal/include/string.h @@ -29,6 +29,7 @@ extern char *strcat(char *_MLIBC_RESTRICT dest, 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 strcspn(const char *s1, const char *s2); extern int memcmp(const void *m1, const void *m2, size_t n); extern void *memmove(void *d, const void *s, size_t n); diff --git a/lib/libc/minimal/source/string/string.c b/lib/libc/minimal/source/string/string.c index a97422767601b..bce36ce641f0f 100644 --- a/lib/libc/minimal/source/string/string.c +++ b/lib/libc/minimal/source/string/string.c @@ -169,6 +169,28 @@ char *strncat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src, return orig_dest; } +/** + * @brief Get the length of a complementary substring + * + * @return length of the maximum initial segment of the string pointed to + * by s1 which consists entirely of bytes not from the string pointed to by s2. + */ + +size_t strcspn(const char *s1, const char *s2) +{ + int i, j; + + for (i = 0; i < strlen(s2); ++i) { + for (j = 0; j < strlen(s1); ++j) { + if (s1[j] == s2[i]) { + return j; + } + } + } + + return strlen(s1); +} + /** * * @brief Compare two memory areas From c515c2406c3276e9236b6a7dceb04af18f48e823 Mon Sep 17 00:00:00 2001 From: Tomasz Gorochowik Date: Tue, 28 May 2019 11:56:38 +0200 Subject: [PATCH 2/2] libc: add iscntrl() to minimal libc From IEEE Std 1003.1-2017: The iscntrl() and function shall test whether the c is a character of class cntrl. Signed-off-by: Tomasz Gorochowik --- lib/libc/minimal/include/string.h | 1 + lib/libc/minimal/source/string/string.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/libc/minimal/include/string.h b/lib/libc/minimal/include/string.h index 74c7bd5f37648..2f954e84dc4e4 100644 --- a/lib/libc/minimal/include/string.h +++ b/lib/libc/minimal/include/string.h @@ -30,6 +30,7 @@ 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 strcspn(const char *s1, const char *s2); +extern int iscntrl(int a); extern int memcmp(const void *m1, const void *m2, size_t n); extern void *memmove(void *d, const void *s, size_t n); diff --git a/lib/libc/minimal/source/string/string.c b/lib/libc/minimal/source/string/string.c index bce36ce641f0f..d29f0d3caf0e6 100644 --- a/lib/libc/minimal/source/string/string.c +++ b/lib/libc/minimal/source/string/string.c @@ -191,6 +191,20 @@ size_t strcspn(const char *s1, const char *s2) return strlen(s1); } +/** + * @brief Test for a control character + * + * @return non-zero if c is a control character, otherwise 0. + */ + +int iscntrl(int c) +{ + /* All the characters placed before the space on the ASCII table + * and the 0x7F character (DEL) are control characters. + */ + return (int)(c < ' ' || c == 0x7F); +} + /** * * @brief Compare two memory areas