]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: string.cpp | |
3 | // Purpose: wxString class | |
59059feb | 4 | // Author: Vadim Zeitlin, Ryan Norton |
c801d85f KB |
5 | // Modified by: |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
59059feb | 9 | // (c) 2004 Ryan Norton <wxprojects@comcast.net> |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
c801d85f KB |
13 | /* |
14 | * About ref counting: | |
15 | * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init()) | |
16 | * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one | |
17 | * 3) Unlock() decrements nRefs and frees memory if it goes to 0 | |
18 | */ | |
19 | ||
20 | // =========================================================================== | |
21 | // headers, declarations, constants | |
22 | // =========================================================================== | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
30b21f9a | 28 | #pragma hdrstop |
c801d85f KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
3c024cc2 VZ |
32 | #include "wx/defs.h" |
33 | #include "wx/string.h" | |
34 | #include "wx/intl.h" | |
3096bd2f | 35 | #include "wx/thread.h" |
6b769f3d | 36 | #endif |
c801d85f KB |
37 | |
38 | #include <ctype.h> | |
39 | #include <string.h> | |
40 | #include <stdlib.h> | |
9a08c20e | 41 | |
ce3ed50d | 42 | #ifdef __SALFORDC__ |
30b21f9a | 43 | #include <clib.h> |
ce3ed50d JS |
44 | #endif |
45 | ||
3168a13f VZ |
46 | // allocating extra space for each string consumes more memory but speeds up |
47 | // the concatenation operations (nLen is the current string's length) | |
77ca46e7 VZ |
48 | // NB: EXTRA_ALLOC must be >= 0! |
49 | #define EXTRA_ALLOC (19 - nLen % 16) | |
3168a13f | 50 | |
c801d85f KB |
51 | // --------------------------------------------------------------------------- |
52 | // static class variables definition | |
53 | // --------------------------------------------------------------------------- | |
54 | ||
e87b7833 | 55 | #if !wxUSE_STL |
2cbfa061 RN |
56 | //According to STL _must_ be a -1 size_t |
57 | const size_t wxStringBase::npos = (size_t) -1; | |
e87b7833 | 58 | #endif |
c801d85f | 59 | |
3168a13f VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // static data | |
62 | // ---------------------------------------------------------------------------- | |
c801d85f | 63 | |
e87b7833 MB |
64 | #if wxUSE_STL |
65 | ||
66 | extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T(""); | |
67 | ||
68 | #else | |
69 | ||
3c024cc2 VZ |
70 | // for an empty string, GetStringData() will return this address: this |
71 | // structure has the same layout as wxStringData and it's data() method will | |
72 | // return the empty string (dummy pointer) | |
73 | static const struct | |
74 | { | |
75 | wxStringData data; | |
2bb67b80 | 76 | wxChar dummy; |
223d09f6 | 77 | } g_strEmpty = { {-1, 0, 0}, wxT('\0') }; |
3c024cc2 | 78 | |
c801d85f | 79 | // empty C style string: points to 'string data' byte of g_strEmpty |
fd242375 | 80 | extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy; |
c801d85f | 81 | |
e87b7833 MB |
82 | #endif |
83 | ||
3168a13f | 84 | // ---------------------------------------------------------------------------- |
c801d85f | 85 | // global functions |
3168a13f | 86 | // ---------------------------------------------------------------------------- |
c801d85f | 87 | |
e87b7833 | 88 | #if wxUSE_STD_IOSTREAM |
c801d85f KB |
89 | |
90 | // MS Visual C++ version 5.0 provides the new STL headers as well as the old | |
91 | // iostream ones. | |
92 | // | |
93 | // ATTN: you can _not_ use both of these in the same program! | |
a38b83c3 | 94 | |
6eaebb29 JS |
95 | #include <iostream> |
96 | ||
dd107c50 | 97 | wxSTD istream& operator>>(wxSTD istream& is, wxString& WXUNUSED(str)) |
c801d85f KB |
98 | { |
99 | #if 0 | |
100 | int w = is.width(0); | |
101 | if ( is.ipfx(0) ) { | |
3f4a0c5b | 102 | streambuf *sb = is.rdbuf(); |
c801d85f KB |
103 | str.erase(); |
104 | while ( true ) { | |
105 | int ch = sb->sbumpc (); | |
106 | if ( ch == EOF ) { | |
3f4a0c5b | 107 | is.setstate(ios::eofbit); |
c801d85f KB |
108 | break; |
109 | } | |
110 | else if ( isspace(ch) ) { | |
111 | sb->sungetc(); | |
112 | break; | |
113 | } | |
dd1eaa89 | 114 | |
c801d85f KB |
115 | str += ch; |
116 | if ( --w == 1 ) | |
117 | break; | |
118 | } | |
119 | } | |
120 | ||
121 | is.isfx(); | |
122 | if ( str.length() == 0 ) | |
3f4a0c5b | 123 | is.setstate(ios::failbit); |
c801d85f KB |
124 | #endif |
125 | return is; | |
126 | } | |
127 | ||
dd107c50 | 128 | wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str) |
825ba8f0 | 129 | { |
ad5bb7d6 WS |
130 | #ifdef __BORLANDC__ |
131 | os << str.mb_str(); | |
132 | #else | |
133 | os << str.c_str(); | |
134 | #endif | |
135 | return os; | |
825ba8f0 SB |
136 | } |
137 | ||
e87b7833 | 138 | #endif // wxUSE_STD_IOSTREAM |
c801d85f | 139 | |
3168a13f VZ |
140 | // ---------------------------------------------------------------------------- |
141 | // private classes | |
142 | // ---------------------------------------------------------------------------- | |
143 | ||
144 | // this small class is used to gather statistics for performance tuning | |
145 | //#define WXSTRING_STATISTICS | |
146 | #ifdef WXSTRING_STATISTICS | |
147 | class Averager | |
148 | { | |
149 | public: | |
f6f5941b | 150 | Averager(const wxChar *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } |
2c3b684c | 151 | ~Averager() |
f6f5941b | 152 | { wxPrintf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); } |
3168a13f | 153 | |
c86f1403 | 154 | void Add(size_t n) { m_nTotal += n; m_nCount++; } |
3168a13f VZ |
155 | |
156 | private: | |
c86f1403 | 157 | size_t m_nCount, m_nTotal; |
f6f5941b | 158 | const wxChar *m_sz; |
3168a13f VZ |
159 | } g_averageLength("allocation size"), |
160 | g_averageSummandLength("summand length"), | |
161 | g_averageConcatHit("hit probability in concat"), | |
162 | g_averageInitialLength("initial string length"); | |
163 | ||
164 | #define STATISTICS_ADD(av, val) g_average##av.Add(val) | |
165 | #else | |
166 | #define STATISTICS_ADD(av, val) | |
167 | #endif // WXSTRING_STATISTICS | |
168 | ||
4cc6a9db VZ |
169 | #if !wxUSE_STL |
170 | ||
ca5e07c7 GD |
171 | // =========================================================================== |
172 | // wxStringData class deallocation | |
173 | // =========================================================================== | |
174 | ||
8ecf21b7 GD |
175 | #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL) |
176 | # pragma message (__FILE__ ": building with Multithreaded non DLL runtime has a performance impact on wxString!") | |
ca5e07c7 GD |
177 | void wxStringData::Free() |
178 | { | |
179 | free(this); | |
180 | } | |
8ecf21b7 | 181 | #endif |
ca5e07c7 | 182 | |
c801d85f | 183 | // =========================================================================== |
e87b7833 | 184 | // wxStringBase |
c801d85f KB |
185 | // =========================================================================== |
186 | ||
c801d85f | 187 | // takes nLength elements of psz starting at nPos |
e87b7833 | 188 | void wxStringBase::InitWith(const wxChar *psz, size_t nPos, size_t nLength) |
c801d85f KB |
189 | { |
190 | Init(); | |
191 | ||
f6bcfd97 | 192 | // if the length is not given, assume the string to be NUL terminated |
e87b7833 | 193 | if ( nLength == npos ) { |
f6bcfd97 | 194 | wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") ); |
c801d85f | 195 | |
f6bcfd97 BP |
196 | nLength = wxStrlen(psz + nPos); |
197 | } | |
6c68273f | 198 | |
3168a13f VZ |
199 | STATISTICS_ADD(InitialLength, nLength); |
200 | ||
c801d85f KB |
201 | if ( nLength > 0 ) { |
202 | // trailing '\0' is written in AllocBuffer() | |
b1801e0e | 203 | if ( !AllocBuffer(nLength) ) { |
e87b7833 | 204 | wxFAIL_MSG( _T("out of memory in wxStringBase::InitWith") ); |
b1801e0e GD |
205 | return; |
206 | } | |
2c09fb3b | 207 | wxTmemcpy(m_pchData, psz + nPos, nLength); |
c801d85f KB |
208 | } |
209 | } | |
dd1eaa89 | 210 | |
c801d85f | 211 | // poor man's iterators are "void *" pointers |
e87b7833 | 212 | wxStringBase::wxStringBase(const void *pStart, const void *pEnd) |
c801d85f | 213 | { |
2bb67b80 OK |
214 | InitWith((const wxChar *)pStart, 0, |
215 | (const wxChar *)pEnd - (const wxChar *)pStart); | |
c801d85f KB |
216 | } |
217 | ||
e87b7833 | 218 | wxStringBase::wxStringBase(size_type n, wxChar ch) |
c801d85f | 219 | { |
e87b7833 MB |
220 | Init(); |
221 | append(n, ch); | |
c801d85f | 222 | } |
2bb67b80 | 223 | |
c801d85f KB |
224 | // --------------------------------------------------------------------------- |
225 | // memory allocation | |
226 | // --------------------------------------------------------------------------- | |
227 | ||
228 | // allocates memory needed to store a C string of length nLen | |
e87b7833 | 229 | bool wxStringBase::AllocBuffer(size_t nLen) |
c801d85f | 230 | { |
13111b2a VZ |
231 | // allocating 0 sized buffer doesn't make sense, all empty strings should |
232 | // reuse g_strEmpty | |
233 | wxASSERT( nLen > 0 ); | |
234 | ||
235 | // make sure that we don't overflow | |
236 | wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) - | |
237 | (sizeof(wxStringData) + EXTRA_ALLOC + 1) ); | |
c801d85f | 238 | |
3168a13f VZ |
239 | STATISTICS_ADD(Length, nLen); |
240 | ||
c801d85f KB |
241 | // allocate memory: |
242 | // 1) one extra character for '\0' termination | |
243 | // 2) sizeof(wxStringData) for housekeeping info | |
3168a13f | 244 | wxStringData* pData = (wxStringData*) |
2bb67b80 | 245 | malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar)); |
e015c2a3 | 246 | |
b1801e0e GD |
247 | if ( pData == NULL ) { |
248 | // allocation failures are handled by the caller | |
d775fa82 | 249 | return false; |
b1801e0e | 250 | } |
e015c2a3 | 251 | |
c801d85f | 252 | pData->nRefs = 1; |
c801d85f | 253 | pData->nDataLength = nLen; |
3168a13f | 254 | pData->nAllocLength = nLen + EXTRA_ALLOC; |
c801d85f | 255 | m_pchData = pData->data(); // data starts after wxStringData |
223d09f6 | 256 | m_pchData[nLen] = wxT('\0'); |
d775fa82 | 257 | return true; |
c801d85f KB |
258 | } |
259 | ||
c801d85f | 260 | // must be called before changing this string |
e87b7833 | 261 | bool wxStringBase::CopyBeforeWrite() |
c801d85f KB |
262 | { |
263 | wxStringData* pData = GetStringData(); | |
264 | ||
265 | if ( pData->IsShared() ) { | |
266 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 267 | size_t nLen = pData->nDataLength; |
b1801e0e GD |
268 | if ( !AllocBuffer(nLen) ) { |
269 | // allocation failures are handled by the caller | |
d775fa82 | 270 | return false; |
b1801e0e | 271 | } |
2c09fb3b | 272 | wxTmemcpy(m_pchData, pData->data(), nLen); |
c801d85f KB |
273 | } |
274 | ||
3bbb630a | 275 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
b1801e0e | 276 | |
d775fa82 | 277 | return true; |
c801d85f KB |
278 | } |
279 | ||
280 | // must be called before replacing contents of this string | |
e87b7833 | 281 | bool wxStringBase::AllocBeforeWrite(size_t nLen) |
c801d85f KB |
282 | { |
283 | wxASSERT( nLen != 0 ); // doesn't make any sense | |
284 | ||
285 | // must not share string and must have enough space | |
3168a13f | 286 | wxStringData* pData = GetStringData(); |
fbf0c83d | 287 | if ( pData->IsShared() || pData->IsEmpty() ) { |
c801d85f KB |
288 | // can't work with old buffer, get new one |
289 | pData->Unlock(); | |
b1801e0e GD |
290 | if ( !AllocBuffer(nLen) ) { |
291 | // allocation failures are handled by the caller | |
d775fa82 | 292 | return false; |
b1801e0e | 293 | } |
c801d85f | 294 | } |
471aebdd | 295 | else { |
fbf0c83d VZ |
296 | if ( nLen > pData->nAllocLength ) { |
297 | // realloc the buffer instead of calling malloc() again, this is more | |
298 | // efficient | |
299 | STATISTICS_ADD(Length, nLen); | |
300 | ||
301 | nLen += EXTRA_ALLOC; | |
302 | ||
fbf0c83d VZ |
303 | pData = (wxStringData*) |
304 | realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); | |
e015c2a3 | 305 | |
b1801e0e GD |
306 | if ( pData == NULL ) { |
307 | // allocation failures are handled by the caller | |
308 | // keep previous data since reallocation failed | |
d775fa82 | 309 | return false; |
fbf0c83d VZ |
310 | } |
311 | ||
312 | pData->nAllocLength = nLen; | |
313 | m_pchData = pData->data(); | |
314 | } | |
471aebdd | 315 | } |
c801d85f | 316 | |
f1da2f03 | 317 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
b1801e0e | 318 | |
a6ed2b09 VZ |
319 | // it doesn't really matter what the string length is as it's going to be |
320 | // overwritten later but, for extra safety, set it to 0 for now as we may | |
321 | // have some junk in m_pchData | |
94ac840b | 322 | GetStringData()->nDataLength = 0; |
a6ed2b09 | 323 | |
d775fa82 | 324 | return true; |
c801d85f KB |
325 | } |
326 | ||
e87b7833 MB |
327 | wxStringBase& wxStringBase::append(size_t n, wxChar ch) |
328 | { | |
329 | size_type len = length(); | |
330 | ||
c08d805c | 331 | if ( !Alloc(len + n) || !CopyBeforeWrite() ) { |
e87b7833 MB |
332 | wxFAIL_MSG( _T("out of memory in wxStringBase::append") ); |
333 | } | |
334 | GetStringData()->nDataLength = len + n; | |
335 | m_pchData[len + n] = '\0'; | |
336 | for ( size_t i = 0; i < n; ++i ) | |
337 | m_pchData[len + i] = ch; | |
338 | return *this; | |
339 | } | |
340 | ||
341 | void wxStringBase::resize(size_t nSize, wxChar ch) | |
342 | { | |
343 | size_t len = length(); | |
344 | ||
345 | if ( nSize < len ) | |
346 | { | |
347 | erase(begin() + nSize, end()); | |
348 | } | |
349 | else if ( nSize > len ) | |
350 | { | |
351 | append(nSize - len, ch); | |
352 | } | |
353 | //else: we have exactly the specified length, nothing to do | |
354 | } | |
355 | ||
dd1eaa89 | 356 | // allocate enough memory for nLen characters |
e87b7833 | 357 | bool wxStringBase::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 | ||
17a1ebd1 VZ |
364 | pData = (wxStringData *) |
365 | malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); | |
b1801e0e GD |
366 | |
367 | if ( pData == NULL ) { | |
368 | // allocation failure handled by caller | |
d775fa82 | 369 | return false; |
b1801e0e | 370 | } |
e015c2a3 | 371 | |
9fbd8b8d VZ |
372 | pData->nRefs = 1; |
373 | pData->nDataLength = 0; | |
374 | pData->nAllocLength = nLen; | |
375 | m_pchData = pData->data(); // data starts after wxStringData | |
223d09f6 | 376 | m_pchData[0u] = wxT('\0'); |
9fbd8b8d | 377 | } |
3168a13f VZ |
378 | else if ( pData->IsShared() ) { |
379 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 380 | size_t nOldLen = pData->nDataLength; |
b1801e0e GD |
381 | if ( !AllocBuffer(nLen) ) { |
382 | // allocation failure handled by caller | |
d775fa82 | 383 | return false; |
b1801e0e | 384 | } |
83efadb7 RN |
385 | // +1 to copy the terminator, too |
386 | memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxChar)); | |
387 | GetStringData()->nDataLength = nOldLen; | |
3168a13f | 388 | } |
dd1eaa89 | 389 | else { |
3168a13f VZ |
390 | nLen += EXTRA_ALLOC; |
391 | ||
b1801e0e | 392 | pData = (wxStringData *) |
2bb67b80 | 393 | realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); |
3168a13f | 394 | |
b1801e0e GD |
395 | if ( pData == NULL ) { |
396 | // allocation failure handled by caller | |
397 | // keep previous data since reallocation failed | |
d775fa82 | 398 | return false; |
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) | |
b1801e0e GD |
403 | pData->nAllocLength = nLen; |
404 | m_pchData = pData->data(); | |
dd1eaa89 VZ |
405 | } |
406 | } | |
407 | //else: we've already got enough | |
d775fa82 | 408 | return true; |
dd1eaa89 | 409 | } |
d775fa82 | 410 | |
ac3c86ee VS |
411 | wxStringBase::iterator wxStringBase::begin() |
412 | { | |
413 | if (length() > 0) | |
414 | CopyBeforeWrite(); | |
415 | return m_pchData; | |
416 | } | |
417 | ||
418 | wxStringBase::iterator wxStringBase::end() | |
419 | { | |
420 | if (length() > 0) | |
421 | CopyBeforeWrite(); | |
422 | return m_pchData + length(); | |
423 | } | |
dd1eaa89 | 424 | |
e87b7833 | 425 | wxStringBase::iterator wxStringBase::erase(iterator it) |
dd1eaa89 | 426 | { |
e87b7833 MB |
427 | size_type idx = it - begin(); |
428 | erase(idx, 1); | |
429 | return begin() + idx; | |
430 | } | |
3bbb630a | 431 | |
e87b7833 MB |
432 | wxStringBase& wxStringBase::erase(size_t nStart, size_t nLen) |
433 | { | |
7448de8d WS |
434 | wxASSERT(nStart <= length()); |
435 | size_t strLen = length() - nStart; | |
436 | // delete nLen or up to the end of the string characters | |
437 | nLen = strLen < nLen ? strLen : nLen; | |
438 | wxString strTmp(c_str(), nStart); | |
439 | strTmp.append(c_str() + nStart + nLen, length() - nStart - nLen); | |
440 | ||
441 | swap(strTmp); | |
442 | return *this; | |
e87b7833 | 443 | } |
337a0010 | 444 | |
e87b7833 MB |
445 | wxStringBase& wxStringBase::insert(size_t nPos, const wxChar *sz, size_t n) |
446 | { | |
7448de8d | 447 | wxASSERT( nPos <= length() ); |
3bbb630a | 448 | |
7448de8d WS |
449 | if ( n == npos ) n = wxStrlen(sz); |
450 | if ( n == 0 ) return *this; | |
e87b7833 | 451 | |
7448de8d WS |
452 | if ( !Alloc(length() + n) || !CopyBeforeWrite() ) { |
453 | wxFAIL_MSG( _T("out of memory in wxStringBase::insert") ); | |
454 | } | |
fbf0c83d | 455 | |
7448de8d WS |
456 | memmove(m_pchData + nPos + n, m_pchData + nPos, |
457 | (length() - nPos) * sizeof(wxChar)); | |
458 | memcpy(m_pchData + nPos, sz, n * sizeof(wxChar)); | |
459 | GetStringData()->nDataLength = length() + n; | |
460 | m_pchData[length()] = '\0'; | |
b1801e0e | 461 | |
7448de8d | 462 | return *this; |
dd1eaa89 VZ |
463 | } |
464 | ||
e87b7833 | 465 | void wxStringBase::swap(wxStringBase& str) |
c801d85f | 466 | { |
e87b7833 MB |
467 | wxChar* tmp = str.m_pchData; |
468 | str.m_pchData = m_pchData; | |
469 | m_pchData = tmp; | |
c801d85f KB |
470 | } |
471 | ||
e87b7833 | 472 | size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const |
097c080b | 473 | { |
7448de8d WS |
474 | wxASSERT( str.GetStringData()->IsValid() ); |
475 | wxASSERT( nStart <= length() ); | |
e87b7833 | 476 | |
7448de8d WS |
477 | //anchor |
478 | const wxChar* p = (const wxChar*)wxTmemchr(c_str() + nStart, | |
479 | str.c_str()[0], | |
480 | length() - nStart); | |
7663d0d4 | 481 | |
7448de8d WS |
482 | if(!p) |
483 | return npos; | |
dcb68102 | 484 | |
7448de8d WS |
485 | while(p - c_str() + str.length() <= length() && |
486 | wxTmemcmp(p, str.c_str(), str.length()) ) | |
487 | { | |
488 | //Previosly passed as the first argument to wxTmemchr, | |
489 | //but C/C++ standard does not specify evaluation order | |
490 | //of arguments to functions - | |
491 | //http://embedded.com/showArticle.jhtml?articleID=9900607 | |
492 | ++p; | |
493 | ||
494 | //anchor again | |
495 | p = (const wxChar*)wxTmemchr(p, | |
496 | str.c_str()[0], | |
497 | length() - (p - c_str())); | |
498 | ||
499 | if(!p) | |
500 | return npos; | |
501 | } | |
7663d0d4 | 502 | |
7448de8d | 503 | return (p - c_str() + str.length() <= length()) ? p - c_str() : npos; |
097c080b VZ |
504 | } |
505 | ||
e87b7833 | 506 | size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const |
8f06a017 | 507 | { |
7448de8d | 508 | return find(wxStringBase(sz, n), nStart); |
8f06a017 RD |
509 | } |
510 | ||
e87b7833 MB |
511 | size_t wxStringBase::find(wxChar ch, size_t nStart) const |
512 | { | |
7448de8d | 513 | wxASSERT( nStart <= length() ); |
c801d85f | 514 | |
7448de8d | 515 | const wxChar *p = (const wxChar*)wxTmemchr(c_str() + nStart, ch, length() - nStart); |
c801d85f | 516 | |
7448de8d | 517 | return p == NULL ? npos : p - c_str(); |
c801d85f KB |
518 | } |
519 | ||
e87b7833 | 520 | size_t wxStringBase::rfind(const wxStringBase& str, size_t nStart) const |
c801d85f | 521 | { |
e2101186 MB |
522 | wxASSERT( str.GetStringData()->IsValid() ); |
523 | wxASSERT( nStart == npos || nStart <= length() ); | |
524 | ||
525 | if ( length() >= str.length() ) | |
526 | { | |
527 | // avoids a corner case later | |
528 | if ( length() == 0 && str.length() == 0 ) | |
529 | return 0; | |
530 | ||
531 | // "top" is the point where search starts from | |
532 | size_t top = length() - str.length(); | |
c801d85f | 533 | |
e2101186 MB |
534 | if ( nStart == npos ) |
535 | nStart = length() - 1; | |
536 | if ( nStart < top ) | |
537 | top = nStart; | |
538 | ||
539 | const wxChar *cursor = c_str() + top; | |
540 | do | |
541 | { | |
2c09fb3b | 542 | if ( wxTmemcmp(cursor, str.c_str(), |
dcb68102 | 543 | str.length()) == 0 ) |
e2101186 MB |
544 | { |
545 | return cursor - c_str(); | |
546 | } | |
78e6050b | 547 | } while ( cursor-- > c_str() ); |
e2101186 | 548 | } |
d775fa82 | 549 | |
e2101186 | 550 | return npos; |
c801d85f KB |
551 | } |
552 | ||
e87b7833 | 553 | size_t wxStringBase::rfind(const wxChar* sz, size_t nStart, size_t n) const |
c801d85f | 554 | { |
e87b7833 | 555 | return rfind(wxStringBase(sz, n), nStart); |
c801d85f KB |
556 | } |
557 | ||
e87b7833 | 558 | size_t wxStringBase::rfind(wxChar ch, size_t nStart) const |
c801d85f | 559 | { |
e87b7833 MB |
560 | if ( nStart == npos ) |
561 | { | |
562 | nStart = length(); | |
563 | } | |
564 | else | |
565 | { | |
566 | wxASSERT( nStart <= length() ); | |
567 | } | |
c801d85f | 568 | |
93c83507 MB |
569 | const wxChar *actual; |
570 | for ( actual = c_str() + ( nStart == npos ? length() : nStart + 1 ); | |
571 | actual > c_str(); --actual ) | |
572 | { | |
573 | if ( *(actual - 1) == ch ) | |
574 | return (actual - 1) - c_str(); | |
575 | } | |
e87b7833 | 576 | |
93c83507 | 577 | return npos; |
c801d85f KB |
578 | } |
579 | ||
e87b7833 | 580 | size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart) const |
c801d85f | 581 | { |
dcb68102 RN |
582 | wxASSERT(nStart <= length()); |
583 | ||
584 | size_t len = wxStrlen(sz); | |
585 | ||
586 | size_t i; | |
587 | for(i = nStart; i < this->length(); ++i) | |
588 | { | |
2c09fb3b | 589 | if (wxTmemchr(sz, *(c_str() + i), len)) |
dcb68102 RN |
590 | break; |
591 | } | |
592 | ||
593 | if(i == this->length()) | |
e87b7833 | 594 | return npos; |
dcb68102 RN |
595 | else |
596 | return i; | |
c801d85f | 597 | } |
e87b7833 | 598 | |
e2101186 MB |
599 | size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart, |
600 | size_t n) const | |
601 | { | |
602 | return find_first_of(wxStringBase(sz, n), nStart); | |
603 | } | |
604 | ||
e87b7833 MB |
605 | size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart) const |
606 | { | |
607 | if ( nStart == npos ) | |
608 | { | |
e2101186 | 609 | nStart = length() - 1; |
e87b7833 MB |
610 | } |
611 | else | |
612 | { | |
65c75abe VZ |
613 | wxASSERT_MSG( nStart <= length(), |
614 | _T("invalid index in find_last_of()") ); | |
e87b7833 MB |
615 | } |
616 | ||
dcb68102 | 617 | size_t len = wxStrlen(sz); |
7663d0d4 | 618 | |
e2101186 | 619 | for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p ) |
e87b7833 | 620 | { |
2c09fb3b | 621 | if ( wxTmemchr(sz, *p, len) ) |
e87b7833 MB |
622 | return p - c_str(); |
623 | } | |
624 | ||
625 | return npos; | |
626 | } | |
627 | ||
e2101186 MB |
628 | size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart, |
629 | size_t n) const | |
630 | { | |
631 | return find_last_of(wxStringBase(sz, n), nStart); | |
632 | } | |
633 | ||
e87b7833 MB |
634 | size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart) const |
635 | { | |
636 | if ( nStart == npos ) | |
637 | { | |
638 | nStart = length(); | |
639 | } | |
640 | else | |
641 | { | |
642 | wxASSERT( nStart <= length() ); | |
643 | } | |
644 | ||
dcb68102 RN |
645 | size_t len = wxStrlen(sz); |
646 | ||
647 | size_t i; | |
648 | for(i = nStart; i < this->length(); ++i) | |
649 | { | |
2c09fb3b | 650 | if (!wxTmemchr(sz, *(c_str() + i), len)) |
dcb68102 RN |
651 | break; |
652 | } | |
653 | ||
654 | if(i == this->length()) | |
655 | return npos; | |
656 | else | |
657 | return i; | |
e2101186 MB |
658 | } |
659 | ||
660 | size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart, | |
661 | size_t n) const | |
662 | { | |
663 | return find_first_not_of(wxStringBase(sz, n), nStart); | |
e87b7833 MB |
664 | } |
665 | ||
666 | size_t wxStringBase::find_first_not_of(wxChar ch, size_t nStart) const | |
667 | { | |
668 | wxASSERT( nStart <= length() ); | |
669 | ||
670 | for ( const wxChar *p = c_str() + nStart; *p; p++ ) | |
671 | { | |
672 | if ( *p != ch ) | |
673 | return p - c_str(); | |
674 | } | |
675 | ||
676 | return npos; | |
677 | } | |
678 | ||
679 | size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart) const | |
680 | { | |
681 | if ( nStart == npos ) | |
682 | { | |
e2101186 | 683 | nStart = length() - 1; |
e87b7833 MB |
684 | } |
685 | else | |
686 | { | |
687 | wxASSERT( nStart <= length() ); | |
688 | } | |
689 | ||
dcb68102 RN |
690 | size_t len = wxStrlen(sz); |
691 | ||
e2101186 | 692 | for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p ) |
e87b7833 | 693 | { |
2c09fb3b | 694 | if ( !wxTmemchr(sz, *p,len) ) |
dcb68102 | 695 | return p - c_str(); |
e87b7833 MB |
696 | } |
697 | ||
698 | return npos; | |
699 | } | |
700 | ||
e2101186 MB |
701 | size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart, |
702 | size_t n) const | |
703 | { | |
704 | return find_last_not_of(wxStringBase(sz, n), nStart); | |
705 | } | |
706 | ||
e87b7833 MB |
707 | size_t wxStringBase::find_last_not_of(wxChar ch, size_t nStart) const |
708 | { | |
709 | if ( nStart == npos ) | |
710 | { | |
e2101186 | 711 | nStart = length() - 1; |
e87b7833 MB |
712 | } |
713 | else | |
714 | { | |
715 | wxASSERT( nStart <= length() ); | |
716 | } | |
717 | ||
e2101186 | 718 | for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p ) |
e87b7833 MB |
719 | { |
720 | if ( *p != ch ) | |
721 | return p - c_str(); | |
722 | } | |
723 | ||
724 | return npos; | |
725 | } | |
726 | ||
727 | wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen, | |
728 | const wxChar *sz) | |
729 | { | |
730 | wxASSERT_MSG( nStart <= length(), | |
731 | _T("index out of bounds in wxStringBase::replace") ); | |
732 | size_t strLen = length() - nStart; | |
733 | nLen = strLen < nLen ? strLen : nLen; | |
734 | ||
735 | wxStringBase strTmp; | |
736 | strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs | |
737 | ||
510bb748 RN |
738 | //This is kind of inefficient, but its pretty good considering... |
739 | //we don't want to use character access operators here because on STL | |
740 | //it will freeze the reference count of strTmp, which means a deep copy | |
741 | //at the end when swap is called | |
742 | // | |
743 | //Also, we can't use append with the full character pointer and must | |
744 | //do it manually because this string can contain null characters | |
92413909 RN |
745 | for(size_t i1 = 0; i1 < nStart; ++i1) |
746 | strTmp.append(1, this->c_str()[i1]); | |
ad5bb7d6 | 747 | |
510bb748 RN |
748 | //its safe to do the full version here because |
749 | //sz must be a normal c string | |
e87b7833 | 750 | strTmp.append(sz); |
510bb748 RN |
751 | |
752 | for(size_t i2 = nStart + nLen; i2 < length(); ++i2) | |
753 | strTmp.append(1, this->c_str()[i2]); | |
e87b7833 MB |
754 | |
755 | swap(strTmp); | |
756 | return *this; | |
757 | } | |
758 | ||
759 | wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen, | |
760 | size_t nCount, wxChar ch) | |
761 | { | |
7663d0d4 | 762 | return replace(nStart, nLen, wxStringBase(nCount, ch).c_str()); |
e87b7833 MB |
763 | } |
764 | ||
765 | wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen, | |
766 | const wxStringBase& str, | |
767 | size_t nStart2, size_t nLen2) | |
768 | { | |
769 | return replace(nStart, nLen, str.substr(nStart2, nLen2)); | |
770 | } | |
771 | ||
772 | wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen, | |
773 | const wxChar* sz, size_t nCount) | |
774 | { | |
775 | return replace(nStart, nLen, wxStringBase(sz, nCount).c_str()); | |
776 | } | |
777 | ||
778 | wxStringBase wxStringBase::substr(size_t nStart, size_t nLen) const | |
779 | { | |
780 | if ( nLen == npos ) | |
781 | nLen = length() - nStart; | |
782 | return wxStringBase(*this, nStart, nLen); | |
783 | } | |
784 | ||
785 | // assigns one string to another | |
786 | wxStringBase& wxStringBase::operator=(const wxStringBase& stringSrc) | |
787 | { | |
788 | wxASSERT( stringSrc.GetStringData()->IsValid() ); | |
789 | ||
790 | // don't copy string over itself | |
791 | if ( m_pchData != stringSrc.m_pchData ) { | |
792 | if ( stringSrc.GetStringData()->IsEmpty() ) { | |
793 | Reinit(); | |
794 | } | |
795 | else { | |
796 | // adjust references | |
797 | GetStringData()->Unlock(); | |
798 | m_pchData = stringSrc.m_pchData; | |
799 | GetStringData()->Lock(); | |
800 | } | |
801 | } | |
802 | ||
803 | return *this; | |
804 | } | |
805 | ||
806 | // assigns a single character | |
807 | wxStringBase& wxStringBase::operator=(wxChar ch) | |
808 | { | |
809 | if ( !AssignCopy(1, &ch) ) { | |
810 | wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(wxChar)") ); | |
811 | } | |
812 | return *this; | |
813 | } | |
814 | ||
815 | // assigns C string | |
816 | wxStringBase& wxStringBase::operator=(const wxChar *psz) | |
817 | { | |
818 | if ( !AssignCopy(wxStrlen(psz), psz) ) { | |
819 | wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(const wxChar *)") ); | |
820 | } | |
821 | return *this; | |
822 | } | |
823 | ||
824 | // helper function: does real copy | |
825 | bool wxStringBase::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData) | |
826 | { | |
827 | if ( nSrcLen == 0 ) { | |
828 | Reinit(); | |
829 | } | |
830 | else { | |
831 | if ( !AllocBeforeWrite(nSrcLen) ) { | |
832 | // allocation failure handled by caller | |
d775fa82 | 833 | return false; |
e87b7833 MB |
834 | } |
835 | memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar)); | |
836 | GetStringData()->nDataLength = nSrcLen; | |
837 | m_pchData[nSrcLen] = wxT('\0'); | |
838 | } | |
d775fa82 | 839 | return true; |
e87b7833 MB |
840 | } |
841 | ||
842 | // --------------------------------------------------------------------------- | |
843 | // string concatenation | |
844 | // --------------------------------------------------------------------------- | |
c801d85f | 845 | |
c801d85f | 846 | // add something to this string |
e87b7833 MB |
847 | bool wxStringBase::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData, |
848 | size_t nMaxLen) | |
c801d85f | 849 | { |
3168a13f | 850 | STATISTICS_ADD(SummandLength, nSrcLen); |
c801d85f | 851 | |
e87b7833 MB |
852 | nSrcLen = nSrcLen < nMaxLen ? nSrcLen : nMaxLen; |
853 | ||
05488905 VZ |
854 | // concatenating an empty string is a NOP |
855 | if ( nSrcLen > 0 ) { | |
856 | wxStringData *pData = GetStringData(); | |
857 | size_t nLen = pData->nDataLength; | |
858 | size_t nNewLen = nLen + nSrcLen; | |
c801d85f | 859 | |
05488905 VZ |
860 | // alloc new buffer if current is too small |
861 | if ( pData->IsShared() ) { | |
862 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 863 | |
05488905 VZ |
864 | // we have to allocate another buffer |
865 | wxStringData* pOldData = GetStringData(); | |
b1801e0e GD |
866 | if ( !AllocBuffer(nNewLen) ) { |
867 | // allocation failure handled by caller | |
d775fa82 | 868 | return false; |
b1801e0e | 869 | } |
2bb67b80 | 870 | memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar)); |
05488905 VZ |
871 | pOldData->Unlock(); |
872 | } | |
873 | else if ( nNewLen > pData->nAllocLength ) { | |
874 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 875 | |
e87b7833 | 876 | reserve(nNewLen); |
05488905 | 877 | // we have to grow the buffer |
e87b7833 | 878 | if ( capacity() < nNewLen ) { |
b1801e0e | 879 | // allocation failure handled by caller |
d775fa82 | 880 | return false; |
b1801e0e | 881 | } |
05488905 VZ |
882 | } |
883 | else { | |
884 | STATISTICS_ADD(ConcatHit, 1); | |
3168a13f | 885 | |
05488905 VZ |
886 | // the buffer is already big enough |
887 | } | |
3168a13f | 888 | |
05488905 VZ |
889 | // should be enough space |
890 | wxASSERT( nNewLen <= GetStringData()->nAllocLength ); | |
3168a13f | 891 | |
05488905 | 892 | // fast concatenation - all is done in our buffer |
2bb67b80 | 893 | memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar)); |
3168a13f | 894 | |
e87b7833 MB |
895 | m_pchData[nNewLen] = wxT('\0'); // put terminating '\0' |
896 | GetStringData()->nDataLength = nNewLen; // and fix the length | |
897 | } | |
898 | //else: the string to append was empty | |
d775fa82 | 899 | return true; |
e87b7833 MB |
900 | } |
901 | ||
902 | // --------------------------------------------------------------------------- | |
903 | // simple sub-string extraction | |
904 | // --------------------------------------------------------------------------- | |
905 | ||
906 | // helper function: clone the data attached to this string | |
907 | bool wxStringBase::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const | |
908 | { | |
909 | if ( nCopyLen == 0 ) { | |
910 | dest.Init(); | |
911 | } | |
912 | else { | |
913 | if ( !dest.AllocBuffer(nCopyLen) ) { | |
914 | // allocation failure handled by caller | |
d775fa82 | 915 | return false; |
e87b7833 MB |
916 | } |
917 | memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar)); | |
918 | } | |
d775fa82 | 919 | return true; |
e87b7833 MB |
920 | } |
921 | ||
922 | #endif // !wxUSE_STL | |
923 | ||
924 | #if !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE) | |
925 | ||
926 | #if !wxUSE_STL | |
927 | #define STRINGCLASS wxStringBase | |
928 | #else | |
929 | #define STRINGCLASS wxString | |
930 | #endif | |
931 | ||
932 | static inline int wxDoCmp(const wxChar* s1, size_t l1, | |
933 | const wxChar* s2, size_t l2) | |
934 | { | |
935 | if( l1 == l2 ) | |
2c09fb3b | 936 | return wxTmemcmp(s1, s2, l1); |
e87b7833 MB |
937 | else if( l1 < l2 ) |
938 | { | |
2c09fb3b | 939 | int ret = wxTmemcmp(s1, s2, l1); |
e87b7833 MB |
940 | return ret == 0 ? -1 : ret; |
941 | } | |
2c09fb3b | 942 | else |
e87b7833 | 943 | { |
2c09fb3b | 944 | int ret = wxTmemcmp(s1, s2, l2); |
e87b7833 MB |
945 | return ret == 0 ? +1 : ret; |
946 | } | |
e87b7833 MB |
947 | } |
948 | ||
e87b7833 MB |
949 | int STRINGCLASS::compare(const wxStringBase& str) const |
950 | { | |
951 | return ::wxDoCmp(data(), length(), str.data(), str.length()); | |
952 | } | |
953 | ||
e87b7833 MB |
954 | int STRINGCLASS::compare(size_t nStart, size_t nLen, |
955 | const wxStringBase& str) const | |
956 | { | |
957 | wxASSERT(nStart <= length()); | |
958 | size_type strLen = length() - nStart; | |
959 | nLen = strLen < nLen ? strLen : nLen; | |
960 | return ::wxDoCmp(data() + nStart, nLen, str.data(), str.length()); | |
961 | } | |
962 | ||
963 | int STRINGCLASS::compare(size_t nStart, size_t nLen, | |
964 | const wxStringBase& str, | |
965 | size_t nStart2, size_t nLen2) const | |
966 | { | |
967 | wxASSERT(nStart <= length()); | |
968 | wxASSERT(nStart2 <= str.length()); | |
969 | size_type strLen = length() - nStart, | |
970 | strLen2 = str.length() - nStart2; | |
971 | nLen = strLen < nLen ? strLen : nLen; | |
972 | nLen2 = strLen2 < nLen2 ? strLen2 : nLen2; | |
973 | return ::wxDoCmp(data() + nStart, nLen, str.data() + nStart2, nLen2); | |
974 | } | |
975 | ||
e87b7833 MB |
976 | int STRINGCLASS::compare(const wxChar* sz) const |
977 | { | |
978 | size_t nLen = wxStrlen(sz); | |
979 | return ::wxDoCmp(data(), length(), sz, nLen); | |
980 | } | |
981 | ||
e87b7833 MB |
982 | int STRINGCLASS::compare(size_t nStart, size_t nLen, |
983 | const wxChar* sz, size_t nCount) const | |
984 | { | |
985 | wxASSERT(nStart <= length()); | |
986 | size_type strLen = length() - nStart; | |
987 | nLen = strLen < nLen ? strLen : nLen; | |
988 | if( nCount == npos ) | |
989 | nCount = wxStrlen(sz); | |
990 | ||
991 | return ::wxDoCmp(data() + nStart, nLen, sz, nCount); | |
992 | } | |
993 | ||
994 | #undef STRINGCLASS | |
995 | ||
996 | #endif // !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE) | |
997 | ||
998 | // =========================================================================== | |
999 | // wxString class core | |
1000 | // =========================================================================== | |
1001 | ||
06386448 RN |
1002 | // --------------------------------------------------------------------------- |
1003 | // construction and conversion | |
e87b7833 MB |
1004 | // --------------------------------------------------------------------------- |
1005 | ||
1006 | #if wxUSE_UNICODE | |
1007 | ||
1008 | // from multibyte string | |
1009 | wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) | |
1010 | { | |
e87b7833 | 1011 | // anything to do? |
eec47cc6 | 1012 | if ( psz && nLength != 0 ) |
e87b7833 | 1013 | { |
eec47cc6 VZ |
1014 | if ( nLength == npos ) |
1015 | { | |
1016 | nLength = (size_t)-1; | |
1017 | } | |
1018 | else if ( nLength == length() ) | |
1019 | { | |
1020 | // this is important to avoid copying the string in cMB2WC: we're | |
1021 | // already NUL-terminated so we can pass this NUL with the data | |
1022 | nLength++; | |
1023 | } | |
1024 | ||
1025 | size_t nLenWide; | |
1026 | wxWCharBuffer wbuf = conv.cMB2WC(psz, nLength, &nLenWide); | |
e4e3bbb4 | 1027 | |
eec47cc6 VZ |
1028 | if ( nLenWide ) |
1029 | assign(wbuf, nLenWide); | |
e87b7833 | 1030 | } |
7663d0d4 | 1031 | } |
265d5cce | 1032 | |
06386448 | 1033 | //Convert wxString in Unicode mode to a multi-byte string |
265d5cce RN |
1034 | const wxCharBuffer wxString::mb_str(wxMBConv& conv) const |
1035 | { | |
eec47cc6 | 1036 | return conv.cWC2MB(c_str(), length() + 1 /* size, not length */, NULL); |
e87b7833 MB |
1037 | } |
1038 | ||
1039 | #else // ANSI | |
1040 | ||
1041 | #if wxUSE_WCHAR_T | |
eec47cc6 | 1042 | |
e87b7833 MB |
1043 | // from wide string |
1044 | wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) | |
1045 | { | |
e87b7833 | 1046 | // anything to do? |
eec47cc6 | 1047 | if ( pwz && nLength != 0 ) |
e87b7833 | 1048 | { |
eec47cc6 VZ |
1049 | if ( nLength == npos ) |
1050 | { | |
1051 | nLength = (size_t)-1; | |
1052 | } | |
1053 | else if ( nLength == length() ) | |
1054 | { | |
1055 | // this is important to avoid copying the string in cMB2WC: we're | |
1056 | // already NUL-terminated so we can pass this NUL with the data | |
1057 | nLength++; | |
1058 | } | |
1059 | ||
1060 | size_t nLenMB; | |
1061 | wxCharBuffer buf = conv.cWC2MB(pwz, nLength, &nLenMB); | |
e4e3bbb4 | 1062 | |
eec47cc6 VZ |
1063 | if ( nLenMB ) |
1064 | assign(buf, nLenMB); | |
e87b7833 | 1065 | } |
e87b7833 | 1066 | } |
265d5cce | 1067 | |
7663d0d4 | 1068 | //Converts this string to a wide character string if unicode |
06386448 | 1069 | //mode is not enabled and wxUSE_WCHAR_T is enabled |
265d5cce RN |
1070 | const wxWCharBuffer wxString::wc_str(wxMBConv& conv) const |
1071 | { | |
eec47cc6 | 1072 | return conv.cMB2WC(c_str(), length() + 1 /* size, not length */, NULL); |
265d5cce | 1073 | } |
7663d0d4 | 1074 | |
e87b7833 MB |
1075 | #endif // wxUSE_WCHAR_T |
1076 | ||
1077 | #endif // Unicode/ANSI | |
1078 | ||
1079 | // shrink to minimal size (releasing extra memory) | |
1080 | bool wxString::Shrink() | |
1081 | { | |
1082 | wxString tmp(begin(), end()); | |
1083 | swap(tmp); | |
1084 | return tmp.length() == length(); | |
1085 | } | |
1086 | ||
1087 | #if !wxUSE_STL | |
1088 | // get the pointer to writable buffer of (at least) nLen bytes | |
1089 | wxChar *wxString::GetWriteBuf(size_t nLen) | |
1090 | { | |
1091 | if ( !AllocBeforeWrite(nLen) ) { | |
1092 | // allocation failure handled by caller | |
1093 | return NULL; | |
1094 | } | |
1095 | ||
1096 | wxASSERT( GetStringData()->nRefs == 1 ); | |
d775fa82 | 1097 | GetStringData()->Validate(false); |
e87b7833 MB |
1098 | |
1099 | return m_pchData; | |
1100 | } | |
1101 | ||
1102 | // put string back in a reasonable state after GetWriteBuf | |
1103 | void wxString::UngetWriteBuf() | |
1104 | { | |
1105 | GetStringData()->nDataLength = wxStrlen(m_pchData); | |
d775fa82 | 1106 | GetStringData()->Validate(true); |
e87b7833 MB |
1107 | } |
1108 | ||
1109 | void wxString::UngetWriteBuf(size_t nLen) | |
1110 | { | |
1111 | GetStringData()->nDataLength = nLen; | |
d775fa82 | 1112 | GetStringData()->Validate(true); |
e87b7833 MB |
1113 | } |
1114 | #endif | |
1115 | ||
1116 | // --------------------------------------------------------------------------- | |
1117 | // data access | |
1118 | // --------------------------------------------------------------------------- | |
1119 | ||
1120 | // all functions are inline in string.h | |
1121 | ||
1122 | // --------------------------------------------------------------------------- | |
1123 | // assignment operators | |
1124 | // --------------------------------------------------------------------------- | |
1125 | ||
1126 | #if !wxUSE_UNICODE | |
1127 | ||
1128 | // same as 'signed char' variant | |
1129 | wxString& wxString::operator=(const unsigned char* psz) | |
1130 | { | |
1131 | *this = (const char *)psz; | |
1132 | return *this; | |
1133 | } | |
1134 | ||
1135 | #if wxUSE_WCHAR_T | |
1136 | wxString& wxString::operator=(const wchar_t *pwz) | |
1137 | { | |
1138 | wxString str(pwz); | |
1139 | swap(str); | |
1140 | return *this; | |
c801d85f | 1141 | } |
e87b7833 MB |
1142 | #endif |
1143 | ||
1144 | #endif | |
c801d85f KB |
1145 | |
1146 | /* | |
c801d85f KB |
1147 | * concatenation functions come in 5 flavours: |
1148 | * string + string | |
1149 | * char + string and string + char | |
1150 | * C str + string and string + C str | |
1151 | */ | |
1152 | ||
b1801e0e | 1153 | wxString operator+(const wxString& str1, const wxString& str2) |
c801d85f | 1154 | { |
e87b7833 | 1155 | #if !wxUSE_STL |
b1801e0e GD |
1156 | wxASSERT( str1.GetStringData()->IsValid() ); |
1157 | wxASSERT( str2.GetStringData()->IsValid() ); | |
e87b7833 | 1158 | #endif |
097c080b | 1159 | |
b1801e0e GD |
1160 | wxString s = str1; |
1161 | s += str2; | |
3168a13f | 1162 | |
c801d85f KB |
1163 | return s; |
1164 | } | |
1165 | ||
b1801e0e | 1166 | wxString operator+(const wxString& str, wxChar ch) |
c801d85f | 1167 | { |
e87b7833 | 1168 | #if !wxUSE_STL |
b1801e0e | 1169 | wxASSERT( str.GetStringData()->IsValid() ); |
e87b7833 | 1170 | #endif |
3168a13f | 1171 | |
b1801e0e | 1172 | wxString s = str; |
3168a13f | 1173 | s += ch; |
097c080b | 1174 | |
c801d85f KB |
1175 | return s; |
1176 | } | |
1177 | ||
b1801e0e | 1178 | wxString operator+(wxChar ch, const wxString& str) |
c801d85f | 1179 | { |
e87b7833 | 1180 | #if !wxUSE_STL |
b1801e0e | 1181 | wxASSERT( str.GetStringData()->IsValid() ); |
e87b7833 | 1182 | #endif |
097c080b | 1183 | |
3168a13f | 1184 | wxString s = ch; |
b1801e0e | 1185 | s += str; |
3168a13f | 1186 | |
c801d85f KB |
1187 | return s; |
1188 | } | |
1189 | ||
b1801e0e | 1190 | wxString operator+(const wxString& str, const wxChar *psz) |
c801d85f | 1191 | { |
e87b7833 | 1192 | #if !wxUSE_STL |
b1801e0e | 1193 | wxASSERT( str.GetStringData()->IsValid() ); |
e87b7833 | 1194 | #endif |
097c080b | 1195 | |
c801d85f | 1196 | wxString s; |
b1801e0e GD |
1197 | if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) { |
1198 | wxFAIL_MSG( _T("out of memory in wxString::operator+") ); | |
1199 | } | |
c08d805c | 1200 | s += str; |
3168a13f VZ |
1201 | s += psz; |
1202 | ||
c801d85f KB |
1203 | return s; |
1204 | } | |
1205 | ||
b1801e0e | 1206 | wxString operator+(const wxChar *psz, const wxString& str) |
c801d85f | 1207 | { |
e87b7833 | 1208 | #if !wxUSE_STL |
b1801e0e | 1209 | wxASSERT( str.GetStringData()->IsValid() ); |
e87b7833 | 1210 | #endif |
097c080b | 1211 | |
c801d85f | 1212 | wxString s; |
b1801e0e GD |
1213 | if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) { |
1214 | wxFAIL_MSG( _T("out of memory in wxString::operator+") ); | |
1215 | } | |
3168a13f | 1216 | s = psz; |
b1801e0e | 1217 | s += str; |
3168a13f | 1218 | |
c801d85f KB |
1219 | return s; |
1220 | } | |
1221 | ||
1222 | // =========================================================================== | |
1223 | // other common string functions | |
1224 | // =========================================================================== | |
1225 | ||
dcb68102 RN |
1226 | int wxString::Cmp(const wxString& s) const |
1227 | { | |
1228 | return compare(s); | |
1229 | } | |
1230 | ||
1231 | int wxString::Cmp(const wxChar* psz) const | |
1232 | { | |
1233 | return compare(psz); | |
1234 | } | |
1235 | ||
1236 | static inline int wxDoCmpNoCase(const wxChar* s1, size_t l1, | |
1237 | const wxChar* s2, size_t l2) | |
1238 | { | |
1239 | size_t i; | |
1240 | ||
1241 | if( l1 == l2 ) | |
1242 | { | |
1243 | for(i = 0; i < l1; ++i) | |
1244 | { | |
1245 | if(wxTolower(s1[i]) != wxTolower(s2[i])) | |
1246 | break; | |
1247 | } | |
59059feb | 1248 | return i == l1 ? 0 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1; |
dcb68102 RN |
1249 | } |
1250 | else if( l1 < l2 ) | |
1251 | { | |
1252 | for(i = 0; i < l1; ++i) | |
1253 | { | |
1254 | if(wxTolower(s1[i]) != wxTolower(s2[i])) | |
1255 | break; | |
1256 | } | |
59059feb | 1257 | return i == l1 ? -1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1; |
dcb68102 | 1258 | } |
2c09fb3b | 1259 | else |
dcb68102 RN |
1260 | { |
1261 | for(i = 0; i < l2; ++i) | |
1262 | { | |
1263 | if(wxTolower(s1[i]) != wxTolower(s2[i])) | |
1264 | break; | |
1265 | } | |
35b4f9ca | 1266 | return i == l2 ? 1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1; |
dcb68102 | 1267 | } |
dcb68102 RN |
1268 | } |
1269 | ||
1270 | int wxString::CmpNoCase(const wxString& s) const | |
1271 | { | |
1272 | return wxDoCmpNoCase(data(), length(), s.data(), s.length()); | |
1273 | } | |
1274 | ||
1275 | int wxString::CmpNoCase(const wxChar* psz) const | |
1276 | { | |
1277 | int nLen = wxStrlen(psz); | |
1278 | ||
1279 | return wxDoCmpNoCase(data(), length(), psz, nLen); | |
1280 | } | |
1281 | ||
1282 | ||
b1ac3b56 | 1283 | #if wxUSE_UNICODE |
e015c2a3 | 1284 | |
cf6bedce SC |
1285 | #ifdef __MWERKS__ |
1286 | #ifndef __SCHAR_MAX__ | |
1287 | #define __SCHAR_MAX__ 127 | |
1288 | #endif | |
1289 | #endif | |
1290 | ||
e015c2a3 | 1291 | wxString wxString::FromAscii(const char *ascii) |
b1ac3b56 RR |
1292 | { |
1293 | if (!ascii) | |
1294 | return wxEmptyString; | |
e015c2a3 | 1295 | |
b1ac3b56 RR |
1296 | size_t len = strlen( ascii ); |
1297 | wxString res; | |
e015c2a3 VZ |
1298 | |
1299 | if ( len ) | |
1300 | { | |
1301 | wxStringBuffer buf(res, len); | |
1302 | ||
1303 | wchar_t *dest = buf; | |
1304 | ||
1305 | for ( ;; ) | |
1306 | { | |
1307 | if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' ) | |
1308 | break; | |
1309 | } | |
1310 | } | |
1311 | ||
b1ac3b56 RR |
1312 | return res; |
1313 | } | |
1314 | ||
2b5f62a0 VZ |
1315 | wxString wxString::FromAscii(const char ascii) |
1316 | { | |
1317 | // What do we do with '\0' ? | |
1318 | ||
1319 | wxString res; | |
1320 | res += (wchar_t)(unsigned char) ascii; | |
8760bc65 | 1321 | |
2b5f62a0 VZ |
1322 | return res; |
1323 | } | |
1324 | ||
b1ac3b56 RR |
1325 | const wxCharBuffer wxString::ToAscii() const |
1326 | { | |
e015c2a3 VZ |
1327 | // this will allocate enough space for the terminating NUL too |
1328 | wxCharBuffer buffer(length()); | |
b1ac3b56 | 1329 | |
be7eecf8 | 1330 | |
6e394fc6 | 1331 | char *dest = buffer.data(); |
e015c2a3 VZ |
1332 | |
1333 | const wchar_t *pwc = c_str(); | |
1334 | for ( ;; ) | |
b1ac3b56 | 1335 | { |
6e394fc6 | 1336 | *dest++ = (char)(*pwc > SCHAR_MAX ? wxT('_') : *pwc); |
e015c2a3 VZ |
1337 | |
1338 | // the output string can't have embedded NULs anyhow, so we can safely | |
1339 | // stop at first of them even if we do have any | |
1340 | if ( !*pwc++ ) | |
1341 | break; | |
b1ac3b56 | 1342 | } |
e015c2a3 | 1343 | |
b1ac3b56 RR |
1344 | return buffer; |
1345 | } | |
e015c2a3 VZ |
1346 | |
1347 | #endif // Unicode | |
b1ac3b56 | 1348 | |
c801d85f | 1349 | // extract string of length nCount starting at nFirst |
c801d85f KB |
1350 | wxString wxString::Mid(size_t nFirst, size_t nCount) const |
1351 | { | |
73f507f5 | 1352 | size_t nLen = length(); |
30d9011f | 1353 | |
73f507f5 WS |
1354 | // default value of nCount is npos and means "till the end" |
1355 | if ( nCount == npos ) | |
1356 | { | |
1357 | nCount = nLen - nFirst; | |
1358 | } | |
30d9011f | 1359 | |
73f507f5 WS |
1360 | // out-of-bounds requests return sensible things |
1361 | if ( nFirst + nCount > nLen ) | |
1362 | { | |
1363 | nCount = nLen - nFirst; | |
1364 | } | |
c801d85f | 1365 | |
73f507f5 WS |
1366 | if ( nFirst > nLen ) |
1367 | { | |
1368 | // AllocCopy() will return empty string | |
1369 | return wxEmptyString; | |
1370 | } | |
c801d85f | 1371 | |
73f507f5 WS |
1372 | wxString dest(*this, nFirst, nCount); |
1373 | if ( dest.length() != nCount ) | |
1374 | { | |
1375 | wxFAIL_MSG( _T("out of memory in wxString::Mid") ); | |
1376 | } | |
30d9011f | 1377 | |
73f507f5 | 1378 | return dest; |
c801d85f KB |
1379 | } |
1380 | ||
e87b7833 | 1381 | // check that the string starts with prefix and return the rest of the string |
d775fa82 | 1382 | // in the provided pointer if it is not NULL, otherwise return false |
f6bcfd97 BP |
1383 | bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const |
1384 | { | |
1385 | wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") ); | |
1386 | ||
1387 | // first check if the beginning of the string matches the prefix: note | |
1388 | // that we don't have to check that we don't run out of this string as | |
1389 | // when we reach the terminating NUL, either prefix string ends too (and | |
1390 | // then it's ok) or we break out of the loop because there is no match | |
1391 | const wxChar *p = c_str(); | |
1392 | while ( *prefix ) | |
1393 | { | |
1394 | if ( *prefix++ != *p++ ) | |
1395 | { | |
1396 | // no match | |
d775fa82 | 1397 | return false; |
f6bcfd97 BP |
1398 | } |
1399 | } | |
1400 | ||
1401 | if ( rest ) | |
1402 | { | |
1403 | // put the rest of the string into provided pointer | |
1404 | *rest = p; | |
1405 | } | |
1406 | ||
d775fa82 | 1407 | return true; |
f6bcfd97 BP |
1408 | } |
1409 | ||
c801d85f KB |
1410 | // extract nCount last (rightmost) characters |
1411 | wxString wxString::Right(size_t nCount) const | |
1412 | { | |
e87b7833 MB |
1413 | if ( nCount > length() ) |
1414 | nCount = length(); | |
c801d85f | 1415 | |
e87b7833 MB |
1416 | wxString dest(*this, length() - nCount, nCount); |
1417 | if ( dest.length() != nCount ) { | |
b1801e0e GD |
1418 | wxFAIL_MSG( _T("out of memory in wxString::Right") ); |
1419 | } | |
c801d85f KB |
1420 | return dest; |
1421 | } | |
1422 | ||
1423 | // get all characters after the last occurence of ch | |
1424 | // (returns the whole string if ch not found) | |
2bb67b80 | 1425 | wxString wxString::AfterLast(wxChar ch) const |
c801d85f KB |
1426 | { |
1427 | wxString str; | |
d775fa82 | 1428 | int iPos = Find(ch, true); |
3c67202d | 1429 | if ( iPos == wxNOT_FOUND ) |
c801d85f KB |
1430 | str = *this; |
1431 | else | |
c8cfb486 | 1432 | str = c_str() + iPos + 1; |
c801d85f KB |
1433 | |
1434 | return str; | |
1435 | } | |
1436 | ||
1437 | // extract nCount first (leftmost) characters | |
1438 | wxString wxString::Left(size_t nCount) const | |
1439 | { | |
e87b7833 MB |
1440 | if ( nCount > length() ) |
1441 | nCount = length(); | |
c801d85f | 1442 | |
e87b7833 MB |
1443 | wxString dest(*this, 0, nCount); |
1444 | if ( dest.length() != nCount ) { | |
b1801e0e GD |
1445 | wxFAIL_MSG( _T("out of memory in wxString::Left") ); |
1446 | } | |
c801d85f KB |
1447 | return dest; |
1448 | } | |
1449 | ||
1450 | // get all characters before the first occurence of ch | |
1451 | // (returns the whole string if ch not found) | |
2bb67b80 | 1452 | wxString wxString::BeforeFirst(wxChar ch) const |
c801d85f | 1453 | { |
e87b7833 MB |
1454 | int iPos = Find(ch); |
1455 | if ( iPos == wxNOT_FOUND ) iPos = length(); | |
1456 | return wxString(*this, 0, iPos); | |
c801d85f KB |
1457 | } |
1458 | ||
1459 | /// get all characters before the last occurence of ch | |
1460 | /// (returns empty string if ch not found) | |
2bb67b80 | 1461 | wxString wxString::BeforeLast(wxChar ch) const |
c801d85f KB |
1462 | { |
1463 | wxString str; | |
d775fa82 | 1464 | int iPos = Find(ch, true); |
3c67202d | 1465 | if ( iPos != wxNOT_FOUND && iPos != 0 ) |
d1c9bbf6 | 1466 | str = wxString(c_str(), iPos); |
c801d85f KB |
1467 | |
1468 | return str; | |
1469 | } | |
1470 | ||
1471 | /// get all characters after the first occurence of ch | |
1472 | /// (returns empty string if ch not found) | |
2bb67b80 | 1473 | wxString wxString::AfterFirst(wxChar ch) const |
c801d85f KB |
1474 | { |
1475 | wxString str; | |
1476 | int iPos = Find(ch); | |
3c67202d | 1477 | if ( iPos != wxNOT_FOUND ) |
c801d85f KB |
1478 | str = c_str() + iPos + 1; |
1479 | ||
1480 | return str; | |
1481 | } | |
1482 | ||
1483 | // replace first (or all) occurences of some substring with another one | |
ad5bb7d6 | 1484 | size_t wxString::Replace(const wxChar *szOld, |
510bb748 | 1485 | const wxChar *szNew, bool bReplaceAll) |
c801d85f | 1486 | { |
a8f1f1b2 VZ |
1487 | // if we tried to replace an empty string we'd enter an infinite loop below |
1488 | wxCHECK_MSG( szOld && *szOld && szNew, 0, | |
1489 | _T("wxString::Replace(): invalid parameter") ); | |
1490 | ||
510bb748 | 1491 | size_t uiCount = 0; // count of replacements made |
c801d85f | 1492 | |
510bb748 RN |
1493 | size_t uiOldLen = wxStrlen(szOld); |
1494 | size_t uiNewLen = wxStrlen(szNew); | |
c801d85f | 1495 | |
510bb748 | 1496 | size_t dwPos = 0; |
c801d85f | 1497 | |
ad5bb7d6 | 1498 | while ( this->c_str()[dwPos] != wxT('\0') ) |
510bb748 RN |
1499 | { |
1500 | //DO NOT USE STRSTR HERE | |
1501 | //this string can contain embedded null characters, | |
1502 | //so strstr will function incorrectly | |
1503 | dwPos = find(szOld, dwPos); | |
ad5bb7d6 | 1504 | if ( dwPos == npos ) |
510bb748 | 1505 | break; // exit the loop |
ad5bb7d6 | 1506 | else |
510bb748 RN |
1507 | { |
1508 | //replace this occurance of the old string with the new one | |
1509 | replace(dwPos, uiOldLen, szNew, uiNewLen); | |
1510 | ||
2df0258e RN |
1511 | //move up pos past the string that was replaced |
1512 | dwPos += uiNewLen; | |
510bb748 RN |
1513 | |
1514 | //increase replace count | |
1515 | ++uiCount; | |
ad5bb7d6 | 1516 | |
510bb748 | 1517 | // stop now? |
ad5bb7d6 | 1518 | if ( !bReplaceAll ) |
510bb748 RN |
1519 | break; // exit the loop |
1520 | } | |
c801d85f | 1521 | } |
c801d85f | 1522 | |
510bb748 | 1523 | return uiCount; |
c801d85f KB |
1524 | } |
1525 | ||
1526 | bool wxString::IsAscii() const | |
1527 | { | |
2bb67b80 | 1528 | const wxChar *s = (const wxChar*) *this; |
c801d85f | 1529 | while(*s){ |
d775fa82 | 1530 | if(!isascii(*s)) return(false); |
c801d85f KB |
1531 | s++; |
1532 | } | |
d775fa82 | 1533 | return(true); |
c801d85f | 1534 | } |
dd1eaa89 | 1535 | |
c801d85f KB |
1536 | bool wxString::IsWord() const |
1537 | { | |
2bb67b80 | 1538 | const wxChar *s = (const wxChar*) *this; |
c801d85f | 1539 | while(*s){ |
d775fa82 | 1540 | if(!wxIsalpha(*s)) return(false); |
c801d85f KB |
1541 | s++; |
1542 | } | |
d775fa82 | 1543 | return(true); |
c801d85f | 1544 | } |
dd1eaa89 | 1545 | |
c801d85f KB |
1546 | bool wxString::IsNumber() const |
1547 | { | |
2bb67b80 | 1548 | const wxChar *s = (const wxChar*) *this; |
2f74ed28 | 1549 | if (wxStrlen(s)) |
930357bd | 1550 | if ((s[0] == wxT('-')) || (s[0] == wxT('+'))) s++; |
c801d85f | 1551 | while(*s){ |
d775fa82 | 1552 | if(!wxIsdigit(*s)) return(false); |
c801d85f KB |
1553 | s++; |
1554 | } | |
d775fa82 | 1555 | return(true); |
c801d85f KB |
1556 | } |
1557 | ||
c801d85f KB |
1558 | wxString wxString::Strip(stripType w) const |
1559 | { | |
1560 | wxString s = *this; | |
d775fa82 WS |
1561 | if ( w & leading ) s.Trim(false); |
1562 | if ( w & trailing ) s.Trim(true); | |
c801d85f KB |
1563 | return s; |
1564 | } | |
1565 | ||
c801d85f KB |
1566 | // --------------------------------------------------------------------------- |
1567 | // case conversion | |
1568 | // --------------------------------------------------------------------------- | |
1569 | ||
1570 | wxString& wxString::MakeUpper() | |
1571 | { | |
e87b7833 MB |
1572 | for ( iterator it = begin(), en = end(); it != en; ++it ) |
1573 | *it = (wxChar)wxToupper(*it); | |
c801d85f KB |
1574 | |
1575 | return *this; | |
1576 | } | |
1577 | ||
1578 | wxString& wxString::MakeLower() | |
1579 | { | |
e87b7833 MB |
1580 | for ( iterator it = begin(), en = end(); it != en; ++it ) |
1581 | *it = (wxChar)wxTolower(*it); | |
c801d85f KB |
1582 | |
1583 | return *this; | |
1584 | } | |
1585 | ||
1586 | // --------------------------------------------------------------------------- | |
1587 | // trimming and padding | |
1588 | // --------------------------------------------------------------------------- | |
1589 | ||
d775fa82 | 1590 | // some compilers (VC++ 6.0 not to name them) return true for a call to |
576c608d VZ |
1591 |