From b75bd4cd14d4e4f5bfea4df0c956e57e6039029c Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 20 May 2023 16:39:18 +0200 Subject: [PATCH] libluna: Add PathParser::has_next() --- libluna/include/luna/PathParser.h | 11 +++++++++++ libluna/src/PathParser.cpp | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/libluna/include/luna/PathParser.h b/libluna/include/luna/PathParser.h index cf6de4c0..5f66372c 100644 --- a/libluna/include/luna/PathParser.h +++ b/libluna/include/luna/PathParser.h @@ -3,6 +3,11 @@ #include #include +static inline bool is_not_delim(char c) +{ + return c && c != '/'; +} + class PathParser { public: @@ -27,6 +32,11 @@ class PathParser return is_absolute(StringView { m_original }); } + bool has_next() const + { + return m_already_called_next ? (bool)m_strtok_saved_state : is_not_delim(*m_copy); + } + Result basename(); Result dirname(); @@ -38,4 +48,5 @@ class PathParser const char* m_original { nullptr }; char* m_copy { nullptr }; bool m_already_called_next { false }; + char* m_strtok_saved_state { nullptr }; }; diff --git a/libluna/src/PathParser.cpp b/libluna/src/PathParser.cpp index 3ddd8f4c..4556ca37 100644 --- a/libluna/src/PathParser.cpp +++ b/libluna/src/PathParser.cpp @@ -27,7 +27,7 @@ PathParser::~PathParser() Option PathParser::next() { - char* result = strtok(m_already_called_next ? nullptr : m_copy, "/"); + char* result = strtok_r(m_already_called_next ? nullptr : m_copy, "/", &m_strtok_saved_state); m_already_called_next = true; if (!result) return {};