]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: string.cpp | |
3 | // Purpose: wxString class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
dd1eaa89 | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
30b21f9a | 13 | #pragma implementation "string.h" |
c801d85f KB |
14 | #endif |
15 | ||
16 | /* | |
17 | * About ref counting: | |
18 | * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init()) | |
19 | * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one | |
20 | * 3) Unlock() decrements nRefs and frees memory if it goes to 0 | |
21 | */ | |
22 | ||
23 | // =========================================================================== | |
24 | // headers, declarations, constants | |
25 | // =========================================================================== | |
26 | ||
27 | // For compilers that support precompilation, includes "wx.h". | |
28 | #include "wx/wxprec.h" | |
29 | ||
30 | #ifdef __BORLANDC__ | |
30b21f9a | 31 | #pragma hdrstop |
c801d85f KB |
32 | #endif |
33 | ||
34 | #ifndef WX_PRECOMP | |
3c024cc2 VZ |
35 | #include "wx/defs.h" |
36 | #include "wx/string.h" | |
37 | #include "wx/intl.h" | |
3096bd2f | 38 | #include "wx/thread.h" |
6b769f3d | 39 | #endif |
c801d85f KB |
40 | |
41 | #include <ctype.h> | |
42 | #include <string.h> | |
43 | #include <stdlib.h> | |
44 | ||
ce3ed50d | 45 | #ifdef __SALFORDC__ |
30b21f9a | 46 | #include <clib.h> |
ce3ed50d JS |
47 | #endif |
48 | ||
ede25f5b | 49 | #if wxUSE_WCSRTOMBS |
fb4e5803 VZ |
50 | #include <wchar.h> // for wcsrtombs(), see comments where it's used |
51 | #endif // GNU | |
52 | ||
c801d85f KB |
53 | #ifdef WXSTRING_IS_WXOBJECT |
54 | IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject) | |
55 | #endif //WXSTRING_IS_WXOBJECT | |
56 | ||
ec2ba3aa OK |
57 | #if wxUSE_UNICODE |
58 | #undef wxUSE_EXPERIMENTAL_PRINTF | |
59 | #define wxUSE_EXPERIMENTAL_PRINTF 1 | |
60 | #endif | |
61 | ||
3168a13f VZ |
62 | // allocating extra space for each string consumes more memory but speeds up |
63 | // the concatenation operations (nLen is the current string's length) | |
77ca46e7 VZ |
64 | // NB: EXTRA_ALLOC must be >= 0! |
65 | #define EXTRA_ALLOC (19 - nLen % 16) | |
3168a13f | 66 | |
c801d85f KB |
67 | // --------------------------------------------------------------------------- |
68 | // static class variables definition | |
69 | // --------------------------------------------------------------------------- | |
70 | ||
8de2e39c | 71 | #ifdef wxSTD_STRING_COMPATIBILITY |
566b84d2 | 72 | const size_t wxString::npos = wxSTRING_MAXLEN; |
8de2e39c | 73 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 74 | |
3168a13f VZ |
75 | // ---------------------------------------------------------------------------- |
76 | // static data | |
77 | // ---------------------------------------------------------------------------- | |
c801d85f | 78 | |
3c024cc2 VZ |
79 | // for an empty string, GetStringData() will return this address: this |
80 | // structure has the same layout as wxStringData and it's data() method will | |
81 | // return the empty string (dummy pointer) | |
82 | static const struct | |
83 | { | |
84 | wxStringData data; | |
2bb67b80 | 85 | wxChar dummy; |
223d09f6 | 86 | } g_strEmpty = { {-1, 0, 0}, wxT('\0') }; |
3c024cc2 | 87 | |
5d33ed2c DW |
88 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
89 | // must define this static for VA or else you get multiply defined symbols everywhere | |
90 | const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100; | |
91 | ||
92 | #endif | |
93 | ||
c801d85f | 94 | // empty C style string: points to 'string data' byte of g_strEmpty |
e90c1d2a | 95 | extern const wxChar WXDLLEXPORT *wxEmptyString = &g_strEmpty.dummy; |
c801d85f | 96 | |
89b892a2 VZ |
97 | // ---------------------------------------------------------------------------- |
98 | // conditional compilation | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
dcf924a3 RR |
101 | #if !defined(__WXSW__) && wxUSE_UNICODE |
102 | #ifdef wxUSE_EXPERIMENTAL_PRINTF | |
103 | #undef wxUSE_EXPERIMENTAL_PRINTF | |
104 | #endif | |
105 | #define wxUSE_EXPERIMENTAL_PRINTF 1 | |
106 | #endif | |
107 | ||
89b892a2 VZ |
108 | // we want to find out if the current platform supports vsnprintf()-like |
109 | // function: for Unix this is done with configure, for Windows we test the | |
110 | // compiler explicitly. | |
378b05f7 VZ |
111 | // |
112 | // FIXME currently, this is only for ANSI (!Unicode) strings, so we call this | |
113 | // function wxVsnprintfA (A for ANSI), should also find one for Unicode | |
114 | // strings in Unicode build | |
89b892a2 | 115 | #ifdef __WXMSW__ |
012286eb | 116 | #if defined(__VISUALC__) || (defined(__MINGW32__) && wxUSE_NORLANDER_HEADERS) |
378b05f7 | 117 | #define wxVsnprintfA _vsnprintf |
89b892a2 VZ |
118 | #endif |
119 | #else // !Windows | |
120 | #ifdef HAVE_VSNPRINTF | |
378b05f7 | 121 | #define wxVsnprintfA vsnprintf |
89b892a2 VZ |
122 | #endif |
123 | #endif // Windows/!Windows | |
124 | ||
378b05f7 | 125 | #ifndef wxVsnprintfA |
89b892a2 VZ |
126 | // in this case we'll use vsprintf() (which is ANSI and thus should be |
127 | // always available), but it's unsafe because it doesn't check for buffer | |
128 | // size - so give a warning | |
378b05f7 | 129 | #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg) |
566b84d2 | 130 | |
57493f9f VZ |
131 | #if defined(__VISUALC__) |
132 | #pragma message("Using sprintf() because no snprintf()-like function defined") | |
133 | #elif defined(__GNUG__) && !defined(__UNIX__) | |
134 | #warning "Using sprintf() because no snprintf()-like function defined" | |
135 | #elif defined(__MWERKS__) | |
378b05f7 | 136 | #warning "Using sprintf() because no snprintf()-like function defined" |
57493f9f | 137 | #endif //compiler |
3f4a0c5b | 138 | #endif // no vsnprintf |
89b892a2 | 139 | |
227b5cd7 VZ |
140 | #ifdef _AIX |
141 | // AIX has vsnprintf, but there's no prototype in the system headers. | |
142 | extern "C" int vsnprintf(char* str, size_t n, const char* format, va_list ap); | |
143 | #endif | |
144 | ||
3168a13f | 145 | // ---------------------------------------------------------------------------- |
c801d85f | 146 | // global functions |
3168a13f | 147 | // ---------------------------------------------------------------------------- |
c801d85f | 148 | |
a533f5c1 | 149 | #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM |
c801d85f KB |
150 | |
151 | // MS Visual C++ version 5.0 provides the new STL headers as well as the old | |
152 | // iostream ones. | |
153 | // | |
154 | // ATTN: you can _not_ use both of these in the same program! | |
a38b83c3 | 155 | |
3f4a0c5b | 156 | istream& operator>>(istream& is, wxString& WXUNUSED(str)) |
c801d85f KB |
157 | { |
158 | #if 0 | |
159 | int w = is.width(0); | |
160 | if ( is.ipfx(0) ) { | |
3f4a0c5b | 161 | streambuf *sb = is.rdbuf(); |
c801d85f KB |
162 | str.erase(); |
163 | while ( true ) { | |
164 | int ch = sb->sbumpc (); | |
165 | if ( ch == EOF ) { | |
3f4a0c5b | 166 | is.setstate(ios::eofbit); |
c801d85f KB |
167 | break; |
168 | } | |
169 | else if ( isspace(ch) ) { | |
170 | sb->sungetc(); | |
171 | break; | |
172 | } | |
dd1eaa89 | 173 | |
c801d85f KB |
174 | str += ch; |
175 | if ( --w == 1 ) | |
176 | break; | |
177 | } | |
178 | } | |
179 | ||
180 | is.isfx(); | |
181 | if ( str.length() == 0 ) | |
3f4a0c5b | 182 | is.setstate(ios::failbit); |
c801d85f KB |
183 | #endif |
184 | return is; | |
185 | } | |
186 | ||
825ba8f0 SB |
187 | ostream& operator<<(ostream& os, const wxString& str) |
188 | { | |
189 | os << str.c_str(); | |
190 | return os; | |
191 | } | |
192 | ||
c801d85f KB |
193 | #endif //std::string compatibility |
194 | ||
378b05f7 VZ |
195 | extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len, |
196 | const wxChar *format, va_list argptr) | |
197 | { | |
198 | #if wxUSE_UNICODE | |
199 | // FIXME should use wvsnprintf() or whatever if it's available | |
200 | wxString s; | |
201 | int iLen = s.PrintfV(format, argptr); | |
202 | if ( iLen != -1 ) | |
203 | { | |
204 | wxStrncpy(buf, s.c_str(), iLen); | |
205 | } | |
206 | ||
207 | return iLen; | |
208 | #else // ANSI | |
b568d04f VZ |
209 | // vsnprintf() will not terminate the string with '\0' if there is not |
210 | // enough place, but we want the string to always be NUL terminated | |
211 | int rc = wxVsnprintfA(buf, len - 1, format, argptr); | |
2f02cb89 VZ |
212 | if ( rc == -1 ) |
213 | { | |
214 | buf[len] = 0; | |
215 | } | |
b568d04f VZ |
216 | |
217 | return rc; | |
378b05f7 VZ |
218 | #endif // Unicode/ANSI |
219 | } | |
220 | ||
221 | extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len, | |
222 | const wxChar *format, ...) | |
223 | { | |
224 | va_list argptr; | |
225 | va_start(argptr, format); | |
226 | ||
227 | int iLen = wxVsnprintf(buf, len, format, argptr); | |
228 | ||
229 | va_end(argptr); | |
230 | ||
231 | return iLen; | |
232 | } | |
233 | ||
3168a13f VZ |
234 | // ---------------------------------------------------------------------------- |
235 | // private classes | |
236 | // ---------------------------------------------------------------------------- | |
237 | ||
238 | // this small class is used to gather statistics for performance tuning | |
239 | //#define WXSTRING_STATISTICS | |
240 | #ifdef WXSTRING_STATISTICS | |
241 | class Averager | |
242 | { | |
243 | public: | |
244 | Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } | |
2c3b684c | 245 | ~Averager() |
3168a13f VZ |
246 | { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); } |
247 | ||
c86f1403 | 248 | void Add(size_t n) { m_nTotal += n; m_nCount++; } |
3168a13f VZ |
249 | |
250 | private: | |
c86f1403 | 251 | size_t m_nCount, m_nTotal; |
3168a13f VZ |
252 | const char *m_sz; |
253 | } g_averageLength("allocation size"), | |
254 | g_averageSummandLength("summand length"), | |
255 | g_averageConcatHit("hit probability in concat"), | |
256 | g_averageInitialLength("initial string length"); | |
257 | ||
258 | #define STATISTICS_ADD(av, val) g_average##av.Add(val) | |
259 | #else | |
260 | #define STATISTICS_ADD(av, val) | |
261 | #endif // WXSTRING_STATISTICS | |
262 | ||
c801d85f KB |
263 | // =========================================================================== |
264 | // wxString class core | |
265 | // =========================================================================== | |
266 | ||
267 | // --------------------------------------------------------------------------- | |
268 | // construction | |
269 | // --------------------------------------------------------------------------- | |
270 | ||
c801d85f | 271 | // constructs string of <nLength> copies of character <ch> |
2bb67b80 | 272 | wxString::wxString(wxChar ch, size_t nLength) |
c801d85f KB |
273 | { |
274 | Init(); | |
275 | ||
276 | if ( nLength > 0 ) { | |
277 | AllocBuffer(nLength); | |
f1da2f03 | 278 | |
2bb67b80 OK |
279 | #if wxUSE_UNICODE |
280 | // memset only works on char | |
281 | for (size_t n=0; n<nLength; n++) m_pchData[n] = ch; | |
282 | #else | |
c801d85f | 283 | memset(m_pchData, ch, nLength); |
2bb67b80 | 284 | #endif |
c801d85f KB |
285 | } |
286 | } | |
287 | ||
288 | // takes nLength elements of psz starting at nPos | |
2bb67b80 | 289 | void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength) |
c801d85f KB |
290 | { |
291 | Init(); | |
292 | ||
f6bcfd97 BP |
293 | // if the length is not given, assume the string to be NUL terminated |
294 | if ( nLength == wxSTRING_MAXLEN ) { | |
295 | wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") ); | |
c801d85f | 296 | |
f6bcfd97 BP |
297 | nLength = wxStrlen(psz + nPos); |
298 | } | |
6c68273f | 299 | |
3168a13f VZ |
300 | STATISTICS_ADD(InitialLength, nLength); |
301 | ||
c801d85f KB |
302 | if ( nLength > 0 ) { |
303 | // trailing '\0' is written in AllocBuffer() | |
304 | AllocBuffer(nLength); | |
2bb67b80 | 305 | memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar)); |
c801d85f KB |
306 | } |
307 | } | |
dd1eaa89 | 308 | |
8de2e39c | 309 | #ifdef wxSTD_STRING_COMPATIBILITY |
c801d85f | 310 | |
c801d85f KB |
311 | // poor man's iterators are "void *" pointers |
312 | wxString::wxString(const void *pStart, const void *pEnd) | |
313 | { | |
2bb67b80 OK |
314 | InitWith((const wxChar *)pStart, 0, |
315 | (const wxChar *)pEnd - (const wxChar *)pStart); | |
c801d85f KB |
316 | } |
317 | ||
318 | #endif //std::string compatibility | |
319 | ||
2bb67b80 OK |
320 | #if wxUSE_UNICODE |
321 | ||
322 | // from multibyte string | |
cf2f341a | 323 | wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) |
2bb67b80 OK |
324 | { |
325 | // first get necessary size | |
435595e0 | 326 | size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0; |
2bb67b80 OK |
327 | |
328 | // nLength is number of *Unicode* characters here! | |
eea4f86a | 329 | if ((nLen != (size_t)-1) && (nLen > nLength)) |
2bb67b80 OK |
330 | nLen = nLength; |
331 | ||
332 | // empty? | |
eea4f86a | 333 | if ( (nLen != 0) && (nLen != (size_t)-1) ) { |
2bb67b80 OK |
334 | AllocBuffer(nLen); |
335 | conv.MB2WC(m_pchData, psz, nLen); | |
336 | } | |
337 | else { | |
338 | Init(); | |
339 | } | |
340 | } | |
341 | ||
e90c1d2a | 342 | #else // ANSI |
2bb67b80 | 343 | |
0f3e3e0c | 344 | #if wxUSE_WCHAR_T |
c801d85f | 345 | // from wide string |
f6bcfd97 | 346 | wxString::wxString(const wchar_t *pwz, wxMBConv& conv) |
c801d85f KB |
347 | { |
348 | // first get necessary size | |
f6bcfd97 | 349 | size_t nLen = pwz ? conv.WC2MB((char *) NULL, pwz, 0) : 0; |
c801d85f KB |
350 | |
351 | // empty? | |
eea4f86a | 352 | if ( (nLen != 0) && (nLen != (size_t)-1) ) { |
c801d85f | 353 | AllocBuffer(nLen); |
f6bcfd97 | 354 | conv.WC2MB(m_pchData, pwz, nLen); |
c801d85f KB |
355 | } |
356 | else { | |
357 | Init(); | |
358 | } | |
359 | } | |
e90c1d2a | 360 | #endif // wxUSE_WCHAR_T |
c801d85f | 361 | |
e90c1d2a | 362 | #endif // Unicode/ANSI |
2bb67b80 | 363 | |
c801d85f KB |
364 | // --------------------------------------------------------------------------- |
365 | // memory allocation | |
366 | // --------------------------------------------------------------------------- | |
367 | ||
368 | // allocates memory needed to store a C string of length nLen | |
369 | void wxString::AllocBuffer(size_t nLen) | |
370 | { | |
13111b2a VZ |
371 | // allocating 0 sized buffer doesn't make sense, all empty strings should |
372 | // reuse g_strEmpty | |
373 | wxASSERT( nLen > 0 ); | |
374 | ||
375 | // make sure that we don't overflow | |
376 | wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) - | |
377 | (sizeof(wxStringData) + EXTRA_ALLOC + 1) ); | |
c801d85f | 378 | |
3168a13f VZ |
379 | STATISTICS_ADD(Length, nLen); |
380 | ||
c801d85f KB |
381 | // allocate memory: |
382 | // 1) one extra character for '\0' termination | |
383 | // 2) sizeof(wxStringData) for housekeeping info | |
3168a13f | 384 | wxStringData* pData = (wxStringData*) |
2bb67b80 | 385 | malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar)); |
c801d85f | 386 | pData->nRefs = 1; |
c801d85f | 387 | pData->nDataLength = nLen; |
3168a13f | 388 | pData->nAllocLength = nLen + EXTRA_ALLOC; |
c801d85f | 389 | m_pchData = pData->data(); // data starts after wxStringData |
223d09f6 | 390 | m_pchData[nLen] = wxT('\0'); |
c801d85f KB |
391 | } |
392 | ||
c801d85f KB |
393 | // must be called before changing this string |
394 | void wxString::CopyBeforeWrite() | |
395 | { | |
396 | wxStringData* pData = GetStringData(); | |
397 | ||
398 | if ( pData->IsShared() ) { | |
399 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 400 | size_t nLen = pData->nDataLength; |
3168a13f | 401 | AllocBuffer(nLen); |
2bb67b80 | 402 | memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar)); |
c801d85f KB |
403 | } |
404 | ||
3bbb630a | 405 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
c801d85f KB |
406 | } |
407 | ||
408 | // must be called before replacing contents of this string | |
409 | void wxString::AllocBeforeWrite(size_t nLen) | |
410 | { | |
411 | wxASSERT( nLen != 0 ); // doesn't make any sense | |
412 | ||
413 | // must not share string and must have enough space | |
3168a13f | 414 | wxStringData* pData = GetStringData(); |
fbf0c83d | 415 | if ( pData->IsShared() || pData->IsEmpty() ) { |
c801d85f KB |
416 | // can't work with old buffer, get new one |
417 | pData->Unlock(); | |
418 | AllocBuffer(nLen); | |
419 | } | |
471aebdd | 420 | else { |
fbf0c83d VZ |
421 | if ( nLen > pData->nAllocLength ) { |
422 | // realloc the buffer instead of calling malloc() again, this is more | |
423 | // efficient | |
424 | STATISTICS_ADD(Length, nLen); | |
425 | ||
426 | nLen += EXTRA_ALLOC; | |
427 | ||
428 | wxStringData *pDataOld = pData; | |
429 | pData = (wxStringData*) | |
430 | realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); | |
431 | if ( !pData ) { | |
432 | // out of memory | |
433 | free(pDataOld); | |
434 | ||
435 | // FIXME we're going to crash... | |
436 | return; | |
437 | } | |
438 | ||
439 | pData->nAllocLength = nLen; | |
440 | m_pchData = pData->data(); | |
441 | } | |
442 | ||
443 | // now we have enough space, just update the string length | |
471aebdd VZ |
444 | pData->nDataLength = nLen; |
445 | } | |
c801d85f | 446 | |
f1da2f03 | 447 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
c801d85f KB |
448 | } |
449 | ||
dd1eaa89 | 450 | // allocate enough memory for nLen characters |
c86f1403 | 451 | void wxString::Alloc(size_t nLen) |
dd1eaa89 VZ |
452 | { |
453 | wxStringData *pData = GetStringData(); | |
454 | if ( pData->nAllocLength <= nLen ) { | |
9fbd8b8d VZ |
455 | if ( pData->IsEmpty() ) { |
456 | nLen += EXTRA_ALLOC; | |
457 | ||
458 | wxStringData* pData = (wxStringData*) | |
2bb67b80 | 459 | malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); |
9fbd8b8d VZ |
460 | pData->nRefs = 1; |
461 | pData->nDataLength = 0; | |
462 | pData->nAllocLength = nLen; | |
463 | m_pchData = pData->data(); // data starts after wxStringData | |
223d09f6 | 464 | m_pchData[0u] = wxT('\0'); |
9fbd8b8d | 465 | } |
3168a13f VZ |
466 | else if ( pData->IsShared() ) { |
467 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 468 | size_t nOldLen = pData->nDataLength; |
3168a13f | 469 | AllocBuffer(nLen); |
2bb67b80 | 470 | memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar)); |
3168a13f | 471 | } |
dd1eaa89 | 472 | else { |
3168a13f VZ |
473 | nLen += EXTRA_ALLOC; |
474 | ||
fbf0c83d | 475 | wxStringData *pDataOld = pData; |
dd1eaa89 | 476 | wxStringData *p = (wxStringData *) |
2bb67b80 | 477 | realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); |
3168a13f VZ |
478 | |
479 | if ( p == NULL ) { | |
fbf0c83d VZ |
480 | // don't leak memory |
481 | free(pDataOld); | |
482 | ||
483 | // FIXME what to do on memory error? | |
3168a13f | 484 | return; |
dd1eaa89 | 485 | } |
3168a13f VZ |
486 | |
487 | // it's not important if the pointer changed or not (the check for this | |
488 | // is not faster than assigning to m_pchData in all cases) | |
489 | p->nAllocLength = nLen; | |
490 | m_pchData = p->data(); | |
dd1eaa89 VZ |
491 | } |
492 | } | |
493 | //else: we've already got enough | |
494 | } | |
495 | ||
496 | // shrink to minimal size (releasing extra memory) | |
497 | void wxString::Shrink() | |
498 | { | |
499 | wxStringData *pData = GetStringData(); | |
3bbb630a | 500 | |
fbf0c83d VZ |
501 | // this variable is unused in release build, so avoid the compiler warning |
502 | // by just not declaring it | |
3bbb630a VZ |
503 | #ifdef __WXDEBUG__ |
504 | void *p = | |
505 | #endif | |
2bb67b80 | 506 | realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(wxChar)); |
3bbb630a | 507 | |
fbf0c83d VZ |
508 | // we rely on a reasonable realloc() implementation here - so far I haven't |
509 | // seen any which wouldn't behave like this | |
510 | ||
3168a13f | 511 | wxASSERT( p != NULL ); // can't free memory? |
dd1eaa89 VZ |
512 | wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move! |
513 | } | |
514 | ||
c801d85f | 515 | // get the pointer to writable buffer of (at least) nLen bytes |
2bb67b80 | 516 | wxChar *wxString::GetWriteBuf(size_t nLen) |
c801d85f KB |
517 | { |
518 | AllocBeforeWrite(nLen); | |
097c080b VZ |
519 | |
520 | wxASSERT( GetStringData()->nRefs == 1 ); | |
521 | GetStringData()->Validate(FALSE); | |
522 | ||
c801d85f KB |
523 | return m_pchData; |
524 | } | |
525 | ||
097c080b VZ |
526 | // put string back in a reasonable state after GetWriteBuf |
527 | void wxString::UngetWriteBuf() | |
528 | { | |
2bb67b80 | 529 | GetStringData()->nDataLength = wxStrlen(m_pchData); |
097c080b VZ |
530 | GetStringData()->Validate(TRUE); |
531 | } | |
532 | ||
8f06a017 RD |
533 | void wxString::UngetWriteBuf(size_t nLen) |
534 | { | |
535 | GetStringData()->nDataLength = nLen; | |
536 | GetStringData()->Validate(TRUE); | |
537 | } | |
538 | ||
c801d85f KB |
539 | // --------------------------------------------------------------------------- |
540 | // data access | |
541 | // --------------------------------------------------------------------------- | |
542 | ||
543 | // all functions are inline in string.h | |
544 | ||
545 | // --------------------------------------------------------------------------- | |
546 | // assignment operators | |
547 | // --------------------------------------------------------------------------- | |
548 | ||
dd1eaa89 | 549 | // helper function: does real copy |
2bb67b80 | 550 | void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData) |
c801d85f KB |
551 | { |
552 | if ( nSrcLen == 0 ) { | |
553 | Reinit(); | |
554 | } | |
555 | else { | |
556 | AllocBeforeWrite(nSrcLen); | |
2bb67b80 | 557 | memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar)); |
c801d85f | 558 | GetStringData()->nDataLength = nSrcLen; |
223d09f6 | 559 | m_pchData[nSrcLen] = wxT('\0'); |
c801d85f KB |
560 | } |
561 | } | |
562 | ||
563 | // assigns one string to another | |
564 | wxString& wxString::operator=(const wxString& stringSrc) | |
565 | { | |
097c080b VZ |
566 | wxASSERT( stringSrc.GetStringData()->IsValid() ); |
567 | ||
c801d85f KB |
568 | // don't copy string over itself |
569 | if ( m_pchData != stringSrc.m_pchData ) { | |
570 | if ( stringSrc.GetStringData()->IsEmpty() ) { | |
571 | Reinit(); | |
572 | } | |
573 | else { | |
574 | // adjust references | |
575 | GetStringData()->Unlock(); | |
576 | m_pchData = stringSrc.m_pchData; | |
577 | GetStringData()->Lock(); | |
578 | } | |
579 | } | |
580 | ||
581 | return *this; | |
582 | } | |
583 | ||
584 | // assigns a single character | |
2bb67b80 | 585 | wxString& wxString::operator=(wxChar ch) |
c801d85f KB |
586 | { |
587 | AssignCopy(1, &ch); | |
588 | return *this; | |
589 | } | |
590 | ||
591 | // assigns C string | |
2bb67b80 | 592 | wxString& wxString::operator=(const wxChar *psz) |
c801d85f | 593 | { |
2bb67b80 | 594 | AssignCopy(wxStrlen(psz), psz); |
c801d85f KB |
595 | return *this; |
596 | } | |
597 | ||
2bb67b80 OK |
598 | #if !wxUSE_UNICODE |
599 | ||
c801d85f KB |
600 | // same as 'signed char' variant |
601 | wxString& wxString::operator=(const unsigned char* psz) | |
602 | { | |
603 | *this = (const char *)psz; | |
604 | return *this; | |
605 | } | |
606 | ||
0f3e3e0c | 607 | #if wxUSE_WCHAR_T |
c801d85f KB |
608 | wxString& wxString::operator=(const wchar_t *pwz) |
609 | { | |
610 | wxString str(pwz); | |
611 | *this = str; | |
612 | return *this; | |
613 | } | |
0f3e3e0c | 614 | #endif |
c801d85f | 615 | |
2bb67b80 OK |
616 | #endif |
617 | ||
c801d85f KB |
618 | // --------------------------------------------------------------------------- |
619 | // string concatenation | |
620 | // --------------------------------------------------------------------------- | |
621 | ||
c801d85f | 622 | // add something to this string |
2bb67b80 | 623 | void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData) |
c801d85f | 624 | { |
3168a13f | 625 | STATISTICS_ADD(SummandLength, nSrcLen); |
c801d85f | 626 | |
05488905 VZ |
627 | // concatenating an empty string is a NOP |
628 | if ( nSrcLen > 0 ) { | |
629 | wxStringData *pData = GetStringData(); | |
630 | size_t nLen = pData->nDataLength; | |
631 | size_t nNewLen = nLen + nSrcLen; | |
c801d85f | 632 | |
05488905 VZ |
633 | // alloc new buffer if current is too small |
634 | if ( pData->IsShared() ) { | |
635 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 636 | |
05488905 VZ |
637 | // we have to allocate another buffer |
638 | wxStringData* pOldData = GetStringData(); | |
639 | AllocBuffer(nNewLen); | |
2bb67b80 | 640 | memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar)); |
05488905 VZ |
641 | pOldData->Unlock(); |
642 | } | |
643 | else if ( nNewLen > pData->nAllocLength ) { | |
644 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 645 | |
05488905 VZ |
646 | // we have to grow the buffer |
647 | Alloc(nNewLen); | |
648 | } | |
649 | else { | |
650 | STATISTICS_ADD(ConcatHit, 1); | |
3168a13f | 651 | |
05488905 VZ |
652 | // the buffer is already big enough |
653 | } | |
3168a13f | 654 | |
05488905 VZ |
655 | // should be enough space |
656 | wxASSERT( nNewLen <= GetStringData()->nAllocLength ); | |
3168a13f | 657 | |
05488905 | 658 | // fast concatenation - all is done in our buffer |
2bb67b80 | 659 | memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar)); |
3168a13f | 660 | |
223d09f6 | 661 | m_pchData[nNewLen] = wxT('\0'); // put terminating '\0' |
05488905 VZ |
662 | GetStringData()->nDataLength = nNewLen; // and fix the length |
663 | } | |
664 | //else: the string to append was empty | |
c801d85f KB |
665 | } |
666 | ||
667 | /* | |
c801d85f KB |
668 | * concatenation functions come in 5 flavours: |
669 | * string + string | |
670 | * char + string and string + char | |
671 | * C str + string and string + C str | |
672 | */ | |
673 | ||
674 | wxString operator+(const wxString& string1, const wxString& string2) | |
675 | { | |
097c080b VZ |
676 | wxASSERT( string1.GetStringData()->IsValid() ); |
677 | wxASSERT( string2.GetStringData()->IsValid() ); | |
678 | ||
3168a13f VZ |
679 | wxString s = string1; |
680 | s += string2; | |
681 | ||
c801d85f KB |
682 | return s; |
683 | } | |
684 | ||
2bb67b80 | 685 | wxString operator+(const wxString& string, wxChar ch) |
c801d85f | 686 | { |
3168a13f VZ |
687 | wxASSERT( string.GetStringData()->IsValid() ); |
688 | ||
689 | wxString s = string; | |
690 | s += ch; | |
097c080b | 691 | |
c801d85f KB |
692 | return s; |
693 | } | |
694 | ||
2bb67b80 | 695 | wxString operator+(wxChar ch, const wxString& string) |
c801d85f | 696 | { |
097c080b VZ |
697 | wxASSERT( string.GetStringData()->IsValid() ); |
698 | ||
3168a13f VZ |
699 | wxString s = ch; |
700 | s += string; | |
701 | ||
c801d85f KB |
702 | return s; |
703 | } | |
704 | ||
2bb67b80 | 705 | wxString operator+(const wxString& string, const wxChar *psz) |
c801d85f | 706 | { |
097c080b VZ |
707 | wxASSERT( string.GetStringData()->IsValid() ); |
708 | ||
c801d85f | 709 | wxString s; |
2bb67b80 | 710 | s.Alloc(wxStrlen(psz) + string.Len()); |
3168a13f VZ |
711 | s = string; |
712 | s += psz; | |
713 | ||
c801d85f KB |
714 | return s; |
715 | } | |
716 | ||
2bb67b80 | 717 | wxString operator+(const wxChar *psz, const wxString& string) |
c801d85f | 718 | { |
097c080b VZ |
719 | wxASSERT( string.GetStringData()->IsValid() ); |
720 | ||
c801d85f | 721 | wxString s; |
2bb67b80 | 722 | s.Alloc(wxStrlen(psz) + string.Len()); |
3168a13f VZ |
723 | s = psz; |
724 | s += string; | |
725 | ||
c801d85f KB |
726 | return s; |
727 | } | |
728 | ||
729 | // =========================================================================== | |
730 | // other common string functions | |
731 | // =========================================================================== | |
732 | ||
733 | // --------------------------------------------------------------------------- | |
734 | // simple sub-string extraction | |
735 | // --------------------------------------------------------------------------- | |
736 | ||
737 | // helper function: clone the data attached to this string | |
738 | void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const | |
739 | { | |
3168a13f | 740 | if ( nCopyLen == 0 ) { |
c801d85f KB |
741 | dest.Init(); |
742 | } | |
3168a13f | 743 | else { |
c801d85f | 744 | dest.AllocBuffer(nCopyLen); |
2bb67b80 | 745 | memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar)); |
c801d85f KB |
746 | } |
747 | } | |
748 | ||
749 | // extract string of length nCount starting at nFirst | |
c801d85f KB |
750 | wxString wxString::Mid(size_t nFirst, size_t nCount) const |
751 | { | |
30d9011f VZ |
752 | wxStringData *pData = GetStringData(); |
753 | size_t nLen = pData->nDataLength; | |
754 | ||
566b84d2 VZ |
755 | // default value of nCount is wxSTRING_MAXLEN and means "till the end" |
756 | if ( nCount == wxSTRING_MAXLEN ) | |
30d9011f VZ |
757 | { |
758 | nCount = nLen - nFirst; | |
759 | } | |
760 | ||
c801d85f | 761 | // out-of-bounds requests return sensible things |
30d9011f VZ |
762 | if ( nFirst + nCount > nLen ) |
763 | { | |
764 | nCount = nLen - nFirst; | |
765 | } | |
c801d85f | 766 | |
30d9011f VZ |
767 | if ( nFirst > nLen ) |
768 | { | |
769 | // AllocCopy() will return empty string | |
c801d85f | 770 | nCount = 0; |
30d9011f | 771 | } |
c801d85f KB |
772 | |
773 | wxString dest; | |
774 | AllocCopy(dest, nCount, nFirst); | |
30d9011f | 775 | |
c801d85f KB |
776 | return dest; |
777 | } | |
778 | ||
f6bcfd97 BP |
779 | // check that the tring starts with prefix and return the rest of the string |
780 | // in the provided pointer if it is not NULL, otherwise return FALSE | |
781 | bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const | |
782 | { | |
783 | wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") ); | |
784 | ||
785 | // first check if the beginning of the string matches the prefix: note | |
786 | // that we don't have to check that we don't run out of this string as | |
787 | // when we reach the terminating NUL, either prefix string ends too (and | |
788 | // then it's ok) or we break out of the loop because there is no match | |
789 | const wxChar *p = c_str(); | |
790 | while ( *prefix ) | |
791 | { | |
792 | if ( *prefix++ != *p++ ) | |
793 | { | |
794 | // no match | |
795 | return FALSE; | |
796 | } | |
797 | } | |
798 | ||
799 | if ( rest ) | |
800 | { | |
801 | // put the rest of the string into provided pointer | |
802 | *rest = p; | |
803 | } | |
804 | ||
805 | return TRUE; | |
806 | } | |
807 | ||
c801d85f KB |
808 | // extract nCount last (rightmost) characters |
809 | wxString wxString::Right(size_t nCount) const | |
810 | { | |
811 | if ( nCount > (size_t)GetStringData()->nDataLength ) | |
812 | nCount = GetStringData()->nDataLength; | |
813 | ||
814 | wxString dest; | |
815 | AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount); | |
816 | return dest; | |
817 | } | |
818 | ||
819 | // get all characters after the last occurence of ch | |
820 | // (returns the whole string if ch not found) | |
2bb67b80 | 821 | wxString wxString::AfterLast(wxChar ch) const |
c801d85f KB |
822 | { |
823 | wxString str; | |
824 | int iPos = Find(ch, TRUE); | |
3c67202d | 825 | if ( iPos == wxNOT_FOUND ) |
c801d85f KB |
826 | str = *this; |
827 | else | |
c8cfb486 | 828 | str = c_str() + iPos + 1; |
c801d85f KB |
829 | |
830 | return str; | |
831 | } | |
832 | ||
833 | // extract nCount first (leftmost) characters | |
834 | wxString wxString::Left(size_t nCount) const | |
835 | { | |
836 | if ( nCount > (size_t)GetStringData()->nDataLength ) | |
837 | nCount = GetStringData()->nDataLength; | |
838 | ||
839 | wxString dest; | |
840 | AllocCopy(dest, nCount, 0); | |
841 | return dest; | |
842 | } | |
843 | ||
844 | // get all characters before the first occurence of ch | |
845 | // (returns the whole string if ch not found) | |
2bb67b80 | 846 | wxString wxString::BeforeFirst(wxChar ch) const |
c801d85f KB |
847 | { |
848 | wxString str; | |
223d09f6 | 849 | for ( const wxChar *pc = m_pchData; *pc != wxT('\0') && *pc != ch; pc++ ) |
c801d85f KB |
850 | str += *pc; |
851 | ||
852 | return str; | |
853 | } | |
854 | ||
855 | /// get all characters before the last occurence of ch | |
856 | /// (returns empty string if ch not found) | |
2bb67b80 | 857 | wxString wxString::BeforeLast(wxChar ch) const |
c801d85f KB |
858 | { |
859 | wxString str; | |
860 | int iPos = Find(ch, TRUE); | |
3c67202d | 861 | if ( iPos != wxNOT_FOUND && iPos != 0 ) |
d1c9bbf6 | 862 | str = wxString(c_str(), iPos); |
c801d85f KB |
863 | |
864 | return str; | |
865 | } | |
866 | ||
867 | /// get all characters after the first occurence of ch | |
868 | /// (returns empty string if ch not found) | |
2bb67b80 | 869 | wxString wxString::AfterFirst(wxChar ch) const |
c801d85f KB |
870 | { |
871 | wxString str; | |
872 | int iPos = Find(ch); | |
3c67202d | 873 | if ( iPos != wxNOT_FOUND ) |
c801d85f KB |
874 | str = c_str() + iPos + 1; |
875 | ||
876 | return str; | |
877 | } | |
878 | ||
879 | // replace first (or all) occurences of some substring with another one | |
2bb67b80 | 880 | size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll) |
c801d85f | 881 | { |
c86f1403 | 882 | size_t uiCount = 0; // count of replacements made |
c801d85f | 883 | |
2bb67b80 | 884 | size_t uiOldLen = wxStrlen(szOld); |
c801d85f KB |
885 | |
886 | wxString strTemp; | |
2bb67b80 OK |
887 | const wxChar *pCurrent = m_pchData; |
888 | const wxChar *pSubstr; | |
223d09f6 | 889 | while ( *pCurrent != wxT('\0') ) { |
2bb67b80 | 890 | pSubstr = wxStrstr(pCurrent, szOld); |
c801d85f KB |
891 | if ( pSubstr == NULL ) { |
892 | // strTemp is unused if no replacements were made, so avoid the copy | |
893 | if ( uiCount == 0 ) | |
894 | return 0; | |
895 | ||
896 | strTemp += pCurrent; // copy the rest | |
897 | break; // exit the loop | |
898 | } | |
899 | else { | |
900 | // take chars before match | |
901 | strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent); | |
902 | strTemp += szNew; | |
903 | pCurrent = pSubstr + uiOldLen; // restart after match | |
904 | ||
905 | uiCount++; | |
906 | ||
907 | // stop now? | |
908 | if ( !bReplaceAll ) { | |
909 | strTemp += pCurrent; // copy the rest | |
910 | break; // exit the loop | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | // only done if there were replacements, otherwise would have returned above | |
916 | *this = strTemp; | |
917 | ||
918 | return uiCount; | |
919 | } | |
920 | ||
921 | bool wxString::IsAscii() const | |
922 | { | |
2bb67b80 | 923 | const wxChar *s = (const wxChar*) *this; |
c801d85f KB |
924 | while(*s){ |
925 | if(!isascii(*s)) return(FALSE); | |
926 | s++; | |
927 | } | |
928 | return(TRUE); | |
929 | } | |
dd1eaa89 | 930 | |
c801d85f KB |
931 | bool wxString::IsWord() const |
932 | { | |
2bb67b80 | 933 | const wxChar *s = (const wxChar*) *this; |
c801d85f | 934 | while(*s){ |
2bb67b80 | 935 | if(!wxIsalpha(*s)) return(FALSE); |
c801d85f KB |
936 | s++; |
937 | } | |
938 | return(TRUE); | |
939 | } | |
dd1eaa89 | 940 | |
c801d85f KB |
941 | bool wxString::IsNumber() const |
942 | { | |
2bb67b80 | 943 | const wxChar *s = (const wxChar*) *this; |
2f74ed28 GT |
944 | if (wxStrlen(s)) |
945 | if ((s[0] == '-') || (s[0] == '+')) s++; | |
c801d85f | 946 | while(*s){ |
2bb67b80 | 947 | if(!wxIsdigit(*s)) return(FALSE); |
c801d85f KB |
948 | s++; |
949 | } | |
950 | return(TRUE); | |
951 | } | |
952 | ||
c801d85f KB |
953 | wxString wxString::Strip(stripType w) const |
954 | { | |
955 | wxString s = *this; | |
956 | if ( w & leading ) s.Trim(FALSE); | |
957 | if ( w & trailing ) s.Trim(TRUE); | |
958 | return s; | |
959 | } | |
960 | ||
c801d85f KB |
961 | // --------------------------------------------------------------------------- |
962 | // case conversion | |
963 | // --------------------------------------------------------------------------- | |
964 | ||
965 | wxString& wxString::MakeUpper() | |
966 | { | |
967 | CopyBeforeWrite(); | |
968 | ||
2bb67b80 OK |
969 | for ( wxChar *p = m_pchData; *p; p++ ) |
970 | *p = (wxChar)wxToupper(*p); | |
c801d85f KB |
971 | |
972 | return *this; | |
973 | } | |
974 | ||
975 | wxString& wxString::MakeLower() | |
976 | { | |
977 | CopyBeforeWrite(); | |
dd1eaa89 | 978 | |
2bb67b80 OK |
979 | for ( wxChar *p = m_pchData; *p; p++ ) |
980 | *p = (wxChar)wxTolower(*p); | |
c801d85f KB |
981 | |
982 | return *this; | |
983 | } | |
984 | ||
985 | // --------------------------------------------------------------------------- | |
986 | // trimming and padding | |
987 | // --------------------------------------------------------------------------- | |
988 | ||
989 | // trims spaces (in the sense of isspace) from left or right side | |
990 | wxString& wxString::Trim(bool bFromRight) | |
991 | { | |
2c3b684c | 992 | // first check if we're going to modify the string at all |
913df6f2 DW |
993 | if ( !IsEmpty() && |
994 | ( | |
2bb67b80 OK |
995 | (bFromRight && wxIsspace(GetChar(Len() - 1))) || |
996 | (!bFromRight && wxIsspace(GetChar(0u))) | |
2c3b684c VZ |
997 | ) |
998 | ) | |
c801d85f | 999 | { |
2c3b684c VZ |
1000 | // ok, there is at least one space to trim |
1001 | CopyBeforeWrite(); | |
1002 | ||
1003 | if ( bFromRight ) | |
1004 | { | |
1005 | // find last non-space character | |
2bb67b80 OK |
1006 | wxChar *psz = m_pchData + GetStringData()->nDataLength - 1; |
1007 | while ( wxIsspace(*psz) && (psz >= m_pchData) ) | |
2c3b684c VZ |
1008 | psz--; |
1009 | ||
1010 | // truncate at trailing space start | |
223d09f6 | 1011 | *++psz = wxT('\0'); |
2c3b684c VZ |
1012 | GetStringData()->nDataLength = psz - m_pchData; |
1013 | } | |
1014 | else | |
1015 | { | |
1016 | // find first non-space character | |
2bb67b80 OK |
1017 | const wxChar *psz = m_pchData; |
1018 | while ( wxIsspace(*psz) ) | |
2c3b684c VZ |
1019 | psz++; |
1020 | ||
1021 | // fix up data and length | |
2bb67b80 OK |
1022 | int nDataLength = GetStringData()->nDataLength - (psz - (const wxChar*) m_pchData); |
1023 | memmove(m_pchData, psz, (nDataLength + 1)*sizeof(wxChar)); | |
2c3b684c VZ |
1024 | GetStringData()->nDataLength = nDataLength; |
1025 | } | |
c801d85f KB |
1026 | } |
1027 | ||
1028 | return *this; | |
1029 | } | |
1030 | ||
1031 | // adds nCount characters chPad to the string from either side | |
2bb67b80 | 1032 | wxString& wxString::Pad(size_t nCount, wxChar chPad, bool bFromRight) |
c801d85f KB |
1033 | { |
1034 | wxString s(chPad, nCount); | |
1035 | ||
1036 | if ( bFromRight ) | |
1037 | *this += s; | |
1038 | else | |
1039 | { | |
1040 | s += *this; | |
1041 | *this = s; | |
1042 | } | |
1043 | ||
1044 | return *this; | |
1045 | } | |
1046 | ||
1047 | // truncate the string | |
1048 | wxString& wxString::Truncate(size_t uiLen) | |
1049 | { | |
79a773ba VZ |
1050 | if ( uiLen < Len() ) { |
1051 | CopyBeforeWrite(); | |
1052 | ||
223d09f6 | 1053 | *(m_pchData + uiLen) = wxT('\0'); |
79a773ba VZ |
1054 | GetStringData()->nDataLength = uiLen; |
1055 | } | |
1056 | //else: nothing to do, string is already short enough | |
c801d85f KB |
1057 | |
1058 | return *this; | |
1059 | } | |
1060 | ||
1061 | // --------------------------------------------------------------------------- | |
3c67202d | 1062 | // finding (return wxNOT_FOUND if not found and index otherwise) |
c801d85f KB |
1063 | // --------------------------------------------------------------------------- |
1064 | ||
1065 | // find a character | |
2bb67b80 | 1066 | int wxString::Find(wxChar ch, bool bFromEnd) const |
c801d85f | 1067 | { |
2bb67b80 | 1068 | const wxChar *psz = bFromEnd ? wxStrrchr(m_pchData, ch) : wxStrchr(m_pchData, ch); |
c801d85f | 1069 | |
2bb67b80 | 1070 | return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData; |
c801d85f KB |
1071 | } |
1072 | ||
1073 | // find a sub-string (like strstr) | |
2bb67b80 | 1074 | int wxString::Find(const wxChar *pszSub) const |
c801d85f | 1075 | { |
2bb67b80 | 1076 | const wxChar *psz = wxStrstr(m_pchData, pszSub); |
c801d85f | 1077 | |
2bb67b80 | 1078 | return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData; |
c801d85f KB |
1079 | } |
1080 | ||
cd0b1709 VZ |
1081 | // ---------------------------------------------------------------------------- |
1082 | // conversion to numbers | |
1083 | // ---------------------------------------------------------------------------- | |
1084 | ||
1085 | bool wxString::ToLong(long *val) const | |
1086 | { | |
1087 | wxCHECK_MSG( val, FALSE, _T("NULL pointer in wxString::ToLong") ); | |
1088 | ||
1089 | const wxChar *start = c_str(); | |
1090 | wxChar *end; | |
1091 | *val = wxStrtol(start, &end, 10); | |
1092 | ||
1093 | // return TRUE only if scan was stopped by the terminating NUL and if the | |
1094 | // string was not empty to start with | |
1095 | return !*end && (end != start); | |
1096 | } | |
1097 | ||
1098 | bool wxString::ToULong(unsigned long *val) const | |
1099 | { | |
1100 | wxCHECK_MSG( val, FALSE, _T("NULL pointer in wxString::ToULong") ); | |
1101 | ||
1102 | const wxChar *start = c_str(); | |
1103 | wxChar *end; | |
1104 | *val = wxStrtoul(start, &end, 10); | |
1105 | ||
1106 | // return TRUE only if scan was stopped by the terminating NUL and if the | |
1107 | // string was not empty to start with | |
1108 | return !*end && (end != start); | |
1109 | } | |
1110 | ||
1111 | bool wxString::ToDouble(double *val) const | |
1112 | { | |
1113 | wxCHECK_MSG( val, FALSE, _T("NULL pointer in wxString::ToDouble") ); | |
1114 | ||
1115 | const wxChar *start = c_str(); | |
1116 | wxChar *end; | |
1117 | *val = wxStrtod(start, &end); | |
1118 | ||
1119 | // return TRUE only if scan was stopped by the terminating NUL and if the | |
1120 | // string was not empty to start with | |
1121 | return !*end && (end != start); | |
1122 | } | |
1123 | ||
c801d85f | 1124 | // --------------------------------------------------------------------------- |
9efd3367 | 1125 | // formatted output |
c801d85f | 1126 | // --------------------------------------------------------------------------- |
378b05f7 | 1127 | |
341e7d28 VZ |
1128 | /* static */ |
1129 | wxString wxString::Format(const wxChar *pszFormat, ...) | |
1130 | { | |
77c3e48a VZ |
1131 | va_list argptr; |
1132 | va_start(argptr, pszFormat); | |
341e7d28 | 1133 | |
77c3e48a VZ |
1134 | wxString s; |
1135 | s.PrintfV(pszFormat, argptr); | |
341e7d28 | 1136 | |
77c3e48a | 1137 | va_end(argptr); |
341e7d28 | 1138 | |
77c3e48a | 1139 | return s; |
341e7d28 VZ |
1140 | } |
1141 | ||
1142 | /* static */ | |
1143 | wxString wxString::FormatV(const wxChar *pszFormat, va_list argptr) | |
1144 | { | |
1145 | wxString s; | |
1146 | s.Printf(pszFormat, argptr); | |
1147 | return s; | |
1148 | } | |
1149 | ||
2bb67b80 | 1150 | int wxString::Printf(const wxChar *pszFormat, ...) |
c801d85f KB |
1151 | { |
1152 | va_list argptr; | |
1153 | va_start(argptr, pszFormat); | |
1154 | ||
1155 | int iLen = PrintfV(pszFormat, argptr); | |
1156 | ||
1157 | va_end(argptr); | |
1158 | ||
1159 | return iLen; | |
1160 | } | |
1161 | ||
2bb67b80 | 1162 | int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) |
c801d85f | 1163 | { |
11914352 | 1164 | #if wxUSE_EXPERIMENTAL_PRINTF |
378b05f7 VZ |
1165 | // the new implementation |
1166 | ||
1167 | // buffer to avoid dynamic memory allocation each time for small strings | |
1168 | char szScratch[1024]; | |
2bb67b80 OK |
1169 | |
1170 | Reinit(); | |
1171 | for (size_t n = 0; pszFormat[n]; n++) | |
223d09f6 | 1172 | if (pszFormat[n] == wxT('%')) { |
2bb67b80 OK |
1173 | static char s_szFlags[256] = "%"; |
1174 | size_t flagofs = 1; | |
1175 | bool adj_left = FALSE, in_prec = FALSE, | |
4e57b0d4 | 1176 | prec_dot = FALSE, done = FALSE; |
2bb67b80 OK |
1177 | int ilen = 0; |
1178 | size_t min_width = 0, max_width = wxSTRING_MAXLEN; | |
1179 | do { | |
1180 | #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; } | |
4e57b0d4 | 1181 | switch (pszFormat[++n]) { |
223d09f6 | 1182 | case wxT('\0'): |
4e57b0d4 VZ |
1183 | done = TRUE; |
1184 | break; | |
223d09f6 KB |
1185 | case wxT('%'): |
1186 | *this += wxT('%'); | |
4e57b0d4 VZ |
1187 | done = TRUE; |
1188 | break; | |
223d09f6 KB |
1189 | case wxT('#'): |
1190 | case wxT('0'): | |
1191 | case wxT(' '): | |
1192 | case wxT('+'): | |
1193 | case wxT('\''): | |
4e57b0d4 VZ |
1194 | CHECK_PREC |
1195 | s_szFlags[flagofs++] = pszFormat[n]; | |
1196 | break; | |
223d09f6 | 1197 | case wxT('-'): |
4e57b0d4 VZ |
1198 | CHECK_PREC |
1199 | adj_left = TRUE; | |
1200 | s_szFlags[flagofs++] = pszFormat[n]; | |
1201 | break; | |
223d09f6 | 1202 | case wxT('.'): |
4e57b0d4 VZ |
1203 | CHECK_PREC |
1204 | in_prec = TRUE; | |
1205 | prec_dot = FALSE; | |
1206 | max_width = 0; | |
1207 | // dot will be auto-added to s_szFlags if non-negative number follows | |
1208 | break; | |
223d09f6 | 1209 | case wxT('h'): |
4e57b0d4 VZ |
1210 | ilen = -1; |
1211 | CHECK_PREC | |
1212 | s_szFlags[flagofs++] = pszFormat[n]; | |
1213 | break; | |
223d09f6 | 1214 | case wxT('l'): |
4e57b0d4 VZ |
1215 | ilen = 1; |
1216 | CHECK_PREC | |
1217 | s_szFlags[flagofs++] = pszFormat[n]; | |
1218 | break; | |
223d09f6 KB |
1219 | case wxT('q'): |
1220 | case wxT('L'): | |
4e57b0d4 VZ |
1221 | ilen = 2; |
1222 | CHECK_PREC | |
1223 | s_szFlags[flagofs++] = pszFormat[n]; | |
1224 | break; | |
223d09f6 | 1225 | case wxT('Z'): |
4e57b0d4 VZ |
1226 | ilen = 3; |
1227 | CHECK_PREC | |
1228 | s_szFlags[flagofs++] = pszFormat[n]; | |
1229 | break; | |
223d09f6 | 1230 | case wxT('*'): |
4e57b0d4 VZ |
1231 | { |
1232 | int len = va_arg(argptr, int); | |
1233 | if (in_prec) { | |
1234 | if (len<0) break; | |
1235 | CHECK_PREC | |
1236 | max_width = len; | |
1237 | } else { | |
1238 | if (len<0) { | |
1239 | adj_left = !adj_left; | |
1240 | s_szFlags[flagofs++] = '-'; | |
1241 | len = -len; | |
1242 | } | |
1243 | min_width = len; | |
1244 | } | |
1245 | flagofs += ::sprintf(s_szFlags+flagofs,"%d",len); | |
1246 | } | |
1247 | break; | |
223d09f6 KB |
1248 | case wxT('1'): case wxT('2'): case wxT('3'): |
1249 | case wxT('4'): case wxT('5'): case wxT('6'): | |
1250 | case wxT('7'): case wxT('8'): case wxT('9'): | |
4e57b0d4 VZ |
1251 | { |
1252 | int len = 0; | |
1253 | CHECK_PREC | |
223d09f6 | 1254 | while ((pszFormat[n]>=wxT('0')) && (pszFormat[n]<=wxT('9'))) { |
4e57b0d4 | 1255 | s_szFlags[flagofs++] = pszFormat[n]; |
223d09f6 | 1256 | len = len*10 + (pszFormat[n] - wxT('0')); |
4e57b0d4 VZ |
1257 | n++; |
1258 | } | |
1259 | if (in_prec) max_width = len; | |
1260 | else min_width = len; | |
1261 | n--; // the main loop pre-increments n again | |
1262 | } | |
1263 | break; | |
223d09f6 KB |
1264 | case wxT('d'): |
1265 | case wxT('i'): | |
1266 | case wxT('o'): | |
1267 | case wxT('u'): | |
1268 | case wxT('x'): | |
1269 | case wxT('X'): | |
4e57b0d4 VZ |
1270 | CHECK_PREC |
1271 | s_szFlags[flagofs++] = pszFormat[n]; | |
1272 | s_szFlags[flagofs] = '\0'; | |
1273 | if (ilen == 0 ) { | |
1274 | int val = va_arg(argptr, int); | |
378b05f7 | 1275 | ::sprintf(szScratch, s_szFlags, val); |
4e57b0d4 VZ |
1276 | } |
1277 | else if (ilen == -1) { | |
1278 | short int val = va_arg(argptr, short int); | |
378b05f7 | 1279 | ::sprintf(szScratch, s_szFlags, val); |
4e57b0d4 VZ |
1280 | } |
1281 | else if (ilen == 1) { | |
1282 | long int val = va_arg(argptr, long int); | |
378b05f7 | 1283 | ::sprintf(szScratch, s_szFlags, val); |
4e57b0d4 VZ |
1284 | } |
1285 | else if (ilen == 2) { | |
2bb67b80 | 1286 | #if SIZEOF_LONG_LONG |
4e57b0d4 | 1287 | long long int val = va_arg(argptr, long long int); |
378b05f7 | 1288 | ::sprintf(szScratch, s_szFlags, val); |
2bb67b80 | 1289 | #else |
4e57b0d4 | 1290 | long int val = va_arg(argptr, long int); |
378b05f7 | 1291 | ::sprintf(szScratch, s_szFlags, val); |
2bb67b80 | 1292 | #endif |
4e57b0d4 VZ |
1293 | } |
1294 | else if (ilen == 3) { | |
1295 | size_t val = va_arg(argptr, size_t); | |
378b05f7 | 1296 | ::sprintf(szScratch, s_szFlags, val); |
4e57b0d4 | 1297 | } |
378b05f7 | 1298 | *this += wxString(szScratch); |
4e57b0d4 VZ |
1299 | done = TRUE; |
1300 | break; | |
223d09f6 KB |
1301 | case wxT('e'): |
1302 | case wxT('E'): | |
1303 | case wxT('f'): | |
1304 | case wxT('g'): | |
1305 | case wxT('G'): | |
4e57b0d4 VZ |
1306 | CHECK_PREC |
1307 | s_szFlags[flagofs++] = pszFormat[n]; | |
1308 | s_szFlags[flagofs] = '\0'; | |
1309 | if (ilen == 2) { | |
1310 | long double val = va_arg(argptr, long double); | |
378b05f7 | 1311 | ::sprintf(szScratch, s_szFlags, val); |
4e57b0d4 VZ |
1312 | } else { |
1313 | double val = va_arg(argptr, double); | |
378b05f7 | 1314 | ::sprintf(szScratch, s_szFlags, val); |
4e57b0d4 | 1315 | } |
378b05f7 | 1316 | *this += wxString(szScratch); |
4e57b0d4 VZ |
1317 | done = TRUE; |
1318 | break; | |
223d09f6 | 1319 | case wxT('p'): |
4e57b0d4 VZ |
1320 | { |
1321 | void *val = va_arg(argptr, void *); | |
1322 | CHECK_PREC | |
1323 | s_szFlags[flagofs++] = pszFormat[n]; | |
1324 | s_szFlags[flagofs] = '\0'; | |
378b05f7 VZ |
1325 | ::sprintf(szScratch, s_szFlags, val); |
1326 | *this += wxString(szScratch); | |
4e57b0d4 VZ |
1327 | done = TRUE; |
1328 | } | |
1329 | break; | |
223d09f6 | 1330 | case wxT('c'): |
4e57b0d4 VZ |
1331 | { |
1332 | wxChar val = va_arg(argptr, int); | |
1333 | // we don't need to honor padding here, do we? | |
1334 | *this += val; | |
1335 | done = TRUE; | |
1336 | } | |
1337 | break; | |
223d09f6 | 1338 | case wxT('s'): |
4e57b0d4 VZ |
1339 | if (ilen == -1) { |
1340 | // wx extension: we'll let %hs mean non-Unicode strings | |
1341 | char *val = va_arg(argptr, char *); | |
2bb67b80 | 1342 | #if wxUSE_UNICODE |
4e57b0d4 VZ |
1343 | // ASCII->Unicode constructor handles max_width right |
1344 | wxString s(val, wxConvLibc, max_width); | |
2bb67b80 | 1345 | #else |
4e57b0d4 VZ |
1346 | size_t len = wxSTRING_MAXLEN; |
1347 | if (val) { | |
1348 | for (len = 0; val[len] && (len<max_width); len++); | |
223d09f6 | 1349 | } else val = wxT("(null)"); |
4e57b0d4 | 1350 | wxString s(val, len); |
2bb67b80 | 1351 | #endif |
4e57b0d4 | 1352 | if (s.Len() < min_width) |
223d09f6 | 1353 | s.Pad(min_width - s.Len(), wxT(' '), adj_left); |
4e57b0d4 VZ |
1354 | *this += s; |
1355 | } else { | |
1356 | wxChar *val = va_arg(argptr, wxChar *); | |
1357 | size_t len = wxSTRING_MAXLEN; | |
1358 | if (val) { | |
1359 | for (len = 0; val[len] && (len<max_width); len++); | |
223d09f6 | 1360 | } else val = wxT("(null)"); |
4e57b0d4 VZ |
1361 | wxString s(val, len); |
1362 | if (s.Len() < min_width) | |
223d09f6 | 1363 | s.Pad(min_width - s.Len(), wxT(' '), adj_left); |
4e57b0d4 VZ |
1364 | *this += s; |
1365 | } | |
1366 | done = TRUE; | |
1367 | break; | |
223d09f6 | 1368 | case wxT('n'): |
4e57b0d4 VZ |
1369 | if (ilen == 0) { |
1370 | int *val = va_arg(argptr, int *); | |
1371 | *val = Len(); | |
1372 | } | |
1373 | else if (ilen == -1) { | |
1374 | short int *val = va_arg(argptr, short int *); | |
1375 | *val = Len(); | |
1376 | } | |
1377 | else if (ilen >= 1) { | |
1378 | long int *val = va_arg(argptr, long int *); | |
1379 | *val = Len(); | |
1380 | } | |
1381 | done = TRUE; | |
1382 | break; | |
1383 | default: | |
1384 | if (wxIsalpha(pszFormat[n])) | |
1385 | // probably some flag not taken care of here yet | |
1386 | s_szFlags[flagofs++] = pszFormat[n]; | |
1387 | else { | |
1388 | // bad format | |
223d09f6 | 1389 | *this += wxT('%'); // just to pass the glibc tst-printf.c |
4e57b0d4 VZ |
1390 | n--; |
1391 | done = TRUE; | |
1392 | } | |
1393 | break; | |
1394 | } | |
2bb67b80 OK |
1395 | #undef CHECK_PREC |
1396 | } while (!done); | |
1397 | } else *this += pszFormat[n]; | |
1398 | ||
1399 | #else | |
378b05f7 VZ |
1400 | // buffer to avoid dynamic memory allocation each time for small strings |
1401 | char szScratch[1024]; | |
1402 | ||
1403 | // NB: wxVsnprintf() may return either less than the buffer size or -1 if | |
1404 | // there is not enough place depending on implementation | |
1405 | int iLen = wxVsnprintfA(szScratch, WXSIZEOF(szScratch), pszFormat, argptr); | |
1406 | if ( iLen != -1 ) { | |
1407 | // the whole string is in szScratch | |
1408 | *this = szScratch; | |
7be07660 VZ |
1409 | } |
1410 | else { | |
378b05f7 VZ |
1411 | bool outOfMemory = FALSE; |
1412 | int size = 2*WXSIZEOF(szScratch); | |
1413 | while ( !outOfMemory ) { | |
1414 | char *buf = GetWriteBuf(size); | |
1415 | if ( buf ) | |
1416 | iLen = wxVsnprintfA(buf, size, pszFormat, argptr); | |
1417 | else | |
1418 | outOfMemory = TRUE; | |
1419 | ||
1420 | UngetWriteBuf(); | |
1421 | ||
1422 | if ( iLen != -1 ) { | |
7be07660 VZ |
1423 | // ok, there was enough space |
1424 | break; | |
1425 | } | |
1426 | ||
1427 | // still not enough, double it again | |
378b05f7 | 1428 | size *= 2; |
7be07660 VZ |
1429 | } |
1430 | ||
378b05f7 | 1431 | if ( outOfMemory ) { |
7be07660 VZ |
1432 | // out of memory |
1433 | return -1; | |
1434 | } | |
1435 | } | |
378b05f7 | 1436 | #endif // wxUSE_EXPERIMENTAL_PRINTF/!wxUSE_EXPERIMENTAL_PRINTF |
c801d85f | 1437 | |
2bb67b80 | 1438 | return Len(); |
c801d85f KB |
1439 | } |
1440 | ||
097c080b VZ |
1441 | // ---------------------------------------------------------------------------- |
1442 | // misc other operations | |
1443 | // ---------------------------------------------------------------------------- | |
0c5d3e1c VZ |
1444 | |
1445 | // returns TRUE if the string matches the pattern which may contain '*' and | |
1446 | // '?' metacharacters (as usual, '?' matches any character and '*' any number | |
1447 | // of them) | |
2bb67b80 | 1448 | bool wxString::Matches(const wxChar *pszMask) const |
097c080b VZ |
1449 | { |
1450 | // check char by char | |
2bb67b80 | 1451 | const wxChar *pszTxt; |
223d09f6 | 1452 | for ( pszTxt = c_str(); *pszMask != wxT('\0'); pszMask++, pszTxt++ ) { |
097c080b | 1453 | switch ( *pszMask ) { |
223d09f6 KB |
1454 | case wxT('?'): |
1455 | if ( *pszTxt == wxT('\0') ) | |
097c080b VZ |
1456 | return FALSE; |
1457 | ||
0c5d3e1c VZ |
1458 | // pszText and pszMask will be incremented in the loop statement |
1459 | ||
097c080b VZ |
1460 | break; |
1461 | ||
223d09f6 | 1462 | case wxT('*'): |
097c080b VZ |
1463 | { |
1464 | // ignore special chars immediately following this one | |
223d09f6 | 1465 | while ( *pszMask == wxT('*') || *pszMask == wxT('?') ) |
097c080b VZ |
1466 | pszMask++; |
1467 | ||
1468 | // if there is nothing more, match | |
223d09f6 | 1469 | if ( *pszMask == wxT('\0') ) |
097c080b VZ |
1470 | return TRUE; |
1471 | ||
1472 | // are there any other metacharacters in the mask? | |
c86f1403 | 1473 | size_t uiLenMask; |
223d09f6 | 1474 | const wxChar *pEndMask = wxStrpbrk(pszMask, wxT("*?")); |
097c080b VZ |
1475 | |
1476 | if ( pEndMask != NULL ) { | |
1477 | // we have to match the string between two metachars | |
1478 | uiLenMask = pEndMask - pszMask; | |
1479 | } | |
1480 | else { | |
1481 | // we have to match the remainder of the string | |
2bb67b80 | 1482 | uiLenMask = wxStrlen(pszMask); |
097c080b VZ |
1483 | } |
1484 | ||
1485 | wxString strToMatch(pszMask, uiLenMask); | |
2bb67b80 | 1486 | const wxChar* pMatch = wxStrstr(pszTxt, strToMatch); |
097c080b VZ |
1487 | if ( pMatch == NULL ) |
1488 | return FALSE; | |
1489 | ||
1490 | // -1 to compensate "++" in the loop | |
1491 | pszTxt = pMatch + uiLenMask - 1; | |
1492 | pszMask += uiLenMask - 1; | |
1493 | } | |
1494 | break; | |
1495 | ||
1496 | default: | |
1497 | if ( *pszMask != *pszTxt ) | |
1498 | return FALSE; | |
1499 | break; | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | // match only if nothing left | |
223d09f6 | 1504 | return *pszTxt == wxT('\0'); |
097c080b VZ |
1505 | } |
1506 | ||
1fc5dd6f | 1507 | // Count the number of chars |
2bb67b80 | 1508 | int wxString::Freq(wxChar ch) const |
1fc5dd6f JS |
1509 | { |
1510 | int count = 0; | |
1511 | int len = Len(); | |
1512 | for (int i = 0; i < len; i++) | |
1513 | { | |
1514 | if (GetChar(i) == ch) | |
1515 | count ++; | |
1516 | } | |
1517 | return count; | |
1518 | } | |
1519 | ||
03ab016d JS |
1520 | // convert to upper case, return the copy of the string |
1521 | wxString wxString::Upper() const | |
1522 | { wxString s(*this); return s.MakeUpper(); } | |
1523 | ||
1524 | // convert to lower case, return the copy of the string | |
1525 | wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); } | |
1526 | ||
2bb67b80 | 1527 | int wxString::sprintf(const wxChar *pszFormat, ...) |
8870c26e JS |
1528 | { |
1529 | va_list argptr; | |
1530 | va_start(argptr, pszFormat); | |
1531 | int iLen = PrintfV(pszFormat, argptr); | |
1532 | va_end(argptr); | |
1533 | return iLen; | |
1534 | } | |
1535 | ||
c801d85f KB |
1536 | // --------------------------------------------------------------------------- |
1537 | // standard C++ library string functions | |
1538 | // --------------------------------------------------------------------------- | |
8de2e39c | 1539 | #ifdef wxSTD_STRING_COMPATIBILITY |
c801d85f KB |
1540 | |
1541 | wxString& wxString::insert(size_t nPos, const wxString& str) | |
1542 | { | |
097c080b | 1543 | wxASSERT( str.GetStringData()->IsValid() ); |
c801d85f KB |
1544 | wxASSERT( nPos <= Len() ); |
1545 | ||
cb6780ff VZ |
1546 | if ( !str.IsEmpty() ) { |
1547 | wxString strTmp; | |
2bb67b80 OK |
1548 | wxChar *pc = strTmp.GetWriteBuf(Len() + str.Len()); |
1549 | wxStrncpy(pc, c_str(), nPos); | |
1550 | wxStrcpy(pc + nPos, str); | |
1551 | wxStrcpy(pc + nPos + str.Len(), c_str() + nPos); | |
cb6780ff VZ |
1552 | strTmp.UngetWriteBuf(); |
1553 | *this = strTmp; | |
1554 | } | |
dd1eaa89 VZ |
1555 | |
1556 | return *this; | |
c801d85f KB |
1557 | } |
1558 | ||
1559 | size_t wxString::find(const wxString& str, size_t nStart) const | |
1560 | { | |
097c080b | 1561 | wxASSERT( str.GetStringData()->IsValid() ); |
c801d85f KB |
1562 | wxASSERT( nStart <= Len() ); |
1563 | ||
2bb67b80 | 1564 | const wxChar *p = wxStrstr(c_str() + nStart, str); |
dd1eaa89 | 1565 | |
c801d85f KB |
1566 | return p == NULL ? npos : p - c_str(); |
1567 | } | |
1568 | ||
f0b3249b | 1569 | // VC++ 1.5 can't cope with the default argument in the header. |
3f4a0c5b | 1570 | #if !defined(__VISUALC__) || defined(__WIN32__) |
2bb67b80 | 1571 | size_t wxString::find(const wxChar* sz, size_t nStart, size_t n) const |
c801d85f | 1572 | { |
d71fa6fb | 1573 | return find(wxString(sz, n), nStart); |
c801d85f | 1574 | } |
3f4a0c5b | 1575 | #endif // VC++ 1.5 |
dd1eaa89 | 1576 | |
62448488 JS |
1577 | // Gives a duplicate symbol (presumably a case-insensitivity problem) |
1578 | #if !defined(__BORLANDC__) | |
2bb67b80 | 1579 | size_t wxString::find(wxChar ch, size_t nStart) const |
c801d85f KB |
1580 | { |
1581 | wxASSERT( nStart <= Len() ); | |
1582 | ||
2bb67b80 | 1583 | const wxChar *p = wxStrchr(c_str() + nStart, ch); |
dd1eaa89 | 1584 | |
c801d85f KB |
1585 | return p == NULL ? npos : p - c_str(); |
1586 | } | |
62448488 | 1587 | #endif |
c801d85f KB |
1588 | |
1589 | size_t wxString::rfind(const wxString& str, size_t nStart) const | |
1590 | { | |
097c080b | 1591 | wxASSERT( str.GetStringData()->IsValid() ); |
c801d85f KB |
1592 | wxASSERT( nStart <= Len() ); |
1593 | ||
969d318c | 1594 | // TODO could be made much quicker than that |
2bb67b80 | 1595 | const wxChar *p = c_str() + (nStart == npos ? Len() : nStart); |
c801d85f | 1596 | while ( p >= c_str() + str.Len() ) { |
2bb67b80 | 1597 | if ( wxStrncmp(p - str.Len(), str, str.Len()) == 0 ) |
c801d85f KB |
1598 | return p - str.Len() - c_str(); |
1599 | p--; | |
1600 | } | |
dd1eaa89 | 1601 | |
c801d85f KB |
1602 | return npos; |
1603 | } | |
dd1eaa89 | 1604 | |
f0b3249b | 1605 | // VC++ 1.5 can't cope with the default argument in the header. |
3f4a0c5b | 1606 | #if !defined(__VISUALC__) || defined(__WIN32__) |
2bb67b80 | 1607 | size_t wxString::rfind(const wxChar* sz, size_t nStart, size_t n) const |
c801d85f | 1608 | { |
969d318c | 1609 | return rfind(wxString(sz, n == npos ? 0 : n), nStart); |
c801d85f KB |
1610 | } |
1611 | ||
2bb67b80 | 1612 | size_t wxString::rfind(wxChar ch, size_t nStart) const |
c801d85f | 1613 | { |
969d318c VZ |
1614 | if ( nStart == npos ) |
1615 | { | |
1616 | nStart = Len(); | |
1617 | } | |
1618 | else | |
1619 | { | |
1620 | wxASSERT( nStart <= Len() ); | |
1621 | } | |
c801d85f | 1622 | |
969d318c | 1623 | const wxChar *p = wxStrrchr(c_str(), ch); |
dd1eaa89 | 1624 | |
969d318c VZ |
1625 | if ( p == NULL ) |
1626 | return npos; | |
1627 | ||
1628 | size_t result = p - c_str(); | |
1629 | return ( result > nStart ) ? npos : result; | |
c801d85f | 1630 | } |
3f4a0c5b | 1631 | #endif // VC++ 1.5 |
c801d85f | 1632 | |
969d318c VZ |
1633 | size_t wxString::find_first_of(const wxChar* sz, size_t nStart) const |
1634 | { | |
25dd56b1 BM |
1635 | const wxChar *start = c_str() + nStart; |
1636 | const wxChar *firstOf = wxStrpbrk(start, sz); | |
969d318c | 1637 | if ( firstOf ) |
bbf8fc53 | 1638 | return firstOf - c_str(); |
969d318c VZ |
1639 | else |
1640 | return npos; | |
1641 | } | |
1642 | ||
1643 | size_t wxString::find_last_of(const wxChar* sz, size_t nStart) const | |
1644 | { | |
1645 | if ( nStart == npos ) | |
1646 | { | |
1647 | nStart = Len(); | |
1648 | } | |
1649 | else | |
1650 | { | |
1651 | wxASSERT( nStart <= Len() ); | |
1652 | } | |
1653 | ||
20272888 | 1654 | for ( const wxChar *p = c_str() + length() - 1; p >= c_str(); p-- ) |
969d318c VZ |
1655 | { |
1656 | if ( wxStrchr(sz, *p) ) | |
1657 | return p - c_str(); | |
1658 | } | |
1659 | ||
1660 | return npos; | |
1661 | } | |
1662 | ||
1663 | size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart) const | |
1664 | { | |
1665 | if ( nStart == npos ) | |
1666 | { | |
1667 | nStart = Len(); | |
1668 | } | |
1669 | else | |
1670 | { | |
1671 | wxASSERT( nStart <= Len() ); | |
1672 | } | |
1673 | ||
20272888 | 1674 | size_t nAccept = wxStrspn(c_str() + nStart, sz); |
969d318c VZ |
1675 | if ( nAccept >= length() - nStart ) |
1676 | return npos; | |
1677 | else | |
1678 | return nAccept; | |
1679 | } | |
1680 | ||
1681 | size_t wxString::find_first_not_of(wxChar ch, size_t nStart) const | |
1682 | { | |
1683 | wxASSERT( nStart <= Len() ); | |
1684 | ||
20272888 | 1685 | for ( const wxChar *p = c_str() + nStart; *p; p++ ) |
969d318c VZ |
1686 | { |
1687 | if ( *p != ch ) | |
1688 | return p - c_str(); | |
1689 | } | |
1690 | ||
1691 | return npos; | |
1692 | } | |
1693 | ||
1694 | size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart) const | |
1695 | { | |
1696 | if ( nStart == npos ) | |
1697 | { | |
1698 | nStart = Len(); | |
1699 | } | |
1700 | else | |
1701 | { | |
1702 | wxASSERT( nStart <= Len() ); | |
1703 | } | |
1704 | ||
20272888 | 1705 | for ( const wxChar *p = c_str() + nStart - 1; p >= c_str(); p-- ) |
969d318c VZ |
1706 | { |
1707 | if ( !wxStrchr(sz, *p) ) | |
1708 | return p - c_str(); | |
1709 | } | |
1710 | ||
1711 | return npos; | |
1712 | } | |
1713 | ||
1714 | size_t wxString::find_last_not_of(wxChar ch, size_t nStart) const | |
1715 | { | |
1716 | if ( nStart == npos ) | |
1717 | { | |
1718 | nStart = Len(); | |
1719 | } | |
1720 | else | |
1721 | { | |
1722 | wxASSERT( nStart <= Len() ); | |
1723 | } | |
1724 | ||
20272888 | 1725 | for ( const wxChar *p = c_str() + nStart - 1; p >= c_str(); p-- ) |
969d318c VZ |
1726 | { |
1727 | if ( *p != ch ) | |
1728 | return p - c_str(); | |
1729 | } | |
1730 | ||
1731 | return npos; | |
1732 | } | |
1733 | ||
c801d85f KB |
1734 | wxString& wxString::erase(size_t nStart, size_t nLen) |
1735 | { | |
1736 | wxString strTmp(c_str(), nStart); | |
1737 | if ( nLen != npos ) { | |
1738 | wxASSERT( nStart + nLen <= Len() ); | |
1739 | ||
1740 | strTmp.append(c_str() + nStart + nLen); | |
1741 | } | |
1742 | ||
1743 | *this = strTmp; | |
1744 | return *this; | |
1745 | } | |
1746 | ||
2bb67b80 | 1747 | wxString& wxString::replace(size_t nStart, size_t nLen, const wxChar *sz) |
c801d85f | 1748 | { |
f6bcfd97 BP |
1749 | wxASSERT_MSG( nStart + nLen <= Len(), |
1750 | _T("index out of bounds in wxString::replace") ); | |
c801d85f KB |
1751 | |
1752 | wxString strTmp; | |
f6bcfd97 BP |
1753 | strTmp.Alloc(Len()); // micro optimisation to avoid multiple mem allocs |
1754 | ||
c801d85f KB |
1755 | if ( nStart != 0 ) |
1756 | strTmp.append(c_str(), nStart); | |
f6bcfd97 | 1757 | strTmp << sz << c_str() + nStart + nLen; |
dd1eaa89 | 1758 | |
c801d85f KB |
1759 | *this = strTmp; |
1760 | return *this; | |
1761 | } | |
1762 | ||
2bb67b80 | 1763 | wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch) |
c801d85f KB |
1764 | { |
1765 | return replace(nStart, nLen, wxString(ch, nCount)); | |
1766 | } | |
1767 | ||
dd1eaa89 | 1768 | wxString& wxString::replace(size_t nStart, size_t nLen, |
097c080b | 1769 | const wxString& str, size_t nStart2, size_t nLen2) |
c801d85f KB |
1770 | { |
1771 | return replace(nStart, nLen, str.substr(nStart2, nLen2)); | |
1772 | } | |
1773 | ||
dd1eaa89 | 1774 | wxString& wxString::replace(size_t nStart, size_t nLen, |
2bb67b80 | 1775 | const wxChar* sz, size_t nCount) |
c801d85f KB |
1776 | { |
1777 | return replace(nStart, nLen, wxString(sz, nCount)); | |
1778 | } | |
1779 | ||
1780 | #endif //std::string compatibility | |
1781 | ||
1782 | // ============================================================================ | |
1783 | // ArrayString | |
1784 | // ============================================================================ | |
1785 | ||
1786 | // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT) | |
1787 | #define ARRAY_MAXSIZE_INCREMENT 4096 | |
1788 | #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h | |
1789 | #define ARRAY_DEFAULT_INITIAL_SIZE (16) | |
1790 | #endif | |
1791 | ||
1792 | #define STRING(p) ((wxString *)(&(p))) | |
1793 | ||
1794 | // ctor | |
e87271f3 | 1795 | wxArrayString::wxArrayString(bool autoSort) |
c801d85f KB |
1796 | { |
1797 | m_nSize = | |
1798 | m_nCount = 0; | |
2bb67b80 | 1799 | m_pItems = (wxChar **) NULL; |
e87271f3 | 1800 | m_autoSort = autoSort; |
c801d85f KB |
1801 | } |
1802 | ||
1803 | // copy ctor | |
1804 | wxArrayString::wxArrayString(const wxArrayString& src) | |
1805 | { | |
3bbb630a VZ |
1806 | m_nSize = |
1807 | m_nCount = 0; | |
2bb67b80 | 1808 | m_pItems = (wxChar **) NULL; |
e87271f3 | 1809 | m_autoSort = src.m_autoSort; |
c801d85f | 1810 | |
4d14b524 | 1811 | *this = src; |
c801d85f KB |
1812 | } |
1813 | ||
4d14b524 | 1814 | // assignment operator |
c801d85f KB |
1815 | wxArrayString& wxArrayString::operator=(const wxArrayString& src) |
1816 | { | |
d93f63db VZ |
1817 | if ( m_nSize > 0 ) |
1818 | Clear(); | |
c801d85f | 1819 | |
e87271f3 VZ |
1820 | Copy(src); |
1821 | ||
1822 | return *this; | |
1823 | } | |
1824 | ||
1825 | void wxArrayString::Copy(const wxArrayString& src) | |
1826 | { | |
4d14b524 VZ |
1827 | if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE ) |
1828 | Alloc(src.m_nCount); | |
c801d85f | 1829 | |
e87271f3 VZ |
1830 | for ( size_t n = 0; n < src.m_nCount; n++ ) |
1831 | Add(src[n]); | |
c801d85f KB |
1832 | } |
1833 | ||
1834 | // grow the array | |
1835 | void wxArrayString::Grow() | |
1836 | { | |
1837 | // only do it if no more place | |
1838 | if( m_nCount == m_nSize ) { | |
1839 | if( m_nSize == 0 ) { | |
1840 | // was empty, alloc some memory | |
1841 | m_nSize = ARRAY_DEFAULT_INITIAL_SIZE; | |
2bb67b80 | 1842 | m_pItems = new wxChar *[m_nSize]; |
c801d85f KB |
1843 | } |
1844 | else { | |
3bbb630a VZ |
1845 | // otherwise when it's called for the first time, nIncrement would be 0 |
1846 | // and the array would never be expanded | |
e87271f3 VZ |
1847 | #if defined(__VISAGECPP__) && defined(__WXDEBUG__) |
1848 | int array_size = ARRAY_DEFAULT_INITIAL_SIZE; | |
913df6f2 DW |
1849 | wxASSERT( array_size != 0 ); |
1850 | #else | |
3bbb630a | 1851 | wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE != 0 ); |
913df6f2 | 1852 | #endif |
3bbb630a | 1853 | |
c801d85f | 1854 | // add 50% but not too much |
3bbb630a | 1855 | size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE |
4d14b524 | 1856 | ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; |
c801d85f KB |
1857 | if ( nIncrement > ARRAY_MAXSIZE_INCREMENT ) |
1858 | nIncrement = ARRAY_MAXSIZE_INCREMENT; | |
1859 | m_nSize += nIncrement; | |
2bb67b80 | 1860 | wxChar **pNew = new wxChar *[m_nSize]; |
c801d85f KB |
1861 | |
1862 | // copy data to new location | |
2bb67b80 | 1863 | memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *)); |
c801d85f KB |
1864 | |
1865 | // delete old memory (but do not release the strings!) | |
a3622daa | 1866 | wxDELETEA(m_pItems); |
c801d85f KB |
1867 | |
1868 | m_pItems = pNew; | |
1869 | } | |
1870 | } | |
1871 | } | |
1872 | ||
1873 | void wxArrayString::Free() | |
1874 | { | |
1875 | for ( size_t n = 0; n < m_nCount; n++ ) { | |
1876 | STRING(m_pItems[n])->GetStringData()->Unlock(); | |
1877 | } | |
1878 | } | |
1879 | ||
1880 | // deletes all the strings from the list | |
1881 | void wxArrayString::Empty() | |
1882 | { | |
1883 | Free(); | |
1884 | ||
1885 | m_nCount = 0; | |
1886 | } | |
1887 | ||
1888 | // as Empty, but also frees memory | |
1889 | void wxArrayString::Clear() | |
1890 | { | |
1891 | Free(); | |
1892 | ||
dd1eaa89 | 1893 | m_nSize = |
c801d85f KB |
1894 | m_nCount = 0; |
1895 | ||
a3622daa | 1896 | wxDELETEA(m_pItems); |
c801d85f KB |
1897 | } |
1898 | ||
1899 | // dtor | |
1900 | wxArrayString::~wxArrayString() | |
1901 | { | |
1902 | Free(); | |
1903 | ||
a3622daa | 1904 | wxDELETEA(m_pItems); |
c801d85f KB |
1905 | } |
1906 | ||
1907 | // pre-allocates memory (frees the previous data!) | |
1908 | void wxArrayString::Alloc(size_t nSize) | |
1909 | { | |
1910 | wxASSERT( nSize > 0 ); | |
1911 | ||
1912 | // only if old buffer was not big enough | |
1913 | if ( nSize > m_nSize ) { | |
1914 | Free(); | |
a3622daa | 1915 | wxDELETEA(m_pItems); |
2bb67b80 | 1916 | m_pItems = new wxChar *[nSize]; |
c801d85f KB |
1917 | m_nSize = nSize; |
1918 | } | |
1919 | ||
1920 | m_nCount = 0; | |
1921 | } | |
1922 | ||
d4ffe273 OK |
1923 | // minimizes the memory usage by freeing unused memory |
1924 | void wxArrayString::Shrink() | |
1925 | { | |
1926 | // only do it if we have some memory to free | |
1927 | if( m_nCount < m_nSize ) { | |
1928 | // allocates exactly as much memory as we need | |
913df6f2 | 1929 | wxChar **pNew = new wxChar *[m_nCount]; |
d4ffe273 OK |
1930 | |
1931 | // copy data to new location | |
1932 | memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *)); | |
1933 | delete [] m_pItems; | |
1934 | m_pItems = pNew; | |
1935 | } | |
1936 | } | |
1937 | ||
c801d85f | 1938 | // searches the array for an item (forward or backwards) |
2bb67b80 | 1939 | int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const |
c801d85f | 1940 | { |
e87271f3 VZ |
1941 | if ( m_autoSort ) { |
1942 | // use binary search in the sorted array | |
1943 | wxASSERT_MSG( bCase && !bFromEnd, | |
1944 | wxT("search parameters ignored for auto sorted array") ); | |
1945 | ||
1946 | size_t i, | |
1947 | lo = 0, | |
1948 | hi = m_nCount; | |
1949 | int res; | |
1950 | while ( lo < hi ) { | |
1951 | i = (lo + hi)/2; | |
1952 | ||
1953 | res = wxStrcmp(sz, m_pItems[i]); | |
1954 | if ( res < 0 ) | |
1955 | hi = i; | |
1956 | else if ( res > 0 ) | |
1957 | lo = i + 1; | |
1958 | else | |
1959 | return i; | |
c801d85f | 1960 | } |
e87271f3 VZ |
1961 | |
1962 | return wxNOT_FOUND; | |
c801d85f KB |
1963 | } |
1964 | else { | |
e87271f3 VZ |
1965 | // use linear search in unsorted array |
1966 | if ( bFromEnd ) { | |
1967 | if ( m_nCount > 0 ) { | |
1968 | size_t ui = m_nCount; | |
1969 | do { | |
1970 | if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) ) | |
1971 | return ui; | |
1972 | } | |
1973 | while ( ui != 0 ); | |
1974 | } | |
1975 | } | |
1976 | else { | |
1977 | for( size_t ui = 0; ui < m_nCount; ui++ ) { | |
1978 | if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) ) | |
1979 | return ui; | |
1980 | } | |
c801d85f KB |
1981 | } |
1982 | } | |
1983 | ||
3c67202d | 1984 | return wxNOT_FOUND; |
c801d85f KB |
1985 | } |
1986 | ||
1987 | // add item at the end | |
e01c8145 | 1988 | size_t wxArrayString::Add(const wxString& str) |
c801d85f | 1989 | { |
e87271f3 VZ |
1990 | if ( m_autoSort ) { |
1991 | // insert the string at the correct position to keep the array sorted | |
1992 | size_t i, | |
1993 | lo = 0, | |
1994 | hi = m_nCount; | |
1995 | int res; | |
1996 | while ( lo < hi ) { | |
1997 | i = (lo + hi)/2; | |
1998 | ||
1999 | res = wxStrcmp(str, m_pItems[i]); | |
2000 | if ( res < 0 ) | |
2001 | hi = i; | |
2002 | else if ( res > 0 ) | |
2003 | lo = i + 1; | |
2004 | else { | |
2005 | lo = hi = i; | |
2006 | break; | |
2007 | } | |
2008 | } | |
097c080b | 2009 | |
e87271f3 | 2010 | wxASSERT_MSG( lo == hi, wxT("binary search broken") ); |
c801d85f | 2011 | |
e87271f3 | 2012 | Insert(str, lo); |
e01c8145 VZ |
2013 | |
2014 | return (size_t)lo; | |
e87271f3 VZ |
2015 | } |
2016 | else { | |
2017 | wxASSERT( str.GetStringData()->IsValid() ); | |
2018 | ||
2019 | Grow(); | |
2020 | ||
2021 | // the string data must not be deleted! | |
2022 | str.GetStringData()->Lock(); | |
2023 | ||
2024 | // just append | |
e01c8145 VZ |
2025 | m_pItems[m_nCount] = (wxChar *)str.c_str(); // const_cast |
2026 | ||
2027 | return m_nCount++; | |
e87271f3 | 2028 | } |
c801d85f KB |
2029 | } |
2030 | ||
2031 | // add item at the given position | |
097c080b | 2032 | void wxArrayString::Insert(const wxString& str, size_t nIndex) |
c801d85f | 2033 | { |
097c080b VZ |
2034 | wxASSERT( str.GetStringData()->IsValid() ); |
2035 | ||
e87271f3 | 2036 | wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArrayString::Insert") ); |
c801d85f KB |
2037 | |
2038 | Grow(); | |
2039 | ||
dd1eaa89 | 2040 | memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex], |
2bb67b80 | 2041 | (m_nCount - nIndex)*sizeof(wxChar *)); |
c801d85f | 2042 | |
097c080b | 2043 | str.GetStringData()->Lock(); |
2bb67b80 | 2044 | m_pItems[nIndex] = (wxChar *)str.c_str(); |
c801d85f KB |
2045 | |
2046 | m_nCount++; | |
2047 | } | |
2048 | ||
2049 | // removes item from array (by index) | |
2050 | void wxArrayString::Remove(size_t nIndex) | |
2051 | { | |
e87271f3 | 2052 | wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArrayString::Remove") ); |
c801d85f KB |
2053 | |
2054 | // release our lock | |
2055 | Item(nIndex).GetStringData()->Unlock(); | |
2056 | ||
dd1eaa89 | 2057 | memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1], |
2bb67b80 | 2058 | (m_nCount - nIndex - 1)*sizeof(wxChar *)); |
c801d85f KB |
2059 | m_nCount--; |
2060 | } | |
2061 | ||
2062 | // removes item from array (by value) | |
2bb67b80 | 2063 | void wxArrayString::Remove(const wxChar *sz) |
c801d85f KB |
2064 | { |
2065 | int iIndex = Index(sz); | |
2066 | ||
3c67202d | 2067 | wxCHECK_RET( iIndex != wxNOT_FOUND, |
e87271f3 | 2068 | wxT("removing inexistent element in wxArrayString::Remove") ); |
c801d85f | 2069 | |
c86f1403 | 2070 | Remove(iIndex); |
c801d85f KB |
2071 | } |
2072 | ||
30b21f9a VZ |
2073 | // ---------------------------------------------------------------------------- |
2074 | // sorting | |
2075 | // ---------------------------------------------------------------------------- | |
2076 | ||
2077 | // we can only sort one array at a time with the quick-sort based | |
2078 | // implementation | |
2079 | #if wxUSE_THREADS | |
30b21f9a VZ |
2080 | // need a critical section to protect access to gs_compareFunction and |
2081 | // gs_sortAscending variables | |
26128999 | 2082 | static wxCriticalSection *gs_critsectStringSort = NULL; |
30b21f9a VZ |
2083 | |
2084 | // call this before the value of the global sort vars is changed/after | |
2085 | // you're finished with them | |
26128999 VZ |
2086 | #define START_SORT() wxASSERT( !gs_critsectStringSort ); \ |
2087 | gs_critsectStringSort = new wxCriticalSection; \ | |
2088 | gs_critsectStringSort->Enter() | |
2089 | #define END_SORT() gs_critsectStringSort->Leave(); \ | |
2090 | delete gs_critsectStringSort; \ | |
2091 | gs_critsectStringSort = NULL | |
30b21f9a VZ |
2092 | #else // !threads |
2093 | #define START_SORT() | |
2094 | #define END_SORT() | |
2095 | #endif // wxUSE_THREADS | |
2096 | ||
2097 | // function to use for string comparaison | |
2098 | static wxArrayString::CompareFunction gs_compareFunction = NULL; | |
2099 | ||
2100 | // if we don't use the compare function, this flag tells us if we sort the | |
2101 | // array in ascending or descending order | |
2102 | static bool gs_sortAscending = TRUE; | |
2103 | ||
2104 | // function which is called by quick sort | |
913df6f2 | 2105 | static int LINKAGEMODE wxStringCompareFunction(const void *first, const void *second) |
30b21f9a VZ |
2106 | { |
2107 | wxString *strFirst = (wxString *)first; | |
2108 | wxString *strSecond = (wxString *)second; | |
2109 | ||
64716cd7 | 2110 | if ( gs_compareFunction ) { |
30b21f9a | 2111 | return gs_compareFunction(*strFirst, *strSecond); |
64716cd7 | 2112 | } |
30b21f9a | 2113 | else { |
2bb67b80 OK |
2114 | // maybe we should use wxStrcoll |
2115 | int result = wxStrcmp(strFirst->c_str(), strSecond->c_str()); | |
30b21f9a VZ |
2116 | |
2117 | return gs_sortAscending ? result : -result; | |
2118 | } | |
2119 | } | |
2120 | ||
c801d85f | 2121 | // sort array elements using passed comparaison function |
30b21f9a VZ |
2122 | void wxArrayString::Sort(CompareFunction compareFunction) |
2123 | { | |
2124 | START_SORT(); | |
2125 | ||
2126 | wxASSERT( !gs_compareFunction ); // must have been reset to NULL | |
2127 | gs_compareFunction = compareFunction; | |
2128 | ||
2129 | DoSort(); | |
2130 | ||
f6bcfd97 BP |
2131 | // reset it to NULL so that Sort(bool) will work the next time |
2132 | gs_compareFunction = NULL; | |
2133 | ||
30b21f9a VZ |
2134 | END_SORT(); |
2135 | } | |
2136 | ||
2137 | void wxArrayString::Sort(bool reverseOrder) | |
2138 | { | |
2139 | START_SORT(); | |
2140 | ||
2141 | wxASSERT( !gs_compareFunction ); // must have been reset to NULL | |
2142 | gs_sortAscending = !reverseOrder; | |
2143 | ||
2144 | DoSort(); | |
2145 | ||
2146 | END_SORT(); | |
2147 | } | |
c801d85f | 2148 | |
30b21f9a | 2149 | void wxArrayString::DoSort() |
c801d85f | 2150 | { |
e87271f3 VZ |
2151 | wxCHECK_RET( !m_autoSort, wxT("can't use this method with sorted arrays") ); |
2152 | ||
30b21f9a VZ |
2153 | // just sort the pointers using qsort() - of course it only works because |
2154 | // wxString() *is* a pointer to its data | |
2bb67b80 | 2155 | qsort(m_pItems, m_nCount, sizeof(wxChar *), wxStringCompareFunction); |
c801d85f | 2156 | } |
2bb67b80 | 2157 | |
f6bcfd97 BP |
2158 | bool wxArrayString::operator==(const wxArrayString& a) const |
2159 | { | |
2160 | if ( m_nCount != a.m_nCount ) | |
2161 | return FALSE; | |
2162 | ||
2163 | for ( size_t n = 0; n < m_nCount; n++ ) | |
2164 | { | |
2165 | if ( Item(n) != a[n] ) | |
2166 | return FALSE; | |
2167 | } | |
2168 | ||
2169 | return TRUE; | |
2170 | } | |
2171 |