From 2f2274e959f3452e847800d7496458360f21c341 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Mon, 5 Feb 2024 17:19:22 +0100 Subject: Updated C++ standard to C++17 C++17 should be universal enough by now. This raises the minimum CMake to 3.8, which should also pose no problem since Ubuntu 18.04 already shipped with CMake 3.10. C++17's 'if' initialization statement is now used in an efficient implementation of 'join' for vector, found on StackOverflow. --- CMakeLists.txt | 4 ++-- src/gui/itempopup.cpp | 8 +------- src/utils/stringutils.cpp | 20 ++++++++++++++++---- src/utils/stringutils.h | 12 +++++++++--- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 430fdbc0..c9798acd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 3.1...3.27) +CMAKE_MINIMUM_REQUIRED(VERSION 3.8...3.27) PROJECT(MANA) @@ -6,7 +6,7 @@ IF (NOT VERSION) SET(VERSION 0.6.1) ENDIF() -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 17) STRING(REPLACE "." " " _VERSION ${VERSION}) SEPARATE_ARGUMENTS(_VERSION) diff --git a/src/gui/itempopup.cpp b/src/gui/itempopup.cpp index 3b3dfcd3..966e2c92 100644 --- a/src/gui/itempopup.cpp +++ b/src/gui/itempopup.cpp @@ -195,13 +195,7 @@ void ItemPopup::setItem(const ItemInfo &item, bool showImage) mItemName->setPosition(getPadding() + space, getPadding()); mItemDesc->setTextWrapped(item.getDescription(), ITEMPOPUP_WRAP_WIDTH); - { - const std::vector &effect = item.getEffect(); - std::string temp = ""; - for (const auto &it : effect) - temp += temp.empty() ? it : "\n" + it; - mItemEffect->setTextWrapped(temp, ITEMPOPUP_WRAP_WIDTH); - } + mItemEffect->setTextWrapped(join(item.getEffect(), "\n"), ITEMPOPUP_WRAP_WIDTH); mItemWeight->setTextWrapped(strprintf(_("Weight: %s"), Units::formatWeight(item.getWeight()).c_str()), ITEMPOPUP_WRAP_WIDTH); diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 5083770c..57138005 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -85,7 +85,7 @@ const char *ipToString(int address) std::string strprintf(char const *format, ...) { char buf[256]; - va_list(args); + va_list args; va_start(args, format); int nb = vsnprintf(buf, 256, format, args); va_end(args); @@ -191,7 +191,7 @@ bool getBoolFromString(const std::string &text, bool def) return def; } -std::string autocomplete(std::vector &candidates, +std::string autocomplete(const std::vector &candidates, std::string base) { auto i = candidates.begin(); @@ -232,7 +232,7 @@ std::string normalize(const std::string &name) return toLower(trim(normalized)); } -std::string removeTrailingSymbol(const std::string& s, const char c) +std::string removeTrailingSymbol(const std::string &s, const char c) { // Remove the trailing symblol at the end of the string if (!s.empty() && s.at(s.size() - 1) == c) @@ -240,7 +240,7 @@ std::string removeTrailingSymbol(const std::string& s, const char c) return std::string(s); } -std::string getHostNameFromURL(const std::string& url) +std::string getHostNameFromURL(const std::string &url) { std::string myHostName; @@ -276,3 +276,15 @@ std::string getHostNameFromURL(const std::string& url) return myHostName; } + +std::string join(const std::vector &strings, const char *separator) +{ + std::string result; + if (auto i = strings.begin(), e = strings.end(); i != e) + { + result += *i++; + for (; i != e; ++i) + result.append(separator).append(*i); + } + return result; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index b83bc580..cd80b60f 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -141,7 +141,7 @@ bool getBoolFromString(const std::string &text, bool def = false); /** * Returns the most approaching string of base from candidates. */ -std::string autocomplete(std::vector &candidates, +std::string autocomplete(const std::vector &candidates, std::string base); /** @@ -152,12 +152,18 @@ std::string normalize(const std::string &name); /** * Remove a potential trailing symbol from a string. */ -std::string removeTrailingSymbol(const std::string& s, const char c); +std::string removeTrailingSymbol(const std::string &s, const char c); /** * Gets the hostname out of the given URL. * i.e.: http://www.manasource.org:9601 -> www.manasource.org */ -std::string getHostNameFromURL(const std::string& url); +std::string getHostNameFromURL(const std::string &url); + +/** + * Joins a vector of strings into one string, separated by the given + * separator. + */ +std::string join(const std::vector &strings, const char *separator); #endif // UTILS_STRINGUTILS_H -- cgit v1.2.3-70-g09d2