From c9da6b9a0c4149466e3034f0207b19e7bb84e03d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 Mar 2024 13:31:41 +0300 Subject: [PATCH] stringutil: text case convert functions --- src/content/ContentLoader.cpp | 1 - src/logic/PlayerController.cpp | 3 +-- src/util/stringutil.cpp | 43 ++++++++++++++++++++++++++++++++++ src/util/stringutil.h | 5 ++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 146cb695..2ca5998e 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -104,7 +104,6 @@ void ContentLoader::fixPackIndices() { } } -// TODO: add basic validation and logging void ContentLoader::loadBlock(Block& def, std::string name, fs::path file) { auto root = files::read_json(file); diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 4c5ad80a..99e76f19 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -331,6 +331,7 @@ static void pick_block(ContentIndices* indices, Chunks* chunks, Player* player, } } +// TODO: refactor this nesting nest void PlayerController::updateInteraction(){ auto indices = level->content->getIndices(); Chunks* chunks = level->chunks.get(); @@ -374,7 +375,6 @@ void PlayerController::updateInteraction(){ uint8_t states = determine_rotation(def, norm, camera->dir); if (lclick && !input.shift && item->rt.funcsset.on_block_break_by) { - // TODO: move scripting to interaction callbacks if (scripting::on_item_break_block(player.get(), item, x, y, z)) return; } @@ -428,7 +428,6 @@ void PlayerController::updateInteraction(){ chunks->set(x, y, z, chosenBlock, states); lighting->onBlockSet(x,y,z, chosenBlock); if (def->rt.funcsset.onplaced) { - // TODO: move scripting to interaction callbacks scripting::on_block_placed(player.get(), def, x, y, z); } blocksController->updateSides(x, y, z); diff --git a/src/util/stringutil.cpp b/src/util/stringutil.cpp index 7ab4e6e8..39afa87e 100644 --- a/src/util/stringutil.cpp +++ b/src/util/stringutil.cpp @@ -292,6 +292,49 @@ double util::parse_double(const std::string& str, size_t offset, size_t len) { return parse_double(str.substr(offset, len)); } +std::wstring util::lower_case(const std::wstring& str) { + std::wstring result = str; + static const std::locale loc(""); + for (uint i = 0; i < result.length(); i++) { + result[i] = static_cast(std::tolower(str[i], loc)); + } + return result; +} + +std::wstring util::upper_case(const std::wstring& str) { + std::wstring result = str; + static const std::locale loc(""); + for (uint i = 0; i < result.length(); i++) { + result[i] = static_cast(std::toupper(str[i], loc)); + } + return result; +} + +std::wstring util::capitalized(const std::wstring& str) { + if (str.empty()) + return str; + static const std::locale loc(""); + return std::wstring({static_cast(std::toupper(str[0], loc))}) + str.substr(1); +} + +std::wstring util::pascal_case(const std::wstring& str) { + if (str.empty()) + return str; + static const std::locale loc(""); + std::wstring result = str; + bool upper = true; + for (uint i = 0; i < result.length(); i++) { + auto c = result[i]; + if (c <= ' ') { + upper = true; + } else if (upper) { + result[i] = static_cast(std::toupper(str[i], loc)); + upper = false; + } + } + return result; +} + /// @brief Split string by delimiter /// @param str source string /// @param delimiter split delimiter size diff --git a/src/util/stringutil.h b/src/util/stringutil.h index de43de15..3ab83092 100644 --- a/src/util/stringutil.h +++ b/src/util/stringutil.h @@ -34,6 +34,11 @@ namespace util { extern double parse_double(const std::string& str); extern double parse_double(const std::string& str, size_t offset, size_t len); + extern std::wstring lower_case(const std::wstring& str); + extern std::wstring upper_case(const std::wstring& str); + extern std::wstring capitalized(const std::wstring& str); + extern std::wstring pascal_case(const std::wstring& str); + extern std::vector split(const std::string& str, char delimiter); extern std::vector split(const std::wstring& str, char delimiter); }