ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
functions.cpp
2
3namespace alib {
4
5
6/// This is the reference documentation of sub-namespace \c characters of the \aliblink, which
7/// holds types of library module \alib_characters.
8///
9/// Extensive documentation for this module is provided with
10/// #"alib_mod_characters;ALib Module Characters - Programmer's Manual".
11namespace characters {
12
13//! @cond NO_DOX
14template<typename TChar>
15void Reverse( TChar* array, integer length ) {
16 TChar* start= &array[0];
17 TChar* end = &array[length-1];
18 while( start < end ) {
19 TChar tmp= *start;
20 *start = *end;
21 *end = tmp;
22 ++start;
23 --end;
24} }
25
26template<typename TChar>
27integer IndexOfAnyIncluded( const TChar* haystack, integer length,
28 const TChar* needles, integer needlesLength ) {
29 if ( length == -1 ) length= integer( Length( haystack ) );
30 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
31
32 const TChar* end= haystack + length;
33
34 const TChar* s= haystack;
35 while( s != end ) {
36 for( integer i= 0; i < needlesLength ; ++i )
37 if( *(needles + i) == *s )
38 return s - haystack;
39 ++s;
40 }
41
42 return -1;
43}
44
45template<typename TChar>
46integer IndexOfAnyExcluded( const TChar* haystack, integer length,
47 const TChar* needles, integer needlesLength ) {
48 if ( length == -1 ) length= integer( Length( haystack ) );
49 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
50
51 const TChar* end= haystack + length;
52 const TChar* s = haystack - 1;
53 while( ++s != end ) {
54 integer i;
55 for( i= 0; i < needlesLength ; ++i )
56 if( needles[i] == *s )
57 break;
58 if ( i == needlesLength )
59 return s - haystack;
60 }
61
62 return -1;
63}
64
65template<typename TChar>
66integer LastIndexOfAnyInclude( const TChar* haystack, integer startPos,
67 const TChar* needles, integer needlesLength ) {
68 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
69
70 const TChar* s= haystack + startPos;
71 while( s >= haystack ) {
72 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
73 for( integer i= 0; i < needlesLength ; ++i )
74 if( *(needles + i) == *s )
75 return s - haystack;
76
77 --s;
78 }
79 return -1;
80}
81
82template<typename TChar>
83integer LastIndexOfAnyExclude( const TChar* haystack, integer startPos,
84 const TChar* needles, integer needlesLength ) {
85 if ( needlesLength == -1 ) needlesLength= integer( Length( needles ) );
86
87 const TChar* s= haystack + startPos;
88 while( s >= haystack ) {
89 ALIB_ASSERT_ERROR( *s != '\0', "CHARS", "Found '\\0' in source string")
90 {
91 integer i;
92 for( i= 0; i < needlesLength ; ++i )
93 if( needles[i] == *s )
94 break;
95 if ( i == needlesLength )
96 return s - haystack;
97 }
98 --s;
99 }
100 return -1;
101}
102
103template<typename TChar>
104integer IndexOfFirstDifference( const TChar* haystack, integer haystackLength,
105 const TChar* needle, integer needleLength,
106 lang::Case sensitivity ) {
107 if ( haystackLength == -1 ) haystackLength= integer( Length( haystack ) );
108 if ( needleLength == -1 ) needleLength= integer( Length( needle ) );
109
110 integer idx= 0;
111
112 if ( sensitivity == lang::Case::Sensitive ) {
113 while( idx != haystackLength
114 && idx != needleLength
115 && haystack[idx] == needle[idx] )
116 ++idx;
117 } else {
118 while( idx != haystackLength
119 && idx != needleLength
120 && ToUpper( haystack[idx] )
121 == ToUpper( needle[idx] ) )
122 ++idx;
123 }
124 return idx;
125}
126
127
128//##################################################################################################
129// NString
130//##################################################################################################
131
137template void Reverse <nchar>( nchar*,integer );
138
139//##################################################################################################
140// WString
141//##################################################################################################
142#if !ALIB_CHARACTERS_NATIVE_WCHAR
143template<> void Fill<wchar>( wchar* dest, integer length, wchar c )
144{
145 wchar* end= dest + length;
146 while( dest != end )
147 *dest++= c;
148}
149
150template<> int CompareIgnoreCase<wchar>( const wchar* str1, const wchar* str2, integer cmpLength )
151{
152 const wchar* end= str1 + cmpLength;
153 int diff;
154 while( str1 != end )
155 if( ( diff= int( towupper(wint_t(*str1++)))
156 - int( towupper(wint_t(*str2++))) ) != 0 )
157 return diff;
158 return 0;
159}
160
161template<> integer IndexOfAnyIncludedZT<wchar>( const wchar* haystack, const wchar* needles )
162{
163 const wchar* h= haystack;
164 while( *h != 0)
165 {
166 const wchar* n= needles;
167 while( *n != 0 )
168 {
169 if (*h == *n )
170 return h - haystack;
171 ++n;
172 }
173 ++h;
174 }
175 return -1;
176}
177
178template<> integer IndexOfAnyExcludedZT<wchar>( const wchar* haystack, const wchar* needles )
179{
180 const wchar* h= haystack;
181 while( *h != 0)
182 {
183 const wchar* n= needles;
184 while( *n != 0 )
185 {
186 if (*h == *n )
187 break;
188 ++n;
189 }
190 if(!*n)
191 return h - haystack;
192 ++h;
193 }
194 return -1;
195}
196#endif
197
203template void Reverse <wchar>( wchar*,integer );
204
205
206//##################################################################################################
207// Strange character string
208//##################################################################################################
214template void Reverse <xchar>( xchar*,integer );
215
216#if ALIB_CHARACTERS_NATIVE_WCHAR
217template<> void Fill<xchar>( xchar* dest, integer length, xchar c ) {
218 xchar* end= dest + length;
219 while( dest != end )
220 *dest++= c;
221}
222
223template<> int CompareIgnoreCase<xchar>(const xchar* str1, const xchar* str2, integer cmpLength) {
224 const xchar* end= str1 + cmpLength;
225 int diff;
226 while( str1 != end )
227 if( ( diff= int( towupper(wint_t(*str1++)))
228 - int( towupper(wint_t(*str2++))) ) != 0 )
229 return diff;
230 return 0;
231}
232
233template<> integer IndexOfAnyIncludedZT<xchar>( const xchar* haystack, const xchar* needles ) {
234 const xchar* h= haystack;
235 while( *h != 0) {
236 const xchar* n= needles;
237 while( *n != 0 ) {
238 if (*h == *n )
239 return h - haystack;
240 ++n;
241 }
242 ++h;
243 }
244 return -1;
245}
246
247template<> integer IndexOfAnyExcludedZT<xchar>( const xchar* haystack, const xchar* needles ) {
248 const xchar* h= haystack;
249 while( *h != 0) {
250 const xchar* n= needles;
251 while( *n != 0 ) {
252 if (*h == *n )
253 break;
254 ++n;
255 }
256 if(!*n)
257 return h - haystack;
258 ++h;
259 }
260 return -1;
261}
262#endif
263
264
265//! @endcond
266
267
268}} // namespace [alib::character]
269
270# include "ALib.Lang.CIMethods.H"
#define ALIB_ASSERT_ERROR(cond, domain,...)
PLATFORM_SPECIFIC xchar
Definition chartypes.hpp:51
integer Length(const TChar *cstring)
Definition functions.hpp: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)
TChar ToUpper(TChar c)
integer LastIndexOfAnyInclude(const TChar *haystack, integer startIdx, const TChar *needles, integer needlesLength)
PLATFORM_SPECIFIC wchar
Definition chartypes.hpp:38
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.
Definition alox.cpp:14
strings::TFill< character > Fill
Type alias in namespace #"%alib".
Definition format.hpp:576
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149