diff options
| author | taiyu <[email protected]> | 2015-09-18 07:23:04 -0700 | 
|---|---|---|
| committer | taiyu <[email protected]> | 2015-09-18 07:23:04 -0700 | 
| commit | 0d51f62224a3c2e65894f1725076a588b172447c (patch) | |
| tree | 35529e2a7888e4d3af0997371efd1da5bbf2ad15 /sway/stringop.c | |
| parent | d2e2b04bfd505561981c9137bfc493bb8effe63e (diff) | |
| parent | 318e1be240574e316094e5ea73d32e9f9a1f7c04 (diff) | |
merge + no c_extensions
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 @@  #include <stdlib.h>  #include <stdio.h> +#include <string.h>  #include <strings.h>  #include <ctype.h>  #include "stringop.h" @@ -7,7 +8,7 @@  #include "string.h"  #include "list.h" -const char *whitespace = " \f\n\r\t\v"; +const char whitespace[] = " \f\n\r\t\v";  /* Note: This returns 8 characters for trimmed_start per tab character. */  char *strip_whitespace(char *_str) { @@ -313,13 +314,16 @@ char *join_list(list_t *list, char *separator) {  }  char *cmdsep(char **stringp, const char *delim) { -	char *head = strsep(stringp, delim); -	// But skip over trailing delims. '3   tokens  here' -> '3' 'tokens  here' -	if (*stringp) { -		*stringp += strspn(*stringp, delim); -		// If skiping over delims brings us to the end of string, set to NULL -		if (!**stringp) *stringp = NULL; -	} +	// skip over leading delims +	char *head = *stringp + strspn(*stringp, delim); +	// Find end token +	char *tail = *stringp += strcspn(*stringp, delim); +	// Set stringp to begining of next token +	*stringp += strspn(*stringp, delim); +	// Set stringp to null if last token +	if (!**stringp) *stringp = NULL; +	// Nullify end of first token +	*tail = 0;  	return head;  } @@ -358,3 +362,12 @@ char *argsep(char **stringp, const char *delim) {  	found:  	return start;  } + +char *strdup(const char *str) { +	char *dup = malloc(strlen(str) + 1); +	if (dup) { +		strcpy(dup, str); +	} +	return dup; +} + | 
