mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-23 14:54:30 +01:00
Improve String::Split and add tests
This commit is contained in:
@@ -113,6 +113,9 @@ typedef uint8 colour_t;
|
||||
#endif // __GNUC__
|
||||
#endif // __cplusplus
|
||||
|
||||
// Gets the name of a symbol as a C string
|
||||
#define nameof(symbol) #symbol
|
||||
|
||||
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
||||
#include <unistd.h>
|
||||
#define STUB() log_warning("Function %s at %s:%d is a stub.\n", __PRETTY_FUNCTION__, __FILE__, __LINE__)
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
value = s.substr(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = s.substr(index, nextIndex - index);
|
||||
}
|
||||
results.push_back(value);
|
||||
index = nextIndex + 1;
|
||||
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
|
||||
{
|
||||
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();
|
||||
}
|
||||
while (nextIndex != SIZE_MAX);
|
||||
}
|
||||
while (nextIndex != SIZE_MAX);
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user