ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
search.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16//============================================== Module ============================================
17#if ALIB_C20_MODULES
18 module ALib.Strings.Search;
19 import ALib.Characters.Functions;
20#else
22# include "ALib.Strings.Search.H"
23#endif
24//========================================== Implementation ========================================
25namespace alib { namespace strings { namespace util {
26
27template<typename TChar, lang::Case TSensitivity>
29
30template<typename TChar, lang::Case TSensitivity>
32 if (pNeedle.IsNull() ) {
33 needle.SetNull();
34 return;
35 }
36 needle.Reset( pNeedle );
37 if( needle.Length() == 0 )
38 return;
39
40 if( kmpTableLength < needle.Length() ) {
41 if( kmpTable )
42 delete[] kmpTable;
43
44 kmpTable= new integer[size_t( needle.Length() + 1 )];
45 kmpTableLength= needle.Length();
46 }
47 integer needleIdx= 0;
48 integer pfxLen= kmpTable[0]= -1;
49 for (;;) {
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;
60}
61
62template<typename TChar, lang::Case TSensitivity>
64 integer haystackIdx ) {
65 if ( needle.IsNull() )
66 return -1;
67 if ( haystackIdx < 0 ) haystackIdx= 0;
68 if ( haystackIdx + needle.Length() > haystack.Length() ) return -1;
69
70 if( needle.Length() == 0 )
71 return haystackIdx;
72
73 integer needleIdx = 0;
74 while (haystackIdx != haystack.Length()) {
75 while ((needleIdx != -1) && !characters::Equal<TChar,TSensitivity>( haystack.Buffer()[haystackIdx],
76 needle .Buffer()[needleIdx] ) )
77 needleIdx= kmpTable[needleIdx];
78 ++haystackIdx;
79 ++needleIdx;
80 if ( needleIdx >= needle.Length() )
81 return (haystackIdx - needleIdx);
82 }
83
84 return -1;
85}
86
87//! @cond NO_DOX
104//! @endcond
105
106
107}}} // namespace [alib::strings::util]
constexpr integer Length() const
Definition string.inl:316
constexpr const TChar * Buffer() const
Definition string.inl:311
constexpr bool IsNull() const
Definition string.inl:350
TStringSearch(const TString< TChar > &pNeedle=nullptr)
Definition search.inl:43
TAString< TChar, lang::HeapAllocator > needle
The needle set Knuth-Morris-Pratt prefix length table.
Definition search.inl:29
ALIB_DLL integer Search(const TString< TChar > &haystack, integer startIdx=0)
Definition search.cpp:63
integer kmpTableLength
Length of kmpTable.
Definition search.inl:35
ALIB_DLL ~TStringSearch()
Destructor.
Definition search.cpp:28
ALIB_DLL void Compile(const TString< TChar > &needle)
Definition search.cpp:31
integer * kmpTable
The Knuth-Morris-Pratt prefix length table.
Definition search.inl:32
bool Equal(TChar lhs, TRhs rhs)
Definition functions.inl:62
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149