Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
wildcard.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/utilities/wildcard.hpp
20 \brief utilities for wildcard handling
21 \ingroup utilities
22*/
23
25
26#include <ql/errors.hpp>
27
28#include <boost/algorithm/string/replace.hpp>
29#include <boost/make_shared.hpp>
30
31#include <memory>
32#include <set>
33
34namespace ore {
35namespace data {
36
37using namespace std;
38
39Wildcard::Wildcard(const std::string& pattern, const bool usePrefixes, const bool aggressivePrefixes)
40 : pattern_(pattern), usePrefixes_(usePrefixes), aggressivePrefixes_(aggressivePrefixes) {
41
42 wildCardPos_ = pattern_.find("*");
43
44 if (wildCardPos_ == std::string::npos)
45 return;
46
47 hasWildCard_ = true;
48
49 if (usePrefixes && (aggressivePrefixes || wildCardPos_ == pattern_.size() - 1)) {
51 } else {
53 static std::vector<std::string> specialChars = {"\\", ".", "+", "?", "^", "$", "(",
54 ")", "[", "]", "{", "}", "|"};
55 for (auto const& c : specialChars) {
56 boost::replace_all(*regexString_, c, "\\" + c);
57 }
58 boost::replace_all(*regexString_, "*", ".*");
59 }
60}
61
62bool Wildcard::hasWildcard() const { return hasWildCard_; }
63
64std::size_t Wildcard::wildcardPos() const { return wildCardPos_; }
65
66bool Wildcard::isPrefix() const { return prefixString_ ? true : false; }
67
68bool Wildcard::matches(const std::string& s) const {
69 if (prefixString_) {
70 return s.substr(0, (*prefixString_).size()) == (*prefixString_);
71 } else if (regexString_) {
72 if (regex_ == nullptr)
73 regex_ = QuantLib::ext::make_shared<std::regex>(*regexString_);
74 return std::regex_match(s, *regex_);
75 } else {
76 return s == pattern_;
77 }
78}
79
80const std::string& Wildcard::pattern() const { return pattern_; }
81
82const std::string& Wildcard::regex() const {
83 QL_REQUIRE(regexString_, "string '" << pattern_ << "' is not a regex (usePrefixes = " << std::boolalpha
84 << usePrefixes_ << ", aggressivePrefixes = " << aggressivePrefixes_
85 << ", isPrefix = " << !prefixString_ << ")");
86 return *regexString_;
87}
88
89const std::string& Wildcard::prefix() const {
90 QL_REQUIRE(prefixString_, "string '" << pattern_ << "' is not a prefix (usePrefixes = " << std::boolalpha
91 << usePrefixes_ << ", aggressivePrefixes = " << aggressivePrefixes_
92 << ", isRegex = " << !regexString_ << ")");
93 return *prefixString_;
94}
95
96void partitionQuotes(const set<string>& quoteNames, set<string>& names, set<string>& regexes) {
97
98 for (const string& n : quoteNames) {
99 ore::data::Wildcard w(n, false);
100 if (w.hasWildcard())
101 regexes.insert(w.regex());
102 else
103 names.insert(n);
104 }
105}
106
107void partitionQuotes(const set<string>& quoteNames, set<string>& names, set<string>& regexes,
108 std::set<std::string>& prefixes, const bool aggressivePrefixes) {
109
110 for (const string& n : quoteNames) {
111 ore::data::Wildcard w(n, true, aggressivePrefixes);
112 if (w.hasWildcard()) {
113 if (w.isPrefix())
114 prefixes.insert(w.prefix());
115 else
116 regexes.insert(w.regex());
117 } else
118 names.insert(n);
119 }
120}
121
122} // namespace data
123} // namespace ore
bool isPrefix() const
Definition: wildcard.cpp:66
bool hasWildcard() const
Definition: wildcard.cpp:62
boost::optional< std::string > prefixString_
Definition: wildcard.hpp:60
const std::string & pattern() const
Definition: wildcard.cpp:80
Wildcard(const std::string &pattern, const bool usePrefixes=true, const bool aggressivePrefixes=false)
Definition: wildcard.cpp:39
bool matches(const std::string &s) const
Definition: wildcard.cpp:68
std::size_t wildcardPos() const
Definition: wildcard.cpp:64
boost::optional< std::string > regexString_
Definition: wildcard.hpp:59
std::size_t wildCardPos_
Definition: wildcard.hpp:58
QuantLib::ext::shared_ptr< std::regex > regex_
Definition: wildcard.hpp:61
const std::string & prefix() const
Definition: wildcard.cpp:89
std::string pattern_
Definition: wildcard.hpp:53
const std::string & regex() const
Definition: wildcard.cpp:82
@ data
Definition: log.hpp:77
void partitionQuotes(const set< string > &quoteNames, set< string > &names, set< string > &regexes)
Definition: wildcard.cpp:96
Serializable Credit Default Swap.
Definition: namespaces.docs:23
utilities for wildcard handling