From 9bb5371e8ce0270aa2f39f732d019eecbfb97def Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 20 May 2023 16:37:07 +0200 Subject: [PATCH] libluna+libc: Add strtok_r() --- libc/include/string.h | 3 +++ libluna/include/luna/CString.h | 1 + libluna/src/CString.cpp | 10 ++++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libc/include/string.h b/libc/include/string.h index cf66fbe4..d075e94f 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -71,6 +71,9 @@ extern "C" /* Separate a string into several tokens. */ char* strtok(char* str, const char* delim); + /* Separate a string into several tokens. */ + char* strtok_r(char* str, const char* delim, char** savep); + /* Return a heap-allocated copy of a fixed-size string. */ char* strndup(const char* str, size_t max); diff --git a/libluna/include/luna/CString.h b/libluna/include/luna/CString.h index 0ea31182..fc621a8d 100644 --- a/libluna/include/luna/CString.h +++ b/libluna/include/luna/CString.h @@ -18,6 +18,7 @@ extern "C" usize strcspn(const char* str, const char* reject); char* strtok(char* str, const char* delim); + char* strtok_r(char* str, const char* delim, char** savep); usize wcslen(const wchar_t* str); int wcscmp(const wchar_t* a, const wchar_t* b); diff --git a/libluna/src/CString.cpp b/libluna/src/CString.cpp index 7abd0f10..4f9e7220 100644 --- a/libluna/src/CString.cpp +++ b/libluna/src/CString.cpp @@ -219,9 +219,9 @@ extern "C" return (usize)(s - str); } - char* strtok(char* str, const char* delim) + char* strtok_r(char* str, const char* delim, char** savep) { - static char* s = nullptr; + auto& s = *savep; if (str) s = str; if (!s) return nullptr; @@ -248,6 +248,12 @@ extern "C" return nullptr; } + char* strtok(char* str, const char* delim) + { + static char* s; + return strtok_r(str, delim, &s); + } + char* strpbrk(const char* s, const char* accept) { usize index = strcspn(s, accept);