From ecb789d059a5ff9ce539785f71201b387a9068f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Wed, 9 Aug 2023 20:11:08 +0200 Subject: [PATCH] Replaced boost::trim functions with new Trim functions Changes based on https://github.com/osiox/colobot/commit/f0f6f61cabbc9269c83d74974bb24c2933b4bde4 --- src/app/modman.cpp | 1 - src/app/pathman.cpp | 2 -- src/common/stringutils.cpp | 20 ++++++++++++++++++++ src/common/stringutils.h | 8 ++++++++ src/graphics/model/model_mod.cpp | 9 +++++---- src/graphics/model/model_txt.cpp | 5 +++-- src/level/parser/parser.cpp | 13 ++++++------- src/level/parser/parserparam.cpp | 2 +- src/ui/screen/screen_player_select.cpp | 6 ++---- 9 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/app/modman.cpp b/src/app/modman.cpp index 4498011b..4fe3fdac 100644 --- a/src/app/modman.cpp +++ b/src/app/modman.cpp @@ -35,7 +35,6 @@ #include #include #include -#include CModManager::CModManager(CApplication* app, CPathManager* pathManager) : m_app{app}, diff --git a/src/app/pathman.cpp b/src/app/pathman.cpp index d093984a..511fa55d 100644 --- a/src/app/pathman.cpp +++ b/src/app/pathman.cpp @@ -32,8 +32,6 @@ #include "common/system/system_windows.h" #endif -#include - #include CPathManager::CPathManager(CSystemUtils* systemUtils) diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp index b6af4e89..8ce69cbb 100644 --- a/src/common/stringutils.cpp +++ b/src/common/stringutils.cpp @@ -84,6 +84,26 @@ std::string StrUtils::Replace(const std::string &str, const std::string &oldStr, return result; } +void StrUtils::TrimLeft(std::string& s) +{ + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); +} + +void StrUtils::TrimRight(std::string& s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), s.end()); +} + +void StrUtils::Trim(std::string& str) +{ + TrimLeft(str); + TrimRight(str); +} + std::string StrUtils::UnicodeCharToUtf8(unsigned int ch) { std::string result; diff --git a/src/common/stringutils.h b/src/common/stringutils.h index 3be59683..63e10698 100644 --- a/src/common/stringutils.h +++ b/src/common/stringutils.h @@ -68,6 +68,14 @@ 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); +//! Remove whitespace from the beginning of the given string (in place) +void TrimLeft(std::string& str); + +//! Remove whitespace from the end of the given string (in place) +void TrimRight(std::string& str); + +//! Remove whitespace from both ends of the given string (in place) +void Trim(std::string& str); //! Converts a wide Unicode char to a single UTF-8 encoded char std::string UnicodeCharToUtf8(unsigned int ch); diff --git a/src/graphics/model/model_mod.cpp b/src/graphics/model/model_mod.cpp index 612ebee7..41322c40 100644 --- a/src/graphics/model/model_mod.cpp +++ b/src/graphics/model/model_mod.cpp @@ -20,13 +20,12 @@ #include "graphics/model/model_mod.h" #include "common/ioutils.h" +#include "common/stringutils.h" #include "common/resources/inputstream.h" #include "graphics/model/model_io_exception.h" #include "graphics/model/model_io_structs.h" -#include - #include #include @@ -279,8 +278,10 @@ ModelLODLevel MinMaxToLodLevel(float min, float max) void ConvertOldTex1Name(ModelTriangle& triangle, const char* tex1Name) { triangle.material.albedoTexture = tex1Name; - boost::replace_all(triangle.material.albedoTexture, "bmp", "png"); - boost::replace_all(triangle.material.albedoTexture, "tga", "png"); + triangle.material.albedoTexture = StrUtils::Replace( + triangle.material.albedoTexture, "bmp", "png"); + triangle.material.albedoTexture = StrUtils::Replace( + triangle.material.albedoTexture, "tga", "png"); } void ConvertFromOldRenderState(ModelTriangle& triangle, int state) diff --git a/src/graphics/model/model_txt.cpp b/src/graphics/model/model_txt.cpp index e574c78e..899d5313 100644 --- a/src/graphics/model/model_txt.cpp +++ b/src/graphics/model/model_txt.cpp @@ -20,6 +20,7 @@ #include "graphics/model/model_mod.h" #include "common/ioutils.h" +#include "common/stringutils.h" #include "common/resources/inputstream.h" #include "graphics/model/model_io_exception.h" @@ -255,7 +256,7 @@ std::string ReadLineString(std::istream& stream, const std::string& expectedPref throw CModelIOException("Unexpected EOF"); std::getline(stream, line); - boost::trim_right(line); + StrUtils::TrimRight(line); if (!line.empty() && line[0] != '#') break; } @@ -270,7 +271,7 @@ std::string ReadLineString(std::istream& stream, const std::string& expectedPref std::string value; std::getline(s, value); - boost::trim_left(value); + StrUtils::TrimLeft(value); return value; } diff --git a/src/level/parser/parser.cpp b/src/level/parser/parser.cpp index 397eec6b..c79519d5 100644 --- a/src/level/parser/parser.cpp +++ b/src/level/parser/parser.cpp @@ -38,7 +38,6 @@ #include #include -#include #include CLevelParser::CLevelParser() @@ -190,14 +189,14 @@ void CLevelParser::Load() } } - boost::algorithm::trim(line); + StrUtils::Trim(line); pos = line.find_first_of(" \t\n"); std::string command = line.substr(0, pos); if (pos != std::string::npos) { line = line.substr(pos + 1); - boost::algorithm::trim(line); + StrUtils::Trim(line); } else { @@ -246,9 +245,9 @@ void CLevelParser::Load() { pos = line.find_first_of("="); std::string paramName = line.substr(0, pos); - boost::algorithm::trim(paramName); + StrUtils::Trim(paramName); line = line.substr(pos + 1); - boost::algorithm::trim(line); + StrUtils::Trim(line); if (line[0] == '\"') { @@ -277,14 +276,14 @@ void CLevelParser::Load() } } std::string paramValue = line.substr(0, pos + 1); - boost::algorithm::trim(paramValue); + StrUtils::Trim(paramValue); parserLine->AddParam(paramName, std::make_unique(paramName, paramValue)); if (pos == std::string::npos) break; line = line.substr(pos + 1); - boost::algorithm::trim(line); + StrUtils::Trim(line); } if (parserLine->GetCommand().length() > 1 && parserLine->GetCommand()[0] == '#') diff --git a/src/level/parser/parserparam.cpp b/src/level/parser/parserparam.cpp index b4971027..5be56197 100644 --- a/src/level/parser/parserparam.cpp +++ b/src/level/parser/parserparam.cpp @@ -1115,7 +1115,7 @@ void CLevelParserParam::ParseArray() int i = 0; for (auto& value : values) { - boost::algorithm::trim(value); + StrUtils::Trim(value); if (value.empty()) continue; auto param = std::make_unique(m_name + "[" + StrUtils::ToString(i) + "]", value); param->SetLine(m_line); diff --git a/src/ui/screen/screen_player_select.cpp b/src/ui/screen/screen_player_select.cpp index 9c0cbddb..8dc3d9da 100644 --- a/src/ui/screen/screen_player_select.cpp +++ b/src/ui/screen/screen_player_select.cpp @@ -40,8 +40,6 @@ #include "ui/controls/list.h" #include "ui/controls/window.h" -#include - namespace Ui { @@ -247,7 +245,7 @@ void CScreenPlayerSelect::UpdateNameControl() total = pl->GetTotal(); sel = pl->GetSelect(); name = pe->GetText(100); - boost::trim(name); + StrUtils::Trim(name); pb = static_cast(pw->SearchControl(EVENT_INTERFACE_NDELETE)); if ( pb != nullptr ) @@ -382,7 +380,7 @@ bool CScreenPlayerSelect::NameCreate() std::string name; name = pe->GetText(100); - boost::trim(name); + StrUtils::Trim(name); if ( name.empty() ) { m_sound->Play(SOUND_TZOING);