From a634557a881b59b8be1dc156f9822c6d20bd8741 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sun, 13 Nov 2022 00:43:43 -0500 Subject: fix strptokspn, add strcspn, fix splash.c fix off by 1 error in strptokspn, add strcspn, fix fallout in splash.c Change-Id: I61475d9633fc35db5a8ae30cbe588f69f2f7fabc --- firmware/SOURCES | 2 +- firmware/common/strptokspn.c | 33 +++++++++++--------------------- firmware/libc/strcspn.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 firmware/libc/strcspn.c (limited to 'firmware') diff --git a/firmware/SOURCES b/firmware/SOURCES index 4aa7c38daf..76d6cee921 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -271,7 +271,7 @@ libc/strcat.c libc/strchr.c libc/strcmp.c libc/strcpy.c - +libc/strcspn.c libc/strncmp.c libc/strrchr.c libc/strstr.c diff --git a/firmware/common/strptokspn.c b/firmware/common/strptokspn.c index f4b92c0712..16aafc66ef 100644 --- a/firmware/common/strptokspn.c +++ b/firmware/common/strptokspn.c @@ -39,40 +39,29 @@ * Pointer **end * * Note the returned token is NOT NULL terminated by the function as in strtok_r - * However the caller can use ret[len+1] = '\0'; to emulate a call to strtok_r + * However the caller can use ret[len] = '\0'; to emulate a call to strtok_r */ - const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end) { - *len = 0; - if (!ptr) - /* we got NULL input so then we get our last position instead */ + if (ptr == NULL) /* we got NULL input so then we get last position instead */ + { ptr = *end; + } /* pass all letters that are including in the separator string */ while (*ptr && strchr(sep, *ptr)) ++ptr; - if (*ptr) { + if (*ptr != '\0') + { /* so this is where the next piece of string starts */ const char *start = ptr; - - /* set the end pointer to the first byte after the start */ - *end = start + 1; - - /* scan through the string to find where it ends, it ends on a - null byte or a character that exists in the separator string */ - while (**end && !strchr(sep, **end)) + *len = strcspn(ptr, sep); /* Get span until any sep character in string */ + *end = ptr + *len; + if (**end) /* the end is not a null byte */ ++*end; - *len = (*end - start) - 1; /* this would be the string len if there actually was a NULL */ - if (**end) { /* the end is not a null byte */ - ++*end; /* advance last pointer to beyond the match */ - } - - return start; /* return the position where the string starts */ + return start; } - - /* we ended up on a null byte, there are no more strings to find! */ return NULL; } @@ -82,7 +71,7 @@ char * strtok_r(char *ptr, const char *sep, char **end) size_t len; char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end); if (ret) - ret[len + 1] = '\0'; + ret[len] = '\0'; return ret; } #endif diff --git a/firmware/libc/strcspn.c b/firmware/libc/strcspn.c new file mode 100644 index 0000000000..ee50066ed1 --- /dev/null +++ b/firmware/libc/strcspn.c @@ -0,0 +1,45 @@ +/* +FUNCTION + <>---count characters not in string +INDEX + strcspn +ANSI_SYNOPSIS + size_t strcspn(const char *<[s1]>, const char *<[s2]>); +TRAD_SYNOPSIS + size_t strcspn(<[s1]>, <[s2]>) + char *<[s1]>; + char *<[s2]>; +DESCRIPTION + This function computes the length of the initial part of + the string pointed to by <[s1]> which consists entirely of + characters <[NOT]> from the string pointed to by <[s2]> + (excluding the terminating null character). +RETURNS + <> returns the length of the substring found. +PORTABILITY +<> is ANSI C. +<> requires no supporting OS subroutines. + */ +#include +#include "_ansi.h" /* for _DEFUN */ + +size_t +_DEFUN (strcspn, (s1, s2), + _CONST char *s1 _AND + _CONST char *s2) +{ + _CONST char *s = s1; + _CONST char *c; + while (*s1) + { + for (c = s2; *c; c++) + { + if (*s1 == *c) + break; + } + if (*c) + break; + s1++; + } + return s1 - s; +} -- cgit v1.2.3