summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2025-02-04 17:26:47 +0100
committerGravatar default2025-02-04 17:26:47 +0100
commita2dde5dedaddb7fe91c061997e1314ba569c2edf (patch)
tree320a59d0dbf21fc8c6314ff1e547b6a62473df6b
parentBumped version. (diff)
downloadpenes-snac2-a2dde5dedaddb7fe91c061997e1314ba569c2edf.tar.gz
penes-snac2-a2dde5dedaddb7fe91c061997e1314ba569c2edf.tar.xz
penes-snac2-a2dde5dedaddb7fe91c061997e1314ba569c2edf.zip
Only split real strings in xs_split_n().
-rw-r--r--xs.h15
1 files changed, 10 insertions, 5 deletions
diff --git a/xs.h b/xs.h
index 961425b..b53885e 100644
--- a/xs.h
+++ b/xs.h
@@ -991,16 +991,20 @@ xs_str *xs_join(const xs_list *list, const char *sep)
991xs_list *xs_split_n(const char *str, const char *sep, int times) 991xs_list *xs_split_n(const char *str, const char *sep, int times)
992/* splits a string into a list upto n times */ 992/* splits a string into a list upto n times */
993{ 993{
994 xs_list *list = xs_list_new();
995
996 if (!xs_is_string(str) || !xs_is_string(sep))
997 return list;
998
994 int sz = strlen(sep); 999 int sz = strlen(sep);
995 char *ss; 1000 char *ss;
996 xs_list *list;
997
998 list = xs_list_new();
999 1001
1000 while (times > 0 && (ss = strstr(str, sep)) != NULL) { 1002 while (times > 0 && (ss = strstr(str, sep)) != NULL) {
1001 /* create a new string with this slice and add it to the list */ 1003 /* create a new string with this slice and add it to the list */
1002 xs *s = xs_str_new_sz(str, ss - str); 1004 xs *s = xs_str_new_sz(str, ss - str);
1003 list = xs_list_append(list, s); 1005
1006 if (xs_is_string(s))
1007 list = xs_list_append(list, s);
1004 1008
1005 /* skip past the separator */ 1009 /* skip past the separator */
1006 str = ss + sz; 1010 str = ss + sz;
@@ -1009,7 +1013,8 @@ xs_list *xs_split_n(const char *str, const char *sep, int times)
1009 } 1013 }
1010 1014
1011 /* add the rest of the string */ 1015 /* add the rest of the string */
1012 list = xs_list_append(list, str); 1016 if (xs_is_string(str))
1017 list = xs_list_append(list, str);
1013 1018
1014 return list; 1019 return list;
1015} 1020}