#include "lib/string/compat_string.h"
#include "lib/err/torerr.h"
#ifndef HAVE_STRLCPY
#include "ext/strlcpy.c"
#endif
#ifndef HAVE_STRLCAT
#include "ext/strlcat.c"
#endif
#include <stdlib.h>
#include <string.h>
static char *
strtok_helper(char *cp, const char *sep)
{
if (sep[1]) {
while (*cp && strchr(sep, *cp))
++cp;
} else {
while (*cp && *cp == *sep)
++cp;
}
return cp;
}
char *
tor_strtok_r_impl(char *str, const char *sep, char **lasts)
{
char *cp, *start;
raw_assert(*sep);
if (str) {
str = strtok_helper(str, sep);
if (!*str)
return NULL;
start = cp = *lasts = str;
} else if (!*lasts || !**lasts) {
return NULL;
} else {
start = cp = *lasts;
}
if (sep[1]) {
while (*cp && !strchr(sep, *cp))
++cp;
} else {
cp = strchr(cp, *sep);
}
if (!cp || !*cp) {
*lasts = NULL;
} else {
*cp++ = '\0';
*lasts = strtok_helper(cp, sep);
}
return start;
}