From a0d0621725820b51390425c87821536ddc8e83ec Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 3 Dec 2025 20:48:22 +0100 Subject: [PATCH] build: Start using std::from_chars --- lib/external/libwolv | 2 +- lib/external/pattern_language | 2 +- lib/libimhex/include/hex/helpers/utils.hpp | 8 +++++++- plugins/builtin/source/content/data_inspector.cpp | 10 +++++----- .../content/data_processor_nodes/basic_nodes.cpp | 6 +++++- plugins/builtin/source/content/pl_pragmas.cpp | 9 ++++++--- .../builtin/source/content/tools/base_converter.cpp | 10 ++++++---- 7 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/external/libwolv b/lib/external/libwolv index e539a319a..b2223dda5 160000 --- a/lib/external/libwolv +++ b/lib/external/libwolv @@ -1 +1 @@ -Subproject commit e539a319afe692bda382e9b4cffdd8dbe514527e +Subproject commit b2223dda56a6e62c6a394cd646eb807cc1659831 diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 553cdd4bf..de2ddb6d2 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 553cdd4bf872cc6cfb2308372fd115897cc2eb03 +Subproject commit de2ddb6d2cb8a0d0c3295d6e1585bc182c3c3835 diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index 975fdee60..a82d91b6d 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -28,6 +28,8 @@ #include +#include + namespace hex { #if !defined(HEX_MODULE_EXPORT) @@ -261,7 +263,11 @@ namespace hex { if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1])) return {}; - result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16)); + auto value = wolv::util::from_chars(byteString.substr(i, 2), 16); + if (!value.has_value()) + return {}; + + result.push_back(*value); } return result; diff --git a/plugins/builtin/source/content/data_inspector.cpp b/plugins/builtin/source/content/data_inspector.cpp index a241fb3b7..c9ad23d17 100644 --- a/plugins/builtin/source/content/data_inspector.cpp +++ b/plugins/builtin/source/content/data_inspector.cpp @@ -34,7 +34,7 @@ namespace hex::plugin::builtin { template static std::vector stringToUnsigned(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(u64)) { - u64 result = std::strtoull(value.c_str(), nullptr, 0); + const auto result = wolv::util::from_chars(value).value_or(0); if (result > std::numeric_limits::max()) return {}; std::vector bytes(Size, 0x00); @@ -48,7 +48,7 @@ namespace hex::plugin::builtin { template static std::vector stringToSigned(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(u64)) { - i64 result = std::strtoll(value.c_str(), nullptr, 0); + const auto result = wolv::util::from_chars(value).value_or(0); if (result > std::numeric_limits::max() || result < std::numeric_limits::min()) return {}; std::vector bytes(Size, 0x00); @@ -62,7 +62,7 @@ namespace hex::plugin::builtin { template static std::vector stringToFloat(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(long double)) { - T result = std::strtold(value.c_str(), nullptr); + const T result = wolv::util::from_chars(value).value_or(0); std::vector bytes(sizeof(T), 0x00); std::memcpy(bytes.data(), &result, bytes.size()); @@ -362,7 +362,7 @@ namespace hex::plugin::builtin { [](const std::string &value, std::endian endian) -> std::vector { std::ignore = endian; - return hex::crypt::encodeSleb128(std::strtoll(value.c_str(), nullptr, 0)); + return hex::crypt::encodeSleb128(wolv::util::from_chars(value).value_or(0)); } ); @@ -379,7 +379,7 @@ namespace hex::plugin::builtin { [](const std::string &value, std::endian endian) -> std::vector { std::ignore = endian; - return hex::crypt::encodeUleb128(std::strtoull(value.c_str(), nullptr, 0)); + return hex::crypt::encodeUleb128(wolv::util::from_chars(value).value_or(0)); } ); diff --git a/plugins/builtin/source/content/data_processor_nodes/basic_nodes.cpp b/plugins/builtin/source/content/data_processor_nodes/basic_nodes.cpp index c8118a020..7c649f6c2 100644 --- a/plugins/builtin/source/content/data_processor_nodes/basic_nodes.cpp +++ b/plugins/builtin/source/content/data_processor_nodes/basic_nodes.cpp @@ -54,7 +54,11 @@ namespace hex::plugin::builtin { if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1])) throwNodeError("Invalid byte string format"); - result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16)); + auto value = wolv::util::from_chars(byteString.substr(i, 2), 16); + if (!value.has_value()) + throwNodeError("Invalid number value"); + + result.push_back(*value); } return result; diff --git a/plugins/builtin/source/content/pl_pragmas.cpp b/plugins/builtin/source/content/pl_pragmas.cpp index c6e8f6235..8cc0705a3 100644 --- a/plugins/builtin/source/content/pl_pragmas.cpp +++ b/plugins/builtin/source/content/pl_pragmas.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -10,10 +11,12 @@ namespace hex::plugin::builtin { void registerPatternLanguagePragmas() { ContentRegistry::PatternLanguage::addPragma("base_address", [](pl::PatternLanguage &runtime, const std::string &value) { - auto baseAddress = strtoull(value.c_str(), nullptr, 0); + auto baseAddress = wolv::util::from_chars(value); + if (!baseAddress.has_value()) + return false; - ImHexApi::Provider::get()->setBaseAddress(baseAddress); - runtime.setDataBaseAddress(baseAddress); + ImHexApi::Provider::get()->setBaseAddress(*baseAddress); + runtime.setDataBaseAddress(*baseAddress); return true; }); diff --git a/plugins/builtin/source/content/tools/base_converter.cpp b/plugins/builtin/source/content/tools/base_converter.cpp index fd5a13c3e..495097078 100644 --- a/plugins/builtin/source/content/tools/base_converter.cpp +++ b/plugins/builtin/source/content/tools/base_converter.cpp @@ -4,6 +4,8 @@ #include +#include + namespace hex::plugin::builtin { static u32 digitsNeeded(u32 numDigits, u32 baseX, u32 baseY) { @@ -40,16 +42,16 @@ namespace hex::plugin::builtin { switch (base) { case 10: - number = std::strtoull(buffers[0].c_str(), nullptr, base); + number = wolv::util::from_chars(buffers[0], base).value_or(0); break; case 16: - number = std::strtoull(buffers[1].c_str(), nullptr, base); + number = wolv::util::from_chars(buffers[1], base).value_or(0); break; case 8: - number = std::strtoull(buffers[2].c_str(), nullptr, base); + number = wolv::util::from_chars(buffers[2], base).value_or(0); break; case 2: - number = std::strtoull(buffers[3].c_str(), nullptr, base); + number = wolv::util::from_chars(buffers[3], base).value_or(0); break; default: return;