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