ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
subsearch.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
12#endif // !DOXYGEN
13
14
15namespace alib { namespace strings { namespace util {
16
17
18template<typename TChar, lang::Case TSensitivity>
20{
21 if( kmpTable )
22 delete[] kmpTable;
23}
24
25template<typename TChar, lang::Case TSensitivity>
27{
28 if (pNeedle.IsNull() )
29 {
30 needle.SetNull();
31 return;
32 }
33 needle.Reset( pNeedle );
34 if( needle.Length() == 0 )
35 return;
36
37 if( kmpTableLength < needle.Length() )
38 {
39 if( kmpTable )
40 delete[] kmpTable;
41
42 kmpTable= new integer[size_t( needle.Length() + 1 )];
43 kmpTableLength= needle.Length();
44 }
45 integer needleIdx= 0;
46 integer pfxLen= kmpTable[0]= -1;
48 for (;;)
49 {
50 while ((pfxLen != -1) && !characters::Equal<TChar,TSensitivity>(needle.Buffer()[needleIdx], needle.Buffer()[pfxLen]))
51 pfxLen= kmpTable[pfxLen];
52 ++needleIdx;
53 if (needleIdx == needle.Length())
54 break;
55
56 ++pfxLen;
57 kmpTable[needleIdx]= characters::Equal<TChar,TSensitivity>(needle.Buffer()[needleIdx], needle.Buffer()[pfxLen]) ? kmpTable[pfxLen] : pfxLen;
58 }
59 kmpTable[needleIdx]= pfxLen;
61}
62
63template<typename TChar, lang::Case TSensitivity>
65{
66 if ( needle.IsNull() )
67 return -1;
68 if ( haystackIdx < 0 ) haystackIdx= 0;
69 if ( haystackIdx + needle.Length() > haystack.Length() ) return -1;
70
71 if( needle.Length() == 0 )
72 return haystackIdx;
73
74 integer needleIdx = 0;
75 while (haystackIdx != haystack.Length())
76 {
78 while ((needleIdx != -1) && !characters::Equal<TChar,TSensitivity>( haystack.Buffer()[haystackIdx],
79 needle .Buffer()[needleIdx] ) )
80 needleIdx= kmpTable[needleIdx];
82 ++haystackIdx;
83 ++needleIdx;
84 if ( needleIdx >= needle.Length() )
85 return (haystackIdx - needleIdx);
86 }
87
88 return -1;
89}
90
91//! @cond NO_DOX
108//! @endcond
109
110
111}}} // namespace [alib::strings::util]
112
constexpr bool IsNull() const
Definition string.hpp:364
constexpr integer Length() const
Definition string.hpp:326
constexpr const TChar * Buffer() const
Definition string.hpp:319
ALIB_API ~TSubstringSearch()
Destructor.
Definition subsearch.cpp:19
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_WARNINGS_RESTORE
Definition alib.hpp:849
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:760
bool Equal(TChar lhs, TRhs rhs)
Definition alib.cpp:69
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273