ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
functions.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 ======================================
15#include "alib/alib.inl"
16// =========================================== Module ==========================================
17#if ALIB_C20_MODULES
19 import ALib.Lang;
20#else
21# include "ALib.Lang.H"
23#endif
24// ====================================== Implementation =======================================
26
27namespace alib {
28
29
30/// This is the reference documentation of sub-namespace \c characters of the \aliblink, which
31/// holds types of library module \alib_characters.
32///
33/// Extensive documentation for this module is provided with
34/// \ref alib_mod_characters "ALib Module Characters - Programmer's Manual".
35namespace characters {
36
37//! @cond NO_DOX
38template<typename TChar>
39void Reverse( TChar* array, integer length )
40{
41 TChar* start= &array[0];
42 TChar* end = &array[length-1];
43 while( start < end )
44 {
45 TChar tmp= *start;
46 *start = *end;
47 *end = tmp;
48 ++start;
49 --end;
50 }
51}
52
53template<typename TChar>
54integer IndexOfAnyIncluded( const TChar* haystack, integer length,
55 const TChar* needles, integer needlesLength )
56{
57 if ( length == -1 ) length= integer( Length( haystack ) );
58 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
59
60 const TChar* end= haystack + length;
61
62 const TChar* s= haystack;
63 while( s != end )
64 {
65 for( integer i= 0; i < needlesLength ; ++i )
66 if( *(needles + i) == *s )
67 return s - haystack;
68 ++s;
69 }
70
71 return -1;
72}
73
74template<typename TChar>
75integer IndexOfAnyExcluded( const TChar* haystack, integer length,
76 const TChar* needles, integer needlesLength )
77{
78 if ( length == -1 ) length= integer( Length( haystack ) );
79 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
80
81 const TChar* end= haystack + length;
82 const TChar* s = haystack - 1;
83 while( ++s != end )
84 {
85 integer i;
86 for( i= 0; i < needlesLength ; ++i )
87 if( needles[i] == *s )
88 break;
89 if ( i == needlesLength )
90 return s - haystack;
91 }
92
93 return -1;
94}
95
96template<typename TChar>
97integer LastIndexOfAnyInclude( const TChar* haystack, integer startPos,
98 const TChar* needles, integer needlesLength )
99{
100 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
101
102 const TChar* s= haystack + startPos;
103 while( s >= haystack )
104 {
105 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
106 for( integer i= 0; i < needlesLength ; ++i )
107 if( *(needles + i) == *s )
108 return s - haystack;
109
110 --s;
111 }
112 return -1;
113}
114
115template<typename TChar>
116integer LastIndexOfAnyExclude( const TChar* haystack, integer startPos,
117 const TChar* needles, integer needlesLength )
118{
119 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
120
121 const TChar* s= haystack + startPos;
122 while( s >= haystack )
123 {
124 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
125 {
126 integer i;
127 for( i= 0; i < needlesLength ; ++i )
128 if( needles[i] == *s )
129 break;
130 if ( i == needlesLength )
131 return s - haystack;
132 }
133 --s;
134 }
135 return -1;
136}
137
138template<typename TChar>
139integer IndexOfFirstDifference( const TChar* haystack, integer haystackLength,
140 const TChar* needle, integer needleLength,
141 lang::Case sensitivity )
142{
143 if ( haystackLength == -1 ) haystackLength= integer( Length( haystack ) );
144 if ( needleLength == -1 ) needleLength= integer( Length( needle ) );
145
146 integer idx= 0;
147
148 if ( sensitivity == lang::Case::Sensitive )
149 {
150 while( idx != haystackLength
151 && idx != needleLength
152 && haystack[idx] == needle[idx] )
153 ++idx;
154 }
155 else
156 {
157 while( idx != haystackLength
158 && idx != needleLength
159 && ToUpper( haystack[idx] )
160 == ToUpper( needle[idx] ) )
161 ++idx;
162 }
163 return idx;
164}
165
166
167// #################################################################################################
168// NString
169// #################################################################################################
170
176template void Reverse <nchar>( nchar*,integer );
177
178// #################################################################################################
179// WString
180// #################################################################################################
181#if !ALIB_CHARACTERS_NATIVE_WCHAR
182template<> void Fill<wchar>( wchar* dest, integer length, wchar c )
183{
184 wchar* end= dest + length;
185 while( dest != end )
186 *dest++= c;
187}
188
189template<> int CompareIgnoreCase<wchar>( const wchar* str1, const wchar* str2, integer cmpLength )
190{
191 const wchar* end= str1 + cmpLength;
192 int diff;
193 while( str1 != end )
194 if( ( diff= int( towupper(wint_t(*str1++)))
195 - int( towupper(wint_t(*str2++))) ) != 0 )
196 return diff;
197 return 0;
198}
199
200template<> integer IndexOfAnyIncludedZT<wchar>( const wchar* haystack, const wchar* needles )
201{
202 const wchar* h= haystack;
203 while( *h != 0)
204 {
205 const wchar* n= needles;
206 while( *n != 0 )
207 {
208 if (*h == *n )
209 return h - haystack;
210 ++n;
211 }
212 ++h;
213 }
214 return -1;
215}
216
217template<> integer IndexOfAnyExcludedZT<wchar>( const wchar* haystack, const wchar* needles )
218{
219 const wchar* h= haystack;
220 while( *h != 0)
221 {
222 const wchar* n= needles;
223 while( *n != 0 )
224 {
225 if (*h == *n )
226 break;
227 ++n;
228 }
229 if(!*n)
230 return h - haystack;
231 ++h;
232 }
233 return -1;
234}
235#endif
236
242template void Reverse <wchar>( wchar*,integer );
243
244
245// #################################################################################################
246// Strange character string
247// #################################################################################################
253template void Reverse <xchar>( xchar*,integer );
254
255#if ALIB_CHARACTERS_NATIVE_WCHAR
256template<> void Fill<xchar>( xchar* dest, integer length, xchar c )
257{
258 xchar* end= dest + length;
259 while( dest != end )
260 *dest++= c;
261}
262
263template<> int CompareIgnoreCase<xchar>( const xchar* str1, const xchar* str2, integer cmpLength )
264{
265 const xchar* end= str1 + cmpLength;
266 int diff;
267 while( str1 != end )
268 if( ( diff= int( towupper(wint_t(*str1++)))
269 - int( towupper(wint_t(*str2++))) ) != 0 )
270 return diff;
271 return 0;
272}
273
274template<> integer IndexOfAnyIncludedZT<xchar>( const xchar* haystack, const xchar* needles )
275{
276 const xchar* h= haystack;
277 while( *h != 0)
278 {
279 const xchar* n= needles;
280 while( *n != 0 )
281 {
282 if (*h == *n )
283 return h - haystack;
284 ++n;
285 }
286 ++h;
287 }
288 return -1;
289}
290
291template<> integer IndexOfAnyExcludedZT<xchar>( const xchar* haystack, const xchar* needles )
292{
293 const xchar* h= haystack;
294 while( *h != 0)
295 {
296 const xchar* n= needles;
297 while( *n != 0 )
298 {
299 if (*h == *n )
300 break;
301 ++n;
302 }
303 if(!*n)
304 return h - haystack;
305 ++h;
306 }
307 return -1;
308}
309#endif
310
311
312//! @endcond
313
314
315}} // namespace [alib::character]
316
317# include "ALib.Lang.CIMethods.H"
318
319
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1049
PLATFORM_SPECIFIC wchar
Definition chartypes.inl:38
integer Length(const TChar *cstring)
Definition functions.inl:91
integer IndexOfFirstDifference(const TChar *haystack, integer haystackLength, const TChar *needle, integer needleLength, lang::Case sensitivity)
integer LastIndexOfAnyExclude(const TChar *haystack, integer startIdx, const TChar *needles, integer needlesLength)
void Reverse(TChar *src, integer length)
PLATFORM_SPECIFIC xchar
Definition chartypes.inl:51
TChar ToUpper(TChar c)
integer LastIndexOfAnyInclude(const TChar *haystack, integer startIdx, const TChar *needles, integer needlesLength)
integer IndexOfAnyIncludedZT(const TChar *haystack, const TChar *needles)
integer IndexOfAnyIncluded(const TChar *haystack, integer haystackLength, const TChar *needles, integer needlesLength)
integer IndexOfAnyExcluded(const TChar *haystack, integer haystackLength, const TChar *needles, integer needlesLength)
integer IndexOfAnyExcludedZT(const TChar *haystack, const TChar *needles)
int CompareIgnoreCase(const TChar *lhs, const TChar *rhs, integer cmpLength)
Case
Denotes upper and lower case character treatment.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TFill< character > Fill
Type alias in namespace alib.
Definition format.inl:581