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