strtok_r method
Reentrant string tokenization.
Because Dart does not have out-parameters, we simulate saveptr by
passing a List<String> of length 1 to hold the state.
Example: List<String> state = [""]; strtok_r(str, delim, state);
Implementation
String? strtok_r(String? str, String delim, List<String> saveptr) {
if (saveptr.isEmpty) saveptr.add("");
String state;
if (str != null) {
state = str;
} else {
if (saveptr[0].isEmpty) return null;
state = saveptr[0];
}
int start = 0;
while (start < state.length && delim.contains(state[start])) {
start++;
}
if (start >= state.length) {
saveptr[0] = "";
return null;
}
int end = start;
while (end < state.length && !delim.contains(state[end])) {
end++;
}
String token = state.substring(start, end);
if (end < state.length) {
saveptr[0] = state.substring(end + 1);
} else {
saveptr[0] = "";
}
return token;
}