ALib C++ Library
by
Library Version:
2402 R1
Documentation generated by
Loading...
Searching...
No Matches
home
dev
A-Worx
ALib
src
alib
strings
util
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)
12
# include "
alib/strings/astring.hpp
"
13
#endif
14
15
16
namespace
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
**************************************************************************************************/
32
template
<
typename
TChar, lang::Case TSensitivity= lang::Case::Sensitive>
33
class
TSubstringSearch
34
{
35
protected
:
36
/** The needle set Knuth-Morris-Pratt prefix length table. */
37
TAString<TChar>
needle
;
38
39
/** The Knuth-Morris-Pratt prefix length table. */
40
integer
*
kmpTable
=
nullptr
;
41
42
/** Length of #kmpTable. */
43
integer
kmpTableLength
= 0;
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
******************************************************************************************/
61
ALIB_API
62
~TSubstringSearch
();
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
******************************************************************************************/
69
ALIB_API
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
******************************************************************************************/
81
ALIB_API
82
integer
Search
(
const
TString<TChar>
& haystack,
integer
startIdx= 0 );
83
84
};
// class TSubstringSearch
85
86
extern
template
ALIB_API
TSubstringSearch<nchar, lang::Case::Sensitive>::TSubstringSearch
(
const
TString<nchar>
&);
87
extern
template
ALIB_API
TSubstringSearch<nchar, lang::Case::Ignore >::TSubstringSearch
(
const
TString<nchar>
&);
88
extern
template
ALIB_API
TSubstringSearch<wchar, lang::Case::Sensitive>::TSubstringSearch
(
const
TString<wchar>
&);
89
extern
template
ALIB_API
TSubstringSearch<wchar, lang::Case::Ignore >::TSubstringSearch
(
const
TString<wchar>
&);
90
extern
template
ALIB_API
TSubstringSearch<nchar, lang::Case::Sensitive>::~TSubstringSearch
();
91
extern
template
ALIB_API
TSubstringSearch<nchar, lang::Case::Ignore >::~TSubstringSearch
();
92
extern
template
ALIB_API
TSubstringSearch<wchar, lang::Case::Sensitive>::~TSubstringSearch
();
93
extern
template
ALIB_API
TSubstringSearch<wchar, lang::Case::Ignore >::~TSubstringSearch
();
94
extern
template
ALIB_API
void
TSubstringSearch<nchar, lang::Case::Sensitive>::Compile
(
const
TString<nchar>
&);
95
extern
template
ALIB_API
void
TSubstringSearch<nchar, lang::Case::Ignore >::Compile
(
const
TString<nchar>
&);
96
extern
template
ALIB_API
void
TSubstringSearch<wchar, lang::Case::Sensitive>::Compile
(
const
TString<wchar>
&);
97
extern
template
ALIB_API
void
TSubstringSearch<wchar, lang::Case::Ignore >::Compile
(
const
TString<wchar>
&);
98
extern
template
ALIB_API
integer
TSubstringSearch<nchar, lang::Case::Sensitive>::Search
(
const
TString<nchar>
&,
integer
);
99
extern
template
ALIB_API
integer
TSubstringSearch<nchar, lang::Case::Ignore >::Search
(
const
TString<nchar>
&,
integer
);
100
extern
template
ALIB_API
integer
TSubstringSearch<wchar, lang::Case::Sensitive>::Search
(
const
TString<wchar>
&,
integer
);
101
extern
template
ALIB_API
integer
TSubstringSearch<wchar, lang::Case::Ignore >::Search
(
const
TString<wchar>
&,
integer
);
102
103
}}
// namespace alib[::strings::util]
104
105
106
/// Type alias in namespace \b alib.
107
template
<lang::Case TSensitivity>
108
using
NSubstringSearch
=
strings::util::TSubstringSearch<nchar>
;
109
110
/// Type alias in namespace \b alib.
111
template
<lang::Case TSensitivity>
112
using
WSubstringSearch
=
strings::util::TSubstringSearch<wchar>
;
113
114
/// Type alias in namespace \b alib.
115
template
<lang::Case TSensitivity>
116
using
SubstringSearch
=
strings::util::TSubstringSearch<character>
;
117
118
119
}
// namespace alib
120
121
122
#endif
// HPP_ALIB_STRINGS_UTIL_SUBSEARCH
astring.hpp
alib::strings::TAString
Definition
strings/fwds.hpp:37
alib::strings::TString
Definition
string.hpp:74
alib::strings::util::TSubstringSearch
Definition
subsearch.hpp:34
alib::strings::util::TSubstringSearch::~TSubstringSearch
ALIB_API ~TSubstringSearch()
Definition
subsearch.cpp:20
alib::strings::util::TSubstringSearch::kmpTable
integer * kmpTable
Definition
subsearch.hpp:40
alib::strings::util::TSubstringSearch::kmpTableLength
integer kmpTableLength
Definition
subsearch.hpp:43
alib::strings::util::TSubstringSearch::needle
TAString< TChar > needle
Definition
subsearch.hpp:37
alib::strings::util::TSubstringSearch::TSubstringSearch
TSubstringSearch(const TString< TChar > &pNeedle=nullptr)
Definition
subsearch.hpp:53
alib::strings::util::TSubstringSearch::Search
ALIB_API integer Search(const TString< TChar > &haystack, integer startIdx=0)
Definition
subsearch.cpp:61
alib::strings::util::TSubstringSearch::Compile
ALIB_API void Compile(const TString< TChar > &needle)
Definition
subsearch.cpp:27
ALIB_API
#define ALIB_API
Definition
alib.hpp:538
alib
Definition
alib.cpp:57
alib::integer
lang::integer integer
Type alias in namespace alib.
Definition
integers.hpp:286