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