ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
regexmatcher.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_REGEXMATCHER
9#define HPP_ALIB_STRINGS_UTIL_REGEXMATCHER 1
10
11#if !defined (HPP_ALIB_STRINGS_CSTRING)
13#endif
14
15
16#if ALIB_FEAT_BOOST_REGEX && (!ALIB_CHARACTERS_WIDE || ALIB_CHARACTERS_NATIVE_WCHAR)
17
18namespace alib { namespace strings { namespace util {
19
20/** ************************************************************************************************
21 * This utility class wraps \https{boost::regex library,www.boost.org} and interfaces
22 * \alib_strings with it.
23 *
24 * The availability of the class is dependent on compiler symbol \ref ALIB_FEAT_BOOST_REGEX
25 * which has to be \c true, and in parallel either \ref ALIB_CHARACTERS_WIDE is \c false or
26 * \ref ALIB_CHARACTERS_NATIVE_WCHAR equals \c true.
27 *
28 * Method #Compile accepts the pattern string and compiles it to \b boost::regex.
29 * Subsequent invocations of #Match will then use the compiled regular expression for testing
30 * a given string.
31 *
32 * The syntax of the regular expressions is compatible to
33 * \https{Perl Regular Expressions,perldoc.perl.org/perlretut.html}.
34 *
35 * \note
36 * This a most very basic wrapper that supports a just the <b>bare minimum of the features</b>
37 * available in the original \b boost::regex library.
38 * I.e, the expression syntax is fixed to be \e Perl-compatible and no string replacement
39 * features or even match positioning are available.
40 **************************************************************************************************/
42{
43 protected:
44 /**
45 * This is the internal regex matcher. Yes, a nasty \c reinterpret_cast is performed in the
46 * compilation unit. This is for avoiding the inclusion of external headers with \alib
47 * headers at the expense of an otherwise unnecessary heap allocation.
48 */
49 void* boostRegex = nullptr;
50
51 public:
52 /** ****************************************************************************************
53 * Constructs a RegexMatcher to work on a given string. Passes the optional parameters
54 * to method #Compile.
55 *
56 * @param pattern The string pattern to match.
57 * Defaults to \b NullString() to allow parameterless construction,
58 * with later invocation of #Compile.
59 ******************************************************************************************/
60 RegexMatcher( const String& pattern= NullString() )
61 {
62 Compile( pattern );
63 }
64
65 /** ****************************************************************************************
66 * Destructor.
67 ******************************************************************************************/
70
71 public:
72 /** ****************************************************************************************
73 * Resets this object to use the given pattern.
74 *
75 * @param pattern The string pattern to match.
76 ******************************************************************************************/
78 void Compile( const String& pattern );
79
80 /** ****************************************************************************************
81 * Tests if given \p{haystack} matches the actual pattern.
82 * If #Compile was not invoked or an empty pattern string was given, \c true is returned.
83 *
84 * @param haystack The string to test.
85 * @return \c true if given \p{haystack} matches the actual pattern, \c false otherwise.
86 ******************************************************************************************/
88 bool Match( const String& haystack );
89
90}; // class RegexMatcher
91
92}} // namespace alib[::strings::util]
93
94/// Type alias in namespace \b alib.
96
97} // namespace [alib]
98
99#endif // ALIB_FEAT_BOOST_REGEX && (!ALIB_CHARACTERS_WIDE || ALIB_CHARACTERS_NATIVE_WCHAR)
100#endif // HPP_ALIB_STRINGS_UTIL_REGEXMATCHER
RegexMatcher(const String &pattern=NullString())
ALIB_API void Compile(const String &pattern)
ALIB_API bool Match(const String &haystack)
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57
constexpr String NullString()
Definition string.hpp:2498