BDE 4.39.x 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.
119///
120/// See @ref ball_patternutil
122
123 // CLASS METHODS
124
125 /// Return `true` if the specified `pattern` matches the specified
126 /// `inputString`, and `false` if the pattern does not match or is
127 /// invalid. There are two types of escape sequences that are allowed
128 /// in `pattern`. (See the function-level documentation of
129 /// `PatternUtil::isValidPattern` for the definition of invalid
130 /// patterns.) A '\*` escape sequence in `pattern' matches a single `*`
131 /// in `inputString`. A '\\' escape sequence in `pattern` matches a
132 /// single '\' in `inputString`. If `pattern` ends with an unescaped
133 /// `*`, then `pattern` matches `inputString` if the string indicated by
134 /// `pattern` (after escape sequence processing) with the final `*`
135 /// removed is a prefix of `inputString`. Otherwise `pattern` matches
136 /// `inputString` only if the string indicated by `pattern` (after
137 /// escape sequence processing) and `inputString` are the same.
138 ///
139 /// \pre The behavior is undefined unless both `inputString` and `pattern` are
140 /// null-terminated c-style strings.
141 static bool isMatch(const char *inputString, const char *pattern);
142
143 /// Return `true` if the specified `pattern` does not contain a '\' not
144 /// followed by either '\' or `*`, or an unescaped `*` at locations other than at the end, and `false` otherwise.
145 ///
146 /// \note Note that an
147 /// unescaped `*` not at the end may someday be considered a valid
148 /// pattern.
149 static bool isValidPattern(const char *pattern);
150};
151
152} // close package namespace
153
154
155#endif
156
157// ----------------------------------------------------------------------------
158// Copyright 2015 Bloomberg Finance L.P.
159//
160// Licensed under the Apache License, Version 2.0 (the "License");
161// you may not use this file except in compliance with the License.
162// You may obtain a copy of the License at
163//
164// http://www.apache.org/licenses/LICENSE-2.0
165//
166// Unless required by applicable law or agreed to in writing, software
167// distributed under the License is distributed on an "AS IS" BASIS,
168// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169// See the License for the specific language governing permissions and
170// limitations under the License.
171// ----------------------------- END-OF-FILE ----------------------------------
172
173/** @} */
174/** @} */
175/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition ball_administration.h:214
Definition ball_patternutil.h:121
static bool isMatch(const char *inputString, const char *pattern)
static bool isValidPattern(const char *pattern)