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