]>
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" | |
3096bd2f | 38 | #include "wx/thread.h" |
6b769f3d | 39 | #endif |
c801d85f KB |
40 | |
41 | #include <ctype.h> | |
42 | #include <string.h> | |
43 | #include <stdlib.h> | |
44 | ||
ce3ed50d | 45 | #ifdef __SALFORDC__ |
30b21f9a | 46 | #include <clib.h> |
ce3ed50d JS |
47 | #endif |
48 | ||
ec2ba3aa | 49 | #if wxUSE_UNICODE |
5737d05f VZ |
50 | #undef wxUSE_EXPERIMENTAL_PRINTF |
51 | #define wxUSE_EXPERIMENTAL_PRINTF 1 | |
ec2ba3aa OK |
52 | #endif |
53 | ||
3168a13f VZ |
54 | // allocating extra space for each string consumes more memory but speeds up |
55 | // the concatenation operations (nLen is the current string's length) | |
77ca46e7 VZ |
56 | // NB: EXTRA_ALLOC must be >= 0! |
57 | #define EXTRA_ALLOC (19 - nLen % 16) | |
3168a13f | 58 | |
c801d85f KB |
59 | // --------------------------------------------------------------------------- |
60 | // static class variables definition | |
61 | // --------------------------------------------------------------------------- | |
62 | ||
75ad8feb DW |
63 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
64 | // must define this static for VA or else you get multiply defined symbols | |
65 | // everywhere | |
66 | const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100; | |
67 | #endif // Visual Age | |
68 | ||
8de2e39c | 69 | #ifdef wxSTD_STRING_COMPATIBILITY |
566b84d2 | 70 | const size_t wxString::npos = wxSTRING_MAXLEN; |
8de2e39c | 71 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 72 | |
3168a13f VZ |
73 | // ---------------------------------------------------------------------------- |
74 | // static data | |
75 | // ---------------------------------------------------------------------------- | |
c801d85f | 76 | |
3c024cc2 VZ |
77 | // for an empty string, GetStringData() will return this address: this |
78 | // structure has the same layout as wxStringData and it's data() method will | |
79 | // return the empty string (dummy pointer) | |
80 | static const struct | |
81 | { | |
82 | wxStringData data; | |
2bb67b80 | 83 | wxChar dummy; |
223d09f6 | 84 | } g_strEmpty = { {-1, 0, 0}, wxT('\0') }; |
3c024cc2 | 85 | |
c801d85f | 86 | // empty C style string: points to 'string data' byte of g_strEmpty |
e90c1d2a | 87 | extern const wxChar WXDLLEXPORT *wxEmptyString = &g_strEmpty.dummy; |
c801d85f | 88 | |
89b892a2 VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // conditional compilation | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
dcf924a3 RR |
93 | #if !defined(__WXSW__) && wxUSE_UNICODE |
94 | #ifdef wxUSE_EXPERIMENTAL_PRINTF | |
95 | #undef wxUSE_EXPERIMENTAL_PRINTF | |
96 | #endif | |
97 | #define wxUSE_EXPERIMENTAL_PRINTF 1 | |
98 | #endif | |
99 | ||
89b892a2 VZ |
100 | // we want to find out if the current platform supports vsnprintf()-like |
101 | // function: for Unix this is done with configure, for Windows we test the | |
102 | // compiler explicitly. | |
378b05f7 VZ |
103 | // |
104 | // FIXME currently, this is only for ANSI (!Unicode) strings, so we call this | |
105 | // function wxVsnprintfA (A for ANSI), should also find one for Unicode | |
106 | // strings in Unicode build | |
89b892a2 | 107 | #ifdef __WXMSW__ |
012286eb | 108 | #if defined(__VISUALC__) || (defined(__MINGW32__) && wxUSE_NORLANDER_HEADERS) |
378b05f7 | 109 | #define wxVsnprintfA _vsnprintf |
89b892a2 | 110 | #endif |
03e11df5 GD |
111 | #elif defined(__WXMAC__) |
112 | #define wxVsnprintfA vsnprintf | |
89b892a2 VZ |
113 | #else // !Windows |
114 | #ifdef HAVE_VSNPRINTF | |
378b05f7 | 115 | #define wxVsnprintfA vsnprintf |
89b892a2 VZ |
116 | #endif |
117 | #endif // Windows/!Windows | |
118 | ||
378b05f7 | 119 | #ifndef wxVsnprintfA |
89b892a2 VZ |
120 | // in this case we'll use vsprintf() (which is ANSI and thus should be |
121 | // always available), but it's unsafe because it doesn't check for buffer | |
122 | // size - so give a warning | |
378b05f7 | 123 | #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg) |
566b84d2 | 124 | |
57493f9f VZ |
125 | #if defined(__VISUALC__) |
126 | #pragma message("Using sprintf() because no snprintf()-like function defined") | |
03e11df5 | 127 | #elif defined(__GNUG__) |
378b05f7 | 128 | #warning "Using sprintf() because no snprintf()-like function defined" |
57493f9f | 129 | #endif //compiler |
3f4a0c5b | 130 | #endif // no vsnprintf |
89b892a2 | 131 | |
783fab59 | 132 | #if defined(_AIX) |
227b5cd7 VZ |
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 | |
dd107c50 | 148 | wxSTD istream& operator>>(wxSTD 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 | ||
dd107c50 | 179 | wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str) |
825ba8f0 SB |
180 | { |
181 | os << str.c_str(); | |
182 | return os; | |
183 | } | |
184 | ||
c801d85f KB |
185 | #endif //std::string compatibility |
186 | ||
378b05f7 VZ |
187 | extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len, |
188 | const wxChar *format, va_list argptr) | |
189 | { | |
190 | #if wxUSE_UNICODE | |
191 | // FIXME should use wvsnprintf() or whatever if it's available | |
192 | wxString s; | |
193 | int iLen = s.PrintfV(format, argptr); | |
194 | if ( iLen != -1 ) | |
195 | { | |
7f017c64 VZ |
196 | wxStrncpy(buf, s.c_str(), len); |
197 | buf[len-1] = wxT('\0'); | |
378b05f7 VZ |
198 | } |
199 | ||
200 | return iLen; | |
201 | #else // ANSI | |
b568d04f VZ |
202 | // vsnprintf() will not terminate the string with '\0' if there is not |
203 | // enough place, but we want the string to always be NUL terminated | |
204 | int rc = wxVsnprintfA(buf, len - 1, format, argptr); | |
2f02cb89 VZ |
205 | if ( rc == -1 ) |
206 | { | |
207 | buf[len] = 0; | |
208 | } | |
b568d04f VZ |
209 | |
210 | return rc; | |
378b05f7 VZ |
211 | #endif // Unicode/ANSI |
212 | } | |
213 | ||
214 | extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len, | |
215 | const wxChar *format, ...) | |
216 | { | |
217 | va_list argptr; | |
218 | va_start(argptr, format); | |
219 | ||
220 | int iLen = wxVsnprintf(buf, len, format, argptr); | |
221 | ||
222 | va_end(argptr); | |
223 | ||
224 | return iLen; | |
225 | } | |
226 | ||
3168a13f VZ |
227 | // ---------------------------------------------------------------------------- |
228 | // private classes | |
229 | // ---------------------------------------------------------------------------- | |
230 | ||
231 | // this small class is used to gather statistics for performance tuning | |
232 | //#define WXSTRING_STATISTICS | |
233 | #ifdef WXSTRING_STATISTICS | |
234 | class Averager | |
235 | { | |
236 | public: | |
237 | Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } | |
2c3b684c | 238 | ~Averager() |
3168a13f VZ |
239 | { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); } |
240 | ||
c86f1403 | 241 | void Add(size_t n) { m_nTotal += n; m_nCount++; } |
3168a13f VZ |
242 | |
243 | private: | |
c86f1403 | 244 | size_t m_nCount, m_nTotal; |
3168a13f VZ |
245 | const char *m_sz; |
246 | } g_averageLength("allocation size"), | |
247 | g_averageSummandLength("summand length"), | |
248 | g_averageConcatHit("hit probability in concat"), | |
249 | g_averageInitialLength("initial string length"); | |
250 | ||
251 | #define STATISTICS_ADD(av, val) g_average##av.Add(val) | |
252 | #else | |
253 | #define STATISTICS_ADD(av, val) | |
254 | #endif // WXSTRING_STATISTICS | |
255 | ||
c801d85f KB |
256 | // =========================================================================== |
257 | // wxString class core | |
258 | // =========================================================================== | |
259 | ||
260 | // --------------------------------------------------------------------------- | |
261 | // construction | |
262 | // --------------------------------------------------------------------------- | |
263 | ||
c801d85f | 264 | // constructs string of <nLength> copies of character <ch> |
2bb67b80 | 265 | wxString::wxString(wxChar ch, size_t nLength) |
c801d85f KB |
266 | { |
267 | Init(); | |
268 | ||
269 | if ( nLength > 0 ) { | |
270 | AllocBuffer(nLength); | |
f1da2f03 | 271 | |
2bb67b80 OK |
272 | #if wxUSE_UNICODE |
273 | // memset only works on char | |
274 | for (size_t n=0; n<nLength; n++) m_pchData[n] = ch; | |
275 | #else | |
c801d85f | 276 | memset(m_pchData, ch, nLength); |
2bb67b80 | 277 | #endif |
c801d85f KB |
278 | } |
279 | } | |
280 | ||
281 | // takes nLength elements of psz starting at nPos | |
2bb67b80 | 282 | void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength) |
c801d85f KB |
283 | { |
284 | Init(); | |
285 | ||
f6bcfd97 BP |
286 | // if the length is not given, assume the string to be NUL terminated |
287 | if ( nLength == wxSTRING_MAXLEN ) { | |
288 | wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") ); | |
c801d85f | 289 | |
f6bcfd97 BP |
290 | nLength = wxStrlen(psz + nPos); |
291 | } | |
6c68273f | 292 | |
3168a13f VZ |
293 | STATISTICS_ADD(InitialLength, nLength); |
294 | ||
c801d85f KB |
295 | if ( nLength > 0 ) { |
296 | // trailing '\0' is written in AllocBuffer() | |
297 | AllocBuffer(nLength); | |
2bb67b80 | 298 | memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar)); |
c801d85f KB |
299 | } |
300 | } | |
dd1eaa89 | 301 | |
8de2e39c | 302 | #ifdef wxSTD_STRING_COMPATIBILITY |
c801d85f | 303 | |
c801d85f KB |
304 | // poor man's iterators are "void *" pointers |
305 | wxString::wxString(const void *pStart, const void *pEnd) | |
306 | { | |
2bb67b80 OK |
307 | InitWith((const wxChar *)pStart, 0, |
308 | (const wxChar *)pEnd - (const wxChar *)pStart); | |
c801d85f KB |
309 | } |
310 | ||
311 | #endif //std::string compatibility | |
312 | ||
2bb67b80 OK |
313 | #if wxUSE_UNICODE |
314 | ||
315 | // from multibyte string | |
cf2f341a | 316 | wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) |
2bb67b80 OK |
317 | { |
318 | // first get necessary size | |
435595e0 | 319 | size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0; |
2bb67b80 OK |
320 | |
321 | // nLength is number of *Unicode* characters here! | |
eea4f86a | 322 | if ((nLen != (size_t)-1) && (nLen > nLength)) |
2bb67b80 OK |
323 | nLen = nLength; |
324 | ||
325 | // empty? | |
eea4f86a | 326 | if ( (nLen != 0) && (nLen != (size_t)-1) ) { |
2bb67b80 OK |
327 | AllocBuffer(nLen); |
328 | conv.MB2WC(m_pchData, psz, nLen); | |
329 | } | |
330 | else { | |
331 | Init(); | |
332 | } | |
333 | } | |
334 | ||
e90c1d2a | 335 | #else // ANSI |
2bb67b80 | 336 | |
0f3e3e0c | 337 | #if wxUSE_WCHAR_T |
c801d85f | 338 | // from wide string |
1c2e6a28 | 339 | wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) |
c801d85f KB |
340 | { |
341 | // first get necessary size | |
1c2e6a28 VS |
342 | size_t nLen = 0; |
343 | if (pwz) | |
344 | { | |
345 | if (nLength == wxSTRING_MAXLEN) | |
346 | nLen = conv.WC2MB((char *) NULL, pwz, 0); | |
347 | else | |
348 | nLen = nLength; | |
349 | } | |
c801d85f KB |
350 | |
351 | // empty? | |
eea4f86a | 352 | if ( (nLen != 0) && (nLen != (size_t)-1) ) { |
c801d85f | 353 | AllocBuffer(nLen); |
f6bcfd97 | 354 | conv.WC2MB(m_pchData, pwz, nLen); |
c801d85f KB |
355 | } |
356 | else { | |
357 | Init(); | |
358 | } | |
359 | } | |
e90c1d2a | 360 | #endif // wxUSE_WCHAR_T |
c801d85f | 361 | |
e90c1d2a | 362 | #endif // Unicode/ANSI |
2bb67b80 | 363 | |
c801d85f KB |
364 | // --------------------------------------------------------------------------- |
365 | // memory allocation | |
366 | // --------------------------------------------------------------------------- | |
367 | ||
368 | // allocates memory needed to store a C string of length nLen | |
369 | void wxString::AllocBuffer(size_t nLen) | |
370 | { | |
13111b2a VZ |
371 | // allocating 0 sized buffer doesn't make sense, all empty strings should |
372 | // reuse g_strEmpty | |
373 | wxASSERT( nLen > 0 ); | |
374 | ||
375 | // make sure that we don't overflow | |
376 | wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) - | |
377 | (sizeof(wxStringData) + EXTRA_ALLOC + 1) ); | |
c801d85f | 378 | |
3168a13f VZ |
379 | STATISTICS_ADD(Length, nLen); |
380 | ||
c801d85f KB |
381 | // allocate memory: |
382 | // 1) one extra character for '\0' termination | |
383 | // 2) sizeof(wxStringData) for housekeeping info | |
3168a13f | 384 | wxStringData* pData = (wxStringData*) |
2bb67b80 | 385 | malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar)); |
c801d85f | 386 | pData->nRefs = 1; |
c801d85f | 387 | pData->nDataLength = nLen; |
3168a13f | 388 | pData->nAllocLength = nLen + EXTRA_ALLOC; |
c801d85f | 389 | m_pchData = pData->data(); // data starts after wxStringData |
223d09f6 | 390 | m_pchData[nLen] = wxT('\0'); |
c801d85f KB |
391 | } |
392 | ||
c801d85f KB |
393 | // must be called before changing this string |
394 | void wxString::CopyBeforeWrite() | |
395 | { | |
396 | wxStringData* pData = GetStringData(); | |
397 | ||
398 | if ( pData->IsShared() ) { | |
399 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 400 | size_t nLen = pData->nDataLength; |
3168a13f | 401 | AllocBuffer(nLen); |
2bb67b80 | 402 | memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar)); |
c801d85f KB |
403 | } |
404 | ||
3bbb630a | 405 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
c801d85f KB |
406 | } |
407 | ||
408 | // must be called before replacing contents of this string | |
409 | void wxString::AllocBeforeWrite(size_t nLen) | |
410 | { | |
411 | wxASSERT( nLen != 0 ); // doesn't make any sense | |
412 | ||
413 | // must not share string and must have enough space | |
3168a13f | 414 | wxStringData* pData = GetStringData(); |
fbf0c83d | 415 | if ( pData->IsShared() || pData->IsEmpty() ) { |
c801d85f KB |
416 | // can't work with old buffer, get new one |
417 | pData->Unlock(); | |
418 | AllocBuffer(nLen); | |
419 | } | |
471aebdd | 420 | else { |
fbf0c83d VZ |
421 | if ( nLen > pData->nAllocLength ) { |
422 | // realloc the buffer instead of calling malloc() again, this is more | |
423 | // efficient | |
424 | STATISTICS_ADD(Length, nLen); | |
425 | ||
426 | nLen += EXTRA_ALLOC; | |
427 | ||
428 | wxStringData *pDataOld = pData; | |
429 | pData = (wxStringData*) | |
430 | realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); | |
431 | if ( !pData ) { | |
432 | // out of memory | |
433 | free(pDataOld); | |
434 | ||
435 | // FIXME we're going to crash... | |
436 | return; | |
437 | } | |
438 | ||
439 | pData->nAllocLength = nLen; | |
440 | m_pchData = pData->data(); | |
441 | } | |
442 | ||
443 | // now we have enough space, just update the string length | |
471aebdd VZ |
444 | pData->nDataLength = nLen; |
445 | } | |
c801d85f | 446 | |
f1da2f03 | 447 | wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner |
c801d85f KB |
448 | } |
449 | ||
dd1eaa89 | 450 | // allocate enough memory for nLen characters |
c86f1403 | 451 | void wxString::Alloc(size_t nLen) |
dd1eaa89 VZ |
452 | { |
453 | wxStringData *pData = GetStringData(); | |
454 | if ( pData->nAllocLength <= nLen ) { | |
9fbd8b8d VZ |
455 | if ( pData->IsEmpty() ) { |
456 | nLen += EXTRA_ALLOC; | |
457 | ||
458 | wxStringData* pData = (wxStringData*) | |
2bb67b80 | 459 | malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); |
9fbd8b8d VZ |
460 | pData->nRefs = 1; |
461 | pData->nDataLength = 0; | |
462 | pData->nAllocLength = nLen; | |
463 | m_pchData = pData->data(); // data starts after wxStringData | |
223d09f6 | 464 | m_pchData[0u] = wxT('\0'); |
9fbd8b8d | 465 | } |
3168a13f VZ |
466 | else if ( pData->IsShared() ) { |
467 | pData->Unlock(); // memory not freed because shared | |
c86f1403 | 468 | size_t nOldLen = pData->nDataLength; |
3168a13f | 469 | AllocBuffer(nLen); |
2bb67b80 | 470 | memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar)); |
3168a13f | 471 | } |
dd1eaa89 | 472 | else { |
3168a13f VZ |
473 | nLen += EXTRA_ALLOC; |
474 | ||
fbf0c83d | 475 | wxStringData *pDataOld = pData; |
dd1eaa89 | 476 | wxStringData *p = (wxStringData *) |
2bb67b80 | 477 | realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); |
3168a13f VZ |
478 | |
479 | if ( p == NULL ) { | |
fbf0c83d VZ |
480 | // don't leak memory |
481 | free(pDataOld); | |
482 | ||
483 | // FIXME what to do on memory error? | |
3168a13f | 484 | return; |
dd1eaa89 | 485 | } |
3168a13f VZ |
486 | |
487 | // it's not important if the pointer changed or not (the check for this | |
488 | // is not faster than assigning to m_pchData in all cases) | |
489 | p->nAllocLength = nLen; | |
490 | m_pchData = p->data(); | |
dd1eaa89 VZ |
491 | } |
492 | } | |
493 | //else: we've already got enough | |
494 | } | |
495 | ||
496 | // shrink to minimal size (releasing extra memory) | |
497 | void wxString::Shrink() | |
498 | { | |
499 | wxStringData *pData = GetStringData(); | |
3bbb630a | 500 | |
337a0010 VZ |
501 | size_t nLen = pData->nDataLength; |
502 | void *p = realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); | |
503 | ||
504 | wxASSERT_MSG( p != NULL, _T("can't free memory?") ); | |
505 | ||
506 | if ( p != pData ) | |
507 | { | |
508 | // contrary to what one might believe, some realloc() implementation do | |
509 | // move the memory block even when its size is reduced | |
510 | pData = (wxStringData *)p; | |
3bbb630a | 511 | |
337a0010 VZ |
512 | m_pchData = pData->data(); |
513 | } | |
fbf0c83d | 514 | |
337a0010 | 515 | pData->nAllocLength = nLen; |
dd1eaa89 VZ |
516 | } |
517 | ||
c801d85f | 518 | // get the pointer to writable buffer of (at least) nLen bytes |
2bb67b80 | 519 | wxChar *wxString::GetWriteBuf(size_t nLen) |
c801d85f KB |
520 | { |
521 | AllocBeforeWrite(nLen); | |
097c080b VZ |
522 | |
523 | wxASSERT( GetStringData()->nRefs == 1 ); | |
524 | GetStringData()->Validate(FALSE); | |
525 | ||
c801d85f KB |
526 | return m_pchData; |
527 | } | |
528 | ||
097c080b VZ |
529 | // put string back in a reasonable state after GetWriteBuf |
530 | void wxString::UngetWriteBuf() | |
531 | { | |
2bb67b80 | 532 | GetStringData()->nDataLength = wxStrlen(m_pchData); |
097c080b VZ |
533 | GetStringData()->Validate(TRUE); |
534 | } | |
535 | ||
8f06a017 RD |
536 | void wxString::UngetWriteBuf(size_t nLen) |
537 | { | |
538 | GetStringData()->nDataLength = nLen; | |
539 | GetStringData()->Validate(TRUE); | |
540 | } | |
541 | ||
c801d85f KB |
542 | // --------------------------------------------------------------------------- |
543 | // data access | |
544 | // --------------------------------------------------------------------------- | |
545 | ||
546 | // all functions are inline in string.h | |
547 | ||
548 | // --------------------------------------------------------------------------- | |
549 | // assignment operators | |
550 | // --------------------------------------------------------------------------- | |
551 | ||
dd1eaa89 | 552 | // helper function: does real copy |
2bb67b80 | 553 | void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData) |
c801d85f KB |
554 | { |
555 | if ( nSrcLen == 0 ) { | |
556 | Reinit(); | |
557 | } | |
558 | else { | |
559 | AllocBeforeWrite(nSrcLen); | |
2bb67b80 | 560 | memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar)); |
c801d85f | 561 | GetStringData()->nDataLength = nSrcLen; |
223d09f6 | 562 | m_pchData[nSrcLen] = wxT('\0'); |
c801d85f KB |
563 | } |
564 | } | |
565 | ||
566 | // assigns one string to another | |
567 | wxString& wxString::operator=(const wxString& stringSrc) | |
568 | { | |
097c080b VZ |
569 | wxASSERT( stringSrc.GetStringData()->IsValid() ); |
570 | ||
c801d85f KB |
571 | // don't copy string over itself |
572 | if ( m_pchData != stringSrc.m_pchData ) { | |
573 | if ( stringSrc.GetStringData()->IsEmpty() ) { | |
574 | Reinit(); | |
575 | } | |
576 | else { | |
577 | // adjust references | |
578 | GetStringData()->Unlock(); | |
579 | m_pchData = stringSrc.m_pchData; | |
580 | GetStringData()->Lock(); | |
581 | } | |
582 | } | |
583 | ||
584 | return *this; | |
585 | } | |
586 | ||
587 | // assigns a single character | |
2bb67b80 | 588 | wxString& wxString::operator=(wxChar ch) |
c801d85f KB |
589 | { |
590 | AssignCopy(1, &ch); | |
591 | return *this; | |
592 | } | |
593 | ||
a3291804 | 594 | |
c801d85f | 595 | // assigns C string |
2bb67b80 | 596 | wxString& wxString::operator=(const wxChar *psz) |
c801d85f | 597 | { |
2bb67b80 | 598 | AssignCopy(wxStrlen(psz), psz); |
c801d85f KB |
599 | return *this; |
600 | } | |
601 | ||
2bb67b80 OK |
602 | #if !wxUSE_UNICODE |
603 | ||
c801d85f KB |
604 | // same as 'signed char' variant |
605 | wxString& wxString::operator=(const unsigned char* psz) | |
606 | { | |
607 | *this = (const char *)psz; | |
608 | return *this; | |
609 | } | |
610 | ||
0f3e3e0c | 611 | #if wxUSE_WCHAR_T |
c801d85f KB |
612 | wxString& wxString::operator=(const wchar_t *pwz) |
613 | { | |
614 | wxString str(pwz); | |
615 | *this = str; | |
616 | return *this; | |
617 | } | |
0f3e3e0c | 618 | #endif |
c801d85f | 619 | |
2bb67b80 OK |
620 | #endif |
621 | ||
c801d85f KB |
622 | // --------------------------------------------------------------------------- |
623 | // string concatenation | |
624 | // --------------------------------------------------------------------------- | |
625 | ||
c801d85f | 626 | // add something to this string |
2bb67b80 | 627 | void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData) |
c801d85f | 628 | { |
3168a13f | 629 | STATISTICS_ADD(SummandLength, nSrcLen); |
c801d85f | 630 | |
05488905 VZ |
631 | // concatenating an empty string is a NOP |
632 | if ( nSrcLen > 0 ) { | |
633 | wxStringData *pData = GetStringData(); | |
634 | size_t nLen = pData->nDataLength; | |
635 | size_t nNewLen = nLen + nSrcLen; | |
c801d85f | 636 | |
05488905 VZ |
637 | // alloc new buffer if current is too small |
638 | if ( pData->IsShared() ) { | |
639 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 640 | |
05488905 VZ |
641 | // we have to allocate another buffer |
642 | wxStringData* pOldData = GetStringData(); | |
643 | AllocBuffer(nNewLen); | |
2bb67b80 | 644 | memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar)); |
05488905 VZ |
645 | pOldData->Unlock(); |
646 | } | |
647 | else if ( nNewLen > pData->nAllocLength ) { | |
648 | STATISTICS_ADD(ConcatHit, 0); | |
3168a13f | 649 | |
05488905 VZ |
650 | // we have to grow the buffer |
651 | Alloc(nNewLen); | |
652 | } | |
653 | else { | |
654 | STATISTICS_ADD(ConcatHit, 1); | |
3168a13f | 655 | |
05488905 VZ |
656 | // the buffer is already big enough |
657 | } | |
3168a13f | 658 | |
05488905 VZ |
659 | // should be enough space |
660 | wxASSERT( nNewLen <= GetStringData()->nAllocLength ); | |
3168a13f | 661 | |
05488905 | 662 | // fast concatenation - all is done in our buffer |
2bb67b80 | 663 | memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar)); |
3168a13f | 664 | |
223d09f6 | 665 | m_pchData[nNewLen] = wxT('\0'); // put terminating '\0' |
05488905 VZ |
666 | GetStringData()->nDataLength = nNewLen; // and fix the length |
667 | } | |
668 | //else: the string to append was empty | |
c801d85f KB |
669 | } |
670 | ||
671 | /* | |
c801d85f KB |
672 | * concatenation functions come in 5 flavours: |
673 | * string + string | |
674 | * char + string and string + char | |
675 | * C str + string and string + C str | |
676 | */ | |
677 | ||
678 | wxString operator+(const wxString& string1, const wxString& string2) | |
679 | { | |
097c080b VZ |
680 | wxASSERT( string1.GetStringData()->IsValid() ); |
681 | wxASSERT( string2.GetStringData()->IsValid() ); | |
682 | ||
3168a13f VZ |
683 | wxString s = string1; |
684 | s += string2; | |
685 | ||
c801d85f KB |
686 | return s; |
687 | } | |
688 | ||
2bb67b80 | 689 | wxString operator+(const wxString& string, wxChar ch) |
c801d85f | 690 | { |
3168a13f VZ |
691 | wxASSERT( string.GetStringData()->IsValid() ); |
692 | ||
693 | wxString s = string; | |
694 | s += ch; | |
097c080b | 695 | |
c801d85f KB |
696 | return s; |
697 | } | |
698 | ||
2bb67b80 | 699 | wxString operator+(wxChar ch, const wxString& string) |
c801d85f | 700 | { |
097c080b VZ |
701 | wxASSERT( string.GetStringData()->IsValid() ); |
702 | ||
3168a13f VZ |
703 | wxString s = ch; |
704 | s += string; | |
705 | ||
c801d85f KB |
706 | return s; |
707 | } | |
708 | ||
2bb67b80 | 709 | wxString operator+(const wxString& string, const wxChar *psz) |
c801d85f | 710 | { |
097c080b VZ |
711 | wxASSERT( string.GetStringData()->IsValid() ); |
712 | ||
c801d85f | 713 | wxString s; |
2bb67b80 | 714 | s.Alloc(wxStrlen(psz) + string.Len()); |
3168a13f VZ |
715 | s = string; |
716 | s += psz; | |
717 | ||
c801d85f KB |
718 | return s; |
719 | } | |
720 | ||
2bb67b80 | 721 | wxString operator+(const wxChar *psz, const wxString& string) |
c801d85f | 722 | { |
097c080b VZ |
723 | wxASSERT( string.GetStringData()->IsValid() ); |
724 | ||
c801d85f | 725 | wxString s; |
2bb67b80 | 726 | s.Alloc(wxStrlen(psz) + string.Len()); |
3168a13f VZ |
727 | s = psz; |
728 | s += string; | |
729 | ||
c801d85f KB |
730 | return s; |
731 | } | |
732 | ||
733 | // =========================================================================== | |
734 | // other common string functions | |
735 | // =========================================================================== | |
736 | ||
737 | // --------------------------------------------------------------------------- | |
738 | // simple sub-string extraction | |
739 | // --------------------------------------------------------------------------- | |
740 | ||
741 | // helper function: clone the data attached to this string | |
742 | void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const | |
743 | { | |
3168a13f | 744 | if ( nCopyLen == 0 ) { |
c801d85f KB |
745 | dest.Init(); |
746 | } | |
3168a13f | 747 | else { |
c801d85f | 748 | dest.AllocBuffer(nCopyLen); |
2bb67b80 | 749 | memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar)); |
c801d85f KB |
750 | } |
751 | } | |
752 | ||
753 | // extract string of length nCount starting at nFirst | |
c801d85f KB |
754 | wxString wxString::Mid(size_t nFirst, size_t nCount) const |
755 | { | |
30d9011f VZ |
756 | wxStringData *pData = GetStringData(); |
757 | size_t nLen = pData->nDataLength; | |
758 | ||
566b84d2 VZ |
759 | // default value of nCount is wxSTRING_MAXLEN and means "till the end" |
760 | if ( nCount == wxSTRING_MAXLEN ) | |
30d9011f VZ |
761 | { |
762 | nCount = nLen - nFirst; | |
763 | } | |
764 | ||
c801d85f | 765 | // out-of-bounds requests return sensible things |
30d9011f VZ |
766 | if ( nFirst + nCount > nLen ) |
767 | { | |
768 | nCount = nLen - nFirst; | |
769 | } | |
c801d85f | 770 | |
30d9011f VZ |
771 | if ( nFirst > nLen ) |
772 | { | |
773 | // AllocCopy() will return empty string | |
c801d85f | 774 | nCount = 0; |
30d9011f | 775 | } |
c801d85f KB |
776 | |
777 | wxString dest; | |
778 | AllocCopy(dest, nCount, nFirst); | |
30d9011f | 779 | |
c801d85f KB |
780 | return dest; |
781 | } | |
782 | ||
f6bcfd97 BP |
783 | // check that the tring starts with prefix and return the rest of the string |
784 | // in the provided pointer if it is not NULL, otherwise return FALSE | |
785 | bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const | |
786 | { | |
787 | wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") ); | |
788 | ||
789 | // first check if the beginning of the string matches the prefix: note | |
790 | // that we don't have to check that we don't run out of this string as | |
791 | // when we reach the terminating NUL, either prefix string ends too (and | |
792 | // then it's ok) or we break out of the loop because there is no match | |
793 | const wxChar *p = c_str(); | |
794 | while ( *prefix ) | |
795 | { | |
796 | if ( *prefix++ != *p++ ) | |
797 | { | |
798 | // no match | |
799 | return FALSE; | |
800 | } | |
801 | } | |
802 | ||
803 | if ( rest ) | |
804 | { | |
805 | // put the rest of the string into provided pointer | |
806 | *rest = p; | |
807 | } | |
808 | ||
809 | return TRUE; | |
810 | } | |
811 | ||
c801d85f KB |
812 | // extract nCount last (rightmost) characters |
813 | wxString wxString::Right(size_t nCount) const | |
814 | { | |
815 | if ( nCount > (size_t)GetStringData()->nDataLength ) | |
816 | nCount = GetStringData()->nDataLength; | |
817 | ||
818 | wxString dest; | |
819 | AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount); | |
820 | return dest; | |
821 | } | |
822 | ||
823 | // get all characters after the last occurence of ch | |
824 | // (returns the whole string if ch not found) | |
2bb67b80 | 825 | wxString wxString::AfterLast(wxChar ch) const |
c801d85f KB |
826 | { |
827 | wxString str; | |
828 | int iPos = Find(ch, TRUE); | |
3c67202d | 829 | if ( iPos == wxNOT_FOUND ) |
c801d85f KB |
830 | str = *this; |
831 | else | |
c8cfb486 | 832 | str = c_str() + iPos + 1; |
c801d85f KB |
833 | |
834 | return str; | |
835 | } | |
836 | ||
837 | // extract nCount first (leftmost) characters | |
838 | wxString wxString::Left(size_t nCount) const | |
839 | { | |
840 | if ( nCount > (size_t)GetStringData()->nDataLength ) | |
841 | nCount = GetStringData()->nDataLength; | |
842 | ||
843 | wxString dest; | |
844 | AllocCopy(dest, nCount, 0); | |
845 | return dest; | |
846 | } | |
847 | ||
848 | // get all characters before the first occurence of ch | |
849 | // (returns the whole string if ch not found) | |
2bb67b80 | 850 | wxString wxString::BeforeFirst(wxChar ch) const |
c801d85f KB |
851 | { |
852 | wxString str; | |
223d09f6 | 853 | for ( const wxChar *pc = m_pchData; *pc != wxT('\0') && *pc != ch; pc++ ) |
c801d85f KB |
854 | str += *pc; |
855 | ||
856 | return str; | |
857 | } | |
858 | ||
859 | /// get all characters before the last occurence of ch | |
860 | /// (returns empty string if ch not found) | |
2bb67b80 | 861 | wxString wxString::BeforeLast(wxChar ch) const |
c801d85f KB |
862 | { |
863 | wxString str; | |
864 | int iPos = Find(ch, TRUE); | |
3c67202d | 865 | if ( iPos != wxNOT_FOUND && iPos != 0 ) |
d1c9bbf6 | 866 | str = wxString(c_str(), iPos); |
c801d85f KB |
867 | |
868 | return str; | |
869 | } | |
870 | ||
871 | /// get all characters after the first occurence of ch | |
872 | /// (returns empty string if ch not found) | |
2bb67b80 | 873 | wxString wxString::AfterFirst(wxChar ch) const |
c801d85f KB |
874 | { |
875 | wxString str; | |
876 | int iPos = Find(ch); | |
3c67202d | 877 | if ( iPos != wxNOT_FOUND ) |
c801d85f KB |
878 | str = c_str() + iPos + 1; |
879 | ||
880 | return str; | |
881 | } | |
882 | ||
883 | // replace first (or all) occurences of some substring with another one | |
2bb67b80 | 884 | size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll) |
c801d85f | 885 | { |
c86f1403 | 886 | size_t uiCount = 0; // count of replacements made |
c801d85f | 887 | |
2bb67b80 | 888 | size_t uiOldLen = wxStrlen(szOld); |
c801d85f KB |
889 | |
890 | wxString strTemp; | |
2bb67b80 OK |
891 | const wxChar *pCurrent = m_pchData; |
892 | const wxChar *pSubstr; | |
223d09f6 | 893 | while ( *pCurrent != wxT('\0') ) { |
2bb67b80 | 894 | pSubstr = wxStrstr(pCurrent, szOld); |
c801d85f KB |
895 | if ( pSubstr == NULL ) { |
896 | // strTemp is unused if no replacements were made, so avoid the copy | |
897 | if ( uiCount == 0 ) | |
898 | return 0; | |
899 | ||
900 | strTemp += pCurrent; // copy the rest | |
901 | break; // exit the loop | |
902 | } | |
903 | else { | |
904 | // take chars before match | |
905 | strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent); | |
906 | strTemp += szNew; | |
907 | pCurrent = pSubstr + uiOldLen; // restart after match | |
908 | ||
909 | uiCount++; | |
910 | ||
911 | // stop now? | |
912 | if ( !bReplaceAll ) { | |
913 | strTemp += pCurrent; // copy the rest | |
914 | break; // exit the loop | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
919 | // only done if there were replacements, otherwise would have returned above | |
920 | *this = strTemp; | |
921 | ||
922 | return uiCount; | |
923 | } | |
924 | ||
925 | bool wxString::IsAscii() const | |
926 | { | |
2bb67b80 | 927 | const wxChar *s = (const wxChar*) *this; |
c801d85f KB |
928 | while(*s){ |
929 | if(!isascii(*s)) return(FALSE); | |
930 | s++; | |
931 | } | |
932 | return(TRUE); | |
933 | } | |
dd1eaa89 | 934 | |
c801d85f KB |
935 | bool wxString::IsWord() const |
936 | { | |
2bb67b80 | 937 | const wxChar *s = (const wxChar*) *this; |
c801d85f | 938 | while(*s){ |
2bb67b80 | 939 | if(!wxIsalpha(*s)) return(FALSE); |
c801d85f KB |
940 | s++; |
941 | } | |
942 | return(TRUE); | |
943 | } | |
dd1eaa89 | 944 | |
c801d85f KB |
945 | bool wxString::IsNumber() const |
946 | { | |
2bb67b80 | 947 | const wxChar *s = (const wxChar*) *this; |
2f74ed28 GT |
948 | if (wxStrlen(s)) |
949 | if ((s[0] == '-') || (s[0] == '+')) s++; | |
c801d85f | 950 | while(*s){ |
2bb67b80 | 951 | if(!wxIsdigit(*s)) return(FALSE); |
c801d85f KB |
952 | s++; |
953 | } | |
954 | return(TRUE); | |
955 | } | |
956 | ||
c801d85f KB |
957 | wxString wxString::Strip(stripType w) const |
958 | { | |
959 | wxString s = *this; | |
960 | if ( w & leading ) s.Trim(FALSE); | |
961 | if ( w & trailing ) s.Trim(TRUE); | |
962 | return s; | |
963 | } | |
964 | ||
c801d85f KB |
965 | // --------------------------------------------------------------------------- |
966 | // case conversion | |
967 | // --------------------------------------------------------------------------- | |
968 | ||
969 | wxString& wxString::MakeUpper() | |
970 | { | |
971 | CopyBeforeWrite(); | |
972 | ||
2bb67b80 OK |
973 | for ( wxChar *p = m_pchData; *p; p++ ) |
974 | *p = (wxChar)wxToupper(*p); | |
c801d85f KB |
975 | |
976 | return *this; | |
977 | } | |
978 | ||
979 | wxString& wxString::MakeLower() | |
980 | { | |
981 | CopyBeforeWrite(); | |
dd1eaa89 | 982 | |
2bb67b80 OK |
983 | for ( wxChar *p = m_pchData; *p; p++ ) |
984 | *p = (wxChar)wxTolower(*p); | |
c801d85f KB |
985 | |
986 | return *this; | |
987 | } | |
988 | ||
989 | // --------------------------------------------------------------------------- | |
990 | // trimming and padding | |
991 | // --------------------------------------------------------------------------- | |
992 | ||
576c608d VZ |
993 | // some compilers (VC++ 6.0 not to name them) return TRUE for a call to |
994 |