From c1dbafc101694107fd518ad8b7382dbae2d548c4 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 30 Mar 2023 16:35:38 +0300 Subject: [PATCH] BAN: StringView::split(char, bool) is now thread safe I had to duplicate some code, but this is better since I would like to not use BAN::Function for something like this --- BAN/BAN/StringView.cpp | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/BAN/BAN/StringView.cpp b/BAN/BAN/StringView.cpp index 213fa728..54fd1f9d 100644 --- a/BAN/BAN/StringView.cpp +++ b/BAN/BAN/StringView.cpp @@ -63,9 +63,38 @@ namespace BAN ErrorOr> StringView::split(char delim, bool allow_empties) { - // FIXME: Won't work while multithreading - static char s_delim = delim; - return split([](char c){ return c == s_delim; }, allow_empties); + size_type count = 0; + { + size_type start = 0; + for (size_type i = 0; i < m_size; i++) + { + if (m_data[i] == delim) + { + if (allow_empties || start != i) + count++; + start = i + 1; + } + } + if (start != m_size) + count++; + } + + Vector result; + TRY(result.reserve(count)); + + size_type start = 0; + for (size_type i = 0; i < m_size; i++) + { + if (m_data[i] == delim) + { + if (allow_empties || start != i) + TRY(result.push_back(this->substring(start, i - start))); + start = i + 1; + } + } + if (start != m_size) + TRY(result.push_back(this->substring(start))); + return result; } ErrorOr> StringView::split(bool(*comp)(char), bool allow_empties)