ALib C++ Library
by
Library Version:
2402 R1
Documentation generated by
Loading...
Searching...
No Matches
home
dev
A-Worx
ALib
src
alib
strings
util
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)
12
# include "
alib/strings/cstring.hpp
"
13
#endif
14
15
#if !defined (_GLIBCXX_VECTOR) && !defined(_VECTOR_)
16
#include <vector>
17
#endif
18
19
namespace
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
**************************************************************************************************/
36
template
<
typename
TChar>
37
class
TWildcardMatcher
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
******************************************************************************************/
51
TWildcardMatcher
(
const
TString<TChar>
& pattern=
NullString
() )
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
******************************************************************************************/
62
ALIB_API
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
******************************************************************************************/
74
ALIB_API
75
bool
Match
(
const
TString<TChar>
& haystack,
lang::Case
sensitivity =
lang::Case::Sensitive
);
76
77
};
// class WildcardMatcher
78
79
80
extern
template
ALIB_API
void
TWildcardMatcher<nchar>::Compile
(
const
TString<nchar>
& );
81
extern
template
ALIB_API
bool
TWildcardMatcher<nchar>::Match
(
const
TString<nchar>
&,
lang::Case
);
82
extern
template
ALIB_API
void
TWildcardMatcher<wchar>::Compile
(
const
TString<wchar>
& );
83
extern
template
ALIB_API
bool
TWildcardMatcher<wchar>::Match
(
const
TString<wchar>
&,
lang::Case
);
84
85
}}
// namespace alib[::strings::util]
86
87
/// Type alias in namespace \b alib.
88
using
WildcardMatcher
=
strings::util::TWildcardMatcher<character>
;
89
90
/// Type alias in namespace \b alib.
91
using
WildcardMatcherN
=
strings::util::TWildcardMatcher<nchar>
;
92
93
/// Type alias in namespace \b alib.
94
using
WildcardMatcherW
=
strings::util::TWildcardMatcher<wchar>
;
95
96
}
// namespace [alib]
97
98
#endif
// HPP_ALIB_STRINGS_UTIL_WILDCARDMATCHER
alib::strings::TString
Definition
string.hpp:74
alib::strings::util::TWildcardMatcher
Definition
wildcardmatcher.hpp:38
alib::strings::util::TWildcardMatcher::Match
ALIB_API bool Match(const TString< TChar > &haystack, lang::Case sensitivity=lang::Case::Sensitive)
Definition
wildcardmatcher.cpp:90
alib::strings::util::TWildcardMatcher::TWildcardMatcher
TWildcardMatcher(const TString< TChar > &pattern=NullString())
Definition
wildcardmatcher.hpp:51
alib::strings::util::TWildcardMatcher::commands
std::vector< std::pair< int, TString< TChar > > > commands
Definition
wildcardmatcher.hpp:40
alib::strings::util::TWildcardMatcher::Compile
ALIB_API void Compile(const TString< TChar > &pattern)
Definition
wildcardmatcher.cpp:25
cstring.hpp
ALIB_API
#define ALIB_API
Definition
alib.hpp:538
alib::lang::Case
Case
Definition
commonenumdefs.hpp:67
alib::lang::Case::Sensitive
@ Sensitive
alib
Definition
alib.cpp:57
alib::NullString
constexpr String NullString()
Definition
string.hpp:2498