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