Improve String::Split and add tests

This commit is contained in:
Ted John committed 2017-03-08 19:06:42 +00:00
1 parent 7ccce750a6
commit 0ec43e3611
5 files changed
+74 -15

No files matched your search

+24 -15
View File
@@ -15,6 +15,7 @@
#pragma endregion
#include <cwctype>
#include <stdexcept>
#include <vector>
extern "C"
@@ -356,25 +357,33 @@ namespace String
std::vector<std::string> Split(const std::string &s, const std::string &delimiter)
{
std::vector<std::string> results;
size_t index = 0;
size_t nextIndex;
do
if (delimiter.empty())
{
nextIndex = s.find_first_of(delimiter, index);
std::string value;
if (nextIndex == std::string::npos)
throw std::invalid_argument(nameof(delimiter) " can not be empty.");
}
std::vector<std::string> results;
if (!s.empty())
{
size_t index = 0;
size_t nextIndex;
do
{
value = s.substr(index);
nextIndex = s.find(delimiter, index);
std::string value;
if (nextIndex == std::string::npos)
{
value = s.substr(index);
}
else
{
value = s.substr(index, nextIndex - index);
}
results.push_back(value);
index = nextIndex + delimiter.size();
}
else
{
value = s.substr(index, nextIndex - index);
}
results.push_back(value);
index = nextIndex + 1;
while (nextIndex != SIZE_MAX);
}
while (nextIndex != SIZE_MAX);
return results;
}