ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
wildcardmatcher.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_strings of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace strings { namespace util {
9
10//==================================================================================================
11/// This utility class implements so-called <em>wildcard string matching</em>. Wildcard characters
12/// are
13/// - <c>'*'</c>: Matches zero or more characters.
14/// - <c>'?'</c>: Matches exactly one character.
15///
16/// Method #Compile accepts the pattern string and translates it to an internal (simple) list of
17/// "matching commands". This way, the class is optimized for performance, because after compilation,
18/// subsequent invocations of #Match do not need to parse the pattern string.
19///
20/// @tparam TChar The character type. Implementations for \c nchar and \c wchar are provided
21/// with type definitions \ref alib::WildcardMatcherN and
22/// \ref alib::WildcardMatcherW.
23//==================================================================================================
24template<typename TChar>
26{
27 /// The result list of commands created with #Compile and executed with #Match.
28 std::vector<std::pair<int,TString<TChar>>> commands;
29
30 public:
31 //==========================================================================================
32 /// Constructs a WildcardMatcher to work on a given string. Passes the optional parameters
33 /// to method #Compile.
34 ///
35 /// @param pattern The string pattern to match.
36 /// Defaults to \b NULL_STRING to allow parameterless construction,
37 /// with later invocation of #Compile.
38 //==========================================================================================
40 {
41 Compile( pattern );
42 }
43
44 public:
45 //==========================================================================================
46 /// Resets this object to use the given pattern.
47 ///
48 /// @param pattern The string pattern to match.
49 //==========================================================================================
51 void Compile( const TString<TChar>& pattern );
52
53 //==========================================================================================
54 /// Tests if given \p{haystack} matches the actual pattern.
55 /// If #Compile was not invoked or an empty pattern string was given, \c true is returned.
56 ///
57 /// @param haystack The string to test.
58 /// @param sensitivity Denotes whether the matching is performed case-sensitive.
59 /// Defaults to \c %Case::Sensitive.
60 /// @return \c true if given \p{haystack} matches the actual pattern, \c false otherwise.
61 //==========================================================================================
63 bool Match( const TString<TChar>& haystack, lang::Case sensitivity = lang::Case::Sensitive );
64
65}; // class WildcardMatcher
66
67
68extern template ALIB_DLL void TWildcardMatcher<nchar>::Compile( const TString<nchar>& );
70extern template ALIB_DLL void TWildcardMatcher<wchar>::Compile( const TString<wchar>& );
72
73}} // namespace alib[::strings::util]
74
75/// Type alias in namespace \b alib.
77
78/// Type alias in namespace \b alib.
80
81/// Type alias in namespace \b alib.
83
84} // namespace [alib]
85
86
ALIB_DLL bool Match(const TString< TChar > &haystack, lang::Case sensitivity=lang::Case::Sensitive)
TWildcardMatcher(const TString< TChar > &pattern=NULL_STRING)
ALIB_DLL void Compile(const TString< TChar > &pattern)
std::vector< std::pair< int, TString< character > > > commands
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_EXPORT
Definition alib.inl:488
Case
Denotes upper and lower case character treatment.
strings::util::TWildcardMatcher< wchar > WildcardMatcherW
Type alias in namespace alib.
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2463
strings::util::TWildcardMatcher< character > WildcardMatcher
Type alias in namespace alib.
strings::util::TWildcardMatcher< nchar > WildcardMatcherN
Type alias in namespace alib.