]>
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__ | |
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 | |
3c024cc2 VZ |
35 | #include "wx/defs.h" |
36 | #include "wx/string.h" | |
37 | #include "wx/intl.h" | |
c801d85f KB |
38 | #endif |
39 | ||
40 | #include <ctype.h> | |
41 | #include <string.h> | |
42 | #include <stdlib.h> | |
43 | ||
44 | #ifdef WXSTRING_IS_WXOBJECT | |
45 | IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject) | |
46 | #endif //WXSTRING_IS_WXOBJECT | |
47 | ||
3168a13f VZ |
48 | // allocating extra space for each string consumes more memory but speeds up |
49 | // the concatenation operations (nLen is the current string's length) | |
77ca46e7 VZ |
50 | // NB: EXTRA_ALLOC must be >= 0! |
51 | #define EXTRA_ALLOC (19 - nLen % 16) | |
3168a13f | 52 | |
c801d85f KB |
53 | // --------------------------------------------------------------------------- |
54 | // static class variables definition | |
55 | // --------------------------------------------------------------------------- | |
56 | ||
57 | #ifdef STD_STRING_COMPATIBILITY | |
58 | const size_t wxString::npos = STRING_MAXLEN; | |
59 | #endif | |
60 | ||
3168a13f VZ |
61 | // ---------------------------------------------------------------------------- |
62 | // static data | |
63 | // ---------------------------------------------------------------------------- | |
c801d85f | 64 | |
3c024cc2 VZ |
65 | // for an empty string, GetStringData() will return this address: this |
66 | // structure has the same layout as wxStringData and it's data() method will | |
67 | // return the empty string (dummy pointer) | |
68 | static const struct | |
69 | { | |
70 | wxStringData data; | |
71 | char dummy; | |
72 | } g_strEmpty = { {-1, 0, 0}, '\0' }; | |
73 | ||
c801d85f | 74 | // empty C style string: points to 'string data' byte of g_strEmpty |
3c024cc2 | 75 | extern const char *g_szNul = &g_strEmpty.dummy; |
c801d85f | 76 | |
3168a13f | 77 | // ---------------------------------------------------------------------------- |
c801d85f | 78 | // global functions |
3168a13f | 79 | // ---------------------------------------------------------------------------- |
c801d85f KB |
80 | |
81 | #ifdef STD_STRING_COMPATIBILITY | |
82 | ||
83 | // MS Visual C++ version 5.0 provides the new STL headers as well as the old | |
84 | // iostream ones. | |
85 | // | |
86 | // ATTN: you can _not_ use both of these in the same program! | |
87 | #if 0 // def _MSC_VER | |
88 | #include <iostream> | |
89 | #define NAMESPACE std:: | |
90 | #else | |
91 | #include <iostream.h> | |
92 | #define NAMESPACE | |
93 | #endif //Visual C++ | |
94 | ||
95 | NAMESPACE istream& operator>>(NAMESPACE istream& is, wxString& WXUNUSED(str)) | |
96 | { | |
97 | #if 0 | |
98 | int w = is.width(0); | |
99 | if ( is.ipfx(0) ) { | |
100 | NAMESPACE streambuf *sb = is.rdbuf(); | |
101 | str.erase(); | |
102 | while ( true ) { | |
103 | int ch = sb->sbumpc (); | |
104 | if ( ch == EOF ) { | |
105 | is.setstate(NAMESPACE ios::eofbit); | |
106 | break; | |
107 | } | |
108 | else if ( isspace(ch) ) { | |
109 | sb->sungetc(); | |
110 | break; | |
111 | } | |
dd1eaa89 | 112 | |
c801d85f KB |
113 | str += ch; |
114 | if ( --w == 1 ) | |
115 | break; | |
116 | } | |
117 | } | |
118 | ||
119 | is.isfx(); | |
120 | if ( str.length() == 0 ) | |
121 | is.setstate(NAMESPACE ios::failbit); | |
122 | #endif | |
123 | return is; | |
124 | } | |
125 | ||
126 | #endif //std::string compatibility | |
127 | ||
3168a13f VZ |
128 | // ---------------------------------------------------------------------------- |
129 | // private classes | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
132 | // this small class is used to gather statistics for performance tuning | |
133 | //#define WXSTRING_STATISTICS | |
134 | #ifdef WXSTRING_STATISTICS | |
135 | class Averager | |
136 | { | |
137 | public: | |
138 | Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } | |
139 | ~Averager() | |
140 | { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); } | |
141 | ||
c86f1403 | 142 | void Add(size_t n) { m_nTotal += n; m_nCount++; } |
3168a13f VZ |
143 | |
144 | private: | |
c86f1403 | 145 | size_t m_nCount, m_nTotal; |
3168a13f VZ |
146 | const char *m_sz; |
147 | } g_averageLength("allocation size"), | |
148 | g_averageSummandLength("summand length"), | |
149 | g_averageConcatHit("hit probability in concat"), | |
150 | g_averageInitialLength("initial string length"); | |
151 | ||
152 | #define STATISTICS_ADD(av, val) g_average##av.Add(val) | |
153 | #else | |
154 | #define STATISTICS_ADD(av, val) | |
155 | #endif // WXSTRING_STATISTICS | |
156 | ||
c801d85f KB |
157 | // =========================================================================== |
158 | // wxString class core | |
159 | // =========================================================================== | |
160 | ||
161 | // --------------------------------------------------------------------------- | |
162 | // construction | |
163 | // --------------------------------------------------------------------------- | |
164 | ||
c801d85f KB |
165 | // constructs string of <nLength> copies of character <ch> |
166 | wxString::wxString(char ch, size_t nLength) | |
167 | { | |
168 | Init(); | |
169 | ||
170 | if ( nLength > 0 ) { | |
171 | AllocBuffer(nLength); | |
f1da2f03 | 172 | |
c801d85f KB |
173 | wxASSERT( sizeof(char) == 1 ); // can't use memset if not |
174 | ||
175 | memset(m_pchData, ch, nLength); | |
176 | } | |
177 | } | |
178 | ||
179 | // takes nLength elements of psz starting at nPos | |
180 | void wxString::InitWith(const char *psz, size_t nPos, size_t nLength) | |
181 | { | |
182 | Init(); | |
183 | ||
184 | wxASSERT( nPos <= Strlen(psz) ); | |
185 | ||
186 | if ( nLength == STRING_MAXLEN ) | |
187 | nLength = Strlen(psz + nPos); | |
188 | ||
3168a13f VZ |
189 | STATISTICS_ADD(InitialLength, nLength); |
190 | ||
c801d85f KB |
191 | if ( nLength > 0 ) { |
192 | // trailing '\0' is written in AllocBuffer() | |
193 | AllocBuffer(nLength); | |
194 | memcpy(m_pchData, psz + nPos, nLength*sizeof(char)); | |
195 | } | |
196 | } | |
dd1eaa89 | 197 | |
c801d85f KB |
198 | // the same as previous constructor, but for compilers using unsigned char |
199 | wxString::wxString(const unsigned char* psz, size_t nLength) | |
200 | { | |
201 | InitWith((const char *)psz, 0, nLength); | |
dd1eaa89 VZ |
202 | } |
203 | ||
c801d85f KB |
204 | #ifdef STD_STRING_COMPATIBILITY |
205 | ||
c801d85f KB |
206 | // poor man's iterators are "void *" pointers |
207 | wxString::wxString(const void *pStart, const void *pEnd) | |
208 | { | |
dd1eaa89 | 209 | InitWith((const char *)pStart, 0, |
c801d85f KB |
210 | (const char *)pEnd - (const char *)pStart); |
211 | } | |
212 | ||
213 | #endif //std::string compatibility | |
214 | ||
215 | // from wide string | |
216 | wxString::wxString(const wchar_t *pwz) | |
217 | { | |
218 | // first get necessary size | |
c67daf87 | 219 | size_t nLen = wcstombs((char *) NULL, pwz, 0); |
c801d85f KB |
220 | |
221 | // empty? | |
222 | if ( nLen != 0 ) { | |
223 | AllocBuffer(nLen); | |
224 | wcstombs(m_pchData, pwz, nLen); | |
225 | } | |
226 | else { | |
227 | Init(); | |
228 | } | |
229 | } | |
230 | ||
231 | // --------------------------------------------------------------------------- | |
232 | // memory allocation | |
233 | // --------------------------------------------------------------------------- | |
234 | ||
235 | // allocates memory needed to store a C string of length nLen | |
236 | void wxString::AllocBuffer(size_t nLen) | |
237 | { | |
238 | wxASSERT( nLen > 0 ); // | |
239 | wxASSERT( nLen <= INT_MAX-1 ); // max size (enough room for 1 extra) | |
240 | ||
3168a13f VZ |
241 | STATISTICS_ADD(Length, nLen); |
242 | ||
c801d85f KB |
243 | // allocate memory: |
244 | // 1) one extra character for '\0' termination | |
245 | // 2) sizeof(wxStringData) for housekeeping info | |
3168a13f VZ |
246 | wxStringData* pData = (wxStringData*) |
247 | malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(char)); | |
c801d85f | 248 | pData->nRefs = 1; |
c801d85f | 249 | pData->nDataLength = nLen; |
3168a13f | 250 | pData->nAllocLength = nLen + EXTRA_ALLOC; |
c801d85f | 251 | m_pchData = pData->data(); // data starts after wxStringData |
3168a13f | 252 | m_pchData[nLen] = '\0'; |
c801d85f KB |
253 | } |
254 | ||
c801d85f KB |
255 | // must be called before changing this string |
256 | void wxString::CopyBeforeWrite() | |
257 | { | |
258 | wxStringData* pData = GetStringData(); | |
259 | ||
260 | if ( pData->IsShared() ) { | |
261 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 262 | size_t nLen = pData->nDataLength; |
3168a13f VZ |
263 | AllocBuffer(nLen); |
264 | memcpy(m_pchData, pData->data(), nLen*sizeof(char)); | |
c801d85f KB |
265 | } |
266 | ||
3bbb630a | 267 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
c801d85f KB |
268 | } |
269 | ||
270 | // must be called before replacing contents of this string | |
271 | void wxString::AllocBeforeWrite(size_t nLen) | |
272 | { | |
273 | wxASSERT( nLen != 0 ); // doesn't make any sense | |
274 | ||
275 | // must not share string and must have enough space | |
3168a13f | 276 | wxStringData* pData = GetStringData(); |
c801d85f KB |
277 | if ( pData->IsShared() || (nLen > pData->nAllocLength) ) { |
278 | // can't work with old buffer, get new one | |
279 | pData->Unlock(); | |
280 | AllocBuffer(nLen); | |
281 | } | |
282 | ||
f1da2f03 | 283 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
c801d85f KB |
284 | } |
285 | ||
dd1eaa89 | 286 | // allocate enough memory for nLen characters |
c86f1403 | 287 | void wxString::Alloc(size_t nLen) |
dd1eaa89 VZ |
288 | { |
289 | wxStringData *pData = GetStringData(); | |
290 | if ( pData->nAllocLength <= nLen ) { | |
9fbd8b8d VZ |
291 | if ( pData->IsEmpty() ) { |
292 | nLen += EXTRA_ALLOC; | |
293 | ||
294 | wxStringData* pData = (wxStringData*) | |
295 | malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(char)); | |
296 | pData->nRefs = 1; | |
297 | pData->nDataLength = 0; | |
298 | pData->nAllocLength = nLen; | |
299 | m_pchData = pData->data(); // data starts after wxStringData | |
300 | m_pchData[0u] = '\0'; | |
301 | } | |
3168a13f VZ |
302 | else if ( pData->IsShared() ) { |
303 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 304 | size_t nOldLen = pData->nDataLength; |
3168a13f | 305 | AllocBuffer(nLen); |
9fbd8b8d | 306 | memcpy(m_pchData, pData->data(), nOldLen*sizeof(char)); |
3168a13f | 307 | } |
dd1eaa89 | 308 | else { |
3168a13f VZ |
309 | nLen += EXTRA_ALLOC; |
310 | ||
dd1eaa89 VZ |
311 | wxStringData *p = (wxStringData *) |
312 | realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(char)); | |
3168a13f VZ |
313 | |
314 | if ( p == NULL ) { | |
315 | // @@@ what to do on memory error? | |
316 | return; | |
dd1eaa89 | 317 | } |
3168a13f VZ |
318 | |
319 | // it's not important if the pointer changed or not (the check for this | |
320 | // is not faster than assigning to m_pchData in all cases) | |
321 | p->nAllocLength = nLen; | |
322 | m_pchData = p->data(); | |
dd1eaa89 VZ |
323 | } |
324 | } | |
325 | //else: we've already got enough | |
326 | } | |
327 | ||
328 | // shrink to minimal size (releasing extra memory) | |
329 | void wxString::Shrink() | |
330 | { | |
331 | wxStringData *pData = GetStringData(); | |
3bbb630a VZ |
332 | |
333 | // this variable is unused in release build, so avoid the compiler warning by | |
334 | // just not declaring it | |
335 | #ifdef __WXDEBUG__ | |
336 | void *p = | |
337 | #endif | |
338 | realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(char)); | |
339 | ||
3168a13f | 340 | wxASSERT( p != NULL ); // can't free memory? |
dd1eaa89 VZ |
341 | wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move! |
342 | } | |
343 | ||
c801d85f | 344 | // get the pointer to writable buffer of (at least) nLen bytes |
c86f1403 | 345 | char *wxString::GetWriteBuf(size_t nLen) |
c801d85f KB |
346 | { |
347 | AllocBeforeWrite(nLen); | |
097c080b VZ |
348 | |
349 | wxASSERT( GetStringData()->nRefs == 1 ); | |
350 | GetStringData()->Validate(FALSE); | |
351 | ||
c801d85f KB |
352 | return m_pchData; |
353 | } | |
354 | ||
097c080b VZ |
355 | // put string back in a reasonable state after GetWriteBuf |
356 | void wxString::UngetWriteBuf() | |
357 | { | |
358 | GetStringData()->nDataLength = strlen(m_pchData); | |
359 | GetStringData()->Validate(TRUE); | |
360 | } | |
361 | ||
c801d85f KB |
362 | // --------------------------------------------------------------------------- |
363 | // data access | |
364 | // --------------------------------------------------------------------------- | |
365 | ||
366 | // all functions are inline in string.h | |
367 | ||
368 | // --------------------------------------------------------------------------- | |
369 | // assignment operators | |
370 | // --------------------------------------------------------------------------- | |
371 | ||
dd1eaa89 | 372 | // helper function: does real copy |
c801d85f KB |
373 | void wxString::AssignCopy(size_t nSrcLen, const char *pszSrcData) |
374 | { | |
375 | if ( nSrcLen == 0 ) { | |
376 | Reinit(); | |
377 | } | |
378 | else { | |
379 | AllocBeforeWrite(nSrcLen); | |
380 | memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(char)); | |
381 | GetStringData()->nDataLength = nSrcLen; | |
382 | m_pchData[nSrcLen] = '\0'; | |
383 | } | |
384 | } | |
385 | ||
386 | // assigns one string to another | |
387 | wxString& wxString::operator=(const wxString& stringSrc) | |
388 | { | |
097c080b VZ |
389 | wxASSERT( stringSrc.GetStringData()->IsValid() ); |
390 | ||
c801d85f KB |
391 | // don't copy string over itself |
392 | if ( m_pchData != stringSrc.m_pchData ) { | |
393 | if ( stringSrc.GetStringData()->IsEmpty() ) { | |
394 | Reinit(); | |
395 | } | |
396 | else { | |
397 | // adjust references | |
398 | GetStringData()->Unlock(); | |
399 | m_pchData = stringSrc.m_pchData; | |
400 | GetStringData()->Lock(); | |
401 | } | |
402 | } | |
403 | ||
404 | return *this; | |
405 | } | |
406 | ||
407 | // assigns a single character | |
408 | wxString& wxString::operator=(char ch) | |
409 | { | |
410 | AssignCopy(1, &ch); | |
411 | return *this; | |
412 | } | |
413 | ||
414 | // assigns C string | |
415 | wxString& wxString::operator=(const char *psz) | |
416 | { | |
417 | AssignCopy(Strlen(psz), psz); | |
418 | return *this; | |
419 | } | |
420 | ||
421 | // same as 'signed char' variant | |
422 | wxString& wxString::operator=(const unsigned char* psz) | |
423 | { | |
424 | *this = (const char *)psz; | |
425 | return *this; | |
426 | } | |
427 | ||
428 | wxString& wxString::operator=(const wchar_t *pwz) | |
429 | { | |
430 | wxString str(pwz); | |
431 | *this = str; | |
432 | return *this; | |
433 | } | |
434 | ||
435 | // --------------------------------------------------------------------------- | |
436 | // string concatenation | |
437 | // --------------------------------------------------------------------------- | |
438 | ||
c801d85f KB |
439 | // add something to this string |
440 | void wxString::ConcatSelf(int nSrcLen, const char *pszSrcData) | |
441 | { | |
3168a13f | 442 | STATISTICS_ADD(SummandLength, nSrcLen); |
c801d85f | 443 | |
05488905 VZ |
444 | // concatenating an empty string is a NOP |
445 | if ( nSrcLen > 0 ) { | |
446 | wxStringData *pData = GetStringData(); | |
447 | size_t nLen = pData->nDataLength; | |
448 | size_t nNewLen = nLen + nSrcLen; | |
c801d85f | 449 | |
05488905 VZ |
450 | // alloc new buffer if current is too small |
451 | if ( pData->IsShared() ) { | |
452 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 453 | |
05488905 VZ |
454 | // we have to allocate another buffer |
455 | wxStringData* pOldData = GetStringData(); | |
456 | AllocBuffer(nNewLen); | |
457 | memcpy(m_pchData, pOldData->data(), nLen*sizeof(char)); | |
458 | pOldData->Unlock(); | |
459 | } | |
460 | else if ( nNewLen > pData->nAllocLength ) { | |
461 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 462 | |
05488905 VZ |
463 | // we have to grow the buffer |
464 | Alloc(nNewLen); | |
465 | } | |
466 | else { | |
467 | STATISTICS_ADD(ConcatHit, 1); | |
3168a13f | 468 | |
05488905 VZ |
469 | // the buffer is already big enough |
470 | } | |
3168a13f | 471 | |
05488905 VZ |
472 | // should be enough space |
473 | wxASSERT( nNewLen <= GetStringData()->nAllocLength ); | |
3168a13f | 474 | |
05488905 VZ |
475 | // fast concatenation - all is done in our buffer |
476 | memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(char)); | |
3168a13f | 477 | |
05488905 VZ |
478 | m_pchData[nNewLen] = '\0'; // put terminating '\0' |
479 | GetStringData()->nDataLength = nNewLen; // and fix the length | |
480 | } | |
481 | //else: the string to append was empty | |
c801d85f KB |
482 | } |
483 | ||
484 | /* | |
c801d85f KB |
485 | * concatenation functions come in 5 flavours: |
486 | * string + string | |
487 | * char + string and string + char | |
488 | * C str + string and string + C str | |
489 | */ | |
490 | ||
491 | wxString operator+(const wxString& string1, const wxString& string2) | |
492 | { | |
097c080b VZ |
493 | wxASSERT( string1.GetStringData()->IsValid() ); |
494 | wxASSERT( string2.GetStringData()->IsValid() ); | |
495 | ||
3168a13f VZ |
496 | wxString s = string1; |
497 | s += string2; | |
498 | ||
c801d85f KB |
499 | return s; |
500 | } | |
501 | ||
3168a13f | 502 | wxString operator+(const wxString& string, char ch) |
c801d85f | 503 | { |
3168a13f VZ |
504 | wxASSERT( string.GetStringData()->IsValid() ); |
505 | ||
506 | wxString s = string; | |
507 | s += ch; | |
097c080b | 508 | |
c801d85f KB |
509 | return s; |
510 | } | |
511 | ||
512 | wxString operator+(char ch, const wxString& string) | |
513 | { | |
097c080b VZ |
514 | wxASSERT( string.GetStringData()->IsValid() ); |
515 | ||
3168a13f VZ |
516 | wxString s = ch; |
517 | s += string; | |
518 | ||
c801d85f KB |
519 | return s; |
520 | } | |
521 | ||
522 | wxString operator+(const wxString& string, const char *psz) | |
523 | { | |
097c080b VZ |
524 | wxASSERT( string.GetStringData()->IsValid() ); |
525 | ||
c801d85f | 526 | wxString s; |
3168a13f VZ |
527 | s.Alloc(Strlen(psz) + string.Len()); |
528 | s = string; | |
529 | s += psz; | |
530 | ||
c801d85f KB |
531 | return s; |
532 | } | |
533 | ||
534 | wxString operator+(const char *psz, const wxString& string) | |
535 | { | |
097c080b VZ |
536 | wxASSERT( string.GetStringData()->IsValid() ); |
537 | ||
c801d85f | 538 | wxString s; |
3168a13f VZ |
539 | s.Alloc(Strlen(psz) + string.Len()); |
540 | s = psz; | |
541 | s += string; | |
542 | ||
c801d85f KB |
543 | return s; |
544 | } | |
545 | ||
546 | // =========================================================================== | |
547 | // other common string functions | |
548 | // =========================================================================== | |
549 | ||
550 | // --------------------------------------------------------------------------- | |
551 | // simple sub-string extraction | |
552 | // --------------------------------------------------------------------------- | |
553 | ||
554 | // helper function: clone the data attached to this string | |
555 | void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const | |
556 | { | |
3168a13f | 557 | if ( nCopyLen == 0 ) { |
c801d85f KB |
558 | dest.Init(); |
559 | } | |
3168a13f | 560 | else { |
c801d85f KB |
561 | dest.AllocBuffer(nCopyLen); |
562 | memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(char)); | |
563 | } | |
564 | } | |
565 | ||
566 | // extract string of length nCount starting at nFirst | |
567 | // default value of nCount is 0 and means "till the end" | |
568 | wxString wxString::Mid(size_t nFirst, size_t nCount) const | |
569 | { | |
570 | // out-of-bounds requests return sensible things | |
571 | if ( nCount == 0 ) | |
572 | nCount = GetStringData()->nDataLength - nFirst; | |
573 | ||
574 | if ( nFirst + nCount > (size_t)GetStringData()->nDataLength ) | |
575 | nCount = GetStringData()->nDataLength - nFirst; | |
576 | if ( nFirst > (size_t)GetStringData()->nDataLength ) | |
577 | nCount = 0; | |
578 | ||
579 | wxString dest; | |
580 | AllocCopy(dest, nCount, nFirst); | |
581 | return dest; | |
582 | } | |
583 | ||
584 | // extract nCount last (rightmost) characters | |
585 | wxString wxString::Right(size_t nCount) const | |
586 | { | |
587 | if ( nCount > (size_t)GetStringData()->nDataLength ) | |
588 | nCount = GetStringData()->nDataLength; | |
589 | ||
590 | wxString dest; | |
591 | AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount); | |
592 | return dest; | |
593 | } | |
594 | ||
595 | // get all characters after the last occurence of ch | |
596 | // (returns the whole string if ch not found) | |
597 | wxString wxString::Right(char ch) const | |
598 | { | |
599 | wxString str; | |
600 | int iPos = Find(ch, TRUE); | |
601 | if ( iPos == NOT_FOUND ) | |
602 | str = *this; | |
603 | else | |
c8cfb486 | 604 | str = c_str() + iPos + 1; |
c801d85f KB |
605 | |
606 | return str; | |
607 | } | |
608 | ||
609 | // extract nCount first (leftmost) characters | |
610 | wxString wxString::Left(size_t nCount) const | |
611 | { | |
612 | if ( nCount > (size_t)GetStringData()->nDataLength ) | |
613 | nCount = GetStringData()->nDataLength; | |
614 | ||
615 | wxString dest; | |
616 | AllocCopy(dest, nCount, 0); | |
617 | return dest; | |
618 | } | |
619 | ||
620 | // get all characters before the first occurence of ch | |
621 | // (returns the whole string if ch not found) | |
622 | wxString wxString::Left(char ch) const | |
623 | { | |
624 | wxString str; | |
625 | for ( const char *pc = m_pchData; *pc != '\0' && *pc != ch; pc++ ) | |
626 | str += *pc; | |
627 | ||
628 | return str; | |
629 | } | |
630 | ||
631 | /// get all characters before the last occurence of ch | |
632 | /// (returns empty string if ch not found) | |
633 | wxString wxString::Before(char ch) const | |
634 | { | |
635 | wxString str; | |
636 | int iPos = Find(ch, TRUE); | |
637 | if ( iPos != NOT_FOUND && iPos != 0 ) | |
d1c9bbf6 | 638 | str = wxString(c_str(), iPos); |
c801d85f KB |
639 | |
640 | return str; | |
641 | } | |
642 | ||
643 | /// get all characters after the first occurence of ch | |
644 | /// (returns empty string if ch not found) | |
645 | wxString wxString::After(char ch) const | |
646 | { | |
647 | wxString str; | |
648 | int iPos = Find(ch); | |
649 | if ( iPos != NOT_FOUND ) | |
650 | str = c_str() + iPos + 1; | |
651 | ||
652 | return str; | |
653 | } | |
654 | ||
655 | // replace first (or all) occurences of some substring with another one | |
c86f1403 | 656 | size_t wxString::Replace(const char *szOld, const char *szNew, bool bReplaceAll) |
c801d85f | 657 | { |
c86f1403 | 658 | size_t uiCount = 0; // count of replacements made |
c801d85f | 659 | |
c86f1403 | 660 | size_t uiOldLen = Strlen(szOld); |
c801d85f KB |
661 | |
662 | wxString strTemp; | |
663 | const char *pCurrent = m_pchData; | |
dd1eaa89 | 664 | const char *pSubstr; |
c801d85f KB |
665 | while ( *pCurrent != '\0' ) { |
666 | pSubstr = strstr(pCurrent, szOld); | |
667 | if ( pSubstr == NULL ) { | |
668 | // strTemp is unused if no replacements were made, so avoid the copy | |
669 | if ( uiCount == 0 ) | |
670 | return 0; | |
671 | ||
672 | strTemp += pCurrent; // copy the rest | |
673 | break; // exit the loop | |
674 | } | |
675 | else { | |
676 | // take chars before match | |
677 | strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent); | |
678 | strTemp += szNew; | |
679 | pCurrent = pSubstr + uiOldLen; // restart after match | |
680 | ||
681 | uiCount++; | |
682 | ||
683 | // stop now? | |
684 | if ( !bReplaceAll ) { | |
685 | strTemp += pCurrent; // copy the rest | |
686 | break; // exit the loop | |
687 | } | |
688 | } | |
689 | } | |
690 | ||
691 | // only done if there were replacements, otherwise would have returned above | |
692 | *this = strTemp; | |
693 | ||
694 | return uiCount; | |
695 | } | |
696 | ||
697 | bool wxString::IsAscii() const | |
698 | { | |
699 | const char *s = (const char*) *this; | |
700 | while(*s){ | |
701 | if(!isascii(*s)) return(FALSE); | |
702 | s++; | |
703 | } | |
704 | return(TRUE); | |
705 | } | |
dd1eaa89 | 706 | |
c801d85f KB |
707 | bool wxString::IsWord() const |
708 | { | |
709 | const char *s = (const char*) *this; | |
710 | while(*s){ | |
711 | if(!isalpha(*s)) return(FALSE); | |
712 | s++; | |
713 | } | |
714 | return(TRUE); | |
715 | } | |
dd1eaa89 | 716 | |
c801d85f KB |
717 | bool wxString::IsNumber() const |
718 | { | |
719 | const char *s = (const char*) *this; | |
720 | while(*s){ | |
721 | if(!isdigit(*s)) return(FALSE); | |
722 | s++; | |
723 | } | |
724 | return(TRUE); | |
725 | } | |
726 | ||
c801d85f KB |
727 | wxString wxString::Strip(stripType w) const |
728 | { | |
729 | wxString s = *this; | |
730 | if ( w & leading ) s.Trim(FALSE); | |
731 | if ( w & trailing ) s.Trim(TRUE); | |
732 | return s; | |
733 | } | |
734 | ||
c801d85f KB |
735 | // --------------------------------------------------------------------------- |
736 | // case conversion | |
737 | // --------------------------------------------------------------------------- | |
738 | ||
739 | wxString& wxString::MakeUpper() | |
740 | { | |
741 | CopyBeforeWrite(); | |
742 | ||
743 | for ( char *p = m_pchData; *p; p++ ) | |
744 | *p = (char)toupper(*p); | |
745 | ||
746 | return *this; | |
747 | } | |
748 | ||
749 | wxString& wxString::MakeLower() | |
750 | { | |
751 | CopyBeforeWrite(); | |
dd1eaa89 | 752 | |
c801d85f KB |
753 | for ( char *p = m_pchData; *p; p++ ) |
754 | *p = (char)tolower(*p); | |
755 | ||
756 | return *this; | |
757 | } | |
758 | ||
759 | // --------------------------------------------------------------------------- | |
760 | // trimming and padding | |
761 | // --------------------------------------------------------------------------- | |
762 | ||
763 | // trims spaces (in the sense of isspace) from left or right side | |
764 | wxString& wxString::Trim(bool bFromRight) | |
765 | { | |
766 | CopyBeforeWrite(); | |
767 | ||
768 | if ( bFromRight ) | |
769 | { | |
770 | // find last non-space character | |
771 | char *psz = m_pchData + GetStringData()->nDataLength - 1; | |
772 | while ( isspace(*psz) && (psz >= m_pchData) ) | |
773 | psz--; | |
774 | ||
775 | // truncate at trailing space start | |
776 | *++psz = '\0'; | |
777 | GetStringData()->nDataLength = psz - m_pchData; | |
778 | } | |
779 | else | |
780 | { | |
781 | // find first non-space character | |
782 | const char *psz = m_pchData; | |
783 | while ( isspace(*psz) ) | |
784 | psz++; | |
785 | ||
786 | // fix up data and length | |
787 | int nDataLength = GetStringData()->nDataLength - (psz - m_pchData); | |
788 | memmove(m_pchData, psz, (nDataLength + 1)*sizeof(char)); | |
789 | GetStringData()->nDataLength = nDataLength; | |
790 | } | |
791 | ||
792 | return *this; | |
793 | } | |
794 | ||
795 | // adds nCount characters chPad to the string from either side | |
796 | wxString& wxString::Pad(size_t nCount, char chPad, bool bFromRight) | |
797 | { | |
798 | wxString s(chPad, nCount); | |
799 | ||
800 | if ( bFromRight ) | |
801 | *this += s; | |
802 | else | |
803 | { | |
804 | s += *this; | |
805 | *this = s; | |
806 | } | |
807 | ||
808 | return *this; | |
809 | } | |
810 | ||
811 | // truncate the string | |
812 | wxString& wxString::Truncate(size_t uiLen) | |
813 | { | |
814 | *(m_pchData + uiLen) = '\0'; | |
815 | GetStringData()->nDataLength = uiLen; | |
816 | ||
817 | return *this; | |
818 | } | |
819 | ||
820 | // --------------------------------------------------------------------------- | |
821 | // finding (return NOT_FOUND if not found and index otherwise) | |
822 | // --------------------------------------------------------------------------- | |
823 | ||
824 | // find a character | |
825 | int wxString::Find(char ch, bool bFromEnd) const | |
826 | { | |
827 | const char *psz = bFromEnd ? strrchr(m_pchData, ch) : strchr(m_pchData, ch); | |
828 | ||
829 | return (psz == NULL) ? NOT_FOUND : psz - m_pchData; | |
830 | } | |
831 | ||
832 | // find a sub-string (like strstr) | |
833 | int wxString::Find(const char *pszSub) const | |
834 | { | |
835 | const char *psz = strstr(m_pchData, pszSub); | |
836 | ||
837 | return (psz == NULL) ? NOT_FOUND : psz - m_pchData; | |
838 | } | |
839 | ||
840 | // --------------------------------------------------------------------------- | |
9efd3367 | 841 | // formatted output |
c801d85f KB |
842 | // --------------------------------------------------------------------------- |
843 | int wxString::Printf(const char *pszFormat, ...) | |
844 | { | |
845 | va_list argptr; | |
846 | va_start(argptr, pszFormat); | |
847 | ||
848 | int iLen = PrintfV(pszFormat, argptr); | |
849 | ||
850 | va_end(argptr); | |
851 | ||
852 | return iLen; | |
853 | } | |
854 | ||
855 | int wxString::PrintfV(const char* pszFormat, va_list argptr) | |
856 | { | |
9efd3367 | 857 | static char s_szScratch[1024]; |
c801d85f KB |
858 | |
859 | int iLen = vsprintf(s_szScratch, pszFormat, argptr); | |
860 | AllocBeforeWrite(iLen); | |
861 | strcpy(m_pchData, s_szScratch); | |
862 | ||
863 | return iLen; | |
864 | } | |
865 | ||
097c080b VZ |
866 | // ---------------------------------------------------------------------------- |
867 | // misc other operations | |
868 | // ---------------------------------------------------------------------------- | |
869 | bool wxString::Matches(const char *pszMask) const | |
870 | { | |
871 | // check char by char | |
872 | const char *pszTxt; | |
873 | for ( pszTxt = c_str(); *pszMask != '\0'; pszMask++, pszTxt++ ) { | |
874 | switch ( *pszMask ) { | |
875 | case '?': | |
876 | if ( *pszTxt == '\0' ) | |
877 | return FALSE; | |
878 | ||
879 | pszTxt++; | |
880 | pszMask++; | |
881 | break; | |
882 | ||
883 | case '*': | |
884 | { | |
885 | // ignore special chars immediately following this one | |
886 | while ( *pszMask == '*' || *pszMask == '?' ) | |
887 | pszMask++; | |
888 | ||
889 | // if there is nothing more, match | |
890 | if ( *pszMask == '\0' ) | |
891 | return TRUE; | |
892 | ||
893 | // are there any other metacharacters in the mask? | |
c86f1403 | 894 | size_t uiLenMask; |
097c080b VZ |
895 | const char *pEndMask = strpbrk(pszMask, "*?"); |
896 | ||
897 | if ( pEndMask != NULL ) { | |
898 | // we have to match the string between two metachars | |
899 | uiLenMask = pEndMask - pszMask; | |
900 | } | |
901 | else { | |
902 | // we have to match the remainder of the string | |
903 | uiLenMask = strlen(pszMask); | |
904 | } | |
905 | ||
906 | wxString strToMatch(pszMask, uiLenMask); | |
907 | const char* pMatch = strstr(pszTxt, strToMatch); | |
908 | if ( pMatch == NULL ) | |
909 | return FALSE; | |
910 | ||
911 | // -1 to compensate "++" in the loop | |
912 | pszTxt = pMatch + uiLenMask - 1; | |
913 | pszMask += uiLenMask - 1; | |
914 | } | |
915 | break; | |
916 | ||
917 | default: | |
918 | if ( *pszMask != *pszTxt ) | |
919 | return FALSE; | |
920 | break; | |
921 | } | |
922 | } | |
923 | ||
924 | // match only if nothing left | |
925 | return *pszTxt == '\0'; | |
926 | } | |
927 | ||
c801d85f KB |
928 | // --------------------------------------------------------------------------- |
929 | // standard C++ library string functions | |
930 | // --------------------------------------------------------------------------- | |
931 | #ifdef STD_STRING_COMPATIBILITY | |
932 | ||
933 | wxString& wxString::insert(size_t nPos, const wxString& str) | |
934 | { | |
097c080b | 935 | wxASSERT( str.GetStringData()->IsValid() ); |
c801d85f KB |
936 | wxASSERT( nPos <= Len() ); |
937 | ||
cb6780ff VZ |
938 | if ( !str.IsEmpty() ) { |
939 | wxString strTmp; | |
940 | char *pc = strTmp.GetWriteBuf(Len() + str.Len()); | |
941 | strncpy(pc, c_str(), nPos); | |
942 | strcpy(pc + nPos, str); | |
943 | strcpy(pc + nPos + str.Len(), c_str() + nPos); | |
944 | strTmp.UngetWriteBuf(); | |
945 | *this = strTmp; | |
946 | } | |
dd1eaa89 VZ |
947 | |
948 | return *this; | |
c801d85f KB |
949 | } |
950 | ||
951 | size_t wxString::find(const wxString& str, size_t nStart) const | |
952 | { | |
097c080b | 953 | wxASSERT( str.GetStringData()->IsValid() ); |
c801d85f KB |
954 | wxASSERT( nStart <= Len() ); |
955 | ||
956 | const char *p = strstr(c_str() + nStart, str); | |
dd1eaa89 | 957 | |
c801d85f KB |
958 | return p == NULL ? npos : p - c_str(); |
959 | } | |
960 | ||
f0b3249b JS |
961 | // VC++ 1.5 can't cope with the default argument in the header. |
962 | #if ! (defined(_MSC_VER) && !defined(__WIN32__)) | |
c801d85f KB |
963 | size_t wxString::find(const char* sz, size_t nStart, size_t n) const |
964 | { | |
965 | return find(wxString(sz, n == npos ? 0 : n), nStart); | |
966 | } | |
f0b3249b | 967 | #endif |
dd1eaa89 | 968 | |
c801d85f KB |
969 | size_t wxString::find(char ch, size_t nStart) const |
970 | { | |
971 | wxASSERT( nStart <= Len() ); | |
972 | ||
973 | const char *p = strchr(c_str() + nStart, ch); | |
dd1eaa89 | 974 | |
c801d85f KB |
975 | return p == NULL ? npos : p - c_str(); |
976 | } | |
977 | ||
978 | size_t wxString::rfind(const wxString& str, size_t nStart) const | |
979 | { | |
097c080b | 980 | wxASSERT( str.GetStringData()->IsValid() ); |
c801d85f KB |
981 | wxASSERT( nStart <= Len() ); |
982 | ||
983 | // # could be quicker than that | |
984 | const char *p = c_str() + (nStart == npos ? Len() : nStart); | |
985 | while ( p >= c_str() + str.Len() ) { | |
986 | if ( strncmp(p - str.Len(), str, str.Len()) == 0 ) | |
987 | return p - str.Len() - c_str(); | |
988 | p--; | |
989 | } | |
dd1eaa89 | 990 | |
c801d85f KB |
991 | return npos; |
992 | } | |
dd1eaa89 | 993 | |
f0b3249b JS |
994 | // VC++ 1.5 can't cope with the default argument in the header. |
995 | #if ! (defined(_MSC_VER) && !defined(__WIN32__)) | |
c801d85f KB |
996 | size_t wxString::rfind(const char* sz, size_t nStart, size_t n) const |
997 | { | |
998 | return rfind(wxString(sz, n == npos ? 0 : n), nStart); | |
999 | } | |
1000 | ||
1001 | size_t wxString::rfind(char ch, size_t nStart) const | |
1002 | { | |
1003 | wxASSERT( nStart <= Len() ); | |
1004 | ||
1005 | const char *p = strrchr(c_str() + nStart, ch); | |
dd1eaa89 | 1006 | |
c801d85f KB |
1007 | return p == NULL ? npos : p - c_str(); |
1008 | } | |
f0b3249b | 1009 | #endif |
c801d85f KB |
1010 | |
1011 | wxString wxString::substr(size_t nStart, size_t nLen) const | |
1012 | { | |
1013 | // npos means 'take all' | |
1014 | if ( nLen == npos ) | |
1015 | nLen = 0; | |
1016 | ||
1017 | wxASSERT( nStart + nLen <= Len() ); | |
1018 | ||
1019 | return wxString(c_str() + nStart, nLen == npos ? 0 : nLen); | |
1020 | } | |
1021 | ||
1022 | wxString& wxString::erase(size_t nStart, size_t nLen) | |
1023 | { | |
1024 | wxString strTmp(c_str(), nStart); | |
1025 | if ( nLen != npos ) { | |
1026 | wxASSERT( nStart + nLen <= Len() ); | |
1027 | ||
1028 | strTmp.append(c_str() + nStart + nLen); | |
1029 | } | |
1030 | ||
1031 | *this = strTmp; | |
1032 | return *this; | |
1033 | } | |
1034 | ||
1035 | wxString& wxString::replace(size_t nStart, size_t nLen, const char *sz) | |
1036 | { | |
1037 | wxASSERT( nStart + nLen <= Strlen(sz) ); | |
1038 | ||
1039 | wxString strTmp; | |
1040 | if ( nStart != 0 ) | |
1041 | strTmp.append(c_str(), nStart); | |
1042 | strTmp += sz; | |
1043 | strTmp.append(c_str() + nStart + nLen); | |
dd1eaa89 | 1044 | |
c801d85f KB |
1045 | *this = strTmp; |
1046 | return *this; | |
1047 | } | |
1048 | ||
1049 | wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, char ch) | |
1050 | { | |
1051 | return replace(nStart, nLen, wxString(ch, nCount)); | |
1052 | } | |
1053 | ||
dd1eaa89 | 1054 | wxString& wxString::replace(size_t nStart, size_t nLen, |
097c080b | 1055 | const wxString& str, size_t nStart2, size_t nLen2) |
c801d85f KB |
1056 | { |
1057 | return replace(nStart, nLen, str.substr(nStart2, nLen2)); | |
1058 | } | |
1059 | ||
dd1eaa89 | 1060 | wxString& wxString::replace(size_t nStart, size_t nLen, |
c801d85f KB |
1061 | const char* sz, size_t nCount) |
1062 | { | |
1063 | return replace(nStart, nLen, wxString(sz, nCount)); | |
1064 | } | |
1065 | ||
1066 | #endif //std::string compatibility | |
1067 | ||
1068 | // ============================================================================ | |
1069 | // ArrayString | |
1070 | // ============================================================================ | |
1071 | ||
1072 | // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT) | |
1073 | #define ARRAY_MAXSIZE_INCREMENT 4096 | |
1074 | #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h | |
1075 | #define ARRAY_DEFAULT_INITIAL_SIZE (16) | |
1076 | #endif | |
1077 | ||
1078 | #define STRING(p) ((wxString *)(&(p))) | |
1079 | ||
1080 | // ctor | |
1081 | wxArrayString::wxArrayString() | |
1082 | { | |
1083 | m_nSize = | |
1084 | m_nCount = 0; | |
c67daf87 | 1085 | m_pItems = (char **) NULL; |
c801d85f KB |
1086 | } |
1087 | ||
1088 | // copy ctor | |
1089 | wxArrayString::wxArrayString(const wxArrayString& src) | |
1090 | { | |
3bbb630a VZ |
1091 | m_nSize = |
1092 | m_nCount = 0; | |
c67daf87 | 1093 | m_pItems = (char **) NULL; |
c801d85f | 1094 | |
4d14b524 | 1095 | *this = src; |
c801d85f KB |
1096 | } |
1097 | ||
4d14b524 | 1098 | // assignment operator |
c801d85f KB |
1099 | wxArrayString& wxArrayString::operator=(const wxArrayString& src) |
1100 | { | |
d93f63db VZ |
1101 | if ( m_nSize > 0 ) |
1102 | Clear(); | |
c801d85f | 1103 | |
4d14b524 VZ |
1104 | if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE ) |
1105 | Alloc(src.m_nCount); | |
c801d85f | 1106 | |
4d14b524 VZ |
1107 | // we can't just copy the pointers here because otherwise we would share |
1108 | // the strings with another array | |
c86f1403 | 1109 | for ( size_t n = 0; n < src.m_nCount; n++ ) |
4d14b524 | 1110 | Add(src[n]); |
c801d85f | 1111 | |
3bbb630a VZ |
1112 | if ( m_nCount != 0 ) |
1113 | memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(char *)); | |
1114 | ||
c801d85f KB |
1115 | return *this; |
1116 | } | |
1117 | ||
1118 | // grow the array | |
1119 | void wxArrayString::Grow() | |
1120 | { | |
1121 | // only do it if no more place | |
1122 | if( m_nCount == m_nSize ) { | |
1123 | if( m_nSize == 0 ) { | |
1124 | // was empty, alloc some memory | |
1125 | m_nSize = ARRAY_DEFAULT_INITIAL_SIZE; | |
1126 | m_pItems = new char *[m_nSize]; | |
1127 | } | |
1128 | else { | |
3bbb630a VZ |
1129 | // otherwise when it's called for the first time, nIncrement would be 0 |
1130 | // and the array would never be expanded | |
1131 | wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE != 0 ); | |
1132 | ||
c801d85f | 1133 | // add 50% but not too much |
3bbb630a | 1134 | size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE |
4d14b524 | 1135 | ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; |
c801d85f KB |
1136 | if ( nIncrement > ARRAY_MAXSIZE_INCREMENT ) |
1137 | nIncrement = ARRAY_MAXSIZE_INCREMENT; | |
1138 | m_nSize += nIncrement; | |
1139 | char **pNew = new char *[m_nSize]; | |
1140 | ||
1141 | // copy data to new location | |
1142 | memcpy(pNew, m_pItems, m_nCount*sizeof(char *)); | |
1143 | ||
1144 | // delete old memory (but do not release the strings!) | |
a3622daa | 1145 | wxDELETEA(m_pItems); |
c801d85f KB |
1146 | |
1147 | m_pItems = pNew; | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | void wxArrayString::Free() | |
1153 | { | |
1154 | for ( size_t n = 0; n < m_nCount; n++ ) { | |
1155 | STRING(m_pItems[n])->GetStringData()->Unlock(); | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | // deletes all the strings from the list | |
1160 | void wxArrayString::Empty() | |
1161 | { | |
1162 | Free(); | |
1163 | ||
1164 | m_nCount = 0; | |
1165 | } | |
1166 | ||
1167 | // as Empty, but also frees memory | |
1168 | void wxArrayString::Clear() | |
1169 | { | |
1170 | Free(); | |
1171 | ||
dd1eaa89 | 1172 | m_nSize = |
c801d85f KB |
1173 | m_nCount = 0; |
1174 | ||
a3622daa | 1175 | wxDELETEA(m_pItems); |
c801d85f KB |
1176 | } |
1177 | ||
1178 | // dtor | |
1179 | wxArrayString::~wxArrayString() | |
1180 | { | |
1181 | Free(); | |
1182 | ||
a3622daa | 1183 | wxDELETEA(m_pItems); |
c801d85f KB |
1184 | } |
1185 | ||
1186 | // pre-allocates memory (frees the previous data!) | |
1187 | void wxArrayString::Alloc(size_t nSize) | |
1188 | { | |
1189 | wxASSERT( nSize > 0 ); | |
1190 | ||
1191 | // only if old buffer was not big enough | |
1192 | if ( nSize > m_nSize ) { | |
1193 | Free(); | |
a3622daa | 1194 | wxDELETEA(m_pItems); |
c801d85f KB |
1195 | m_pItems = new char *[nSize]; |
1196 | m_nSize = nSize; | |
1197 | } | |
1198 | ||
1199 | m_nCount = 0; | |
1200 | } | |
1201 | ||
1202 | // searches the array for an item (forward or backwards) | |
c801d85f KB |
1203 | int wxArrayString::Index(const char *sz, bool bCase, bool bFromEnd) const |
1204 | { | |
1205 | if ( bFromEnd ) { | |
1206 | if ( m_nCount > 0 ) { | |
c86f1403 | 1207 | size_t ui = m_nCount; |
c801d85f KB |
1208 | do { |
1209 | if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) ) | |
1210 | return ui; | |
1211 | } | |
1212 | while ( ui != 0 ); | |
1213 | } | |
1214 | } | |
1215 | else { | |
c86f1403 | 1216 | for( size_t ui = 0; ui < m_nCount; ui++ ) { |
c801d85f KB |
1217 | if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) ) |
1218 | return ui; | |
1219 | } | |
1220 | } | |
1221 | ||
1222 | return NOT_FOUND; | |
1223 | } | |
1224 | ||
1225 | // add item at the end | |
097c080b | 1226 | void wxArrayString::Add(const wxString& str) |
c801d85f | 1227 | { |
097c080b VZ |
1228 | wxASSERT( str.GetStringData()->IsValid() ); |
1229 | ||
c801d85f KB |
1230 | Grow(); |
1231 | ||
1232 | // the string data must not be deleted! | |
097c080b VZ |
1233 | str.GetStringData()->Lock(); |
1234 | m_pItems[m_nCount++] = (char *)str.c_str(); | |
c801d85f KB |
1235 | } |
1236 | ||
1237 | // add item at the given position | |
097c080b | 1238 | void wxArrayString::Insert(const wxString& str, size_t nIndex) |
c801d85f | 1239 | { |
097c080b VZ |
1240 | wxASSERT( str.GetStringData()->IsValid() ); |
1241 | ||
1a5a8367 | 1242 | wxCHECK_RET( nIndex <= m_nCount, ("bad index in wxArrayString::Insert") ); |
c801d85f KB |
1243 | |
1244 | Grow(); | |
1245 | ||
dd1eaa89 | 1246 | memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex], |
c801d85f KB |
1247 | (m_nCount - nIndex)*sizeof(char *)); |
1248 | ||
097c080b VZ |
1249 | str.GetStringData()->Lock(); |
1250 | m_pItems[nIndex] = (char *)str.c_str(); | |
c801d85f KB |
1251 | |
1252 | m_nCount++; | |
1253 | } | |
1254 | ||
1255 | // removes item from array (by index) | |
1256 | void wxArrayString::Remove(size_t nIndex) | |
1257 | { | |
1a5a8367 | 1258 | wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Remove") ); |
c801d85f KB |
1259 | |
1260 | // release our lock | |
1261 | Item(nIndex).GetStringData()->Unlock(); | |
1262 | ||
dd1eaa89 | 1263 | memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1], |
c801d85f KB |
1264 | (m_nCount - nIndex - 1)*sizeof(char *)); |
1265 | m_nCount--; | |
1266 | } | |
1267 | ||
1268 | // removes item from array (by value) | |
1269 | void wxArrayString::Remove(const char *sz) | |
1270 | { | |
1271 | int iIndex = Index(sz); | |
1272 | ||
9efd3367 | 1273 | wxCHECK_RET( iIndex != NOT_FOUND, |
1a5a8367 | 1274 | _("removing inexistent element in wxArrayString::Remove") ); |
c801d85f | 1275 | |
c86f1403 | 1276 | Remove(iIndex); |
c801d85f KB |
1277 | } |
1278 | ||
1279 | // sort array elements using passed comparaison function | |
1280 | ||
d355d3fe | 1281 | void wxArrayString::Sort(bool WXUNUSED(bCase), bool WXUNUSED(bReverse) ) |
c801d85f KB |
1282 | { |
1283 | //@@@@ TO DO | |
1284 | //qsort(m_pItems, m_nCount, sizeof(char *), fCmp); | |
1285 | } |