diff options
Diffstat (limited to 'sway/stringop.c')
-rw-r--r-- | sway/stringop.c | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/sway/stringop.c b/sway/stringop.c index 90f963d6..31a036c3 100644 --- a/sway/stringop.c +++ b/sway/stringop.c | |||
@@ -1,5 +1,6 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <string.h> | ||
3 | #include <strings.h> | 4 | #include <strings.h> |
4 | #include <ctype.h> | 5 | #include <ctype.h> |
5 | #include "stringop.h" | 6 | #include "stringop.h" |
@@ -7,7 +8,7 @@ | |||
7 | #include "string.h" | 8 | #include "string.h" |
8 | #include "list.h" | 9 | #include "list.h" |
9 | 10 | ||
10 | const char *whitespace = " \f\n\r\t\v"; | 11 | const char whitespace[] = " \f\n\r\t\v"; |
11 | 12 | ||
12 | /* Note: This returns 8 characters for trimmed_start per tab character. */ | 13 | /* Note: This returns 8 characters for trimmed_start per tab character. */ |
13 | char *strip_whitespace(char *_str) { | 14 | char *strip_whitespace(char *_str) { |
@@ -313,13 +314,16 @@ char *join_list(list_t *list, char *separator) { | |||
313 | } | 314 | } |
314 | 315 | ||
315 | char *cmdsep(char **stringp, const char *delim) { | 316 | char *cmdsep(char **stringp, const char *delim) { |
316 | char *head = strsep(stringp, delim); | 317 | // skip over leading delims |
317 | // But skip over trailing delims. '3 tokens here' -> '3' 'tokens here' | 318 | char *head = *stringp + strspn(*stringp, delim); |
318 | if (*stringp) { | 319 | // Find end token |
319 | *stringp += strspn(*stringp, delim); | 320 | char *tail = *stringp += strcspn(*stringp, delim); |
320 | // If skiping over delims brings us to the end of string, set to NULL | 321 | // Set stringp to begining of next token |
321 | if (!**stringp) *stringp = NULL; | 322 | *stringp += strspn(*stringp, delim); |
322 | } | 323 | // Set stringp to null if last token |
324 | if (!**stringp) *stringp = NULL; | ||
325 | // Nullify end of first token | ||
326 | *tail = 0; | ||
323 | return head; | 327 | return head; |
324 | } | 328 | } |
325 | 329 | ||
@@ -358,3 +362,12 @@ char *argsep(char **stringp, const char *delim) { | |||
358 | found: | 362 | found: |
359 | return start; | 363 | return start; |
360 | } | 364 | } |
365 | |||
366 | char *strdup(const char *str) { | ||
367 | char *dup = malloc(strlen(str) + 1); | ||
368 | if (dup) { | ||
369 | strcpy(dup, str); | ||
370 | } | ||
371 | return dup; | ||
372 | } | ||
373 | |||