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