Skip to content

Commit

Permalink
Soft upgrade do C++20
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszkax86 committed Nov 24, 2023
1 parent bc84e64 commit 48b3694
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ project(colobot
LANGUAGES C CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)

Expand Down
3 changes: 2 additions & 1 deletion colobot-base/src/app/pathman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "common/config.h"
#include "common/logger.h"
#include "common/stringutils.h"

#include "common/resources/resourcemanager.h"

Expand Down Expand Up @@ -175,7 +176,7 @@ std::vector<std::string> CPathManager::FindModsInDir(const std::string &dir) con

for(; iterator != std::filesystem::directory_iterator(); ++iterator)
{
ret.push_back(iterator->path().u8string());
ret.push_back(StrUtils::Convert(iterator->path().u8string()));
}
}
catch (std::exception &e)
Expand Down
27 changes: 15 additions & 12 deletions colobot-base/src/app/pausemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ struct ActivePause

static std::string GetPauseName(PauseType type)
{
std::vector<std::string> x;
std::string result;

if ((type & PAUSE_ENGINE) != 0) x.push_back("engine");
if ((type & PAUSE_HIDE_SHORTCUTS) != 0) x.push_back("hide_shortcuts");
if ((type & PAUSE_PHOTO) != 0) x.push_back("photo");
if ((type & PAUSE_OBJECT_UPDATES) != 0) x.push_back("object_updates");
if ((type & PAUSE_MUTE_SOUND) != 0) x.push_back("mute_sound");
if ((type & PAUSE_CAMERA) != 0) x.push_back("camera");
auto append = [&](std::string_view text)
{
if (!result.empty()) result.push_back('|');

return std::accumulate(x.cbegin(), x.cend(), std::string(),
[](auto& acc, auto& value)
{
return acc.empty() ? value : acc + "|" + value;
});
result.append(text);
};

if ((type & PAUSE_ENGINE) != 0) append("engine");
if ((type & PAUSE_HIDE_SHORTCUTS) != 0) append("hide_shortcuts");
if ((type & PAUSE_PHOTO) != 0) append("photo");
if ((type & PAUSE_OBJECT_UPDATES) != 0) append("object_updates");
if ((type & PAUSE_MUTE_SOUND) != 0) append("mute_sound");
if ((type & PAUSE_CAMERA) != 0) append("camera");

return result;
}

CPauseManager::CPauseManager()
Expand Down
5 changes: 3 additions & 2 deletions colobot-base/src/common/resources/resourcemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#endif

#include "common/logger.h"
#include "common/stringutils.h"

#include <physfs.h>

Expand Down Expand Up @@ -59,10 +60,10 @@ CResourceManager::~CResourceManager()

std::string CResourceManager::CleanPath(const std::filesystem::path& path)
{
return std::regex_replace(path.generic_u8string(), std::regex("(.*)/\\.\\./"), "");
return std::regex_replace(StrUtils::Convert(path.generic_u8string()),
std::regex("(.*)/\\.\\./"), "");
}


bool CResourceManager::AddLocation(const std::string &location, bool prepend, const std::string &mountPoint)
{
if (!PHYSFS_mount(location.c_str(), mountPoint.c_str(), prepend ? 0 : 1))
Expand Down
20 changes: 20 additions & 0 deletions colobot-common/src/common/stringutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,23 @@ std::string StrUtils::ToUpper(std::string_view text)

return result;
}

std::string StrUtils::Convert(std::u8string_view text)
{
std::string result(text.size(), '\0');

for (std::size_t i = 0; i < text.size(); i++)
result[i] = static_cast<char>(text[i]);

return result;
}

std::u8string StrUtils::Convert(std::string_view text)
{
std::u8string result(text.size(), u8'\0');

for (std::size_t i = 0; i < text.size(); i++)
result[i] = static_cast<char8_t>(text[i]);

return result;
}
6 changes: 6 additions & 0 deletions colobot-common/src/common/stringutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,10 @@ std::string ToLower(std::string_view text);
//! Returns the string with characters converted to upper case when possible
std::string ToUpper(std::string_view text);

//! Converts type of UTF-8 string to std::string
std::string Convert(std::u8string_view text);

//! Converts type of UTF-8 string to std::u8string
std::u8string Convert(std::string_view text);

} // namespace StrUtil
16 changes: 8 additions & 8 deletions test/src/common/stringutils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ TEST(StringUtilTests, SplitMultiple)

TEST(StringUtilTests, StringCodePointCounts)
{
EXPECT_EQ(StrUtils::UTF8CharLength(u8"a"), 1);
EXPECT_EQ(StrUtils::UTF8CharLength(u8"ą"), 2);
EXPECT_EQ(StrUtils::UTF8CharLength(u8""), 3);
EXPECT_EQ(StrUtils::UTF8CharLength("a"), 1);
EXPECT_EQ(StrUtils::UTF8CharLength("ą"), 2);
EXPECT_EQ(StrUtils::UTF8CharLength(""), 3);
}

TEST(StringUtilTests, StringConversion)
{
std::string text = u8",./;AaZzĄąĘę中";
std::string text = ",./;AaZzĄąĘę中";
std::u32string expected = U",./;AaZzĄąĘę中";

std::u32string unicode = StrUtils::ToUTF32(text);
Expand All @@ -153,8 +153,8 @@ TEST(StringUtilTests, StringConversion)

TEST(StringUtilTests, ToLowerTest)
{
std::string text = u8",./;AaBbĄąĘę中";
std::string expected = u8",./;aabbąąęę中";
std::string text = ",./;AaBbĄąĘę中";
std::string expected = ",./;aabbąąęę中";

auto result = StrUtils::ToLower(text);

Expand All @@ -163,8 +163,8 @@ TEST(StringUtilTests, ToLowerTest)

TEST(StringUtilTests, ToUpperTest)
{
std::string text = u8",./;AaBbĄąĘę中";
std::string expected = u8",./;AABBĄĄĘĘ中";
std::string text = ",./;AaBbĄąĘę中";
std::string expected = ",./;AABBĄĄĘĘ中";

auto result = StrUtils::ToUpper(text);

Expand Down

0 comments on commit 48b3694

Please sign in to comment.