Skip to content

Commit 87d4c4a

Browse files
committed
add new method TRestTools::GetMatchingStrings
1 parent 99d8a9d commit 87d4c4a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

source/framework/tools/inc/TRestTools.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <memory>
3232
#include <string>
3333
#include <vector>
34+
#include <set>
3435

3536
#define UNUSED(x) (void)x
3637

@@ -85,6 +86,8 @@ class TRestTools {
8586
static std::string GetFileNameRoot(const std::string& fullname);
8687
static std::vector<std::string> GetObservablesInString(const std::string& observablesStr,
8788
bool removeDuplicates = true);
89+
static std::set<std::string> GetMatchingStrings(const std::vector<std::string>& stack,
90+
const std::vector<std::string>& wantedStrings);
8891

8992
static int GetBinaryFileColumns(std::string fname);
9093

source/framework/tools/src/TRestTools.cxx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,31 @@ std::vector<std::string> TRestTools::GetObservablesInString(const std::string& o
861861
return obsList;
862862
}
863863

864+
////////////////////////////////////////////////////////////
865+
/// \brief Returns a set of strings that match the wanted strings from the stack.
866+
/// The wanted strings can contain wildcards "*" and "?".
867+
/// \param stack: vector of strings to be searched
868+
/// \param wantedStrings: vector of strings with the wanted strings to be matched.
869+
/// \return a set of strings that match the wanted strings
870+
/// e.g.
871+
/// Input: stack = {"x1", "x2", "x11", "y1", "y2", "y11", "z1", "z2"},
872+
/// wantedStrings = {"x*", "y?", "z1"},
873+
/// Output: {"x1", "x11", "x2", "y1", "y2", "z1"}
874+
///
875+
std::set<std::string> TRestTools::GetMatchingStrings(const std::vector<std::string>& stack,
876+
const std::vector<std::string>& wantedStrings) {
877+
std::set<std::string> result;
878+
for (auto& ws : wantedStrings) {
879+
if (ws.find("*") != std::string::npos || ws.find("?") != std::string::npos) {
880+
for (auto& c : stack)
881+
if (MatchString(c, ws)) result.insert(c);
882+
} else if (std::find(stack.begin(), stack.end(), ws) != stack.end())
883+
result.insert(ws);
884+
}
885+
//return std::vector<std::string>(result.begin(), result.end()); // convert to vector
886+
return result;
887+
}
888+
864889
///////////////////////////////////////////////
865890
/// \brief Returns the input string but without multiple slashes ("/")
866891
///

0 commit comments

Comments
 (0)