From a950796306aa7d18d10e095f65a2453b770749fd Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 27 Jun 2024 22:40:19 +0200 Subject: [PATCH] fix: Recent file entry name encoding being broken --- lib/libimhex/include/hex/helpers/utils.hpp | 26 +--------------------- lib/libimhex/source/helpers/utils.cpp | 26 ++++++++++++++++++++++ plugins/builtin/source/content/recent.cpp | 4 +++- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index e87281b55..067f0a3b7 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -298,31 +298,7 @@ namespace hex { [[nodiscard]] std::optional getEnvironmentVariable(const std::string &env); - [[nodiscard]] inline std::string limitStringLength(const std::string &string, size_t maxLength) { - // If the string is shorter than the max length, return it as is - if (string.size() < maxLength) - return string; - - // If the string is longer than the max length, find the last space before the max length - auto it = string.begin() + maxLength; - while (it != string.begin() && !std::isspace(*it)) --it; - - // If there's no space before the max length, just cut the string - if (it == string.begin()) { - it = string.begin() + maxLength; - - // Try to find a UTF-8 character boundary - while (it != string.begin() && (*it & 0x80) != 0x00) --it; - ++it; - } - - // If we still didn't find a valid boundary, just return the string as is - if (it == string.begin()) - return string; - - // Append - return std::string(string.begin(), it) + "…"; - } + [[nodiscard]] std::string limitStringLength(const std::string &string, size_t maxLength); [[nodiscard]] std::optional getInitialFilePath(); diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index 59f88d468..1ac7d2ff7 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -679,6 +679,32 @@ namespace hex { return value; } + [[nodiscard]] std::string limitStringLength(const std::string &string, size_t maxLength) { + // If the string is shorter than the max length, return it as is + if (string.size() < maxLength) + return string; + + // If the string is longer than the max length, find the last space before the max length + auto it = string.begin() + maxLength; + while (it != string.begin() && !std::isspace(*it)) --it; + + // If there's no space before the max length, just cut the string + if (it == string.begin()) { + it = string.begin() + maxLength; + + // Try to find a UTF-8 character boundary + while (it != string.begin() && (*it & 0x80) == 0x00) --it; + ++it; + } + + // If we still didn't find a valid boundary, just return the string as is + if (it == string.begin()) + return string; + + // Append + return std::string(string.begin(), it) + "…"; + } + static std::optional s_fileToOpen; extern "C" void openFile(const char *path) { log::info("Opening file: {0}", path); diff --git a/plugins/builtin/source/content/recent.cpp b/plugins/builtin/source/content/recent.cpp index b5dd996cb..de4e4062b 100644 --- a/plugins/builtin/source/content/recent.cpp +++ b/plugins/builtin/source/content/recent.cpp @@ -183,7 +183,9 @@ namespace hex::plugin::builtin::recent { .entryFilePath = path, .data = jsonData }); - } catch (...) { } + } catch (const std::exception &e) { + log::error("Failed to parse recent file: {}", e.what()); + } } // Delete all recent provider files that are not in the list