diff --git a/.gitmodules b/.gitmodules index 71242f28..78114a52 100644 --- a/.gitmodules +++ b/.gitmodules @@ -34,3 +34,6 @@ [submodule "Algorithms/deps/QuickXorHash"] path = Algorithms/deps/QuickXorHash url = https://github.com/namazso/QuickXorHash.git +[submodule "compile-time-regular-expressions"] + path = compile-time-regular-expressions + url = https://github.com/hanickadot/compile-time-regular-expressions.git diff --git a/OpenHashTab/OpenHashTab.vcxproj b/OpenHashTab/OpenHashTab.vcxproj index df71e728..339324fe 100644 --- a/OpenHashTab/OpenHashTab.vcxproj +++ b/OpenHashTab/OpenHashTab.vcxproj @@ -64,14 +64,14 @@ true - $(SolutionDir)concurrentqueue;$(SolutionDir)phnt;$(SolutionDir)tiny-json;$(IncludePath) + $(SolutionDir)concurrentqueue;$(SolutionDir)phnt;$(SolutionDir)tiny-json;$(SolutionDir)compile-time-regular-expressions\include;$(IncludePath) $(SolutionDir)bin\$(Configuration)\$(Platform)\ CI_VERSION="$(CI_VERSION)";CI_VERSION_MAJOR=$(CI_VERSION_MAJOR);CI_VERSION_MINOR=$(CI_VERSION_MINOR);CI_VERSION_PATCH=$(CI_VERSION_PATCH);%(PreprocessorDefinitions) false - stdcpp17 + stdcpp20 Use diff --git a/OpenHashTab/SumFileParser.cpp b/OpenHashTab/SumFileParser.cpp index 921d2f66..ae43c648 100644 --- a/OpenHashTab/SumFileParser.cpp +++ b/OpenHashTab/SumFileParser.cpp @@ -17,23 +17,19 @@ #include "SumFileParser.h" -#include +#include #include #include "base64.h" #include "utl.h" #include "../AlgorithmsLoader/Hasher.h" -constexpr char k_regex_hex[] = R"(([0-9a-fA-F]{8,512}) [ \*](.+))"; -constexpr char k_regex_b64[] = R"(([0-9a-zA-Z=+\/,\-_]{6,512}) [ \*](.+))"; -constexpr char k_regex_sfv[] = R"((.+)\s+([0-9a-fA-F]{8}))"; +auto k_regex_hex = ctre::match; +auto k_regex_b64 = ctre::match; +auto k_regex_sfv = ctre::match; class SumFileParser2 { - std::regex _hex{ k_regex_hex }; - std::regex _b64{ k_regex_b64 }; - std::regex _sfv{ k_regex_sfv }; - enum class CommentStyle { Unknown, @@ -54,8 +50,6 @@ class SumFileParser2 bool ProcessLine(std::string_view sv) { - using svmatch = std::match_results; - if (sv.find_first_not_of("\r\n\t\f\v ") == std::string_view::npos) return true; // empty line @@ -73,15 +67,13 @@ class SumFileParser2 if ((_hash == HashStyle::Unknown || _hash == HashStyle::Sfv)) { - svmatch pieces; - if(std::regex_match(begin(sv), end(sv), pieces, _sfv)) + if(auto [whole, file_match, hash_str] = k_regex_sfv(sv); whole) { _hash = HashStyle::Sfv; - auto file = pieces[1].str(); // According to wikipedia delimiter is always space. + auto file = std::string(file_match); const auto notspace = file.find_last_not_of(' '); file.resize(notspace == std::string_view::npos ? 0 : notspace + 1); - const auto hash_str = pieces[2].str(); auto hash = utl::HashStringToBytes(std::string_view{ hash_str }); if (!hash.empty()) { @@ -93,15 +85,13 @@ class SumFileParser2 if ((_hash == HashStyle::Unknown || _hash == HashStyle::Hex)) { - svmatch pieces; - if (std::regex_match(begin(sv), end(sv), pieces, _hex)) + if (auto [whole, hash_str, file] = k_regex_hex(sv); whole) { _hash = HashStyle::Hex; - const auto hash_str = pieces[1].str(); auto hash = utl::HashStringToBytes(std::string_view{ hash_str }); if (!hash.empty()) { - files.emplace_back(pieces[2], std::move(hash)); + files.emplace_back(file, std::move(hash)); return true; } } @@ -109,15 +99,14 @@ class SumFileParser2 if ((_hash == HashStyle::Unknown || _hash == HashStyle::Base64)) { - svmatch pieces; - if (std::regex_match(begin(sv), end(sv), pieces, _b64)) + if (auto [whole, hash_match, file] = k_regex_hex(sv); whole) { _hash = HashStyle::Base64; - const auto str = pieces[1].str(); - auto hash = b64::decode(str.c_str(), str.size()); + const auto hash_str = std::string(hash_match); + auto hash = b64::decode(hash_str.c_str(), hash_str.size()); if (!hash.empty()) { - files.emplace_back(pieces[2], std::move(hash)); + files.emplace_back(file, std::move(hash)); return true; } } diff --git a/OpenHashTab/utl.cpp b/OpenHashTab/utl.cpp index c0c2f349..88db00f2 100644 --- a/OpenHashTab/utl.cpp +++ b/OpenHashTab/utl.cpp @@ -18,7 +18,7 @@ #include "utl.h" #include -#include +#include #include "Settings.h" @@ -35,13 +35,10 @@ extern "C" NTSTATUS NTAPI RtlLoadString( std::vector utl::FindHashInString(std::wstring_view wv) { - using wvmatch = std::match_results; - constexpr static wchar_t regex_str[] = LR"(((?:[0-9A-F]{2} ?){4,}|(?:[0-9a-f]{2} ?){4,}))"; - static std::wregex regex{ regex_str }; - - wvmatch pieces; - if (std::regex_search(begin(wv), end(wv), pieces, regex)) - return HashStringToBytes(std::wstring_view{ pieces[1].str() }); + static auto regex = ctre::match; + + if (auto [whole, hash] = regex(wv); whole) + return HashStringToBytes(std::wstring_view{ std::wstring(hash)}); return {}; } diff --git a/compile-time-regular-expressions b/compile-time-regular-expressions new file mode 160000 index 00000000..331aebc7 --- /dev/null +++ b/compile-time-regular-expressions @@ -0,0 +1 @@ +Subproject commit 331aebc79d715d1d0036d2d32021584eefecbcc9