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