ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
chararray.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_CHARACTERS_CHARARRAY)
12#endif
13#endif // !defined(ALIB_DOX)
14
15
17
18namespace alib {
19
20/**
21 * This is the reference documentation of sub-namespace \c characters of the \aliblink, which
22 * holds types of library module \alib_characters.
23 *
24 * Extensive documentation for this module is provided with
25 * \ref alib_mod_characters "ALib Module Characters - Programmer's Manual".
26 */
27namespace characters {
28
29//! @cond NO_DOX
30template<typename TChar>
31void CharArray<TChar>::Reverse( TChar* array, integer length )
32{
33 TChar* start= &array[0];
34 TChar* end = &array[length-1];
35 while( start < end )
36 {
37 TChar tmp= *start;
38 *start = *end;
39 *end = tmp;
40 ++start;
41 --end;
42 }
43}
44
45template<typename TChar>
46integer CharArray<TChar>::IndexOfAnyIncluded( const TChar* haystack, integer length,
47 const TChar* needles, integer needlesLength )
48{
49 if ( length == -1 ) length= static_cast<integer>( Length( haystack ) );
50 if ( needlesLength == -1 ) needlesLength= static_cast<integer>( Length( needles ) );
51
52 const TChar* end= haystack + length;
53
54 const TChar* s= haystack;
55 while( s != end )
56 {
57 for( integer i= 0; i < needlesLength ; ++i )
58 if( *(needles + i) == *s )
59 return s - haystack;
60 ++s;
61 }
62
63 return -1;
64}
65
66template<typename TChar>
67integer CharArray<TChar>::IndexOfAnyExcluded( const TChar* haystack, integer length,
68 const TChar* needles, integer needlesLength )
69{
70 if ( length == -1 ) length= static_cast<integer>( Length( haystack ) );
71 if ( needlesLength == -1 ) needlesLength= static_cast<integer>( Length( needles ) );
72
73 const TChar* end= haystack + length;
74 const TChar* s = haystack - 1;
75 while( ++s != end )
76 {
77 integer i;
78 for( i= 0; i < needlesLength ; ++i )
79 if( needles[i] == *s )
80 break;
81 if ( i == needlesLength )
82 return s - haystack;
83 }
84
85 return -1;
86}
87
88template<typename TChar>
89integer CharArray<TChar>::LastIndexOfAnyInclude( const TChar* haystack, integer startPos,
90 const TChar* needles, integer needlesLength )
91{
92 if ( needlesLength == -1 ) needlesLength= static_cast<integer>( Length( needles ) );
93
94 const TChar* s= haystack + startPos;
95 while( s >= haystack )
96 {
97 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
98 for( integer i= 0; i < needlesLength ; ++i )
99 if( *(needles + i) == *s )
100 return s - haystack;
101
102 --s;
103 }
104 return -1;
105}
106
107template<typename TChar>
108integer CharArray<TChar>::LastIndexOfAnyExclude( const TChar* haystack, integer startPos,
109 const TChar* needles, integer needlesLength )
110{
111 if ( needlesLength == -1 ) needlesLength= static_cast<integer>( Length( needles ) );
112
113 const TChar* s= haystack + startPos;
114 while( s >= haystack )
115 {
116 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
117 {
118 integer i;
119 for( i= 0; i < needlesLength ; ++i )
120 if( needles[i] == *s )
121 break;
122 if ( i == needlesLength )
123 return s - haystack;
124 }
125 --s;
126 }
127 return -1;
128}
129
130template<typename TChar>
131integer CharArray<TChar>::IndexOfFirstDifference( const TChar* haystack, integer haystackLength,
132 const TChar* needle, integer needleLength,
133 lang::Case sensitivity )
134{
135 if ( haystackLength == -1 ) haystackLength= static_cast<integer>( Length( haystack ) );
136 if ( needleLength == -1 ) needleLength= static_cast<integer>( Length( needle ) );
137
138 integer idx= 0;
139
140 if ( sensitivity == lang::Case::Sensitive )
141 {
142 while( idx != haystackLength
143 && idx != needleLength
144 && haystack[idx] == needle[idx] )
145 ++idx;
146 }
147 else
148 {
149 while( idx != haystackLength
150 && idx != needleLength
151 && ToUpper( haystack[idx] )
152 == ToUpper( needle[idx] ) )
153 ++idx;
154 }
155 return idx;
156}
157
158
159// #################################################################################################
160// NString
161// #################################################################################################
162
168template void CharArray<nchar>::Reverse ( nchar*,integer );
169
170// #################################################################################################
171// WString
172// #################################################################################################
173#if !ALIB_CHARACTERS_NATIVE_WCHAR
174template<> void CharArray<wchar>::Fill( wchar* dest, integer length, wchar c )
175{
176 wchar* end= dest + length;
177 while( dest != end )
178 *dest++= c;
179}
180
181template<> int CharArray<wchar>::CompareIgnoreCase( const wchar* str1, const wchar* str2, integer cmpLength )
182{
183 const wchar* end= str1 + cmpLength;
184 int diff;
185 while( str1 != end )
186 if( ( diff= static_cast<int>( towupper(static_cast<wint_t>(*str1++)))
187 - static_cast<int>( towupper(static_cast<wint_t>(*str2++))) ) != 0 )
188 return diff;
189 return 0;
190}
191
192template<> integer CharArray<wchar>::IndexOfAnyIncludedZT( const wchar* haystack, const wchar* needles )
193{
194 const wchar* h= haystack;
195 while( *h != 0)
196 {
197 const wchar* n= needles;
198 while( *n != 0 )
199 {
200 if (*h == *n )
201 return h - haystack;
202 ++n;
203 }
204 ++h;
205 }
206 return -1;
207}
208
209template<> integer CharArray<wchar>::IndexOfAnyExcludedZT( const wchar* haystack, const wchar* needles )
210{
211 const wchar* h= haystack;
212 while( *h != 0)
213 {
214 const wchar* n= needles;
215 while( *n != 0 )
216 {
217 if (*h == *n )
218 break;
219 ++n;
220 }
221 if(!*n)
222 return h - haystack;
223 ++h;
224 }
225 return -1;
226}
227#endif
228
234template void CharArray<wchar>::Reverse ( wchar*,integer );
235
236
237// #################################################################################################
238// Strange character string
239// #################################################################################################
245template void CharArray<xchar>::Reverse ( xchar*,integer );
246
247#if ALIB_CHARACTERS_NATIVE_WCHAR
248template<> void CharArray<xchar>::Fill( xchar* dest, integer length, xchar c )
249{
250 xchar* end= dest + length;
251 while( dest != end )
252 *dest++= c;
253}
254
255template<> int CharArray<xchar>::CompareIgnoreCase( const xchar* str1, const xchar* str2, integer cmpLength )
256{
257 const xchar* end= str1 + cmpLength;
258 int diff;
259 while( str1 != end )
260 if( ( diff= static_cast<int>( towupper(static_cast<wint_t>(*str1++)))
261 - static_cast<int>( towupper(static_cast<wint_t>(*str2++))) ) != 0 )
262 return diff;
263 return 0;
264}
265
266template<> integer CharArray<xchar>::IndexOfAnyIncludedZT( const xchar* haystack, const xchar* needles )
267{
268 const xchar* h= haystack;
269 while( *h != 0)
270 {
271 const xchar* n= needles;
272 while( *n != 0 )
273 {
274 if (*h == *n )
275 return h - haystack;
276 ++n;
277 }
278 ++h;
279 }
280 return -1;
281}
282
283template<> integer CharArray<xchar>::IndexOfAnyExcludedZT( const xchar* haystack, const xchar* needles )
284{
285 const xchar* h= haystack;
286 while( *h != 0)
287 {
288 const xchar* n= needles;
289 while( *n != 0 )
290 {
291 if (*h == *n )
292 break;
293 ++n;
294 }
295 if(!*n)
296 return h - haystack;
297 ++h;
298 }
299 return -1;
300}
301#endif
302
303
304//! @endcond
305
306
307}} // namespace [alib::character]
308
309
310// #################################################################################################
311// Set 'flags' for pretty printers by defining global symbols.
312// See file $ALOX_LIB_PATH/ALox/tools/ideplugins/gdb/alibpp.py for more information
313// #################################################################################################
314
315#if defined(ALIB_GDB_PP_SUPPRESS_CHILDREN)
316 extern int ALIB_PRETTY_PRINTERS_SUPPRESS_CHILDREN;
317 int ALIB_PRETTY_PRINTERS_SUPPRESS_CHILDREN;
318#endif
319
320#if defined(ALIB_GDB_PP_FIND_POINTER_TYPES)
321 extern int ALIB_PRETTY_PRINTERS_FIND_POINTER_TYPES;
322 int ALIB_PRETTY_PRINTERS_FIND_POINTER_TYPES;
323#endif
324
325
326//
327// define further global symbols which are detected by the pretty printer python script for gdb
328//
329#if ALIB_CHARACTERS_WIDE
330 extern int ALIB_PRETTY_PRINTERS_DEFAULT_CHAR_IS_WIDE;
331 int ALIB_PRETTY_PRINTERS_DEFAULT_CHAR_IS_WIDE;
332#endif
333
334#if ALIB_SIZEOF_WCHAR_T == 4
335 extern int ALIB_PRETTY_PRINTERS_WCHAR_SIZE_IS_4;
336 int ALIB_PRETTY_PRINTERS_WCHAR_SIZE_IS_4;
337
338#endif
339
340ALIB_WARNINGS_RESTORE // ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:715
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:984
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
PLATFORM_SPECIFIC xchar
PLATFORM_SPECIFIC wchar
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
static integer IndexOfFirstDifference(const TChar *haystack, integer haystackLength, const TChar *needle, integer needleLength, lang::Case sensitivity)
static integer LastIndexOfAnyExclude(const TChar *haystack, integer startIdx, const TChar *needles, integer needlesLength)
static integer IndexOfAnyIncludedZT(const TChar *haystack, const TChar *needles)
static integer IndexOfAnyExcludedZT(const TChar *haystack, const TChar *needles)
static ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE void Reverse(TChar *src, integer length)
static void Fill(TChar *dest, integer length, TChar value)
static integer IndexOfAnyExcluded(const TChar *haystack, integer haystackLength, const TChar *needles, integer needlesLength)
static int CompareIgnoreCase(const TChar *lhs, const TChar *rhs, integer cmpLength)
static integer LastIndexOfAnyInclude(const TChar *haystack, integer startIdx, const TChar *needles, integer needlesLength)
static ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE integer IndexOfAnyIncluded(const TChar *haystack, integer haystackLength, const TChar *needles, integer needlesLength)