mirror of
https://github.com/amadvance/snapraid.git
synced 2026-12-02 12:57:14 -05:00
Add trim and trim_empty support to strsplit and update all callers
This commit is contained in:
parent
929ba5cebe
commit
8e66b42ceb
5 changed files with 163 additions and 23 deletions
|
|
@ -22,27 +22,42 @@ char* strpolish(char* s)
|
|||
return s;
|
||||
}
|
||||
|
||||
unsigned strsplit(char** split_map, unsigned split_max, char* str, const char* delimiters)
|
||||
unsigned strsplit(char** split_map, unsigned split_max, char* str, const char* delimiters, const char* trim, int trim_empty)
|
||||
{
|
||||
unsigned mac = 0;
|
||||
|
||||
/* skip initial delimiters */
|
||||
str += strspn(str, delimiters);
|
||||
if (*str == 0)
|
||||
return 0;
|
||||
|
||||
while (*str != 0 && mac < split_max) {
|
||||
/* start of the token */
|
||||
split_map[mac] = str;
|
||||
++mac;
|
||||
while (mac < split_max) {
|
||||
char* begin = str;
|
||||
|
||||
/* find the first delimiter or the end of the string */
|
||||
str += strcspn(str, delimiters);
|
||||
|
||||
/* put the final terminator if missing */
|
||||
if (*str != 0)
|
||||
*str++ = 0;
|
||||
char* end = str;
|
||||
|
||||
/* skip trailing delimiters */
|
||||
str += strspn(str, delimiters);
|
||||
int has_next = *str != 0;
|
||||
if (has_next) {
|
||||
*str = 0;
|
||||
++str;
|
||||
}
|
||||
|
||||
if (trim != 0) {
|
||||
begin += strspn(begin, trim);
|
||||
|
||||
while (end > begin && strchr(trim, end[-1]) != 0) {
|
||||
--end;
|
||||
*end = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!trim_empty || *begin != 0) {
|
||||
split_map[mac] = begin;
|
||||
++mac;
|
||||
}
|
||||
|
||||
if (!has_next)
|
||||
break;
|
||||
}
|
||||
|
||||
return mac;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue