commit a2dde5d

default  ·  2025-02-04 16:26:47 +0000 UTC
parent d1d9e13
Only split real strings in xs_split_n().
1 files changed,  +10, -5
M xs.h
M xs.h
+10, -5
 1@@ -991,16 +991,20 @@ xs_str *xs_join(const xs_list *list, const char *sep)
 2 xs_list *xs_split_n(const char *str, const char *sep, int times)
 3 /* splits a string into a list upto n times */
 4 {
 5+    xs_list *list = xs_list_new();
 6+
 7+    if (!xs_is_string(str) || !xs_is_string(sep))
 8+        return list;
 9+
10     int sz = strlen(sep);
11     char *ss;
12-    xs_list *list;
13-
14-    list = xs_list_new();
15 
16     while (times > 0 && (ss = strstr(str, sep)) != NULL) {
17         /* create a new string with this slice and add it to the list */
18         xs *s = xs_str_new_sz(str, ss - str);
19-        list = xs_list_append(list, s);
20+
21+        if (xs_is_string(s))
22+            list = xs_list_append(list, s);
23 
24         /* skip past the separator */
25         str = ss + sz;
26@@ -1009,7 +1013,8 @@ xs_list *xs_split_n(const char *str, const char *sep, int times)
27     }
28 
29     /* add the rest of the string */
30-    list = xs_list_append(list, str);
31+    if (xs_is_string(str))
32+        list = xs_list_append(list, str);
33 
34     return list;
35 }