From e85216d9a6b15d54a7a556bc2d049c4a60cb6be3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 26 Jul 2025 13:35:08 +0200 Subject: [PATCH] impr: Remove duplicated string functions and use libwolv ones instead --- lib/external/libwolv | 2 +- lib/libimhex/include/hex/helpers/utils.hpp | 4 -- lib/libimhex/source/helpers/encoding_file.cpp | 2 +- lib/libimhex/source/helpers/utils.cpp | 47 ++----------------- .../text_highlighting/pattern_language.cpp | 34 +++++++------- .../source/content/views/view_bookmarks.cpp | 2 +- .../content/views/view_pattern_editor.cpp | 6 +-- 7 files changed, 28 insertions(+), 69 deletions(-) diff --git a/lib/external/libwolv b/lib/external/libwolv index 2a7fdc9b7..50f646b06 160000 --- a/lib/external/libwolv +++ b/lib/external/libwolv @@ -1 +1 @@ -Subproject commit 2a7fdc9b75bb4f0c3aac3d54d58a746adefc56f9 +Subproject commit 50f646b064763765774a34297d8a91a840d91175 diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index d6b909962..88a1e2b56 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -252,10 +252,6 @@ namespace hex { return result; } - [[nodiscard]] std::vector splitString(const std::string &string, const std::string &delimiter); - [[nodiscard]] std::string combineStrings(const std::vector &strings, const std::string &delimiter = ""); - [[nodiscard]] std::string replaceStrings(std::string string, const std::string &search, const std::string &replace); - [[nodiscard]] std::string toEngineeringString(double value); [[nodiscard]] inline std::vector parseByteString(const std::string &string) { diff --git a/lib/libimhex/source/helpers/encoding_file.cpp b/lib/libimhex/source/helpers/encoding_file.cpp index 24b4ec8ea..f597062a9 100644 --- a/lib/libimhex/source/helpers/encoding_file.cpp +++ b/lib/libimhex/source/helpers/encoding_file.cpp @@ -118,7 +118,7 @@ namespace hex { void EncodingFile::parse(const std::string &content) { m_tableContent = content; - for (const auto &line : splitString(m_tableContent, "\n")) { + for (const auto &line : wolv::util::splitString(m_tableContent, "\n")) { std::string from, to; { diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index 9450ceba9..9bf88170e 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -13,6 +13,8 @@ #include #include +#include + #if defined(OS_WINDOWS) #include #include @@ -99,9 +101,9 @@ namespace hex { return { }; // Remove common hex prefixes and commas - string = hex::replaceStrings(string, "0x", ""); - string = hex::replaceStrings(string, "0X", ""); - string = hex::replaceStrings(string, ",", ""); + string = wolv::util::replaceStrings(string, "0x", ""); + string = wolv::util::replaceStrings(string, "0X", ""); + string = wolv::util::replaceStrings(string, ",", ""); // Check for non-hex characters bool isValidHexString = std::find_if(string.begin(), string.end(), [](char c) { @@ -279,45 +281,6 @@ namespace hex { } } - std::vector splitString(const std::string &string, const std::string &delimiter) { - size_t start = 0, end = 0; - std::vector res; - - while ((end = string.find(delimiter, start)) != std::string::npos) { - size_t size = end - start; - if (start + size > string.length()) - break; - - std::string token = string.substr(start, end - start); - start = end + delimiter.length(); - res.push_back(token); - } - - res.emplace_back(string.substr(start)); - return res; - } - - std::string combineStrings(const std::vector &strings, const std::string &delimiter) { - std::string result; - for (const auto &string : strings) { - result += string; - result += delimiter; - } - - return result.substr(0, result.length() - delimiter.length()); - } - - std::string replaceStrings(std::string string, const std::string &search, const std::string &replace) { - if (search.empty()) - return string; - - std::size_t pos; - while ((pos = string.find(search)) != std::string::npos) - string.replace(pos, search.size(), replace); - - return string; - } - std::string toEngineeringString(double value) { constexpr static std::array Suffixes = { "a", "f", "p", "n", "u", "m", "", "k", "M", "G", "T", "P", "E" }; diff --git a/plugins/builtin/source/content/text_highlighting/pattern_language.cpp b/plugins/builtin/source/content/text_highlighting/pattern_language.cpp index b01b92961..bd288c811 100644 --- a/plugins/builtin/source/content/text_highlighting/pattern_language.cpp +++ b/plugins/builtin/source/content/text_highlighting/pattern_language.cpp @@ -117,11 +117,11 @@ namespace hex::plugin::builtin { return true; std::vector vectorString; if (identifierName.contains("::")) { - vectorString = hex::splitString(identifierName, "::"); + vectorString = wolv::util::splitString(identifierName, "::"); if (vectorString.size() > 1) { shortName = vectorString.back(); vectorString.pop_back(); - identifierName = hex::combineStrings(vectorString, "::"); + identifierName = wolv::util::combineStrings(vectorString, "::"); } } bool found = true; @@ -415,7 +415,7 @@ namespace hex::plugin::builtin { separator = "::"; else separator = "."; - auto vectorString = hex::splitString(identifierName, separator); + auto vectorString = wolv::util::splitString(identifierName, separator); std::string nameSpace; u32 index = 0; @@ -623,7 +623,7 @@ namespace hex::plugin::builtin { if (!getQualifiedName(variableName, identifiers, true)) return false; - auto vectorString = hex::splitString(variableName, "."); + auto vectorString = wolv::util::splitString(variableName, "."); u32 index = 0; @@ -742,7 +742,7 @@ namespace hex::plugin::builtin { if (!getQualifiedName(identifierName, identifiers, true)) return false; auto tokenCount = m_tokens.size(); - auto vectorString = hex::splitString(identifierName, "::"); + auto vectorString = wolv::util::splitString(identifierName, "::"); auto vectorStringCount = vectorString.size(); if (identifiers.size() != vectorStringCount) return false; @@ -917,7 +917,7 @@ namespace hex::plugin::builtin { functionName = namespaceName + "::" + functionName; } else { - auto vectorString = hex::splitString(functionName, "::"); + auto vectorString = wolv::util::splitString(functionName, "::"); vectorString.pop_back(); for (auto nameSpace: vectorString) { @@ -987,7 +987,7 @@ namespace hex::plugin::builtin { if (optionalFullName.empty()) forwardIdentifierName(fullName, identifiers); - auto nameParts = hex::splitString(fullName, "."); + auto nameParts = wolv::util::splitString(fullName, "."); std::vector grandpaTypes; findParentTypes(parentTypes); @@ -1013,7 +1013,7 @@ namespace hex::plugin::builtin { nameParts.insert(nameParts.begin(), currentName); identifiers.insert(identifiers.begin(), identifier); - optionalFullName = hex::combineStrings(nameParts, "."); + optionalFullName = wolv::util::combineStrings(nameParts, "."); return true; } @@ -1080,7 +1080,7 @@ namespace hex::plugin::builtin { std::optional &result, std::vector &identifiers) { - auto vectorString = hex::splitString(variableName, "."); + auto vectorString = wolv::util::splitString(variableName, "."); auto count = vectorString.size(); auto UDTName = parentType; auto currentName = vectorString[0]; @@ -1097,7 +1097,7 @@ namespace hex::plugin::builtin { } vectorString.erase(vectorString.begin()); - variableName = hex::combineStrings(vectorString, "."); + variableName = wolv::util::combineStrings(vectorString, "."); Identifier *identifier = identifiers[0]; identifiers.erase(identifiers.begin()); skipArray(200, true); @@ -1135,10 +1135,10 @@ namespace hex::plugin::builtin { forwardIdentifierName(fullName, identifiers); std::vector parentTypes; - auto vectorString = hex::splitString(fullName, "."); + auto vectorString = wolv::util::splitString(fullName, "."); if (vectorString[0] == "Parent") { vectorString.erase(vectorString.begin()); - fullName = hex::combineStrings(vectorString, "."); + fullName = wolv::util::combineStrings(vectorString, "."); identifiers.erase(identifiers.begin()); if (!findAllParentTypes(parentTypes, identifiers, fullName)) { m_curr = curr; @@ -1223,7 +1223,7 @@ namespace hex::plugin::builtin { // Render the compilation errors using squiggly lines void TextHighlighter::renderErrors() { const auto processMessage = [](const auto &message) { - auto lines = hex::splitString(message, "\n"); + auto lines = wolv::util::splitString(message, "\n"); std::ranges::transform(lines, lines.begin(), [](auto line) { @@ -1233,7 +1233,7 @@ namespace hex::plugin::builtin { return hex::limitStringLength(line, 128); }); - return hex::combineStrings(lines, "\n"); + return wolv::util::combineStrings(lines, "\n"); }; TextEditor::ErrorMarkers errorMarkers; @@ -1721,7 +1721,7 @@ namespace hex::plugin::builtin { } std::vector candidates; for (auto name: m_UDTs) { - auto vectorString = hex::splitString(name, "::"); + auto vectorString = wolv::util::splitString(name, "::"); if (typeStr == vectorString.back()) candidates.push_back(name); @@ -1737,7 +1737,7 @@ namespace hex::plugin::builtin { } // Definitions of global variables and placed variables. - void TextHighlighter::loadGlobalDefinitions( Scopes tokenRangeSet, std::vector identifierTypes, Variables &variables) { + void TextHighlighter::loadGlobalDefinitions(Scopes tokenRangeSet, std::vector identifierTypes, Variables &variables) { m_startToken = m_originalPosition = m_partOriginalPosition = TokenIter(m_tokens.begin(), m_tokens.end()); for (auto range: tokenRangeSet) { @@ -1917,7 +1917,7 @@ namespace hex::plugin::builtin { if (m_text.empty()) m_text = m_viewPatternEditor->getTextEditor().GetText(); - m_lines = hex::splitString(m_text, "\n"); + m_lines = wolv::util::splitString(m_text, "\n"); m_lines.push_back(""); m_firstTokenIdOfLine.clear(); m_firstTokenIdOfLine.resize(m_lines.size(), -1); diff --git a/plugins/builtin/source/content/views/view_bookmarks.cpp b/plugins/builtin/source/content/views/view_bookmarks.cpp index 298d1c7e6..89414a5a8 100644 --- a/plugins/builtin/source/content/views/view_bookmarks.cpp +++ b/plugins/builtin/source/content/views/view_bookmarks.cpp @@ -184,7 +184,7 @@ namespace hex::plugin::builtin { for (const auto &[bookmark, highlightVisible] : bookmarks) { result += hex::format("### {} [0x{:04X} - 0x{:04X}]\n\n", hex::changeEndianness(bookmark.color, std::endian::big) >> 8, bookmark.name, bookmark.region.getStartAddress(), bookmark.region.getEndAddress()); - for (const auto &line : hex::splitString(bookmark.comment, "\n")) + for (const auto &line : wolv::util::splitString(bookmark.comment, "\n")) result += hex::format("> {}\n", line); result += "\n"; diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index ca9323d05..b99ea3045 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -1938,7 +1938,7 @@ namespace hex::plugin::builtin { TextEditor::Selection selection = m_selection.get(newProvider); m_textEditor.get(newProvider).SetSelection(selection.mStart, selection.mEnd); m_textEditor.get(newProvider).SetBreakpoints(m_breakpoints.get(newProvider)); - m_consoleEditor.get(newProvider).SetText(hex::combineStrings(m_console.get(newProvider), "\n")); + m_consoleEditor.get(newProvider).SetText(wolv::util::combineStrings(m_console.get(newProvider), "\n")); m_consoleEditor.get(newProvider).SetCursorPosition(m_consoleCursorPosition.get(newProvider)); m_consoleEditor.get(newProvider).SetLongestLineLength(m_consoleLongestLineLength.get(newProvider)); selection = m_consoleSelection.get(newProvider); @@ -2161,9 +2161,9 @@ namespace hex::plugin::builtin { if (type->isTemplateType()) continue; - createNestedMenu(hex::splitString(typeName, "::"), [&, this] { + createNestedMenu(wolv::util::splitString(typeName, "::"), [&, this] { std::string variableName; - for (const char c : hex::replaceStrings(typeName, "::", "_")) + for (const char c : wolv::util::replaceStrings(typeName, "::", "_")) variableName += static_cast(std::tolower(c)); variableName += hex::format("_at_0x{:02X}", selection->getStartAddress());