From 6375531fd2d5f94e045e5ad4bf2b1b09dcf30609 Mon Sep 17 00:00:00 2001 From: Elson Costa Date: Tue, 25 Jun 2024 23:25:46 -0300 Subject: [PATCH] improvements on getArticle function. --- .../players/cyclopedia/player_cyclopedia.cpp | 22 ++++++++----------- src/utils/tools.cpp | 22 ++++++++++++++++--- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/creatures/players/cyclopedia/player_cyclopedia.cpp b/src/creatures/players/cyclopedia/player_cyclopedia.cpp index e72363145f7..c25a54cfd74 100644 --- a/src/creatures/players/cyclopedia/player_cyclopedia.cpp +++ b/src/creatures/players/cyclopedia/player_cyclopedia.cpp @@ -51,24 +51,20 @@ void PlayerCyclopedia::loadDeathHistory(uint16_t page, uint16_t entriesPerPage) std::vector entries; entries.reserve(result->countResults()); do { - std::string cause1 = result->getString("killed_by"); - std::string cause2 = result->getString("mostdamage_by"); + std::string killed_by = result->getString("killed_by"); + std::string mostdamage_by = result->getString("mostdamage_by"); - std::ostringstream cause; - cause << "Died at Level " << result->getNumber("level") << " by"; - if (!cause1.empty()) { - cause << getArticle(cause1) << cause1; + std::string cause = fmt::format("Died at Level {}", result->getNumber("level")); + + if (!killed_by.empty()) { + cause.append(fmt::format(" by{}", getArticle(killed_by))); } - if (!cause2.empty()) { - if (!cause1.empty()) { - cause << " and"; - } - cause << getArticle(cause2) << cause2; + if (!mostdamage_by.empty()) { + cause.append(fmt::format("{}{}", !killed_by.empty() ? " and" : "", getArticle(mostdamage_by))); } - cause << '.'; - entries.emplace_back(std::move(cause.str()), result->getNumber("time")); + entries.emplace_back(cause, result->getNumber("time")); } while (result->next()); player->sendCyclopediaCharacterRecentDeaths(page, static_cast(pages), entries); }; diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index 9a3ec56f8ed..9184bbb1f70 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -1838,9 +1838,25 @@ std::string getArticle(const std::string &value, bool withSpace) { if (value.empty()) { return ""; } - const char &character = value.front(); - auto result = character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' ? "an" : "a"; - return withSpace ? fmt::format(" {} ", result) : result; + + auto removeArticle = [](const std::string &str) -> std::string { + const std::string articles[] = { "a ", "an " }; + for (const auto &article : articles) { + if (str.size() > article.size() && std::equal(article.begin(), article.end(), str.begin(), [](char a, char b) { return std::tolower(a) == std::tolower(b); })) { + return str.substr(article.size()); + } + } + return str; + }; + + std::string modifiedValue = removeArticle(value); + if (modifiedValue.empty()) { + return ""; + } + + const char &character = std::tolower(modifiedValue.front()); + auto article = character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' ? "an" : "a"; + return fmt::format("{}{} {}.", withSpace ? " " : "", article, modifiedValue); } std::vector split(const std::string &str, char delimiter /* = ','*/) {