From 471b798eb03b361cf4275db1ce24415d2efd2e7f Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 10 Jan 2022 21:44:44 +0100 Subject: LibC: Implement strsep() --- Userland/Libraries/LibC/string.cpp | 17 +++++++++++++++++ Userland/Libraries/LibC/string.h | 1 + 2 files changed, 18 insertions(+) (limited to 'Userland') diff --git a/Userland/Libraries/LibC/string.cpp b/Userland/Libraries/LibC/string.cpp index 914ad00e5b..20c6bc8294 100644 --- a/Userland/Libraries/LibC/string.cpp +++ b/Userland/Libraries/LibC/string.cpp @@ -448,6 +448,23 @@ size_t strxfrm(char* dest, const char* src, size_t n) return i; } +// Not in POSIX, originated in BSD but also supported on Linux. +// https://man.openbsd.org/strsep.3 +char* strsep(char** str, char const* delim) +{ + if (*str == nullptr) + return nullptr; + auto* begin = *str; + auto* end = begin + strcspn(begin, delim); + if (*end) { + *end = '\0'; + *str = ++end; + } else { + *str = nullptr; + } + return begin; +} + void explicit_bzero(void* ptr, size_t size) { secure_zero(ptr, size); diff --git a/Userland/Libraries/LibC/string.h b/Userland/Libraries/LibC/string.h index 71ffb914b2..b1052cf0c4 100644 --- a/Userland/Libraries/LibC/string.h +++ b/Userland/Libraries/LibC/string.h @@ -59,5 +59,6 @@ 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); +char* strsep(char** str, char const* delim); __END_DECLS -- cgit v1.2.3