ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
subsearch.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_SUBSEARCH
9#define HPP_ALIB_STRINGS_UTIL_SUBSEARCH 1
10
11#if !defined (HPP_ALIB_STRINGS_ASTRING)
13#endif
14
15
16namespace alib { namespace strings { namespace util {
17
18/** ************************************************************************************************
19 * Implements "Knuth-Morris-Pratt" algorithm for searching a sub-string within a string.
20 *
21 * While the well known "Boyer-Moore-Algorithm" is even faster in the average case, for
22 * uni-code characters its implementation would be efficient only with very long haystack strings.
23 *
24 * For convenience, the following alias type names are available:
25 * - \ref alib::SubstringSearch,
26 * - \ref alib::NSubstringSearch and
27 * - \ref alib::WSubstringSearch.
28 *
29 * @tparam TChar The character type of the haystack and needle strings.
30 * @tparam TSensitivity The letter case sensitivity of the search..
31 **************************************************************************************************/
32template<typename TChar, lang::Case TSensitivity= lang::Case::Sensitive>
34{
35 protected:
36 /** The needle set Knuth-Morris-Pratt prefix length table. */
38
39 /** The Knuth-Morris-Pratt prefix length table. */
40 integer* kmpTable = nullptr;
41
42 /** Length of #kmpTable. */
44
45 public:
46 /** ****************************************************************************************
47 * Constructor. Passes the optional parameters to method #Compile.
48 *
49 * @param pNeedle The string to search.
50 * Defaults to \b NullString() to allow parameterless construction with later
51 * invocation of #Compile.
52 ******************************************************************************************/
53 TSubstringSearch( const TString<TChar>& pNeedle= nullptr )
54 {
55 Compile( pNeedle );
56 }
57
58 /** ****************************************************************************************
59 * Destructor.
60 ******************************************************************************************/
63
64 public:
65 /** ****************************************************************************************
66 * Resets this object to use the given string as the needle to search.
67 * @param needle The needle to search.
68 ******************************************************************************************/
70 void Compile( const TString<TChar>& needle );
71
72 /** ****************************************************************************************
73 * Searches for the needle in \p{haystack} starting at \p{startIdx}.
74 *
75 * @param haystack The string to search in.
76 * @param startIdx The start of the search.
77 * Defaults to \c 0.
78 * @return The index of the next occurrence of the needle in given \p{haystack}.
79 * \c -1 if not found.
80 ******************************************************************************************/
82 integer Search( const TString<TChar>& haystack, integer startIdx= 0 );
83
84}; // class TSubstringSearch
85
102
103}} // namespace alib[::strings::util]
104
105
106/// Type alias in namespace \b alib.
107template<lang::Case TSensitivity>
109
110/// Type alias in namespace \b alib.
111template<lang::Case TSensitivity>
113
114/// Type alias in namespace \b alib.
115template<lang::Case TSensitivity>
117
118
119} // namespace alib
120
121
122#endif // HPP_ALIB_STRINGS_UTIL_SUBSEARCH
TSubstringSearch(const TString< TChar > &pNeedle=nullptr)
Definition subsearch.hpp:53
ALIB_API integer Search(const TString< TChar > &haystack, integer startIdx=0)
Definition subsearch.cpp:61
ALIB_API void Compile(const TString< TChar > &needle)
Definition subsearch.cpp:27
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286