/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/include/wx/string.h
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/include/wx/string.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations) (download)
Mon Sep 6 11:40:06 2010 UTC (9 years, 5 months ago) by william
File MIME type: text/plain
File size: 66498 byte(s)
exported r3113 from ./upstream/trunk
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/string.h
3 // Purpose: wxString and wxArrayString classes
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id: string.h 53135 2008-04-12 02:31:04Z VZ $
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 /*
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
16 */
17
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 #include "wx/defs.h" // everybody should include this
26
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
28 #include <ctype.h>
29 #endif
30
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
34 # include <stdio.h>
35 # include <string.h>
36 # include <stdarg.h>
37 # include <limits.h>
38 #else
39 # include <string.h>
40 # include <stdio.h>
41 # include <stdarg.h>
42 # include <limits.h>
43 # include <stdlib.h>
44 #endif
45
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
49
50 #ifdef __WXPALMOS__
51 #include <StringMgr.h>
52 #endif
53
54 #include "wx/wxchar.h" // for wxChar
55 #include "wx/buffer.h" // for wxCharBuffer
56 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
57
58 class WXDLLIMPEXP_FWD_BASE wxString;
59
60 // ---------------------------------------------------------------------------
61 // macros
62 // ---------------------------------------------------------------------------
63
64 // casts [unfortunately!] needed to call some broken functions which require
65 // "char *" instead of "const char *"
66 #define WXSTRINGCAST (wxChar *)(const wxChar *)
67 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
68 #define wxMBSTRINGCAST (char *)(const char *)
69 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
70
71 // implementation only
72 #define wxASSERT_VALID_INDEX(i) \
73 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
74
75 // ----------------------------------------------------------------------------
76 // constants
77 // ----------------------------------------------------------------------------
78
79 #if WXWIN_COMPATIBILITY_2_6
80
81 // deprecated in favour of wxString::npos, don't use in new code
82 //
83 // maximum possible length for a string means "take all string" everywhere
84 #define wxSTRING_MAXLEN wxStringBase::npos
85
86 #endif // WXWIN_COMPATIBILITY_2_6
87
88 // ----------------------------------------------------------------------------
89 // global data
90 // ----------------------------------------------------------------------------
91
92 // global pointer to empty string
93 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
94
95 // ---------------------------------------------------------------------------
96 // global functions complementing standard C string library replacements for
97 // strlen() and portable strcasecmp()
98 //---------------------------------------------------------------------------
99
100 // Use wxXXX() functions from wxchar.h instead! These functions are for
101 // backwards compatibility only.
102
103 // checks whether the passed in pointer is NULL and if the string is empty
104 inline bool IsEmpty(const char *p) { return (!p || !*p); }
105
106 // safe version of strlen() (returns 0 if passed NULL pointer)
107 inline size_t Strlen(const char *psz)
108 { return psz ? strlen(psz) : 0; }
109
110 // portable strcasecmp/_stricmp
111 inline int Stricmp(const char *psz1, const char *psz2)
112 {
113 #if defined(__VISUALC__) && defined(__WXWINCE__)
114 register char c1, c2;
115 do {
116 c1 = tolower(*psz1++);
117 c2 = tolower(*psz2++);
118 } while ( c1 && (c1 == c2) );
119
120 return c1 - c2;
121 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
122 return _stricmp(psz1, psz2);
123 #elif defined(__SC__)
124 return _stricmp(psz1, psz2);
125 #elif defined(__SALFORDC__)
126 return stricmp(psz1, psz2);
127 #elif defined(__BORLANDC__)
128 return stricmp(psz1, psz2);
129 #elif defined(__WATCOMC__)
130 return stricmp(psz1, psz2);
131 #elif defined(__DJGPP__)
132 return stricmp(psz1, psz2);
133 #elif defined(__EMX__)
134 return stricmp(psz1, psz2);
135 #elif defined(__WXPM__)
136 return stricmp(psz1, psz2);
137 #elif defined(__WXPALMOS__) || \
138 defined(HAVE_STRCASECMP_IN_STRING_H) || \
139 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
140 defined(__GNUWIN32__)
141 return strcasecmp(psz1, psz2);
142 #elif defined(__MWERKS__) && !defined(__INTEL__)
143 register char c1, c2;
144 do {
145 c1 = tolower(*psz1++);
146 c2 = tolower(*psz2++);
147 } while ( c1 && (c1 == c2) );
148
149 return c1 - c2;
150 #else
151 // almost all compilers/libraries provide this function (unfortunately under
152 // different names), that's why we don't implement our own which will surely
153 // be more efficient than this code (uncomment to use):
154 /*
155 register char c1, c2;
156 do {
157 c1 = tolower(*psz1++);
158 c2 = tolower(*psz2++);
159 } while ( c1 && (c1 == c2) );
160
161 return c1 - c2;
162 */
163
164 #error "Please define string case-insensitive compare for your OS/compiler"
165 #endif // OS/compiler
166 }
167
168 // ----------------------------------------------------------------------------
169 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
170 // ----------------------------------------------------------------------------
171
172 // in both cases we need to define wxStdString
173 #if wxUSE_STL || wxUSE_STD_STRING
174
175 #include "wx/beforestd.h"
176 #include <string>
177 #if wxUSE_PRIVATE_HEAP && defined(__WXMSW__)
178 # include "wx/msw/HeapAllocator.h"
179 #endif
180 #include "wx/afterstd.h"
181
182 #if wxUSE_UNICODE
183 #ifdef HAVE_STD_WSTRING
184 #if wxUSE_PRIVATE_HEAP && defined(__WXMSW__)
185 typedef std::basic_string< wchar_t, std::char_traits< wchar_t >, wxStringAllocator<wchar_t> > wxStdString;
186 #else
187 //typedef std::wstring wxStdString;
188 #endif
189 #else
190 typedef std::basic_string<wxChar> wxStdString;
191 #endif
192 #else
193 typedef std::string wxStdString;
194 #endif
195
196 #endif // need <string>
197
198 #if wxUSE_STL
199
200 // we don't need an extra ctor from std::string when copy ctor already does
201 // the work
202 #undef wxUSE_STD_STRING
203 #define wxUSE_STD_STRING 0
204
205 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
206 (defined(_MSC_VER) && (_MSC_VER <= 1200))
207 #define wxSTRING_BASE_HASNT_CLEAR
208 #endif
209
210 typedef wxStdString wxStringBase;
211 #else // if !wxUSE_STL
212
213 #if !defined(HAVE_STD_STRING_COMPARE) && \
214 (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
215 #define HAVE_STD_STRING_COMPARE
216 #endif
217
218 // ---------------------------------------------------------------------------
219 // string data prepended with some housekeeping info (used by wxString class),
220 // is never used directly (but had to be put here to allow inlining)
221 // ---------------------------------------------------------------------------
222
223 struct WXDLLIMPEXP_BASE wxStringData
224 {
225 int nRefs; // reference count
226 size_t nDataLength, // actual string length
227 nAllocLength; // allocated memory size
228
229 // mimics declaration 'wxChar data[nAllocLength]'
230 wxChar* data() const { return (wxChar*)(this + 1); }
231
232 // empty string has a special ref count so it's never deleted
233 bool IsEmpty() const { return (nRefs == -1); }
234 bool IsShared() const { return (nRefs > 1); }
235
236 // lock/unlock
237 void Lock() { if ( !IsEmpty() ) nRefs++; }
238
239 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
240 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
241 __forceinline
242 #endif
243 // VC++ free must take place in same DLL as allocation when using non dll
244 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
245 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
246 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
247 // we must not inline deallocation since allocation is not inlined
248 void Free();
249 #else
250 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
251 #endif
252
253 // if we had taken control over string memory (GetWriteBuf), it's
254 // intentionally put in invalid state
255 void Validate(bool b) { nRefs = (b ? 1 : 0); }
256 bool IsValid() const { return (nRefs != 0); }
257 };
258
259 class WXDLLIMPEXP_BASE wxStringBase
260 {
261 #if !wxUSE_STL
262 friend class WXDLLIMPEXP_FWD_BASE wxArrayString;
263 #endif
264 public :
265 // an 'invalid' value for string index, moved to this place due to a CW bug
266 static const size_t npos;
267 protected:
268 // points to data preceded by wxStringData structure with ref count info
269 wxChar *m_pchData;
270
271 // accessor to string data
272 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
273
274 // string (re)initialization functions
275 // initializes the string to the empty value (must be called only from
276 // ctors, use Reinit() otherwise)
277 void Init() { m_pchData = (wxChar *)wxEmptyString; }
278 // initializes the string with (a part of) C-string
279 void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = npos);
280 // as Init, but also frees old data
281 void Reinit() { GetStringData()->Unlock(); Init(); }
282
283 // memory allocation
284 // allocates memory for string of length nLen
285 bool AllocBuffer(size_t nLen);
286 // copies data to another string
287 bool AllocCopy(wxString&, int, int) const;
288 // effectively copies data to string
289 bool AssignCopy(size_t, const wxChar *);
290
291 // append a (sub)string
292 bool ConcatSelf(size_t nLen, const wxChar *src, size_t nMaxLen);
293 bool ConcatSelf(size_t nLen, const wxChar *src)
294 { return ConcatSelf(nLen, src, nLen); }
295
296 // functions called before writing to the string: they copy it if there
297 // are other references to our data (should be the only owner when writing)
298 bool CopyBeforeWrite();
299 bool AllocBeforeWrite(size_t);
300
301 // compatibility with wxString
302 bool Alloc(size_t nLen);
303 public:
304 // standard types
305 typedef wxChar value_type;
306 typedef wxChar char_type;
307 typedef size_t size_type;
308 typedef value_type& reference;
309 typedef const value_type& const_reference;
310 typedef value_type* pointer;
311 typedef const value_type* const_pointer;
312 typedef value_type *iterator;
313 typedef const value_type *const_iterator;
314
315 #define wxSTRING_REVERSE_ITERATOR(name, const_or_not) \
316 class name \
317 { \
318 public: \
319 typedef wxChar value_type; \
320 typedef const_or_not value_type& reference; \
321 typedef const_or_not value_type *pointer; \
322 typedef const_or_not value_type *iterator_type; \
323 \
324 name(iterator_type i) : m_cur(i) { } \
325 name(const name& ri) : m_cur(ri.m_cur) { } \
326 \
327 iterator_type base() const { return m_cur; } \
328 \
329 reference operator*() const { return *(m_cur - 1); } \
330 \
331 name& operator++() { --m_cur; return *this; } \
332 name operator++(int) { name tmp = *this; --m_cur; return tmp; } \
333 name& operator--() { ++m_cur; return *this; } \
334 name operator--(int) { name tmp = *this; ++m_cur; return tmp; } \
335 \
336 bool operator==(name ri) const { return m_cur == ri.m_cur; } \
337 bool operator!=(name ri) const { return !(*this == ri); } \
338 \
339 private: \
340 iterator_type m_cur; \
341 }
342
343 wxSTRING_REVERSE_ITERATOR(const_reverse_iterator, const);
344
345 #define wxSTRING_CONST
346 wxSTRING_REVERSE_ITERATOR(reverse_iterator, wxSTRING_CONST);
347 #undef wxSTRING_CONST
348
349 #undef wxSTRING_REVERSE_ITERATOR
350
351
352 // constructors and destructor
353 // ctor for an empty string
354 wxStringBase() { Init(); }
355 // copy ctor
356 wxStringBase(const wxStringBase& stringSrc)
357 {
358 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
359 _T("did you forget to call UngetWriteBuf()?") );
360
361 if ( stringSrc.empty() ) {
362 // nothing to do for an empty string
363 Init();
364 }
365 else {
366 m_pchData = stringSrc.m_pchData; // share same data
367 GetStringData()->Lock(); // => one more copy
368 }
369 }
370 // string containing nRepeat copies of ch
371 wxStringBase(size_type nRepeat, wxChar ch);
372 // ctor takes first nLength characters from C string
373 // (default value of npos means take all the string)
374 wxStringBase(const wxChar *psz)
375 { InitWith(psz, 0, npos); }
376 wxStringBase(const wxChar *psz, size_t nLength)
377 { InitWith(psz, 0, nLength); }
378 wxStringBase(const wxChar *psz,
379 const wxMBConv& WXUNUSED(conv),
380 size_t nLength = npos)
381 { InitWith(psz, 0, nLength); }
382 // take nLen chars starting at nPos
383 wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen)
384 {
385 wxASSERT_MSG( str.GetStringData()->IsValid(),
386 _T("did you forget to call UngetWriteBuf()?") );
387 Init();
388 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
389 InitWith(str.c_str(), nPos, nLen);
390 }
391 // take all characters from pStart to pEnd
392 wxStringBase(const void *pStart, const void *pEnd);
393
394 // dtor is not virtual, this class must not be inherited from!
395 ~wxStringBase()
396 {
397 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
398 //RN - according to the above VC++ does indeed inline this,
399 //even though it spits out two warnings
400 #pragma warning (disable:4714)
401 #endif
402
403 GetStringData()->Unlock();
404 }
405
406 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
407 //re-enable inlining warning
408 #pragma warning (default:4714)
409 #endif
410 // overloaded assignment
411 // from another wxString
412 wxStringBase& operator=(const wxStringBase& stringSrc);
413 // from a character
414 wxStringBase& operator=(wxChar ch);
415 // from a C string
416 wxStringBase& operator=(const wxChar *psz);
417
418 // return the length of the string
419 size_type length() const { return GetStringData()->nDataLength; }
420 // return the length of the string
421 size_type size() const { return length(); }
422 // return the maximum size of the string
423 size_type max_size() const { return npos; }
424 // resize the string, filling the space with c if c != 0
425 void resize(size_t nSize, wxChar ch = wxT('\0'));
426 // delete the contents of the string
427 void clear() { erase(0, npos); }
428 // returns true if the string is empty
429 bool empty() const { return length() == 0; }
430 // inform string about planned change in size
431 void reserve(size_t sz) { Alloc(sz); }
432 size_type capacity() const { return GetStringData()->nAllocLength; }
433
434 // lib.string.access
435 // return the character at position n
436 value_type at(size_type n) const
437 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
438 // returns the writable character at position n
439 reference at(size_type n)
440 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
441
442 // lib.string.modifiers
443 // append elements str[pos], ..., str[pos+n]
444 wxStringBase& append(const wxStringBase& str, size_t pos, size_t n)
445 {
446 wxASSERT(pos <= str.length());
447 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
448 return *this;
449 }
450 // append a string
451 wxStringBase& append(const wxStringBase& str)
452 { ConcatSelf(str.length(), str.c_str()); return *this; }
453 // append first n (or all if n == npos) characters of sz
454 wxStringBase& append(const wxChar *sz)
455 { ConcatSelf(wxStrlen(sz), sz); return *this; }
456 wxStringBase& append(const wxChar *sz, size_t n)
457 { ConcatSelf(n, sz); return *this; }
458 // append n copies of ch
459 wxStringBase& append(size_t n, wxChar ch);
460 // append from first to last
461 wxStringBase& append(const_iterator first, const_iterator last)
462 { ConcatSelf(last - first, first); return *this; }
463
464 // same as `this_string = str'
465 wxStringBase& assign(const wxStringBase& str)
466 { return *this = str; }
467 // same as ` = str[pos..pos + n]
468 wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n)
469 { clear(); return append(str, pos, n); }
470 // same as `= first n (or all if n == npos) characters of sz'
471 wxStringBase& assign(const wxChar *sz)
472 { clear(); return append(sz, wxStrlen(sz)); }
473 wxStringBase& assign(const wxChar *sz, size_t n)
474 { clear(); return append(sz, n); }
475 // same as `= n copies of ch'
476 wxStringBase& assign(size_t n, wxChar ch)
477 { clear(); return append(n, ch); }
478 // assign from first to last
479 wxStringBase& assign(const_iterator first, const_iterator last)
480 { clear(); return append(first, last); }
481
482 // first valid index position
483 const_iterator begin() const { return m_pchData; }
484 iterator begin();
485 // position one after the last valid one
486 const_iterator end() const { return m_pchData + length(); }
487 iterator end();
488
489 // first element of the reversed string
490 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
491 reverse_iterator rbegin() { return reverse_iterator(end()); }
492 // one beyond the end of the reversed string
493 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
494 reverse_iterator rend() { return reverse_iterator(begin()); }
495
496 // insert another string
497 wxStringBase& insert(size_t nPos, const wxStringBase& str)
498 {
499 wxASSERT( str.GetStringData()->IsValid() );
500 return insert(nPos, str.c_str(), str.length());
501 }
502 // insert n chars of str starting at nStart (in str)
503 wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n)
504 {
505 wxASSERT( str.GetStringData()->IsValid() );
506 wxASSERT( nStart < str.length() );
507 size_t strLen = str.length() - nStart;
508 n = strLen < n ? strLen : n;
509 return insert(nPos, str.c_str() + nStart, n);
510 }
511 // insert first n (or all if n == npos) characters of sz
512 wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos);
513 // insert n copies of ch
514 wxStringBase& insert(size_t nPos, size_t n, wxChar ch)
515 { return insert(nPos, wxStringBase(n, ch)); }
516 iterator insert(iterator it, wxChar ch)
517 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
518 void insert(iterator it, const_iterator first, const_iterator last)
519 { insert(it - begin(), first, last - first); }
520 void insert(iterator it, size_type n, wxChar ch)
521 { insert(it - begin(), n, ch); }
522
523 // delete characters from nStart to nStart + nLen
524 wxStringBase& erase(size_type pos = 0, size_type n = npos);
525 iterator erase(iterator first, iterator last)
526 {
527 size_t idx = first - begin();
528 erase(idx, last - first);
529 return begin() + idx;
530 }
531 iterator erase(iterator first);
532
533 // explicit conversion to C string (use this with printf()!)
534 const wxChar* c_str() const { return m_pchData; }
535 const wxChar* data() const { return m_pchData; }
536
537 // replaces the substring of length nLen starting at nStart
538 wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz);
539 // replaces the substring of length nLen starting at nStart
540 wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str)
541 { return replace(nStart, nLen, str.c_str()); }
542 // replaces the substring with nCount copies of ch
543 wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch);
544 // replaces a substring with another substring
545 wxStringBase& replace(size_t nStart, size_t nLen,
546 const wxStringBase& str, size_t nStart2, size_t nLen2);
547 // replaces the substring with first nCount chars of sz
548 wxStringBase& replace(size_t nStart, size_t nLen,
549 const wxChar* sz, size_t nCount);
550 wxStringBase& replace(iterator first, iterator last, const_pointer s)
551 { return replace(first - begin(), last - first, s); }
552 wxStringBase& replace(iterator first, iterator last, const_pointer s,
553 size_type n)
554 { return replace(first - begin(), last - first, s, n); }
555 wxStringBase& replace(iterator first, iterator last, const wxStringBase& s)
556 { return replace(first - begin(), last - first, s); }
557 wxStringBase& replace(iterator first, iterator last, size_type n, wxChar c)
558 { return replace(first - begin(), last - first, n, c); }
559 wxStringBase& replace(iterator first, iterator last,
560 const_iterator first1, const_iterator last1)
561 { return replace(first - begin(), last - first, first1, last1 - first1); }
562
563 // swap two strings
564 void swap(wxStringBase& str);
565
566 // All find() functions take the nStart argument which specifies the
567 // position to start the search on, the default value is 0. All functions
568 // return npos if there were no match.
569
570 // find a substring
571 size_t find(const wxStringBase& str, size_t nStart = 0) const;
572
573 // find first n characters of sz
574 size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;
575
576 // find the first occurence of character ch after nStart
577 size_t find(wxChar ch, size_t nStart = 0) const;
578
579 // rfind() family is exactly like find() but works right to left
580
581 // as find, but from the end
582 size_t rfind(const wxStringBase& str, size_t nStart = npos) const;
583
584 // as find, but from the end
585 size_t rfind(const wxChar* sz, size_t nStart = npos,
586 size_t n = npos) const;
587 // as find, but from the end
588 size_t rfind(wxChar ch, size_t nStart = npos) const;
589
590 // find first/last occurence of any character in the set
591
592 // as strpbrk() but starts at nStart, returns npos if not found
593 size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const
594 { return find_first_of(str.c_str(), nStart); }
595 // same as above
596 size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
597 size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const;
598 // same as find(char, size_t)
599 size_t find_first_of(wxChar c, size_t nStart = 0) const
600 { return find(c, nStart); }
601 // find the last (starting from nStart) char from str in this string
602 size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const
603 { return find_last_of(str.c_str(), nStart); }
604 // same as above
605 size_t find_last_of (const wxChar* sz, size_t nStart = npos) const;
606 size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const;
607 // same as above
608 size_t find_last_of(wxChar c, size_t nStart = npos) const
609 { return rfind(c, nStart); }
610
611 // find first/last occurence of any character not in the set
612
613 // as strspn() (starting from nStart), returns npos on failure
614 size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const
615 { return find_first_not_of(str.c_str(), nStart); }
616 // same as above
617 size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const;
618 size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const;
619 // same as above
620 size_t find_first_not_of(wxChar ch, size_t nStart = 0) const;
621 // as strcspn()
622 size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const
623 { return find_last_not_of(str.c_str(), nStart); }
624 // same as above
625 size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const;
626 size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const;
627 // same as above
628 size_t find_last_not_of(wxChar ch, size_t nStart = npos) const;
629
630 // All compare functions return -1, 0 or 1 if the [sub]string is less,
631 // equal or greater than the compare() argument.
632
633 // comparison with another string
634 int compare(const wxStringBase& str) const;
635 // comparison with a substring
636 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
637 // comparison of 2 substrings
638 int compare(size_t nStart, size_t nLen,
639 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
640 // comparison with a c string
641 int compare(const wxChar* sz) const;
642 // substring comparison with first nCount characters of sz
643 int compare(size_t nStart, size_t nLen,
644 const wxChar* sz, size_t nCount = npos) const;
645
646 size_type copy(wxChar* s, size_type n, size_type pos = 0);
647
648 // substring extraction
649 wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const;
650
651 // string += string
652 wxStringBase& operator+=(const wxStringBase& s) { return append(s); }
653 // string += C string
654 wxStringBase& operator+=(const wxChar *psz) { return append(psz); }
655 // string += char
656 wxStringBase& operator+=(wxChar ch) { return append(1, ch); }
657 };
658
659 #endif // !wxUSE_STL
660
661 // ----------------------------------------------------------------------------
662 // wxString: string class trying to be compatible with std::string, MFC
663 // CString and wxWindows 1.x wxString all at once
664 // ---------------------------------------------------------------------------
665
666 class WXDLLIMPEXP_BASE wxString : public wxStringBase
667 {
668 #if !wxUSE_STL
669 friend class WXDLLIMPEXP_FWD_BASE wxArrayString;
670 #endif
671
672 // NB: special care was taken in arranging the member functions in such order
673 // that all inline functions can be effectively inlined, verify that all
674 // performance critical functions are still inlined if you change order!
675 private:
676 // if we hadn't made these operators private, it would be possible to
677 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
678 // converted to char in C and we do have operator=(char)
679 //
680 // NB: we don't need other versions (short/long and unsigned) as attempt
681 // to assign another numeric type to wxString will now result in
682 // ambiguity between operator=(char) and operator=(int)
683 wxString& operator=(int);
684
685 // these methods are not implemented - there is _no_ conversion from int to
686 // string, you're doing something wrong if the compiler wants to call it!
687 //
688 // try `s << i' or `s.Printf("%d", i)' instead
689 wxString(int);
690
691 public:
692 // constructors and destructor
693 // ctor for an empty string
694 wxString() : wxStringBase() { }
695 // copy ctor
696 wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { }
697 wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { }
698 // string containing nRepeat copies of ch
699 wxString(wxChar ch, size_t nRepeat = 1)
700 : wxStringBase(nRepeat, ch) { }
701 wxString(size_t nRepeat, wxChar ch)
702 : wxStringBase(nRepeat, ch) { }
703 // ctor takes first nLength characters from C string
704 // (default value of npos means take all the string)
705 wxString(const wxChar *psz)
706 : wxStringBase(psz ? psz : wxT("")) { }
707 wxString(const wxChar *psz, size_t nLength)
708 : wxStringBase(psz, nLength) { }
709 wxString(const wxChar *psz,
710 const wxMBConv& WXUNUSED(conv),
711 size_t nLength = npos)
712 : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { }
713
714 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
715 // implicit conversions from std::string to wxString as this allows to use
716 // the same strings in non-GUI and GUI code, however we don't want to
717 // unconditionally add this ctor as it would make wx lib dependent on
718 // libstdc++ on some Linux versions which is bad, so instead we ask the
719 // client code to define this wxUSE_STD_STRING symbol if they need it
720 #if wxUSE_STD_STRING
721 wxString(const wxStdString& s)
722 : wxStringBase(s.c_str()) { }
723 #endif // wxUSE_STD_STRING
724
725 #if wxUSE_UNICODE
726 // from multibyte string
727 wxString(const char *psz, const wxMBConv& conv, size_t nLength = npos);
728 // from wxWCharBuffer (i.e. return from wxGetString)
729 wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { }
730 #else // ANSI
731 // from C string (for compilers using unsigned char)
732 wxString(const unsigned char* psz)
733 : wxStringBase((const char*)psz) { }
734 // from part of C string (for compilers using unsigned char)
735 wxString(const unsigned char* psz, size_t nLength)
736 : wxStringBase((const char*)psz, nLength) { }
737
738 #if wxUSE_WCHAR_T
739 // from wide (Unicode) string
740 wxString(const wchar_t *pwz,
741 const wxMBConv& conv = wxConvLibc,
742 size_t nLength = npos);
743 #endif // !wxUSE_WCHAR_T
744
745 // from wxCharBuffer
746 wxString(const wxCharBuffer& psz)
747 : wxStringBase(psz) { }
748 #endif // Unicode/ANSI
749
750 // generic attributes & operations
751 // as standard strlen()
752 size_t Len() const { return length(); }
753 // string contains any characters?
754 bool IsEmpty() const { return empty(); }
755 // empty string is "false", so !str will return true
756 bool operator!() const { return empty(); }
757 // truncate the string to given length
758 wxString& Truncate(size_t uiLen);
759 // empty string contents
760 void Empty()
761 {
762 Truncate(0);
763
764 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
765 }
766 // empty the string and free memory
767 void Clear()
768 {
769 wxString tmp(wxEmptyString);
770 swap(tmp);
771 }
772
773 // contents test
774 // Is an ascii value
775 bool IsAscii() const;
776 // Is a number
777 bool IsNumber() const;
778 // Is a word
779 bool IsWord() const;
780
781 // data access (all indexes are 0 based)
782 // read access
783 wxChar GetChar(size_t n) const
784 { return at(n); }
785 // read/write access
786 wxChar& GetWritableChar(size_t n)
787 { return at(n); }
788 // write access
789 void SetChar(size_t n, wxChar ch)
790 { at(n) = ch; }
791
792 // get last character
793 wxChar Last() const
794 {
795 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
796
797 return at(length() - 1);
798 }
799
800 // get writable last character
801 wxChar& Last()
802 {
803 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
804 return at(length() - 1);
805 }
806
807 /*
808 Note that we we must define all of the overloads below to avoid
809 ambiguity when using str[0]. Also note that for a conforming compiler we
810 don't need const version of operatorp[] at all as indexed access to
811 const string is provided by implicit conversion to "const wxChar *"
812 below and defining them would only result in ambiguities, but some other
813 compilers refuse to compile "str[0]" without them.
814 */
815
816 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
817 wxChar operator[](int n) const
818 { return wxStringBase::at(n); }
819 wxChar operator[](size_type n) const
820 { return wxStringBase::at(n); }
821 #ifndef wxSIZE_T_IS_UINT
822 wxChar operator[](unsigned int n) const
823 { return wxStringBase::at(n); }
824 #endif // size_t != unsigned int
825 #endif // broken compiler
826
827
828 // operator versions of GetWriteableChar()
829 wxChar& operator[](int n)
830 { return wxStringBase::at(n); }
831 wxChar& operator[](size_type n)
832 { return wxStringBase::at(n); }
833 #ifndef wxSIZE_T_IS_UINT
834 wxChar& operator[](unsigned int n)
835 { return wxStringBase::at(n); }
836 #endif // size_t != unsigned int
837
838 // implicit conversion to C string
839 operator const wxChar*() const { return c_str(); }
840
841 // identical to c_str(), for wxWin 1.6x compatibility
842 const wxChar* wx_str() const { return c_str(); }
843 // identical to c_str(), for MFC compatibility
844 const wxChar* GetData() const { return c_str(); }
845
846 #if wxABI_VERSION >= 20804
847 // conversion to *non-const* multibyte or widestring buffer; modifying
848 // returned buffer won't affect the string, these methods are only useful
849 // for passing values to const-incorrect functions
850 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
851 { return mb_str(conv); }
852 #if wxUSE_WCHAR_T
853 wxWritableWCharBuffer wchar_str() const { return wc_str(wxConvLibc); }
854 #endif
855 #endif // wxABI_VERSION >= 20804
856
857 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
858 // converting numbers or strings which are certain not to contain special
859 // chars (typically system functions, X atoms, environment variables etc.)
860 //
861 // the behaviour of these functions with the strings containing anything
862 // else than 7 bit ASCII characters is undefined, use at your own risk.
863 #if wxUSE_UNICODE
864 static wxString FromAscii(const char *ascii); // string
865 static wxString FromAscii(const char ascii); // char
866 const wxCharBuffer ToAscii() const;
867 #else // ANSI
868 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
869 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
870 const char *ToAscii() const { return c_str(); }
871 #endif // Unicode/!Unicode
872
873 #if wxABI_VERSION >= 20804
874 // conversion to/from UTF-8:
875 #if wxUSE_UNICODE
876 static wxString FromUTF8(const char *utf8)
877 { return wxString(utf8, wxConvUTF8); }
878 static wxString FromUTF8(const char *utf8, size_t len)
879 { return wxString(utf8, wxConvUTF8, len); }
880 const wxCharBuffer utf8_str() const { return mb_str(wxConvUTF8); }
881 const wxCharBuffer ToUTF8() const { return utf8_str(); }
882 #elif wxUSE_WCHAR_T // ANSI
883 static wxString FromUTF8(const char *utf8)
884 { return wxString(wxConvUTF8.cMB2WC(utf8)); }
885 static wxString FromUTF8(const char *utf8, size_t len)
886 {
887 size_t wlen;
888 wxWCharBuffer buf(wxConvUTF8.cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
889 return wxString(buf.data(), wxConvLibc, wlen);
890 }
891 const wxCharBuffer utf8_str() const
892 { return wxConvUTF8.cWC2MB(wc_str(wxConvLibc)); }
893 const wxCharBuffer ToUTF8() const { return utf8_str(); }
894 #endif // Unicode/ANSI
895 #endif // wxABI_VERSION >= 20804
896
897 #if wxABI_VERSION >= 20804
898 // functions for storing binary data in wxString:
899 #if wxUSE_UNICODE
900 static wxString From8BitData(const char *data, size_t len)
901 { return wxString(data, wxConvISO8859_1, len); }
902 // version for NUL-terminated data:
903 static wxString From8BitData(const char *data)
904 { return wxString(data, wxConvISO8859_1); }
905 const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); }
906 #else // ANSI
907 static wxString From8BitData(const char *data, size_t len)
908 { return wxString(data, len); }
909 // version for NUL-terminated data:
910 static wxString From8BitData(const char *data)
911 { return wxString(data); }
912 const char *To8BitData() const { return c_str(); }
913 #endif // Unicode/ANSI
914 #endif // wxABI_VERSION >= 20804
915
916 // conversions with (possible) format conversions: have to return a
917 // buffer with temporary data
918 //
919 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
920 // return an ANSI (multibyte) string, wc_str() to return a wide string and
921 // fn_str() to return a string which should be used with the OS APIs
922 // accepting the file names. The return value is always the same, but the
923 // type differs because a function may either return pointer to the buffer
924 // directly or have to use intermediate buffer for translation.
925 #if wxUSE_UNICODE
926 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
927
928 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
929
930 const wxChar* wc_str() const { return c_str(); }
931
932 // for compatibility with !wxUSE_UNICODE version
933 const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
934
935 #if wxMBFILES
936 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
937 #else // !wxMBFILES
938 const wxChar* fn_str() const { return c_str(); }
939 #endif // wxMBFILES/!wxMBFILES
940 #else // ANSI
941 const wxChar* mb_str() const { return c_str(); }
942
943 // for compatibility with wxUSE_UNICODE version
944 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
945
946 const wxWX2MBbuf mbc_str() const { return mb_str(); }
947
948 #if wxUSE_WCHAR_T
949 const wxWCharBuffer wc_str(const wxMBConv& conv) const;
950 #endif // wxUSE_WCHAR_T
951 #ifdef __WXOSX__
952 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
953 #else
954 const wxChar* fn_str() const { return c_str(); }
955 #endif
956 #endif // Unicode/ANSI
957
958 // overloaded assignment
959 // from another wxString
960 wxString& operator=(const wxStringBase& stringSrc)
961 { return (wxString&)wxStringBase::operator=(stringSrc); }
962 // from a character
963 wxString& operator=(wxChar ch)
964 { return (wxString&)wxStringBase::operator=(ch); }
965 // from a C string - STL probably will crash on NULL,
966 // so we need to compensate in that case
967 #if wxUSE_STL
968 wxString& operator=(const wxChar *psz)
969 { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; }
970 #else
971 wxString& operator=(const wxChar *psz)
972 { return (wxString&)wxStringBase::operator=(psz); }
973 #endif
974
975 #if wxUSE_UNICODE
976 // from wxWCharBuffer
977 wxString& operator=(const wxWCharBuffer& psz)
978 { (void) operator=((const wchar_t *)psz); return *this; }
979 #else // ANSI
980 // from another kind of C string
981 wxString& operator=(const unsigned char* psz);
982 #if wxUSE_WCHAR_T
983 // from a wide string
984 wxString& operator=(const wchar_t *pwz);
985 #endif
986 // from wxCharBuffer
987 wxString& operator=(const wxCharBuffer& psz)
988 { (void) operator=((const char *)psz); return *this; }
989 #endif // Unicode/ANSI
990
991 // string concatenation
992 // in place concatenation
993 /*
994 Concatenate and return the result. Note that the left to right
995 associativity of << allows to write things like "str << str1 << str2
996 << ..." (unlike with +=)
997 */
998 // string += string
999 wxString& operator<<(const wxString& s)
1000 {
1001 #if !wxUSE_STL
1002 wxASSERT_MSG( s.GetStringData()->IsValid(),
1003 _T("did you forget to call UngetWriteBuf()?") );
1004 #endif
1005
1006 append(s);
1007 return *this;
1008 }
1009 // string += C string
1010 wxString& operator<<(const wxChar *psz)
1011 { append(psz); return *this; }
1012 // string += char
1013 wxString& operator<<(wxChar ch) { append(1, ch); return *this; }
1014
1015 // string += buffer (i.e. from wxGetString)
1016 #if wxUSE_UNICODE
1017 wxString& operator<<(const wxWCharBuffer& s)
1018 { (void)operator<<((const wchar_t *)s); return *this; }
1019 void operator+=(const wxWCharBuffer& s)
1020 { (void)operator<<((const wchar_t *)s); }
1021 #else // !wxUSE_UNICODE
1022 wxString& operator<<(const wxCharBuffer& s)
1023 { (void)operator<<((const char *)s); return *this; }
1024 void operator+=(const wxCharBuffer& s)
1025 { (void)operator<<((const char *)s); }
1026 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1027
1028 // string += C string
1029 wxString& Append(const wxString& s)
1030 {
1031 // test for empty() to share the string if possible
1032 if ( empty() )
1033 *this = s;
1034 else
1035 append(s);
1036 return *this;
1037 }
1038 wxString& Append(const wxChar* psz)
1039 { append(psz); return *this; }
1040 // append count copies of given character
1041 wxString& Append(wxChar ch, size_t count = 1u)
1042 { append(count, ch); return *this; }
1043 wxString& Append(const wxChar* psz, size_t nLen)
1044 { append(psz, nLen); return *this; }
1045
1046 // prepend a string, return the string itself
1047 wxString& Prepend(const wxString& str)
1048 { *this = str + *this; return *this; }
1049
1050 // non-destructive concatenation
1051 // two strings
1052 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1053 const wxString& string2);
1054 // string with a single char
1055 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
1056 // char with a string
1057 friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
1058 // string with C string
1059 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1060 const wxChar *psz);
1061 // C string with string
1062 friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz,
1063 const wxString& string);
1064
1065 // stream-like functions
1066 // insert an int into string
1067 wxString& operator<<(int i)
1068 { return (*this) << Format(_T("%d"), i); }
1069 // insert an unsigned int into string
1070 wxString& operator<<(unsigned int ui)
1071 { return (*this) << Format(_T("%u"), ui); }
1072 // insert a long into string
1073 wxString& operator<<(long l)
1074 { return (*this) << Format(_T("%ld"), l); }
1075 // insert an unsigned long into string
1076 wxString& operator<<(unsigned long ul)
1077 { return (*this) << Format(_T("%lu"), ul); }
1078 #if defined wxLongLong_t && !defined wxLongLongIsLong
1079 // insert a long long if they exist and aren't longs
1080 wxString& operator<<(wxLongLong_t ll)
1081 {
1082 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1083 return (*this) << Format(fmt, ll);
1084 }
1085 // insert an unsigned long long
1086 wxString& operator<<(wxULongLong_t ull)
1087 {
1088 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1089 return (*this) << Format(fmt , ull);
1090 }
1091 #endif
1092 // insert a float into string
1093 wxString& operator<<(float f)
1094 { return (*this) << Format(_T("%f"), f); }
1095 // insert a double into string
1096 wxString& operator<<(double d)
1097 { return (*this) << Format(_T("%g"), d); }
1098
1099 // string comparison
1100 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1101 int Cmp(const wxChar *psz) const;
1102 int Cmp(const wxString& s) const;
1103 // same as Cmp() but not case-sensitive
1104 int CmpNoCase(const wxChar *psz) const;
1105 int CmpNoCase(const wxString& s) const;
1106 // test for the string equality, either considering case or not
1107 // (if compareWithCase then the case matters)
1108 bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const
1109 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
1110 // comparison with a single character: returns true if equal
1111 bool IsSameAs(wxChar c, bool compareWithCase = true) const
1112 {
1113 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
1114 : wxToupper(GetChar(0u)) == wxToupper(c));
1115 }
1116
1117 // simple sub-string extraction
1118 // return substring starting at nFirst of length nCount (or till the end
1119 // if nCount = default value)
1120 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1121
1122 // operator version of Mid()
1123 wxString operator()(size_t start, size_t len) const
1124 { return Mid(start, len); }
1125
1126 // check if the string starts with the given prefix and return the rest
1127 // of the string in the provided pointer if it is not NULL; otherwise
1128 // return false
1129 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
1130 // check if the string ends with the given suffix and return the
1131 // beginning of the string before the suffix in the provided pointer if
1132 // it is not NULL; otherwise return false
1133 bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const;
1134
1135 // get first nCount characters
1136 wxString Left(size_t nCount) const;
1137 // get last nCount characters
1138 wxString Right(size_t nCount) const;
1139 // get all characters before the first occurance of ch
1140 // (returns the whole string if ch not found)
1141 wxString BeforeFirst(wxChar ch) const;
1142 // get all characters before the last occurence of ch
1143 // (returns empty string if ch not found)
1144 wxString BeforeLast(wxChar ch) const;
1145 // get all characters after the first occurence of ch
1146 // (returns empty string if ch not found)
1147 wxString AfterFirst(wxChar ch) const;
1148 // get all characters after the last occurence of ch
1149 // (returns the whole string if ch not found)
1150 wxString AfterLast(wxChar ch) const;
1151
1152 // for compatibility only, use more explicitly named functions above
1153 wxString Before(wxChar ch) const { return BeforeLast(ch); }
1154 wxString After(wxChar ch) const { return AfterFirst(ch); }
1155
1156 // case conversion
1157 // convert to upper case in place, return the string itself
1158 wxString& MakeUpper();
1159 // convert to upper case, return the copy of the string
1160 // Here's something to remember: BC++ doesn't like returns in inlines.
1161 wxString Upper() const ;
1162 // convert to lower case in place, return the string itself
1163 wxString& MakeLower();
1164 // convert to lower case, return the copy of the string
1165 wxString Lower() const ;
1166
1167 // trimming/padding whitespace (either side) and truncating
1168 // remove spaces from left or from right (default) side
1169 wxString& Trim(bool bFromRight = true);
1170 // add nCount copies chPad in the beginning or at the end (default)
1171 wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = true);
1172
1173 // searching and replacing
1174 // searching (return starting index, or -1 if not found)
1175 int Find(wxChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1176 // searching (return starting index, or -1 if not found)
1177 int Find(const wxChar *pszSub) const; // like strstr
1178 // replace first (or all of bReplaceAll) occurences of substring with
1179 // another string, returns the number of replacements made
1180 size_t Replace(const wxChar *szOld,
1181 const wxChar *szNew,
1182 bool bReplaceAll = true);
1183
1184 // check if the string contents matches a mask containing '*' and '?'
1185 bool Matches(const wxChar *szMask) const;
1186
1187 // conversion to numbers: all functions return true only if the whole
1188 // string is a number and put the value of this number into the pointer
1189 // provided, the base is the numeric base in which the conversion should be
1190 // done and must be comprised between 2 and 36 or be 0 in which case the
1191 // standard C rules apply (leading '0' => octal, "0x" => hex)
1192 // convert to a signed integer
1193 bool ToLong(long *val, int base = 10) const;
1194 // convert to an unsigned integer
1195 bool ToULong(unsigned long *val, int base = 10) const;
1196 // convert to wxLongLong
1197 #if defined(wxLongLong_t)
1198 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1199 // convert to wxULongLong
1200 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1201 #endif // wxLongLong_t
1202 // convert to a double
1203 bool ToDouble(double *val) const;
1204
1205
1206
1207 // formatted input/output
1208 // as sprintf(), returns the number of characters written or < 0 on error
1209 // (take 'this' into account in attribute parameter count)
1210 int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1211 // as vprintf(), returns the number of characters written or < 0 on error
1212 int PrintfV(const wxChar* pszFormat, va_list argptr);
1213
1214 // returns the string containing the result of Printf() to it
1215 static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1216 // the same as above, but takes a va_list
1217 static wxString FormatV(const wxChar *pszFormat, va_list argptr);
1218
1219 // raw access to string memory
1220 // ensure that string has space for at least nLen characters
1221 // only works if the data of this string is not shared
1222 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1223 // minimize the string's memory
1224 // only works if the data of this string is not shared
1225 bool Shrink();
1226 #if !wxUSE_STL
1227 // get writable buffer of at least nLen bytes. Unget() *must* be called
1228 // a.s.a.p. to put string back in a reasonable state!
1229 wxChar *GetWriteBuf(size_t nLen);
1230 // call this immediately after GetWriteBuf() has been used
1231 void UngetWriteBuf();
1232 void UngetWriteBuf(size_t nLen);
1233 #endif
1234
1235 // wxWidgets version 1 compatibility functions
1236
1237 // use Mid()
1238 wxString SubString(size_t from, size_t to) const
1239 { return Mid(from, (to - from + 1)); }
1240 // values for second parameter of CompareTo function
1241 enum caseCompare {exact, ignoreCase};
1242 // values for first parameter of Strip function
1243 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1244
1245 // use Printf()
1246 // (take 'this' into account in attribute parameter count)
1247 int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1248
1249 // use Cmp()
1250 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1251 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1252
1253 // use Len
1254 size_t Length() const { return length(); }
1255 // Count the number of characters
1256 int Freq(wxChar ch) const;
1257 // use MakeLower
1258 void LowerCase() { MakeLower(); }
1259 // use MakeUpper
1260 void UpperCase() { MakeUpper(); }
1261 // use Trim except that it doesn't change this string
1262 wxString Strip(stripType w = trailing) const;
1263
1264 // use Find (more general variants not yet supported)
1265 size_t Index(const wxChar* psz) const { return Find(psz); }
1266 size_t Index(wxChar ch) const { return Find(ch); }
1267 // use Truncate
1268 wxString& Remove(size_t pos) { return Truncate(pos); }
1269 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1270
1271 wxString& Remove(size_t nStart, size_t nLen)
1272 { return (wxString&)erase( nStart, nLen ); }
1273
1274 // use Find()
1275 int First( const wxChar ch ) const { return Find(ch); }
1276 int First( const wxChar* psz ) const { return Find(psz); }
1277 int First( const wxString &str ) const { return Find(str); }
1278 int Last( const wxChar ch ) const { return Find(ch, true); }
1279 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1280
1281 // use empty()
1282 bool IsNull() const { return empty(); }
1283
1284 // std::string compatibility functions
1285
1286 // take nLen chars starting at nPos
1287 wxString(const wxString& str, size_t nPos, size_t nLen)
1288 : wxStringBase(str, nPos, nLen) { }
1289 // take all characters from pStart to pEnd
1290 wxString(const void *pStart, const void *pEnd)
1291 : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { }
1292 #if wxUSE_STL
1293 wxString(const_iterator first, const_iterator last)
1294 : wxStringBase(first, last) { }
1295 #endif
1296
1297 // lib.string.modifiers
1298 // append elements str[pos], ..., str[pos+n]
1299 wxString& append(const wxString& str, size_t pos, size_t n)
1300 { return (wxString&)wxStringBase::append(str, pos, n); }
1301 // append a string
1302 wxString& append(const wxString& str)
1303 { return (wxString&)wxStringBase::append(str); }
1304 // append first n (or all if n == npos) characters of sz
1305 wxString& append(const wxChar *sz)
1306 { return (wxString&)wxStringBase::append(sz); }
1307 wxString& append(const wxChar *sz, size_t n)
1308 { return (wxString&)wxStringBase::append(sz, n); }
1309 // append n copies of ch
1310 wxString& append(size_t n, wxChar ch)
1311 { return (wxString&)wxStringBase::append(n, ch); }
1312 // append from first to last
1313 wxString& append(const_iterator first, const_iterator last)
1314 { return (wxString&)wxStringBase::append(first, last); }
1315
1316 // same as `this_string = str'
1317 wxString& assign(const wxString& str)
1318 { return (wxString&)wxStringBase::assign(str); }
1319 // same as ` = str[pos..pos + n]
1320 wxString& assign(const wxString& str, size_t pos, size_t n)
1321 { return (wxString&)wxStringBase::assign(str, pos, n); }
1322 // same as `= first n (or all if n == npos) characters of sz'
1323 wxString& assign(const wxChar *sz)
1324 { return (wxString&)wxStringBase::assign(sz); }
1325 wxString& assign(const wxChar *sz, size_t n)
1326 { return (wxString&)wxStringBase::assign(sz, n); }
1327 // same as `= n copies of ch'
1328 wxString& assign(size_t n, wxChar ch)
1329 { return (wxString&)wxStringBase::assign(n, ch); }
1330 // assign from first to last
1331 wxString& assign(const_iterator first, const_iterator last)
1332 { return (wxString&)wxStringBase::assign(first, last); }
1333
1334 // string comparison
1335 #if !defined(HAVE_STD_STRING_COMPARE)
1336 int compare(const wxStringBase& str) const;
1337 // comparison with a substring
1338 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
1339 // comparison of 2 substrings
1340 int compare(size_t nStart, size_t nLen,
1341 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
1342 // just like strcmp()
1343 int compare(const wxChar* sz) const;
1344 // substring comparison with first nCount characters of sz
1345 int compare(size_t nStart, size_t nLen,
1346 const wxChar* sz, size_t nCount = npos) const;
1347 #endif // !defined HAVE_STD_STRING_COMPARE
1348
1349 // insert another string
1350 wxString& insert(size_t nPos, const wxString& str)
1351 { return (wxString&)wxStringBase::insert(nPos, str); }
1352 // insert n chars of str starting at nStart (in str)
1353 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1354 { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); }
1355 // insert first n (or all if n == npos) characters of sz
1356 wxString& insert(size_t nPos, const wxChar *sz)
1357 { return (wxString&)wxStringBase::insert(nPos, sz); }
1358 wxString& insert(size_t nPos, const wxChar *sz, size_t n)
1359 { return (wxString&)wxStringBase::insert(nPos, sz, n); }
1360 // insert n copies of ch
1361 wxString& insert(size_t nPos, size_t n, wxChar ch)
1362 { return (wxString&)wxStringBase::insert(nPos, n, ch); }
1363 iterator insert(iterator it, wxChar ch)
1364 { return wxStringBase::insert(it, ch); }
1365 void insert(iterator it, const_iterator first, const_iterator last)
1366 { wxStringBase::insert(it, first, last); }
1367 void insert(iterator it, size_type n, wxChar ch)
1368 { wxStringBase::insert(it, n, ch); }
1369
1370 // delete characters from nStart to nStart + nLen
1371 wxString& erase(size_type pos = 0, size_type n = npos)
1372 { return (wxString&)wxStringBase::erase(pos, n); }
1373 iterator erase(iterator first, iterator last)
1374 { return wxStringBase::erase(first, last); }
1375 iterator erase(iterator first)
1376 { return wxStringBase::erase(first); }
1377
1378 #ifdef wxSTRING_BASE_HASNT_CLEAR
1379 void clear() { erase(); }
1380 #endif
1381
1382 // replaces the substring of length nLen starting at nStart
1383 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz)
1384 { return (wxString&)wxStringBase::replace(nStart, nLen, sz); }
1385 // replaces the substring of length nLen starting at nStart
1386 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1387 { return (wxString&)wxStringBase::replace(nStart, nLen, str); }
1388 // replaces the substring with nCount copies of ch
1389 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch)
1390 { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); }
1391 // replaces a substring with another substring
1392 wxString& replace(size_t nStart, size_t nLen,
1393 const wxString& str, size_t nStart2, size_t nLen2)
1394 { return (wxString&)wxStringBase::replace(nStart, nLen, str,
1395 nStart2, nLen2); }
1396 // replaces the substring with first nCount chars of sz
1397 wxString& replace(size_t nStart, size_t nLen,
1398 const wxChar* sz, size_t nCount)
1399 { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); }
1400 wxString& replace(iterator first, iterator last, const_pointer s)
1401 { return (wxString&)wxStringBase::replace(first, last, s); }
1402 wxString& replace(iterator first, iterator last, const_pointer s,
1403 size_type n)
1404 { return (wxString&)wxStringBase::replace(first, last, s, n); }
1405 wxString& replace(iterator first, iterator last, const wxString& s)
1406 { return (wxString&)wxStringBase::replace(first, last, s); }
1407 wxString& replace(iterator first, iterator last, size_type n, wxChar c)
1408 { return (wxString&)wxStringBase::replace(first, last, n, c); }
1409 wxString& replace(iterator first, iterator last,
1410 const_iterator first1, const_iterator last1)
1411 { return (wxString&)wxStringBase::replace(first, last, first1, last1); }
1412
1413 // string += string
1414 wxString& operator+=(const wxString& s)
1415 { return (wxString&)wxStringBase::operator+=(s); }
1416 // string += C string
1417 wxString& operator+=(const wxChar *psz)
1418 { return (wxString&)wxStringBase::operator+=(psz); }
1419 // string += char
1420 wxString& operator+=(wxChar ch)
1421 { return (wxString&)wxStringBase::operator+=(ch); }
1422 };
1423
1424 // notice that even though for many compilers the friend declarations above are
1425 // enough, from the point of view of C++ standard we must have the declarations
1426 // here as friend ones are not injected in the enclosing namespace and without
1427 // them the code fails to compile with conforming compilers such as xlC or g++4
1428 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
1429 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
1430 wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
1431 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
1432 wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
1433
1434
1435 // define wxArrayString, for compatibility
1436 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1437 #include "wx/arrstr.h"
1438 #endif
1439
1440 #if wxUSE_STL
1441 // return an empty wxString (not very useful with wxUSE_STL == 1)
1442 inline const wxString wxGetEmptyString() { return wxString(); }
1443 #else // !wxUSE_STL
1444 // return an empty wxString (more efficient than wxString() here)
1445 inline const wxString& wxGetEmptyString()
1446 {
1447 return *(wxString *)&wxEmptyString;
1448 }
1449 #endif // wxUSE_STL/!wxUSE_STL
1450
1451 // ----------------------------------------------------------------------------
1452 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1453 // ----------------------------------------------------------------------------
1454
1455 #if wxUSE_STL
1456
1457 class WXDLLIMPEXP_BASE wxStringBuffer
1458 {
1459 public:
1460 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1461 : m_str(str), m_buf(lenWanted)
1462 { }
1463
1464 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
1465
1466 operator wxChar*() { return m_buf.data(); }
1467
1468 private:
1469 wxString& m_str;
1470 #if wxUSE_UNICODE
1471 wxWCharBuffer m_buf;
1472 #else
1473 wxCharBuffer m_buf;
1474 #endif
1475
1476 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1477 };
1478
1479 class WXDLLIMPEXP_BASE wxStringBufferLength
1480 {
1481 public:
1482 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1483 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
1484 { }
1485
1486 ~wxStringBufferLength()
1487 {
1488 wxASSERT(m_lenSet);
1489 m_str.assign(m_buf.data(), m_len);
1490 }
1491
1492 operator wxChar*() { return m_buf.data(); }
1493 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1494
1495 private:
1496 wxString& m_str;
1497 #if wxUSE_UNICODE
1498 wxWCharBuffer m_buf;
1499 #else
1500 wxCharBuffer m_buf;
1501 #endif
1502 size_t m_len;
1503 bool m_lenSet;
1504
1505 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1506 };
1507
1508 #else // if !wxUSE_STL
1509
1510 class WXDLLIMPEXP_BASE wxStringBuffer
1511 {
1512 public:
1513 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1514 : m_str(str), m_buf(NULL)
1515 { m_buf = m_str.GetWriteBuf(lenWanted); }
1516
1517 ~wxStringBuffer() { m_str.UngetWriteBuf(); }
1518
1519 operator wxChar*() const { return m_buf; }
1520
1521 private:
1522 wxString& m_str;
1523 wxChar *m_buf;
1524
1525 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1526 };
1527
1528 class WXDLLIMPEXP_BASE wxStringBufferLength
1529 {
1530 public:
1531 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1532 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
1533 {
1534 m_buf = m_str.GetWriteBuf(lenWanted);
1535 wxASSERT(m_buf != NULL);
1536 }
1537
1538 ~wxStringBufferLength()
1539 {
1540 wxASSERT(m_lenSet);
1541 m_str.UngetWriteBuf(m_len);
1542 }
1543
1544 operator wxChar*() const { return m_buf; }
1545 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1546
1547 private:
1548 wxString& m_str;
1549 wxChar *m_buf;
1550 size_t m_len;
1551 bool m_lenSet;
1552
1553 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1554 };
1555
1556 #endif // !wxUSE_STL
1557
1558 // ---------------------------------------------------------------------------
1559 // wxString comparison functions: operator versions are always case sensitive
1560 // ---------------------------------------------------------------------------
1561
1562 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1563 // are used and defining them also for wxString would only result in
1564 // compilation ambiguities when comparing std::string and wxString
1565 #if !wxUSE_STL
1566
1567 inline bool operator==(const wxString& s1, const wxString& s2)
1568 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
1569 inline bool operator==(const wxString& s1, const wxChar * s2)
1570 { return s1.Cmp(s2) == 0; }
1571 inline bool operator==(const wxChar * s1, const wxString& s2)
1572 { return s2.Cmp(s1) == 0; }
1573 inline bool operator!=(const wxString& s1, const wxString& s2)
1574 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
1575 inline bool operator!=(const wxString& s1, const wxChar * s2)
1576 { return s1.Cmp(s2) != 0; }
1577 inline bool operator!=(const wxChar * s1, const wxString& s2)
1578 { return s2.Cmp(s1) != 0; }
1579 inline bool operator< (const wxString& s1, const wxString& s2)
1580 { return s1.Cmp(s2) < 0; }
1581 inline bool operator< (const wxString& s1, const wxChar * s2)
1582 { return s1.Cmp(s2) < 0; }
1583 inline bool operator< (const wxChar * s1, const wxString& s2)
1584 { return s2.Cmp(s1) > 0; }
1585 inline bool operator> (const wxString& s1, const wxString& s2)
1586 { return s1.Cmp(s2) > 0; }
1587 inline bool operator> (const wxString& s1, const wxChar * s2)
1588 { return s1.Cmp(s2) > 0; }
1589 inline bool operator> (const wxChar * s1, const wxString& s2)
1590 { return s2.Cmp(s1) < 0; }
1591 inline bool operator<=(const wxString& s1, const wxString& s2)
1592 { return s1.Cmp(s2) <= 0; }
1593 inline bool operator<=(const wxString& s1, const wxChar * s2)
1594 { return s1.Cmp(s2) <= 0; }
1595 inline bool operator<=(const wxChar * s1, const wxString& s2)
1596 { return s2.Cmp(s1) >= 0; }
1597 inline bool operator>=(const wxString& s1, const wxString& s2)
1598 { return s1.Cmp(s2) >= 0; }
1599 inline bool operator>=(const wxString& s1, const wxChar * s2)
1600 { return s1.Cmp(s2) >= 0; }
1601 inline bool operator>=(const wxChar * s1, const wxString& s2)
1602 { return s2.Cmp(s1) <= 0; }
1603
1604 #if wxUSE_UNICODE
1605 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
1606 { return (s1.Cmp((const wchar_t *)s2) == 0); }
1607 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
1608 { return (s2.Cmp((const wchar_t *)s1) == 0); }
1609 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
1610 { return (s1.Cmp((const wchar_t *)s2) != 0); }
1611 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
1612 { return (s2.Cmp((const wchar_t *)s1) != 0); }
1613 #else // !wxUSE_UNICODE
1614 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
1615 { return (s1.Cmp((const char *)s2) == 0); }
1616 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
1617 { return (s2.Cmp((const char *)s1) == 0); }
1618 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
1619 { return (s1.Cmp((const char *)s2) != 0); }
1620 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
1621 { return (s2.Cmp((const char *)s1) != 0); }
1622 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1623
1624 #if wxUSE_UNICODE
1625 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
1626 { return string + (const wchar_t *)buf; }
1627 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
1628 { return (const wchar_t *)buf + string; }
1629 #else // !wxUSE_UNICODE
1630 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
1631 { return string + (const char *)buf; }
1632 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
1633 { return (const char *)buf + string; }
1634 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1635
1636 #endif // !wxUSE_STL
1637
1638 // comparison with char (those are not defined by std::[w]string and so should
1639 // be always available)
1640 inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); }
1641 inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); }
1642 inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); }
1643 inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); }
1644
1645 // ---------------------------------------------------------------------------
1646 // Implementation only from here until the end of file
1647 // ---------------------------------------------------------------------------
1648
1649 // don't pollute the library user's name space
1650 #undef wxASSERT_VALID_INDEX
1651
1652 #if wxUSE_STD_IOSTREAM
1653
1654 #include "wx/iosfwrap.h"
1655
1656 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
1657
1658 #endif // wxSTD_STRING_COMPATIBILITY
1659
1660 #endif // _WX_WXSTRINGH__

  ViewVC Help
Powered by ViewVC 1.1.22