ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
search.inl
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-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace strings { namespace util {
9
10//==================================================================================================
11/// Implements "Knuth-Morris-Pratt" algorithm for searching a substring within a string.
12///
13/// While the well known "Boyer-Moore-Algorithm" is even faster in the average case, for
14/// uni-code characters its implementation would be efficient only with very long haystack strings.
15///
16/// For convenience, the following alias type names are available:
17/// - \ref alib::StringSearch,
18/// - \ref alib::NStringSearch and
19/// - \ref alib::WSubstringSearch.
20///
21/// @tparam TChar The character type of the haystack and needle strings.
22/// @tparam TSensitivity The letter case sensitivity of the search..
23//==================================================================================================
24template<typename TChar, lang::Case TSensitivity= lang::Case::Sensitive>
26{
27 protected:
28 /// The needle set Knuth-Morris-Pratt prefix length table.
30
31 /// The Knuth-Morris-Pratt prefix length table.
32 integer* kmpTable =nullptr;
33
34 /// Length of #kmpTable.
36
37 public:
38 /// Constructor. Passes the optional parameters to method #Compile.
39 ///
40 /// @param pNeedle The string to search.
41 /// Defaults to \b NULL_STRING to allow parameterless construction with later
42 /// invocation of #Compile.
43 TStringSearch( const TString<TChar>& pNeedle= nullptr ) { Compile( pNeedle ); }
44
45 /// Destructor.
48
49 public:
50 /// Resets this object to use the given string as the needle to search.
51 /// @param needle The needle to search.
54
55 /// Searches for the needle in \p{haystack} starting at \p{startIdx}.
56 ///
57 /// @param haystack The string to search in.
58 /// @param startIdx The start of the search.
59 /// Defaults to \c 0.
60 /// @return The index of the next occurrence of the needle in given \p{haystack}.
61 /// \c -1 if not found.
63 integer Search( const TString<TChar>& haystack, integer startIdx= 0 );
64
65}; // class TStringSearch
66
83
84}} // namespace alib[::strings::util]
85
86/// Type alias in namespace \b alib.
87template<lang::Case TSensitivity>
89
90
91/// Type alias in namespace \b alib.
92template<lang::Case TSensitivity>
94
95/// Type alias in namespace \b alib.
96template<lang::Case TSensitivity>
98
99
100} // namespace alib
TStringSearch(const TString< TChar > &pNeedle=nullptr)
Definition search.inl:43
TAString< character, lang::HeapAllocator > needle
Definition search.inl:29
ALIB_DLL integer Search(const TString< TChar > &haystack, integer startIdx=0)
Definition search.cpp:63
ALIB_DLL ~TStringSearch()
Destructor.
Definition search.cpp:28
ALIB_DLL void Compile(const TString< TChar > &needle)
Definition search.cpp:31
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_EXPORT
Definition alib.inl:497
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::util::TStringSearch< character > StringSearch
Type alias in namespace alib.
Definition search.inl:88
strings::util::TStringSearch< nchar > NStringSearch
Type alias in namespace alib.
Definition search.inl:93
strings::util::TStringSearch< wchar > WSubstringSearch
Type alias in namespace alib.
Definition search.inl:97