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