From e27159651f2a5579bb5ee2c320380ef9d6e84142 Mon Sep 17 00:00:00 2001 From: PhilipDeegan Date: Tue, 20 Aug 2024 18:18:24 +0100 Subject: [PATCH] better file regexing (#98) --- mkn.cpp | 2 +- mkn.yaml | 2 +- src/maiken/setup.cpp | 2 +- src/regex.cpp | 23 +++++++++++++++-------- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/mkn.cpp b/mkn.cpp index 3df5fbca..a87f642f 100644 --- a/mkn.cpp +++ b/mkn.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2022, Philip Deegan. +Copyright (c) 2024, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/mkn.yaml b/mkn.yaml index 9718950f..44ac316e 100644 --- a/mkn.yaml +++ b/mkn.yaml @@ -3,7 +3,7 @@ name: mkn version: master property: - DATE: 13-AUG-2024 + DATE: 20-AUG-2024 parent: bin mode: none diff --git a/src/maiken/setup.cpp b/src/maiken/setup.cpp index 5bd3f8ce..6b94c2b3 100644 --- a/src/maiken/setup.cpp +++ b/src/maiken/setup.cpp @@ -57,7 +57,7 @@ void sub_initializer(Application const& app, std::vector const& node while (c) { c = 0; for (auto const& n : nodes) { - if (n[STR_NAME].Scalar() == profile){ + if (n[STR_NAME].Scalar() == profile) { process(n); profile = n[STR_PARENT] ? Properties::RESOLVE(app, n[STR_PARENT].Scalar()) : ""; c = !profile.empty(); diff --git a/src/regex.cpp b/src/regex.cpp index e283007c..f76c07b6 100644 --- a/src/regex.cpp +++ b/src/regex.cpp @@ -34,8 +34,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. std::vector maiken::Regexer::RESOLVE(std::string str) KTHROW(mkn::kul::Exception) { std::vector v; - auto posL = str.find("("); - auto posR = str.find(")"); + + auto const posL = str.find("("); + auto const posR = str.find(")"); if (posL == std::string::npos || posR == std::string::npos) return v; if (str.size() > 1 && str.substr(0, 2) == "./") str = str.substr(2); @@ -45,26 +46,32 @@ std::vector maiken::Regexer::RESOLVE(std::string str) KTHROW(mkn::k mkn::kul::Dir d(mkn::kul::env::CWD()); if (bits.size() > 1) d = mkn::kul::Dir(bits[0]); - str = bits[bits.size() - 1]; - - for (size_t i = 1; i < bits.size() - 1; i++) { + std::size_t i = 1; + for (; i < bits.size() - 1; i++) { if (bits[i].find("(") != std::string::npos && bits[i].find(")") != std::string::npos) break; d = d.join(bits[i]); } - auto regexer = [&](auto items) { + str = bits[i]; + std::string const prefix = str.substr(0, str.find("(")); + str = str.substr(prefix.size(), str.size() - prefix.size()); + + auto regexer = [&](auto const& items) { for (auto const& item : items) { try { + std::string const real = item.real(); + if (prefix.size() && real.find(prefix) == std::string::npos) continue; std::regex re(str); std::smatch match; - std::string subject(item.real()); - if (std::regex_search(subject, match, re) && match.size() > 1) RESOLVE_REC(item.real(), v); + if (std::regex_search(real, match, re) && match.size() > 1) RESOLVE_REC(real, v); } catch (std::regex_error& e) { KEXIT(1, "Regex Failure:\n") << e.what(); } } }; + regexer(d.files(/*recursive=*/true)); + return v; }