ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
regexmatcher.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16//============================================== Module ============================================
17#if ALIB_C20_MODULES
18 module ALib.Strings.Search;
19#else
20# include "ALib.Strings.Search.H"
21#endif
22//========================================== Implementation ========================================
23#if ALIB_FEAT_BOOST_REGEX && ALIB_CHARACTERS_WIDE && !ALIB_CHARACTERS_NATIVE_WCHAR
24# pragma message ( "Warning: Class RegexMatcher will not be available, because ALIB_CHARACTERS_NATIVE_WCHAR is false." )
25#endif
26
27#if ALIB_FEAT_BOOST_REGEX && (!ALIB_CHARACTERS_WIDE || ALIB_CHARACTERS_NATIVE_WCHAR)
28
29namespace alib { namespace strings { namespace util {
30
31void RegexMatcher::Compile( const String& pattern ) {
32 if( pattern.IsNull() )
33 return;
34
35 boostRegex.assign( std::basic_string<character>(pattern.Buffer(), size_t( pattern.Length() ) ),
36 boost::regex_constants::optimize );
37}
38
39bool RegexMatcher::Match( const String& haystack ) {
40 ALIB_ASSERT_ERROR( !boostRegex.empty(), "ALIB/STR", "No regular expression compiled")
41 return boost::regex_match(haystack.begin(), haystack.end(), boostRegex);
42}
43
44RegexMatcher::SRange RegexMatcher::SearchIn( const String& haystack ) {
45 ALIB_ASSERT_ERROR( !boostRegex.empty(), "ALIB/STR", "No regular expression compiled")
46 boost::match_results<String::const_iterator> what;
47 if( boost::regex_search(haystack.begin(), haystack.end(), what, boostRegex ) )
48 return { what.position(), what.length(0) };
49 return {-1,-1};
50}
51
52}}} // namespace [alib::strings::util]
53
54#endif // ALIB_FEAT_BOOST_REGEX && (!ALIB_CHARACTERS_WIDE || ALIB_CHARACTERS_NATIVE_WCHAR)
boost::basic_regex< character, boost::regex_traits< character > > boostRegex
This is the internal regex matcher.
ALIB_DLL SRange SearchIn(const String &haystack)
ALIB_DLL void Compile(const String &pattern)
RegexMatcher(const String &pattern=NULL_STRING)
ALIB_DLL bool Match(const String &haystack)
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189