diff options
| author | 2025-02-04 17:26:47 +0100 | |
|---|---|---|
| committer | 2025-02-04 17:26:47 +0100 | |
| commit | a2dde5dedaddb7fe91c061997e1314ba569c2edf (patch) | |
| tree | 320a59d0dbf21fc8c6314ff1e547b6a62473df6b /xs.h | |
| parent | Bumped version. (diff) | |
| download | snac2-a2dde5dedaddb7fe91c061997e1314ba569c2edf.tar.gz snac2-a2dde5dedaddb7fe91c061997e1314ba569c2edf.tar.xz snac2-a2dde5dedaddb7fe91c061997e1314ba569c2edf.zip | |
Only split real strings in xs_split_n().
Diffstat (limited to 'xs.h')
| -rw-r--r-- | xs.h | 15 |
1 files changed, 10 insertions, 5 deletions
| @@ -991,16 +991,20 @@ xs_str *xs_join(const xs_list *list, const char *sep) | |||
| 991 | xs_list *xs_split_n(const char *str, const char *sep, int times) | 991 | xs_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 | } |