ALib C++ Library
Library Version: 2510 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 //==========================================================================================
39 /// Constructor. Passes the optional parameters to method #Compile.
40 ///
41 /// @param pNeedle The string to search.
42 /// Defaults to \b NULL_STRING to allow parameterless construction with later
43 /// invocation of #Compile.
44 //==========================================================================================
45 TStringSearch( const TString<TChar>& pNeedle= nullptr )
46 {
47 Compile( pNeedle );
48 }
49
50 //==========================================================================================
51 /// Destructor.
52 //==========================================================================================
55
56 public:
57 //==========================================================================================
58 /// Resets this object to use the given string as the needle to search.
59 /// @param needle The needle to search.
60 //==========================================================================================
63
64 //==========================================================================================
65 /// Searches for the needle in \p{haystack} starting at \p{startIdx}.
66 ///
67 /// @param haystack The string to search in.
68 /// @param startIdx The start of the search.
69 /// Defaults to \c 0.
70 /// @return The index of the next occurrence of the needle in given \p{haystack}.
71 /// \c -1 if not found.
72 //==========================================================================================
74 integer Search( const TString<TChar>& haystack, integer startIdx= 0 );
75
76}; // class TStringSearch
77
94
95}} // namespace alib[::strings::util]
96
97/// Type alias in namespace \b alib.
98template<lang::Case TSensitivity>
100
101
102/// Type alias in namespace \b alib.
103template<lang::Case TSensitivity>
105
106/// Type alias in namespace \b alib.
107template<lang::Case TSensitivity>
109
110
111} // namespace alib
112
TStringSearch(const TString< TChar > &pNeedle=nullptr)
Definition search.inl:45
TAString< character, lang::HeapAllocator > needle
Definition search.inl:29
ALIB_DLL integer Search(const TString< TChar > &haystack, integer startIdx=0)
Definition search.cpp:67
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:496
#define ALIB_EXPORT
Definition alib.inl:488
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:99
strings::util::TStringSearch< nchar > NStringSearch
Type alias in namespace alib.
Definition search.inl:104
strings::util::TStringSearch< wchar > WSubstringSearch
Type alias in namespace alib.
Definition search.inl:108