aboutsummaryrefslogtreecommitdiffstats
path: root/sway/stringop.c
diff options
context:
space:
mode:
authorLibravatar taiyu <taiyu.len@gmail.com>2015-09-18 07:23:04 -0700
committerLibravatar taiyu <taiyu.len@gmail.com>2015-09-18 07:23:04 -0700
commit0d51f62224a3c2e65894f1725076a588b172447c (patch)
tree35529e2a7888e4d3af0997371efd1da5bbf2ad15 /sway/stringop.c
parentminor fix (diff)
parentFix warnings introduced by prior commit (diff)
downloadsway-0d51f62224a3c2e65894f1725076a588b172447c.tar.gz
sway-0d51f62224a3c2e65894f1725076a588b172447c.tar.zst
sway-0d51f62224a3c2e65894f1725076a588b172447c.zip
merge + no c_extensions
Diffstat (limited to 'sway/stringop.c')
-rw-r--r--sway/stringop.c29
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
10const char *whitespace = " \f\n\r\t\v"; 11const 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. */
13char *strip_whitespace(char *_str) { 14char *strip_whitespace(char *_str) {
@@ -313,13 +314,16 @@ char *join_list(list_t *list, char *separator) {
313} 314}
314 315
315char *cmdsep(char **stringp, const char *delim) { 316char *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
366char *strdup(const char *str) {
367 char *dup = malloc(strlen(str) + 1);
368 if (dup) {
369 strcpy(dup, str);
370 }
371 return dup;
372}
373