add utils::string::join

This commit is contained in:
ConfiG 2023-08-07 01:47:14 +03:00
parent 4fe5076183
commit 82e128bb57
No known key found for this signature in database
GPG key ID: 44DA1983F524C11B
2 changed files with 51 additions and 16 deletions

View file

@ -48,6 +48,9 @@ namespace geode::utils::string {
GEODE_DLL std::vector<std::string> split(std::string const& str, std::string const& split);
GEODE_DLL std::vector<std::wstring> split(std::wstring const& str, std::wstring const& split);
GEODE_DLL std::string join(std::vector<std::string> const& strs, std::string const& separator);
GEODE_DLL std::wstring join(std::vector<std::wstring> const& strs, std::wstring const& separator);
GEODE_DLL std::vector<char> split(std::string const& str);
GEODE_DLL std::vector<wchar_t> split(std::wstring const& str);

View file

@ -127,29 +127,61 @@ std::wstring utils::string::replace(
std::vector<std::string> utils::string::split(std::string const& str, std::string const& split) {
std::vector<std::string> res;
if (str.size()) {
if (str.empty()) return res;
auto s = str;
size_t pos = 0;
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<std::wstring> utils::string::split(std::wstring const& str, std::wstring const& split) {
std::vector<std::wstring> res;
if (str.size()) {
if (str.empty()) return res;
auto s = str;
size_t pos = 0;
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<std::string> 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<std::wstring> 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;
}