ALib C++ Library
Library Version: 2412 R0
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#pragma once
12#include <vector>
13
14namespace alib { namespace strings { namespace util {
15
16//==================================================================================================
17/// This utility class implements so called <em>wildcard string matching</em>. Wildcard characters
18/// are
19/// - <c>'*'</c>: Matches zero or more characters.
20/// - <c>'?'</c>: Matches exactly one character.
21///
22/// Method #Compile accepts the pattern string and translates it to an internal (simple) list of
23/// "matching commands". This way, the class is optimized for performance, because after compilation,
24/// subsequent invocations of #Match do not need to parse the pattern string.
25///
26/// @tparam TChar The character type. Implementations for \c nchar and \c wchar are provided
27/// with type definitions \ref alib::WildcardMatcherN and
28/// \ref alib::WildcardMatcherW.
29//==================================================================================================
30template<typename TChar>
32{
33 /// The result list of commands created with #Compile and executed with #Match.
34 std::vector<std::pair<int,TString<TChar>>> commands;
35
36 public:
37 //==========================================================================================
38 /// Constructs a WildcardMatcher to work on a given string. Passes the optional parameters
39 /// to method #Compile.
40 ///
41 /// @param pattern The string pattern to match.
42 /// Defaults to \b NULL_STRING to allow parameterless construction,
43 /// with later invocation of #Compile.
44 //==========================================================================================
46 {
47 Compile( pattern );
48 }
49
50 public:
51 //==========================================================================================
52 /// Resets this object to use the given pattern.
53 ///
54 /// @param pattern The string pattern to match.
55 //==========================================================================================
57 void Compile( const TString<TChar>& pattern );
58
59 //==========================================================================================
60 /// Tests if given \p{haystack} matches the actual pattern.
61 /// If #Compile was not invoked or an empty pattern string was given, \c true is returned.
62 ///
63 /// @param haystack The string to test.
64 /// @param sensitivity Denotes whether the matching is performed case-sensitive.
65 /// Defaults to \c %Case::Sensitive.
66 /// @return \c true if given \p{haystack} matches the actual pattern, \c false otherwise.
67 //==========================================================================================
69 bool Match( const TString<TChar>& haystack, lang::Case sensitivity = lang::Case::Sensitive );
70
71}; // class WildcardMatcher
72
73
74extern template ALIB_API void TWildcardMatcher<nchar>::Compile( const TString<nchar>& );
76extern template ALIB_API void TWildcardMatcher<wchar>::Compile( const TString<wchar>& );
78
79}} // namespace alib[::strings::util]
80
81/// Type alias in namespace \b alib.
83
84/// Type alias in namespace \b alib.
86
87/// Type alias in namespace \b alib.
89
90} // namespace [alib]
91
92#endif // HPP_ALIB_STRINGS_UTIL_WILDCARDMATCHER
93
ALIB_API bool Match(const TString< TChar > &haystack, lang::Case sensitivity=lang::Case::Sensitive)
std::vector< std::pair< int, TString< TChar > > > commands
The result list of commands created with Compile and executed with Match.
TWildcardMatcher(const TString< TChar > &pattern=NULL_STRING)
ALIB_API void Compile(const TString< TChar > &pattern)
#define ALIB_API
Definition alib.hpp:639
Case
Denotes upper and lower case character treatment.
Definition alib.cpp:69
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.hpp:2549