Added StrUtils::Split() and replaced boost::split()

dev
Tomasz Kapuściński 2023-08-15 02:41:40 +02:00
parent dccacd6352
commit b296ee4d47
6 changed files with 69 additions and 8 deletions

View File

@ -89,6 +89,36 @@ std::string StrUtils::Replace(const std::string &str, const std::string &oldStr,
return result;
}
std::vector<std::string> StrUtils::Split(const std::string& text, std::string_view separators)
{
std::string_view stream = text;
std::vector<std::string> result;
std::string part;
while (!stream.empty())
{
char c = stream.front();
stream.remove_prefix(1);
if (separators.find(c) != std::string::npos)
{
if (!part.empty())
{
result.push_back(part);
part.clear();
}
continue;
}
part += c;
}
if (!part.empty()) result.push_back(part);
return result;
}
void StrUtils::TrimLeft(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {

View File

@ -25,8 +25,10 @@
#pragma once
#include <cstddef>
#include <string>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
namespace StrUtils
{
@ -68,6 +70,9 @@ std::string Format(const char *fmt, ...);
//! Returns a string with every occurence of \a oldStr in \a str replaced to \a newStr
std::string Replace(const std::string &str, const std::string &oldStr, const std::string &newStr);
//! Splits a string into parts using character separators
std::vector<std::string> Split(const std::string& text, std::string_view separators);
//! Remove whitespace from the beginning of the given string (in place)
void TrimLeft(std::string& str);

View File

@ -1110,8 +1110,7 @@ void CLevelParserParam::ParseArray()
if (m_array.size() != 0)
return;
std::vector<std::string> values;
boost::split(values, m_value, boost::is_any_of(";"));
std::vector<std::string> values = StrUtils::Split(m_value, ";");
int i = 0;
for (auto& value : values)
{

View File

@ -5286,7 +5286,7 @@ Error CRobotMain::ProcessEndMissionTake()
{
GetLogger()->Info("Team %d won\n", team);
m_displayText->DisplayText(("<<< Team "+boost::lexical_cast<std::string>(team)+" won the game >>>").c_str(), glm::vec3(0.0f,0.0f,0.0f));
m_displayText->DisplayText(("<<< Team "+std::to_string(team)+" won the game >>>").c_str(), glm::vec3(0.0f,0.0f,0.0f));
if (m_missionTimerEnabled && m_missionTimerStarted)
{
GetLogger()->Info("Mission time: %s\n", TimeFormat(m_missionTimer).c_str());

View File

@ -1420,8 +1420,7 @@ int CEdit::GetTextLength()
static std::string GetNameParam(std::string cmd, int rank)
{
std::vector<std::string> results;
boost::split(results, cmd, boost::is_any_of(" ;"));
std::vector<std::string> results = StrUtils::Split(cmd, " ;");
if (results.size() > static_cast<unsigned int>(rank))
{
@ -1436,8 +1435,8 @@ static std::string GetNameParam(std::string cmd, int rank)
static int GetValueParam(std::string cmd, int rank)
{
std::vector<std::string> results;
boost::split(results, cmd, boost::is_any_of(" ;"));
std::vector<std::string> results = StrUtils::Split(cmd, " ;");
int return_value = 0;
if (results.size() > static_cast<unsigned int>(rank))

View File

@ -54,6 +54,34 @@ TEST(StringUtilTests, ReplaceSameLength)
EXPECT_EQ(result, expected);
}
TEST(StringUtilTests, SplitSingle)
{
std::string text = "CreateObject test value 123";
auto result = StrUtils::Split(text, " ");
ASSERT_EQ(result.size(), 4);
EXPECT_EQ(result[0], "CreateObject");
EXPECT_EQ(result[1], "test");
EXPECT_EQ(result[2], "value");
EXPECT_EQ(result[3], "123");
}
TEST(StringUtilTests, SplitMultiple)
{
std::string text = "id=123, value=123,test=#45 no=yes";
auto result = StrUtils::Split(text, " ,");
ASSERT_EQ(result.size(), 4);
EXPECT_EQ(result[0], "id=123");
EXPECT_EQ(result[1], "value=123");
EXPECT_EQ(result[2], "test=#45");
EXPECT_EQ(result[3], "no=yes");
}
TEST(StringUtilTests, StringCodePointCounts)
{
EXPECT_EQ(StrUtils::Utf8CharSizeAt("a", 0), 1);