Loading...
Searching...
No Matches
mask.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
44#pragma once
45
46#include "utils.h"
47#if HAVE_BOOST_REGEX_UNICODE
48#include "unistring.h"
49#endif
50
51namespace ledger {
52
53class mask_t
54{
55public:
56#if HAVE_BOOST_REGEX_UNICODE
57 boost::u32regex expr;
58#else
59 boost::regex expr;
60#endif
61
62 explicit mask_t(const string& pattern);
63
64 mask_t() : expr() {
65 TRACE_CTOR(mask_t, "");
66 }
67 mask_t(const mask_t& m) : expr(m.expr) {
68 TRACE_CTOR(mask_t, "copy");
69 }
72 }
73
74 mask_t& operator=(const string& other);
75 mask_t& assign_glob(const string& other);
76
77 bool operator<(const mask_t& other) const {
78 return expr < other.expr;
79 }
80 bool operator==(const mask_t& other) const {
81 return expr == other.expr;
82 }
83
84 bool match(const string& text) const {
85#if HAVE_BOOST_REGEX_UNICODE
86 DEBUG("mask.match",
87 "Matching: \"" << text << "\" =~ /" << str() << "/ = "
88 << (boost::u32regex_search(text, expr) ? "true" : "false"));
89 return boost::u32regex_search(text, expr);
90#else
91 DEBUG("mask.match",
92 "Matching: \"" << text << "\" =~ /" << str() << "/ = "
93 << (boost::regex_search(text, expr) ? "true" : "false"));
94 return boost::regex_search(text, expr);
95#endif
96 }
97
98 bool empty() const {
99 return expr.empty();
100 }
101
102 string str() const {
103 if (! empty()) {
104#if HAVE_BOOST_REGEX_UNICODE
105 assert(sizeof(boost::uint32_t) == sizeof(UChar32));
107 std::basic_string<UChar32> expr_str = expr.str();
108 std::copy(expr_str.begin(), expr_str.end(),
109 std::back_inserter(ustr.utf32chars));
110 return ustr.extract();
111#else
112 return expr.str();
113#endif
114 } else {
115 return empty_string;
116 }
117 }
118
119 bool valid() const {
120 if (expr.status() != 0) {
121 DEBUG("ledger.validate", "mask_t: expr.status() != 0");
122 return false;
123 }
124 return true;
125 }
126};
127
128inline std::ostream& operator<<(std::ostream& out, const mask_t& mask) {
129 out << mask.str();
130 return out;
131}
132
133inline void put_mask(property_tree::ptree& pt, const mask_t& mask) {
134 pt.put_value(mask.str());
135}
136
137} // namespace ledger
General utility facilities used by Ledger.
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define DEBUG(cat, msg)
Definition utils.h:327
#define assert(x)
Definition utils.h:92
string empty_string
std::ostream & operator<<(std::ostream &out, const account_t &account)
T & downcast(U &object)
Definition utils.h:468
void put_mask(property_tree::ptree &pt, const mask_t &mask)
Definition mask.h:133
mask_t & operator=(const string &other)
string str() const
Definition mask.h:102
mask_t(const string &pattern)
mask_t & assign_glob(const string &other)
bool match(const string &text) const
Definition mask.h:84
bool empty() const
Definition mask.h:98
boost::regex expr
Definition mask.h:59
bool operator==(const mask_t &other) const
Definition mask.h:80
bool operator<(const mask_t &other) const
Definition mask.h:77
mask_t(const mask_t &m)
Definition mask.h:67
bool valid() const
Definition mask.h:119
Abstract working with UTF-32 encoded Unicode strings.
Definition unistring.h:57