summaryrefslogtreecommitdiff
path: root/firmware/common/strptokspn.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/common/strptokspn.c')
-rw-r--r--firmware/common/strptokspn.c33
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
45const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end) 44const 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