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