diff options
author | William Wilgus <wilgus.william@gmail.com> | 2022-11-13 00:43:43 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2022-11-13 01:14:49 -0500 |
commit | a634557a881b59b8be1dc156f9822c6d20bd8741 (patch) | |
tree | fd953a0ea1d9a02a8d73a15f1aead27b59c5ffaa /firmware/common | |
parent | ffe2df2e92cbdeb507a49279a85ac88cac2fbe4f (diff) | |
download | rockbox-a634557a881b59b8be1dc156f9822c6d20bd8741.tar.gz rockbox-a634557a881b59b8be1dc156f9822c6d20bd8741.zip |
fix strptokspn, add strcspn, fix splash.c
fix off by 1 error in strptokspn, add strcspn, fix fallout in splash.c
Change-Id: I61475d9633fc35db5a8ae30cbe588f69f2f7fabc
Diffstat (limited to 'firmware/common')
-rw-r--r-- | firmware/common/strptokspn.c | 33 |
1 files changed, 11 insertions, 22 deletions
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 @@ | |||
39 | * Pointer **end | 39 | * Pointer **end |
40 | * | 40 | * |
41 | * Note the returned token is NOT NULL terminated by the function as in strtok_r | 41 | * Note the returned token is NOT NULL terminated by the function as in strtok_r |
42 | * However the caller can use ret[len+1] = '\0'; to emulate a call to strtok_r | 42 | * However the caller can use ret[len] = '\0'; to emulate a call to strtok_r |
43 | */ | 43 | */ |
44 | |||
45 | const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end) | 44 | const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end) |
46 | { | 45 | { |
47 | *len = 0; | 46 | if (ptr == NULL) /* we got NULL input so then we get last position instead */ |
48 | if (!ptr) | 47 | { |
49 | /* we got NULL input so then we get our last position instead */ | ||
50 | ptr = *end; | 48 | ptr = *end; |
49 | } | ||
51 | 50 | ||
52 | /* pass all letters that are including in the separator string */ | 51 | /* pass all letters that are including in the separator string */ |
53 | while (*ptr && strchr(sep, *ptr)) | 52 | while (*ptr && strchr(sep, *ptr)) |
54 | ++ptr; | 53 | ++ptr; |
55 | 54 | ||
56 | if (*ptr) { | 55 | if (*ptr != '\0') |
56 | { | ||
57 | /* so this is where the next piece of string starts */ | 57 | /* so this is where the next piece of string starts */ |
58 | const char *start = ptr; | 58 | const char *start = ptr; |
59 | 59 | *len = strcspn(ptr, sep); /* Get span until any sep character in string */ | |
60 | /* set the end pointer to the first byte after the start */ | 60 | *end = ptr + *len; |
61 | *end = start + 1; | 61 | if (**end) /* the end is not a null byte */ |
62 | |||
63 | /* scan through the string to find where it ends, it ends on a | ||
64 | null byte or a character that exists in the separator string */ | ||
65 | while (**end && !strchr(sep, **end)) | ||
66 | ++*end; | 62 | ++*end; |
67 | *len = (*end - start) - 1; /* this would be the string len if there actually was a NULL */ | 63 | return start; |
68 | if (**end) { /* the end is not a null byte */ | ||
69 | ++*end; /* advance last pointer to beyond the match */ | ||
70 | } | ||
71 | |||
72 | return start; /* return the position where the string starts */ | ||
73 | } | 64 | } |
74 | |||
75 | /* we ended up on a null byte, there are no more strings to find! */ | ||
76 | return NULL; | 65 | return NULL; |
77 | } | 66 | } |
78 | 67 | ||
@@ -82,7 +71,7 @@ char * strtok_r(char *ptr, const char *sep, char **end) | |||
82 | size_t len; | 71 | size_t len; |
83 | char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end); | 72 | char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end); |
84 | if (ret) | 73 | if (ret) |
85 | ret[len + 1] = '\0'; | 74 | ret[len] = '\0'; |
86 | return ret; | 75 | return ret; |
87 | } | 76 | } |
88 | #endif | 77 | #endif |