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