diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-11-10 16:24:52 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-10 21:19:08 +0100 |
commit | 5951c7ca05132580a07d3a09ef118027044ea4f6 (patch) | |
tree | 963807d0c26c26416f76a73bd9e97aa6ea1755d3 /Libraries | |
parent | 72fae05c074e73be760fdf2460c7f08799ad36d5 (diff) | |
download | serenity-5951c7ca05132580a07d3a09ef118027044ea4f6.zip |
LibC: Add strtok_r() and make strtok() a wrapper around it
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibC/string.cpp | 32 | ||||
-rw-r--r-- | Libraries/LibC/string.h | 1 |
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); |