From 82e128bb577b67fb7e4c32bb9f290349f15693c8 Mon Sep 17 00:00:00 2001 From: ConfiG Date: Mon, 7 Aug 2023 01:47:14 +0300 Subject: [PATCH] add utils::string::join --- loader/include/Geode/utils/string.hpp | 3 ++ loader/src/utils/string.cpp | 64 ++++++++++++++++++++------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/loader/include/Geode/utils/string.hpp b/loader/include/Geode/utils/string.hpp index 4e024e4d..ed98ce0f 100644 --- a/loader/include/Geode/utils/string.hpp +++ b/loader/include/Geode/utils/string.hpp @@ -48,6 +48,9 @@ namespace geode::utils::string { GEODE_DLL std::vector split(std::string const& str, std::string const& split); GEODE_DLL std::vector split(std::wstring const& str, std::wstring const& split); + GEODE_DLL std::string join(std::vector const& strs, std::string const& separator); + GEODE_DLL std::wstring join(std::vector const& strs, std::wstring const& separator); + GEODE_DLL std::vector split(std::string const& str); GEODE_DLL std::vector split(std::wstring const& str); diff --git a/loader/src/utils/string.cpp b/loader/src/utils/string.cpp index c839191c..4011ac87 100644 --- a/loader/src/utils/string.cpp +++ b/loader/src/utils/string.cpp @@ -127,29 +127,61 @@ std::wstring utils::string::replace( std::vector utils::string::split(std::string const& str, std::string const& split) { std::vector res; - if (str.size()) { - auto s = str; - size_t pos = 0; - while ((pos = s.find(split)) != std::string::npos) { - res.push_back(s.substr(0, pos)); - s.erase(0, pos + split.length()); - } - res.push_back(s); + if (str.empty()) return res; + auto s = str; + size_t pos; + while ((pos = s.find(split)) != std::string::npos) { + res.push_back(s.substr(0, pos)); + s.erase(0, pos + split.length()); } + res.push_back(s); return res; } std::vector utils::string::split(std::wstring const& str, std::wstring const& split) { std::vector res; - if (str.size()) { - auto s = str; - size_t pos = 0; - while ((pos = s.find(split)) != std::wstring::npos) { - res.push_back(s.substr(0, pos)); - s.erase(0, pos + split.length()); - } - res.push_back(s); + if (str.empty()) return res; + auto s = str; + size_t pos; + while ((pos = s.find(split)) != std::wstring::npos) { + res.push_back(s.substr(0, pos)); + s.erase(0, pos + split.length()); } + res.push_back(s); + return res; +} + +std::string utils::string::join(std::vector const& strs, std::string const& separator) { + std::string res; + if (strs.empty()) + return res; + if (strs.size() == 1) + return strs[0]; + // idk if less allocations but an extra loop is faster but + size_t size = 0; + for (auto const& str : strs) + size += str.size() + separator.size(); + res.reserve(size); + for (auto const& str : strs) + res += str + separator; + res.erase(res.size() - separator.size()); + return res; +} + +std::wstring utils::string::join(std::vector const& strs, std::wstring const& separator) { + std::wstring res; + if (strs.empty()) + return res; + if (strs.size() == 1) + return strs[0]; + // idk if less allocations but an extra loop is faster but + size_t size = 0; + for (auto const& str : strs) + size += str.size() + separator.size(); + res.reserve(size); + for (auto const& str : strs) + res += str + separator; + res.erase(res.size() - separator.size()); return res; }