diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index 702fe0e57..975fdee60 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -362,7 +362,7 @@ namespace hex { [[nodiscard]] std::optional getEnvironmentVariable(const std::string &env); - [[nodiscard]] std::string limitStringLength(const std::string &string, size_t maxLength); + [[nodiscard]] std::string limitStringLength(const std::string &string, size_t maxLength, bool fromBothEnds = true); [[nodiscard]] std::optional getInitialFilePath(); diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index ba1840460..8b0941e46 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -601,13 +601,13 @@ namespace hex { return value; } - [[nodiscard]] std::string limitStringLength(const std::string &string, size_t maxLength) { + [[nodiscard]] std::string limitStringLength(const std::string &string, size_t maxLength, bool fromBothEnds) { // 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 / 2; + auto it = string.begin() + (fromBothEnds ? maxLength / 2 : maxLength); while (it != string.begin() && !std::isspace(*it)) --it; // If there's no space before the max length, just cut the string @@ -624,6 +624,9 @@ namespace hex { auto result = std::string(string.begin(), it) + "…"; + if (!fromBothEnds) + return result; + // If the string is longer than the max length, find the last space before the max length it = string.end() - 1 - maxLength / 2; while (it != string.end() && !std::isspace(*it)) ++it; diff --git a/plugins/builtin/source/content/data_inspector.cpp b/plugins/builtin/source/content/data_inspector.cpp index d75ba7d62..8f566b8cc 100644 --- a/plugins/builtin/source/content/data_inspector.cpp +++ b/plugins/builtin/source/content/data_inspector.cpp @@ -524,7 +524,7 @@ namespace hex::plugin::builtin { } ); - constexpr static auto MaxStringLength = 32; + constexpr static auto MaxStringLength = 64; ContentRegistry::DataInspector::add("hex.builtin.inspector.string", 1, [](auto buffer, auto endian, auto style) { @@ -541,10 +541,8 @@ namespace hex::plugin::builtin { value = copyValue = hex::encodeByteString(stringBuffer); - if (value.size() > MaxStringLength) { - value.resize(MaxStringLength); - value += "..."; - } + copyValue = value; + value = hex::limitStringLength(value, MaxStringLength, false); } else { value = ""; copyValue = ""; @@ -570,21 +568,18 @@ namespace hex::plugin::builtin { std::string value, copyValue; if (currSelection.has_value()) { - std::wstring stringBuffer(std::min(currSelection->size * sizeof(wchar_t), 0x1000), 0x00); + std::wstring stringBuffer(std::min(alignTo(currSelection->size, sizeof(wchar_t)), 0x1000), 0x00); ImHexApi::Provider::get()->read(currSelection->address, stringBuffer.data(), stringBuffer.size()); for (auto &c : stringBuffer) c = hex::changeEndianness(c, endian); - std::erase_if(buffer, [](auto c) { return c == 0x00; }); + std::erase_if(stringBuffer, [](auto c) { return c == 0x00; }); auto string = wolv::util::wstringToUtf8(stringBuffer).value_or("Invalid"); - value = copyValue = hex::encodeByteString({ string.begin(), string.end() }); - if (value.size() > MaxStringLength) { - value.resize(MaxStringLength); - value += "..."; - } + copyValue = string; + value = hex::limitStringLength(string, MaxStringLength, false); } else { value = ""; copyValue = ""; @@ -610,21 +605,18 @@ namespace hex::plugin::builtin { std::string value, copyValue; if (currSelection.has_value()) { - std::u16string stringBuffer(std::min(currSelection->size * sizeof(char16_t), 0x1000), 0x00); + std::u16string stringBuffer(std::min(alignTo(currSelection->size, sizeof(char16_t)), 0x1000), 0x00); ImHexApi::Provider::get()->read(currSelection->address, stringBuffer.data(), stringBuffer.size()); for (auto &c : stringBuffer) c = hex::changeEndianness(c, endian); - std::erase_if(buffer, [](auto c) { return c == 0x00; }); + std::erase_if(stringBuffer, [](auto c) { return c == 0x00; }); auto string = wolv::util::utf16ToUtf8(stringBuffer).value_or("Invalid"); - value = copyValue = hex::encodeByteString({ string.begin(), string.end() }); - if (value.size() > MaxStringLength) { - value.resize(MaxStringLength); - value += "..."; - } + copyValue = string; + value = hex::limitStringLength(string, MaxStringLength, false); } else { value = ""; copyValue = ""; @@ -650,21 +642,18 @@ namespace hex::plugin::builtin { std::string value, copyValue; if (currSelection.has_value()) { - std::u32string stringBuffer(std::min(currSelection->size * sizeof(char32_t), 0x1000), 0x00); + std::u32string stringBuffer(std::min(alignTo(currSelection->size, sizeof(char32_t)), 0x1000), 0x00); ImHexApi::Provider::get()->read(currSelection->address, stringBuffer.data(), stringBuffer.size()); for (auto &c : stringBuffer) c = hex::changeEndianness(c, endian); - std::erase_if(buffer, [](auto c) { return c == 0x00; }); + std::erase_if(stringBuffer, [](auto c) { return c == 0x00; }); auto string = wolv::util::utf32ToUtf8(stringBuffer).value_or("Invalid"); - value = copyValue = hex::encodeByteString({ string.begin(), string.end() }); - if (value.size() > MaxStringLength) { - value.resize(MaxStringLength); - value += "..."; - } + copyValue = string; + value = hex::limitStringLength(string, MaxStringLength, false); } else { value = ""; copyValue = "";