ALib C++ Library
Library Version: 2511 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
18/// compilation, 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 /// Constructs a WildcardMatcher to work on a given string. Passes the optional parameters
32 /// to method #Compile.
33 ///
34 /// @param pattern The string pattern to match.
35 /// Defaults to \b NULL_STRING to allow parameterless construction,
36 /// with later invocation of #Compile.
37 TWildcardMatcher( const TString<TChar>& pattern= NULL_STRING ) { Compile( pattern ); }
38
39 public:
40 /// Resets this object to use the given pattern.
41 ///
42 /// @param pattern The string pattern to match.
44 void Compile( const TString<TChar>& pattern );
45
46 /// Tests if given \p{haystack} matches the actual pattern.
47 /// If #Compile was not invoked or an empty pattern string was given, \c true is returned.
48 ///
49 /// @param haystack The string to test.
50 /// @param sensitivity Denotes whether the matching is performed case-sensitive.
51 /// Defaults to \c %Case::Sensitive.
52 /// @return \c true if given \p{haystack} matches the actual pattern, \c false otherwise.
54 bool Match( const TString<TChar>& haystack, lang::Case sensitivity= lang::Case::Sensitive );
55
56}; // class WildcardMatcher
57
58
59extern template ALIB_DLL void TWildcardMatcher<nchar>::Compile( const TString<nchar>& );
61extern template ALIB_DLL void TWildcardMatcher<wchar>::Compile( const TString<wchar>& );
63
64}} // namespace alib[::strings::util]
65
66/// Type alias in namespace \b alib.
68
69/// Type alias in namespace \b alib.
71
72/// Type alias in namespace \b alib.
74
75} // namespace [alib]
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:503
#define ALIB_EXPORT
Definition alib.inl:497
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:2271
strings::util::TWildcardMatcher< character > WildcardMatcher
Type alias in namespace alib.
strings::util::TWildcardMatcher< nchar > WildcardMatcherN
Type alias in namespace alib.