ALib C++ Library
Library Version: 2511 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
18 module ALib.Characters.Functions;
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 TChar* start= &array[0];
41 TChar* end = &array[length-1];
42 while( start < end ) {
43 TChar tmp= *start;
44 *start = *end;
45 *end = tmp;
46 ++start;
47 --end;
48} }
49
50template<typename TChar>
51integer IndexOfAnyIncluded( const TChar* haystack, integer length,
52 const TChar* needles, integer needlesLength ) {
53 if ( length == -1 ) length= integer( Length( haystack ) );
54 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
55
56 const TChar* end= haystack + length;
57
58 const TChar* s= haystack;
59 while( s != end ) {
60 for( integer i= 0; i < needlesLength ; ++i )
61 if( *(needles + i) == *s )
62 return s - haystack;
63 ++s;
64 }
65
66 return -1;
67}
68
69template<typename TChar>
70integer IndexOfAnyExcluded( const TChar* haystack, integer length,
71 const TChar* needles, integer needlesLength ) {
72 if ( length == -1 ) length= integer( Length( haystack ) );
73 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
74
75 const TChar* end= haystack + length;
76 const TChar* s = haystack - 1;
77 while( ++s != end ) {
78 integer i;
79 for( i= 0; i < needlesLength ; ++i )
80 if( needles[i] == *s )
81 break;
82 if ( i == needlesLength )
83 return s - haystack;
84 }
85
86 return -1;
87}
88
89template<typename TChar>
90integer LastIndexOfAnyInclude( const TChar* haystack, integer startPos,
91 const TChar* needles, integer needlesLength ) {
92 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
93
94 const TChar* s= haystack + startPos;
95 while( s >= haystack ) {
96 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
97 for( integer i= 0; i < needlesLength ; ++i )
98 if( *(needles + i) == *s )
99 return s - haystack;
100
101 --s;
102 }
103 return -1;
104}
105
106template<typename TChar>
107integer LastIndexOfAnyExclude( const TChar* haystack, integer startPos,
108 const TChar* needles, integer needlesLength ) {
109 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
110
111 const TChar* s= haystack + startPos;
112 while( s >= haystack ) {
113 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
114 {
115 integer i;
116 for( i= 0; i < needlesLength ; ++i )
117 if( needles[i] == *s )
118 break;
119 if ( i == needlesLength )
120 return s - haystack;
121 }
122 --s;
123 }
124 return -1;
125}
126
127template<typename TChar>
128integer IndexOfFirstDifference( const TChar* haystack, integer haystackLength,
129 const TChar* needle, integer needleLength,
130 lang::Case sensitivity ) {
131 if ( haystackLength == -1 ) haystackLength= integer( Length( haystack ) );
132 if ( needleLength == -1 ) needleLength= integer( Length( needle ) );
133
134 integer idx= 0;
135
136 if ( sensitivity == lang::Case::Sensitive ) {
137 while( idx != haystackLength
138 && idx != needleLength
139 && haystack[idx] == needle[idx] )
140 ++idx;
141 } else {
142 while( idx != haystackLength
143 && idx != needleLength
144 && ToUpper( haystack[idx] )
145 == ToUpper( needle[idx] ) )
146 ++idx;
147 }
148 return idx;
149}
150
151
152//##################################################################################################
153// NString
154//##################################################################################################
155
161template void Reverse <nchar>( nchar*,integer );
162
163//##################################################################################################
164// WString
165//##################################################################################################
166#if !ALIB_CHARACTERS_NATIVE_WCHAR
167template<> void Fill<wchar>( wchar* dest, integer length, wchar c )
168{
169 wchar* end= dest + length;
170 while( dest != end )
171 *dest++= c;
172}
173
174template<> int CompareIgnoreCase<wchar>( const wchar* str1, const wchar* str2, integer cmpLength )
175{
176 const wchar* end= str1 + cmpLength;
177 int diff;
178 while( str1 != end )
179 if( ( diff= int( towupper(wint_t(*str1++)))
180 - int( towupper(wint_t(*str2++))) ) != 0 )
181 return diff;
182 return 0;
183}
184
185template<> integer IndexOfAnyIncludedZT<wchar>( const wchar* haystack, const wchar* needles )
186{
187 const wchar* h= haystack;
188 while( *h != 0)
189 {
190 const wchar* n= needles;
191 while( *n != 0 )
192 {
193 if (*h == *n )
194 return h - haystack;
195 ++n;
196 }
197 ++h;
198 }
199 return -1;
200}
201
202template<> integer IndexOfAnyExcludedZT<wchar>( const wchar* haystack, const wchar* needles )
203{
204 const wchar* h= haystack;
205 while( *h != 0)
206 {
207 const wchar* n= needles;
208 while( *n != 0 )
209 {
210 if (*h == *n )
211 break;
212 ++n;
213 }
214 if(!*n)
215 return h - haystack;
216 ++h;
217 }
218 return -1;
219}
220#endif
221
227template void Reverse <wchar>( wchar*,integer );
228
229
230//##################################################################################################
231// Strange character string
232//##################################################################################################
238template void Reverse <xchar>( xchar*,integer );
239
240#if ALIB_CHARACTERS_NATIVE_WCHAR
241template<> void Fill<xchar>( xchar* dest, integer length, xchar c ) {
242 xchar* end= dest + length;
243 while( dest != end )
244 *dest++= c;
245}
246
247template<> int CompareIgnoreCase<xchar>(const xchar* str1, const xchar* str2, integer cmpLength) {
248 const xchar* end= str1 + cmpLength;
249 int diff;
250 while( str1 != end )
251 if( ( diff= int( towupper(wint_t(*str1++)))
252 - int( towupper(wint_t(*str2++))) ) != 0 )
253 return diff;
254 return 0;
255}
256
257template<> integer IndexOfAnyIncludedZT<xchar>( const xchar* haystack, const xchar* needles ) {
258 const xchar* h= haystack;
259 while( *h != 0) {
260 const xchar* n= needles;
261 while( *n != 0 ) {
262 if (*h == *n )
263 return h - haystack;
264 ++n;
265 }
266 ++h;
267 }
268 return -1;
269}
270
271template<> integer IndexOfAnyExcludedZT<xchar>( const xchar* haystack, const xchar* needles ) {
272 const xchar* h= haystack;
273 while( *h != 0) {
274 const xchar* n= needles;
275 while( *n != 0 ) {
276 if (*h == *n )
277 break;
278 ++n;
279 }
280 if(!*n)
281 return h - haystack;
282 ++h;
283 }
284 return -1;
285}
286#endif
287
288
289//! @endcond
290
291
292}} // namespace [alib::character]
293
294# include "ALib.Lang.CIMethods.H"
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
PLATFORM_SPECIFIC wchar
Definition chartypes.inl:38
integer Length(const TChar *cstring)
Definition functions.inl:89
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:577