ALib C++ Library
Library Version: 2402 R1
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 !defined(ALIB_DOX)
10#if !defined (HPP_ALIB_STRINGS_UTIL_SUBSEARCH)
12#endif
13#endif // !defined(ALIB_DOX)
14
15
16namespace alib { namespace strings { namespace util {
17
18
19template<typename TChar, lang::Case TSensitivity>
21{
22 if( kmpTable )
23 delete[] kmpTable;
24}
25
26template<typename TChar, lang::Case TSensitivity>
28{
29 if (pNeedle.IsNull() )
30 {
31 needle.SetNull();
32 return;
33 }
34 needle.Reset( pNeedle );
35 if( needle.Length() == 0 )
36 return;
37
38 if( kmpTableLength < needle.Length() )
39 {
40 if( kmpTable )
41 delete[] kmpTable;
42
43 kmpTable= new integer[static_cast<size_t>( needle.Length() + 1 )];
44 kmpTableLength= needle.Length();
45 }
46 integer needleIdx= 0;
47 integer pfxLen= kmpTable[0]= -1;
48 while (needleIdx != needle.Length())
49 {
51 while ((pfxLen != -1) && !characters::CharArray<TChar>::template Equal<TSensitivity>(needle.Buffer()[needleIdx], needle.Buffer()[pfxLen]))
52 pfxLen= kmpTable[pfxLen];
53 ++needleIdx;
54 ++pfxLen;
55 kmpTable[needleIdx]= characters::CharArray<TChar>::template Equal<TSensitivity>(needle.Buffer()[needleIdx], needle.Buffer()[pfxLen]) ? kmpTable[pfxLen] : pfxLen;
57 }
58}
59
60template<typename TChar, lang::Case TSensitivity>
62{
63 if ( needle.IsNull() )
64 return -1;
65 if ( haystackIdx < 0 ) haystackIdx= 0;
66 if ( haystackIdx + needle.Length() > haystack.Length() ) return -1;
67
68 if( needle.Length() == 0 )
69 return haystackIdx;
70
71 integer needleIdx = 0;
72 while (haystackIdx != haystack.Length())
73 {
75 while ((needleIdx != -1) && !characters::CharArray<TChar>::template Equal<TSensitivity>( haystack.Buffer()[haystackIdx],
76 needle .Buffer()[needleIdx] ) )
77 needleIdx= kmpTable[needleIdx];
79 ++haystackIdx;
80 ++needleIdx;
81 if ( needleIdx >= needle.Length() )
82 return (haystackIdx - needleIdx);
83 }
84
85 return -1;
86}
87
88//! @cond NO_DOX
105//! @endcond
106
107
108}}} // namespace [alib::strings::util]
constexpr bool IsNull() const
Definition string.hpp:395
constexpr integer Length() const
Definition string.hpp:357
constexpr const TChar * Buffer() const
Definition string.hpp:350
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_WARNINGS_RESTORE
Definition alib.hpp:715
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286