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