summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibC/string.cpp32
-rw-r--r--Libraries/LibC/string.h1
2 files changed, 19 insertions, 14 deletions
diff --git a/Libraries/LibC/string.cpp b/Libraries/LibC/string.cpp
index 908d399d3b..1df6e6e990 100644
--- a/Libraries/LibC/string.cpp
+++ b/Libraries/LibC/string.cpp
@@ -455,24 +455,22 @@ char* strpbrk(const char* s, const char* accept)
return nullptr;
}
-char* strtok(char* str, const char* delim)
+char* strtok_r(char* str, const char* delim, char** saved_str)
{
- static char* saved_str;
-
if (!str) {
if (!saved_str)
return nullptr;
- str = saved_str;
+ str = *saved_str;
}
-
+
size_t token_start = 0;
size_t token_end = 0;
size_t str_len = strlen(str);
size_t delim_len = strlen(delim);
-
+
for (size_t i = 0; i < str_len; ++i) {
bool is_proper_delim = false;
-
+
for (size_t j = 0; j < delim_len; ++j) {
if (str[i] == delim[j]) {
// Skip beginning delimiters
@@ -480,31 +478,37 @@ char* strtok(char* str, const char* delim)
++token_start;
break;
}
-
+
is_proper_delim = true;
}
}
-
+
++token_end;
if (is_proper_delim && token_end > 0) {
--token_end;
break;
}
}
-
+
if (str[token_start] == '\0')
return nullptr;
-
+
if (token_end == 0) {
- saved_str = nullptr;
+ *saved_str = nullptr;
return &str[token_start];
}
-
- saved_str = &str[token_end + 1];
+
+ *saved_str = &str[token_end + 1];
str[token_end] = '\0';
return &str[token_start];
}
+char* strtok(char* str, const char* delim)
+{
+ static char* saved_str;
+ return strtok_r(str, delim, &saved_str);
+}
+
int strcoll(const char* s1, const char* s2)
{
return strcmp(s1, s2);
diff --git a/Libraries/LibC/string.h b/Libraries/LibC/string.h
index 9d02da28b3..24896a6729 100644
--- a/Libraries/LibC/string.h
+++ b/Libraries/LibC/string.h
@@ -31,6 +31,7 @@ size_t strcspn(const char*, const char* reject);
char* strerror(int errnum);
char* strsignal(int signum);
char* strpbrk(const char*, const char* accept);
+char* strtok_r(char* str, const char* delim, char** saved_str);
char* strtok(char* str, const char* delim);
int strcoll(const char* s1, const char* s2);
size_t strxfrm(char* dest, const char* src, size_t n);