ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
wildcardmatcher.hpp
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-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_STRINGS_UTIL_WILDCARDMATCHER
9#define HPP_ALIB_STRINGS_UTIL_WILDCARDMATCHER 1
10
11#if !defined(HPP_ALIB_STRINGS_CSTRING)
13#endif
14
15#if !defined (_GLIBCXX_VECTOR) && !defined(_VECTOR_)
16 #include <vector>
17#endif
18
19namespace alib { namespace strings { namespace util {
20
21
22/** ************************************************************************************************
23 * This utility class implements so called <em>wildcard string matching</em>. Wildcard characters
24 * are
25 * - <c>'*'</c>: Matches zero or more characters.
26 * - <c>'?'</c>: Matches exactly one character.
27 *
28 * Method #Compile accepts the pattern string and translates it to an internal (simple) list of
29 * "matching commands". This way, the class is optimized for performance, because after compilation,
30 * subsequent invocations of #Match do not need to parse the pattern string.
31 *
32 * @tparam TChar The character type. Implementations for \c nchar and \c wchar are provided
33 * with type definitions \ref alib::WildcardMatcherN and
34 * \ref alib::WildcardMatcherW.
35 **************************************************************************************************/
36template<typename TChar>
38{
39 /** The result list of commands created with #Compile and executed with #Match. */
40 std::vector<std::pair<int,TString<TChar>>> commands;
41
42 public:
43 /** ****************************************************************************************
44 * Constructs a WildcardMatcher to work on a given string. Passes the optional parameters
45 * to method #Compile.
46 *
47 * @param pattern The string pattern to match.
48 * Defaults to \b NullString() to allow parameterless construction,
49 * with later invocation of #Compile.
50 ******************************************************************************************/
52 {
53 Compile( pattern );
54 }
55
56 public:
57 /** ****************************************************************************************
58 * Resets this object to use the given pattern.
59 *
60 * @param pattern The string pattern to match.
61 ******************************************************************************************/
63 void Compile( const TString<TChar>& pattern );
64
65 /** ****************************************************************************************
66 * Tests if given \p{haystack} matches the actual pattern.
67 * If #Compile was not invoked or an empty pattern string was given, \c true is returned.
68 *
69 * @param haystack The string to test.
70 * @param sensitivity Denotes whether the matching is performed case sensitive.
71 * Defaults to \c %Case::Sensitive.
72 * @return \c true if given \p{haystack} matches the actual pattern, \c false otherwise.
73 ******************************************************************************************/
75 bool Match( const TString<TChar>& haystack, lang::Case sensitivity = lang::Case::Sensitive );
76
77}; // class WildcardMatcher
78
79
80extern template ALIB_API void TWildcardMatcher<nchar>::Compile( const TString<nchar>& );
82extern template ALIB_API void TWildcardMatcher<wchar>::Compile( const TString<wchar>& );
84
85}} // namespace alib[::strings::util]
86
87/// Type alias in namespace \b alib.
89
90/// Type alias in namespace \b alib.
92
93/// Type alias in namespace \b alib.
95
96} // namespace [alib]
97
98#endif // HPP_ALIB_STRINGS_UTIL_WILDCARDMATCHER
ALIB_API bool Match(const TString< TChar > &haystack, lang::Case sensitivity=lang::Case::Sensitive)
TWildcardMatcher(const TString< TChar > &pattern=NullString())
std::vector< std::pair< int, TString< TChar > > > commands
ALIB_API void Compile(const TString< TChar > &pattern)
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57
constexpr String NullString()
Definition string.hpp:2498