BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_patternutil.h
Go to the documentation of this file.
1/// @file ball_patternutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_patternutil.h -*-C++-*-
8#ifndef INCLUDED_BALL_PATTERNUTIL
9#define INCLUDED_BALL_PATTERNUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_patternutil ball_patternutil
15/// @brief Provide a utility class for string pattern matching.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_patternutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_patternutil-purpose"> Purpose</a>
25/// * <a href="#ball_patternutil-classes"> Classes </a>
26/// * <a href="#ball_patternutil-description"> Description </a>
27/// * <a href="#ball_patternutil-usage"> Usage </a>
28/// * <a href="#ball_patternutil-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#ball_patternutil-purpose}
31/// Provide a utility class for string pattern matching.
32///
33/// # Classes {#ball_patternutil-classes}
34///
35/// - ball::PatternUtil: utility class for string pattern matching
36///
37/// # Description {#ball_patternutil-description}
38/// This component defines a namespace, `ball::PatternUtil`, that
39/// provides utility functions for matching input strings to a given pattern
40/// based on wild-card and simple escape sequences.
41///
42/// This component participates in the implementation of "Rule-Based Logging".
43/// For more information on how to use that feature, please see the package
44/// level documentation and usage examples for "Rule-Based Logging".
45///
46/// ## Usage {#ball_patternutil-usage}
47///
48///
49/// This section illustrates intended use of this component.
50///
51/// ### Example 1: Basic Usage {#ball_patternutil-example-1-basic-usage}
52///
53///
54/// The following code fragments illustrate basic usage of this component's
55/// utility functions.
56///
57/// A string matches a pattern if they are identical:
58/// @code
59/// assert(ball::PatternUtil::isMatch("EQ", "EQ"));
60/// @endcode
61/// A string matches a pattern containing an (unescaped) trailing `*` if that
62/// pattern (without the trailing `*`) is a prefix of the string:
63/// @code
64/// assert(ball::PatternUtil::isMatch("EQ.MARKET", "EQ*"));
65/// assert(ball::PatternUtil::isMatch("EQ", "EQ*"));
66/// @endcode
67/// An escaped `*` at the end loses its wild-card semantics and matches a single
68/// `*`:
69/// @code
70/// assert(false == ball::PatternUtil::isMatch("EQ.MARKET", "EQ\\*"));
71/// assert(ball::PatternUtil::isMatch("EQ*", "EQ\\*"));
72/// @endcode
73/// Escape sequences include '\\' and '\*' only and they can appear anywhere in
74/// the pattern:
75/// @code
76/// assert(ball::PatternUtil::isMatch("\\EQ", "\\\\EQ"));
77/// assert(ball::PatternUtil::isMatch("E*Q", "E\\*Q"));
78/// @endcode
79/// A pattern is invalid if it contains a non-trailing `*`, or any '\' that is
80/// not followed by either '\' or `*`. The `isValidPattern` function can be
81/// used to determine whether or not a pattern is valid:
82/// @code
83/// assert(false == ball::PatternUtil::isValidPattern("E\\Q"));
84/// assert(false == ball::PatternUtil::isValidPattern("E*Q"));
85/// assert(true == ball::PatternUtil::isValidPattern("E\\\\Q"));
86/// assert(true == ball::PatternUtil::isValidPattern("E\\*Q"));
87/// @endcode
88/// The `isMatch` function always returns `false` on an invalid pattern:
89/// @code
90/// assert(false == ball::PatternUtil::isMatch("E\\Q","E\\Q"));
91/// assert(false == ball::PatternUtil::isMatch("E*Q", "E*Q"));
92/// assert(false == ball::PatternUtil::isMatch("ETQ", "E*Q"));
93/// @endcode
94/// @}
95/** @} */
96/** @} */
97
98/** @addtogroup bal
99 * @{
100 */
101/** @addtogroup ball
102 * @{
103 */
104/** @addtogroup ball_patternutil
105 * @{
106 */
107
108#include <balscm_version.h>
109
110
111namespace ball {
112
113 // ==================
114 // struct PatternUtil
115 // ==================
116
117/// This utility class provides functions relating to pattern matching for
118/// strings.
120
121 // CLASS METHODS
122
123 /// Return `true` if the specified `pattern` matches the specified
124 /// `inputString`, and `false` if the pattern does not match or is
125 /// invalid. There are two types of escape sequences that are allowed
126 /// in `pattern`. (See the function-level documentation of
127 /// `PatternUtil::isValidPattern` for the definition of invalid
128 /// patterns.) A '\*` escape sequence in `pattern' matches a single `*`
129 /// in `inputString`. A '\\' escape sequence in `pattern` matches a
130 /// single '\' in `inputString`. If `pattern` ends with an unescaped
131 /// `*`, then `pattern` matches `inputString` if the string indicated by
132 /// `pattern` (after escape sequence processing) with the final `*`
133 /// removed is a prefix of `inputString`. Otherwise `pattern` matches
134 /// `inputString` only if the string indicated by `pattern` (after
135 /// escape sequence processing) and `inputString` are the same. The
136 /// behavior is undefined unless both `inputString` and `pattern` are
137 /// null-terminated c-style strings.
138 static bool isMatch(const char *inputString, const char *pattern);
139
140 /// Return `true` if the specified `pattern` does not contain a '\' not
141 /// followed by either '\' or `*`, or an unescaped `*` at locations
142 /// other than at the end, and `false` otherwise. Note that an
143 /// unescaped `*` not at the end may someday be considered a valid
144 /// pattern.
145 static bool isValidPattern(const char *pattern);
146};
147
148} // close package namespace
149
150
151#endif
152
153// ----------------------------------------------------------------------------
154// Copyright 2015 Bloomberg Finance L.P.
155//
156// Licensed under the Apache License, Version 2.0 (the "License");
157// you may not use this file except in compliance with the License.
158// You may obtain a copy of the License at
159//
160// http://www.apache.org/licenses/LICENSE-2.0
161//
162// Unless required by applicable law or agreed to in writing, software
163// distributed under the License is distributed on an "AS IS" BASIS,
164// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
165// See the License for the specific language governing permissions and
166// limitations under the License.
167// ----------------------------- END-OF-FILE ----------------------------------
168
169/** @} */
170/** @} */
171/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition ball_administration.h:214
Definition ball_patternutil.h:119
static bool isMatch(const char *inputString, const char *pattern)
static bool isValidPattern(const char *pattern)