]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
Put OnIdle() back. Without deleting pending objects, frames that have
[wxWidgets.git] / include / wx / string.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 wxWindows 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(__APPLE__)
22 #pragma interface "string.h"
23 #endif
24
25 // ----------------------------------------------------------------------------
26 // conditinal compilation
27 // ----------------------------------------------------------------------------
28
29 // compile the std::string compatibility functions if defined
30 #define wxSTD_STRING_COMPATIBILITY
31
32 // ----------------------------------------------------------------------------
33 // headers
34 // ----------------------------------------------------------------------------
35
36 #include "wx/defs.h" // everybody should include this
37
38 #if defined(__WXMAC__) || defined(__VISAGECPP__)
39 #include <ctype.h>
40 #endif
41
42 #ifdef __EMX__
43 #include <std.h>
44 #endif
45
46 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
47 // problem in VACPP V4 with including stdlib.h multiple times
48 // strconv includes it anyway
49 # include <stdio.h>
50 # include <string.h>
51 # include <stdarg.h>
52 # include <limits.h>
53 #else
54 # include <string.h>
55 # include <stdio.h>
56 # include <stdarg.h>
57 # include <limits.h>
58 # include <stdlib.h>
59 #endif
60
61 #ifdef HAVE_STRINGS_H
62 #include <strings.h> // for strcasecmp()
63 #endif // HAVE_STRINGS_H
64
65 #include "wx/wxchar.h" // for wxChar
66 #include "wx/buffer.h" // for wxCharBuffer
67 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
68
69 // ---------------------------------------------------------------------------
70 // macros
71 // ---------------------------------------------------------------------------
72
73 // casts [unfortunately!] needed to call some broken functions which require
74 // "char *" instead of "const char *"
75 #define WXSTRINGCAST (wxChar *)(const wxChar *)
76 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxMBSTRINGCAST (char *)(const char *)
78 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
79
80 // implementation only
81 #define wxASSERT_VALID_INDEX(i) \
82 wxASSERT_MSG( (size_t)(i) <= Len(), _T("invalid index in wxString") )
83
84 // ----------------------------------------------------------------------------
85 // constants
86 // ----------------------------------------------------------------------------
87
88 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
89 // must define this static for VA or else you get multiply defined symbols everywhere
90 extern const unsigned int wxSTRING_MAXLEN;
91
92 #else
93 // maximum possible length for a string means "take all string" everywhere
94 // (as sizeof(StringData) is unknown here, we substract 100)
95 const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100;
96
97 #endif
98
99 // ----------------------------------------------------------------------------
100 // global data
101 // ----------------------------------------------------------------------------
102
103 // global pointer to empty string
104 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
105
106 // ---------------------------------------------------------------------------
107 // global functions complementing standard C string library replacements for
108 // strlen() and portable strcasecmp()
109 //---------------------------------------------------------------------------
110
111 // Use wxXXX() functions from wxchar.h instead! These functions are for
112 // backwards compatibility only.
113
114 // checks whether the passed in pointer is NULL and if the string is empty
115 inline bool IsEmpty(const char *p) { return (!p || !*p); }
116
117 // safe version of strlen() (returns 0 if passed NULL pointer)
118 inline size_t Strlen(const char *psz)
119 { return psz ? strlen(psz) : 0; }
120
121 // portable strcasecmp/_stricmp
122 inline int Stricmp(const char *psz1, const char *psz2)
123 {
124 #if defined(__VISUALC__) && defined(__WXWINCE__)
125 register char c1, c2;
126 do {
127 c1 = tolower(*psz1++);
128 c2 = tolower(*psz2++);
129 } while ( c1 && (c1 == c2) );
130
131 return c1 - c2;
132 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
133 return _stricmp(psz1, psz2);
134 #elif defined(__SC__)
135 return _stricmp(psz1, psz2);
136 #elif defined(__SALFORDC__)
137 return stricmp(psz1, psz2);
138 #elif defined(__BORLANDC__)
139 return stricmp(psz1, psz2);
140 #elif defined(__WATCOMC__)
141 return stricmp(psz1, psz2);
142 #elif defined(__DJGPP__)
143 return stricmp(psz1, psz2);
144 #elif defined(__EMX__)
145 return stricmp(psz1, psz2);
146 #elif defined(__WXPM__)
147 return stricmp(psz1, psz2);
148 #elif defined(__UNIX__) || defined(__GNUWIN32__)
149 return strcasecmp(psz1, psz2);
150 #elif defined(__MWERKS__) && !defined(__INTEL__)
151 register char c1, c2;
152 do {
153 c1 = tolower(*psz1++);
154 c2 = tolower(*psz2++);
155 } while ( c1 && (c1 == c2) );
156
157 return c1 - c2;
158 #else
159 // almost all compilers/libraries provide this function (unfortunately under
160 // different names), that's why we don't implement our own which will surely
161 // be more efficient than this code (uncomment to use):
162 /*
163 register char c1, c2;
164 do {
165 c1 = tolower(*psz1++);
166 c2 = tolower(*psz2++);
167 } while ( c1 && (c1 == c2) );
168
169 return c1 - c2;
170 */
171
172 #error "Please define string case-insensitive compare for your OS/compiler"
173 #endif // OS/compiler
174 }
175
176 // return an empty wxString
177 class WXDLLIMPEXP_BASE wxString; // not yet defined
178 inline const wxString& wxGetEmptyString() { return *(wxString *)&wxEmptyString; }
179
180 // ---------------------------------------------------------------------------
181 // string data prepended with some housekeeping info (used by wxString class),
182 // is never used directly (but had to be put here to allow inlining)
183 // ---------------------------------------------------------------------------
184
185 struct WXDLLIMPEXP_BASE wxStringData
186 {
187 int nRefs; // reference count
188 size_t nDataLength, // actual string length
189 nAllocLength; // allocated memory size
190
191 // mimics declaration 'wxChar data[nAllocLength]'
192 wxChar* data() const { return (wxChar*)(this + 1); }
193
194 // empty string has a special ref count so it's never deleted
195 bool IsEmpty() const { return (nRefs == -1); }
196 bool IsShared() const { return (nRefs > 1); }
197
198 // lock/unlock
199 void Lock() { if ( !IsEmpty() ) nRefs++; }
200
201 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
202 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
203 __forceinline
204 #endif
205 // VC++ free must take place in same DLL as allocation when using non dll
206 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
207 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
208 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
209 // we must not inline deallocation since allocation is not inlined
210 void Free();
211 #else
212 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
213 #endif
214
215 // if we had taken control over string memory (GetWriteBuf), it's
216 // intentionally put in invalid state
217 void Validate(bool b) { nRefs = (b ? 1 : 0); }
218 bool IsValid() const { return (nRefs != 0); }
219 };
220
221 // ---------------------------------------------------------------------------
222 // This is (yet another one) String class for C++ programmers. It doesn't use
223 // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
224 // thus you should be able to compile it with practicaly any C++ compiler.
225 // This class uses copy-on-write technique, i.e. identical strings share the
226 // same memory as long as neither of them is changed.
227 //
228 // This class aims to be as compatible as possible with the new standard
229 // std::string class, but adds some additional functions and should be at
230 // least as efficient than the standard implementation.
231 //
232 // Performance note: it's more efficient to write functions which take "const
233 // String&" arguments than "const char *" if you assign the argument to
234 // another string.
235 //
236 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
237 //
238 // To do:
239 // - ressource support (string tables in ressources)
240 // - more wide character (UNICODE) support
241 // - regular expressions support
242 // ---------------------------------------------------------------------------
243
244 class WXDLLIMPEXP_BASE wxString
245 {
246 #if !wxUSE_STL
247 friend class WXDLLIMPEXP_BASE wxArrayString;
248 #endif
249
250 // NB: special care was taken in arranging the member functions in such order
251 // that all inline functions can be effectively inlined, verify that all
252 // performace critical functions are still inlined if you change order!
253 private:
254 // points to data preceded by wxStringData structure with ref count info
255 wxChar *m_pchData;
256
257 // accessor to string data
258 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
259
260 // string (re)initialization functions
261 // initializes the string to the empty value (must be called only from
262 // ctors, use Reinit() otherwise)
263 void Init() { m_pchData = (wxChar *)wxEmptyString; }
264 // initializaes the string with (a part of) C-string
265 void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = wxSTRING_MAXLEN);
266 // as Init, but also frees old data
267 void Reinit() { GetStringData()->Unlock(); Init(); }
268
269 // memory allocation
270 // allocates memory for string of length nLen
271 bool AllocBuffer(size_t nLen);
272 // copies data to another string
273 bool AllocCopy(wxString&, int, int) const;
274 // effectively copies data to string
275 bool AssignCopy(size_t, const wxChar *);
276
277 // append a (sub)string
278 bool ConcatSelf(size_t nLen, const wxChar *src);
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 // if we hadn't made these operators private, it would be possible to
286 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
287 // converted to char in C and we do have operator=(char)
288 //
289 // NB: we don't need other versions (short/long and unsigned) as attempt
290 // to assign another numeric type to wxString will now result in
291 // ambiguity between operator=(char) and operator=(int)
292 wxString& operator=(int);
293
294 // these methods are not implemented - there is _no_ conversion from int to
295 // string, you're doing something wrong if the compiler wants to call it!
296 //
297 // try `s << i' or `s.Printf("%d", i)' instead
298 wxString(int);
299
300 public:
301 // constructors and destructor
302 // ctor for an empty string
303 wxString() : m_pchData(NULL) { Init(); }
304 // copy ctor
305 wxString(const wxString& stringSrc) : m_pchData(NULL)
306 {
307 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
308 _T("did you forget to call UngetWriteBuf()?") );
309
310 if ( stringSrc.IsEmpty() ) {
311 // nothing to do for an empty string
312 Init();
313 }
314 else {
315 m_pchData = stringSrc.m_pchData; // share same data
316 GetStringData()->Lock(); // => one more copy
317 }
318 }
319 // string containing nRepeat copies of ch
320 wxString(wxChar ch, size_t nRepeat = 1);
321 // ctor takes first nLength characters from C string
322 // (default value of wxSTRING_MAXLEN means take all the string)
323 wxString(const wxChar *psz, size_t nLength = wxSTRING_MAXLEN)
324 : m_pchData(NULL)
325 { InitWith(psz, 0, nLength); }
326 wxString(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = wxSTRING_MAXLEN)
327 : m_pchData(NULL)
328 { InitWith(psz, 0, nLength); }
329
330 #if wxUSE_UNICODE
331 // from multibyte string
332 // (NB: nLength is right now number of Unicode characters, not
333 // characters in psz! So try not to use it yet!)
334 wxString(const char *psz, wxMBConv& conv, size_t nLength = wxSTRING_MAXLEN);
335 // from wxWCharBuffer (i.e. return from wxGetString)
336 wxString(const wxWCharBuffer& psz)
337 { InitWith(psz, 0, wxSTRING_MAXLEN); }
338 #else // ANSI
339 // from C string (for compilers using unsigned char)
340 wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN)
341 : m_pchData(NULL)
342 { InitWith((const char*)psz, 0, nLength); }
343
344 #if wxUSE_WCHAR_T
345 // from wide (Unicode) string
346 wxString(const wchar_t *pwz, wxMBConv& conv = wxConvLibc, size_t nLength = wxSTRING_MAXLEN);
347 #endif // !wxUSE_WCHAR_T
348
349 // from wxCharBuffer
350 wxString(const wxCharBuffer& psz)
351 : m_pchData(NULL)
352 { InitWith(psz, 0, wxSTRING_MAXLEN); }
353 #endif // Unicode/ANSI
354
355 // dtor is not virtual, this class must not be inherited from!
356 ~wxString() { GetStringData()->Unlock(); }
357
358 // generic attributes & operations
359 // as standard strlen()
360 size_t Len() const { return GetStringData()->nDataLength; }
361 // string contains any characters?
362 bool IsEmpty() const { return Len() == 0; }
363 // empty string is "FALSE", so !str will return TRUE
364 bool operator!() const { return IsEmpty(); }
365 // truncate the string to given length
366 wxString& Truncate(size_t uiLen);
367 // empty string contents
368 void Empty()
369 {
370 Truncate(0);
371
372 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
373 }
374 // empty the string and free memory
375 void Clear()
376 {
377 if ( !GetStringData()->IsEmpty() )
378 Reinit();
379
380 wxASSERT_MSG( !GetStringData()->nDataLength &&
381 !GetStringData()->nAllocLength,
382 _T("string should be empty after Clear()") );
383 }
384
385 // contents test
386 // Is an ascii value
387 bool IsAscii() const;
388 // Is a number
389 bool IsNumber() const;
390 // Is a word
391 bool IsWord() const;
392
393 // data access (all indexes are 0 based)
394 // read access
395 wxChar GetChar(size_t n) const
396 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
397 // read/write access
398 wxChar& GetWritableChar(size_t n)
399 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
400 // write access
401 void SetChar(size_t n, wxChar ch)
402 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; }
403
404 // get last character
405 wxChar Last() const
406 {
407 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
408
409 return m_pchData[Len() - 1];
410 }
411
412 // get writable last character
413 wxChar& Last()
414 {
415 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
416 CopyBeforeWrite();
417 return m_pchData[Len()-1];
418 }
419
420 /*
421 So why do we have all these overloaded operator[]s? A bit of history:
422 initially there was only one of them, taking size_t. Then people
423 started complaining because they wanted to use ints as indices (I
424 wonder why) and compilers were giving warnings about it, so we had to
425 add the operator[](int). Then it became apparent that you couldn't
426 write str[0] any longer because there was ambiguity between two
427 overloads and so you now had to write str[0u] (or, of course, use the
428 explicit casts to either int or size_t but nobody did this).
429
430 Finally, someone decided to compile wxWin on an Alpha machine and got
431 a surprize: str[0u] didn't compile there because it is of type
432 unsigned int and size_t is unsigned _long_ on Alpha and so there was
433 ambiguity between converting uint to int or ulong. To fix this one we
434 now add operator[](uint) for the machines where size_t is not already
435 the same as unsigned int - hopefully this fixes the problem (for some
436 time)
437
438 The only real fix is, of course, to remove all versions but the one
439 taking size_t...
440 */
441
442 // operator version of GetChar
443 wxChar operator[](size_t n) const
444 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
445
446 // operator version of GetChar
447 wxChar operator[](int n) const
448 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
449
450 // operator version of GetWriteableChar
451 wxChar& operator[](size_t n)
452 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
453
454 #ifndef wxSIZE_T_IS_UINT
455 // operator version of GetChar
456 wxChar operator[](unsigned int n) const
457 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
458
459 // operator version of GetWriteableChar
460 wxChar& operator[](unsigned int n)
461 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
462 #endif // size_t != unsigned int
463
464 // implicit conversion to C string
465 operator const wxChar*() const { return m_pchData; }
466
467 // explicit conversion to C string (use this with printf()!)
468 const wxChar* c_str() const { return m_pchData; }
469 // identical to c_str(), for wxWin 1.6x compatibility
470 const wxChar* wx_str() const { return m_pchData; }
471 // identical to c_str(), for MFC compatibility
472 const wxChar* GetData() const { return m_pchData; }
473
474 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
475 // converting numbers or strings which are certain not to contain special
476 // chars (typically system functions, X atoms, environment variables etc.)
477 //
478 // the behaviour of these functions with the strings containing anything
479 // else than 7 bit ASCII characters is undefined, use at your own risk.
480 #if wxUSE_UNICODE
481 static wxString FromAscii(const char *ascii); // string
482 static wxString FromAscii(const char ascii); // char
483 const wxCharBuffer ToAscii() const;
484 #else // ANSI
485 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
486 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
487 const char *ToAscii() const { return c_str(); }
488 #endif // Unicode/!Unicode
489
490 // conversions with (possible) format conversions: have to return a
491 // buffer with temporary data
492 //
493 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
494 // return an ANSI (multibyte) string, wc_str() to return a wide string and
495 // fn_str() to return a string which should be used with the OS APIs
496 // accepting the file names. The return value is always the same, but the
497 // type differs because a function may either return pointer to the buffer
498 // directly or have to use intermediate buffer for translation.
499 #if wxUSE_UNICODE
500 const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const
501 { return conv.cWC2MB(m_pchData); }
502
503 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
504
505 const wxChar* wc_str() const { return m_pchData; }
506
507 // for compatibility with !wxUSE_UNICODE version
508 const wxChar* wc_str(wxMBConv& WXUNUSED(conv)) const { return m_pchData; }
509
510 #if wxMBFILES
511 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
512 #else // !wxMBFILES
513 const wxChar* fn_str() const { return m_pchData; }
514 #endif // wxMBFILES/!wxMBFILES
515 #else // ANSI
516 const wxChar* mb_str() const { return m_pchData; }
517
518 // for compatibility with wxUSE_UNICODE version
519 const wxChar* mb_str(wxMBConv& WXUNUSED(conv)) const { return m_pchData; }
520
521 const wxWX2MBbuf mbc_str() const { return mb_str(); }
522
523 #if wxUSE_WCHAR_T
524 const wxWCharBuffer wc_str(wxMBConv& conv) const
525 { return conv.cMB2WC(m_pchData); }
526 #endif // wxUSE_WCHAR_T
527
528 const wxChar* fn_str() const { return m_pchData; }
529 #endif // Unicode/ANSI
530
531 // overloaded assignment
532 // from another wxString
533 wxString& operator=(const wxString& stringSrc);
534 // from a character
535 wxString& operator=(wxChar ch);
536 // from a C string
537 wxString& operator=(const wxChar *psz);
538 #if wxUSE_UNICODE
539 // from wxWCharBuffer
540 wxString& operator=(const wxWCharBuffer& psz)
541 { (void) operator=((const wchar_t *)psz); return *this; }
542 #else // ANSI
543 // from another kind of C string
544 wxString& operator=(const unsigned char* psz);
545 #if wxUSE_WCHAR_T
546 // from a wide string
547 wxString& operator=(const wchar_t *pwz);
548 #endif
549 // from wxCharBuffer
550 wxString& operator=(const wxCharBuffer& psz)
551 { (void) operator=((const char *)psz); return *this; }
552 #endif // Unicode/ANSI
553
554 // string concatenation
555 // in place concatenation
556 /*
557 Concatenate and return the result. Note that the left to right
558 associativity of << allows to write things like "str << str1 << str2
559 << ..." (unlike with +=)
560 */
561 // string += string
562 wxString& operator<<(const wxString& s)
563 {
564 wxASSERT_MSG( s.GetStringData()->IsValid(),
565 _T("did you forget to call UngetWriteBuf()?") );
566
567 ConcatSelf(s.Len(), s);
568 return *this;
569 }
570 // string += C string
571 wxString& operator<<(const wxChar *psz)
572 { ConcatSelf(wxStrlen(psz), psz); return *this; }
573 // string += char
574 wxString& operator<<(wxChar ch) { ConcatSelf(1, &ch); return *this; }
575
576 // string += string
577 void operator+=(const wxString& s) { (void)operator<<(s); }
578 // string += C string
579 void operator+=(const wxChar *psz) { (void)operator<<(psz); }
580 // string += char
581 void operator+=(wxChar ch) { (void)operator<<(ch); }
582
583 // string += buffer (i.e. from wxGetString)
584 #if wxUSE_UNICODE
585 wxString& operator<<(const wxWCharBuffer& s)
586 { (void)operator<<((const wchar_t *)s); return *this; }
587 void operator+=(const wxWCharBuffer& s)
588 { (void)operator<<((const wchar_t *)s); }
589 #else // !wxUSE_UNICODE
590 wxString& operator<<(const wxCharBuffer& s)
591 { (void)operator<<((const char *)s); return *this; }
592 void operator+=(const wxCharBuffer& s)
593 { (void)operator<<((const char *)s); }
594 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
595
596 // string += C string
597 wxString& Append(const wxString& s)
598 {
599 // test for IsEmpty() to share the string if possible
600 if ( IsEmpty() )
601 *this = s;
602 else
603 ConcatSelf(s.Length(), s.c_str());
604 return *this;
605 }
606 wxString& Append(const wxChar* psz)
607 { ConcatSelf(wxStrlen(psz), psz); return *this; }
608 // append count copies of given character
609 wxString& Append(wxChar ch, size_t count = 1u)
610 { wxString str(ch, count); return *this << str; }
611 wxString& Append(const wxChar* psz, size_t nLen)
612 { ConcatSelf(nLen, psz); return *this; }
613
614 // prepend a string, return the string itself
615 wxString& Prepend(const wxString& str)
616 { *this = str + *this; return *this; }
617
618 // non-destructive concatenation
619 //
620 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
621 //
622 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
623 //
624 friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
625 //
626 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
627 //
628 friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
629
630 // stream-like functions
631 // insert an int into string
632 wxString& operator<<(int i)
633 { return (*this) << Format(_T("%d"), i); }
634 // insert an unsigned int into string
635 wxString& operator<<(unsigned int ui)
636 { return (*this) << Format(_T("%u"), ui); }
637 // insert a long into string
638 wxString& operator<<(long l)
639 { return (*this) << Format(_T("%ld"), l); }
640 // insert an unsigned long into string
641 wxString& operator<<(unsigned long ul)
642 { return (*this) << Format(_T("%lu"), ul); }
643 // insert a float into string
644 wxString& operator<<(float f)
645 { return (*this) << Format(_T("%f"), f); }
646 // insert a double into string
647 wxString& operator<<(double d)
648 { return (*this) << Format(_T("%g"), d); }
649
650 // string comparison
651 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
652 int Cmp(const wxChar *psz) const { return wxStrcmp(c_str(), psz); }
653 // same as Cmp() but not case-sensitive
654 int CmpNoCase(const wxChar *psz) const { return wxStricmp(c_str(), psz); }
655 // test for the string equality, either considering case or not
656 // (if compareWithCase then the case matters)
657 bool IsSameAs(const wxChar *psz, bool compareWithCase = TRUE) const
658 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
659 // comparison with a signle character: returns TRUE if equal
660 bool IsSameAs(wxChar c, bool compareWithCase = TRUE) const
661 {
662 return (Len() == 1) && (compareWithCase ? GetChar(0u) == c
663 : wxToupper(GetChar(0u)) == wxToupper(c));
664 }
665
666 // simple sub-string extraction
667 // return substring starting at nFirst of length nCount (or till the end
668 // if nCount = default value)
669 wxString Mid(size_t nFirst, size_t nCount = wxSTRING_MAXLEN) const;
670
671 // operator version of Mid()
672 wxString operator()(size_t start, size_t len) const
673 { return Mid(start, len); }
674
675 // check that the string starts with prefix and return the rest of the
676 // string in the provided pointer if it is not NULL, otherwise return
677 // FALSE
678 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
679
680 // get first nCount characters
681 wxString Left(size_t nCount) const;
682 // get last nCount characters
683 wxString Right(size_t nCount) const;
684 // get all characters before the first occurance of ch
685 // (returns the whole string if ch not found)
686 wxString BeforeFirst(wxChar ch) const;
687 // get all characters before the last occurence of ch
688 // (returns empty string if ch not found)
689 wxString BeforeLast(wxChar ch) const;
690 // get all characters after the first occurence of ch
691 // (returns empty string if ch not found)
692 wxString AfterFirst(wxChar ch) const;
693 // get all characters after the last occurence of ch
694 // (returns the whole string if ch not found)
695 wxString AfterLast(wxChar ch) const;
696
697 // for compatibility only, use more explicitly named functions above
698 wxString Before(wxChar ch) const { return BeforeLast(ch); }
699 wxString After(wxChar ch) const { return AfterFirst(ch); }
700
701 // case conversion
702 // convert to upper case in place, return the string itself
703 wxString& MakeUpper();
704 // convert to upper case, return the copy of the string
705 // Here's something to remember: BC++ doesn't like returns in inlines.
706 wxString Upper() const ;
707 // convert to lower case in place, return the string itself
708 wxString& MakeLower();
709 // convert to lower case, return the copy of the string
710 wxString Lower() const ;
711
712 // trimming/padding whitespace (either side) and truncating
713 // remove spaces from left or from right (default) side
714 wxString& Trim(bool bFromRight = TRUE);
715 // add nCount copies chPad in the beginning or at the end (default)
716 wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = TRUE);
717
718 // searching and replacing
719 // searching (return starting index, or -1 if not found)
720 int Find(wxChar ch, bool bFromEnd = FALSE) const; // like strchr/strrchr
721 // searching (return starting index, or -1 if not found)
722 int Find(const wxChar *pszSub) const; // like strstr
723 // replace first (or all of bReplaceAll) occurences of substring with
724 // another string, returns the number of replacements made
725 size_t Replace(const wxChar *szOld,
726 const wxChar *szNew,
727 bool bReplaceAll = TRUE);
728
729 // check if the string contents matches a mask containing '*' and '?'
730 bool Matches(const wxChar *szMask) const;
731
732 // conversion to numbers: all functions return TRUE only if the whole
733 // string is a number and put the value of this number into the pointer
734 // provided, the base is the numeric base in which the conversion should be
735 // done and must be comprised between 2 and 36 or be 0 in which case the
736 // standard C rules apply (leading '0' => octal, "0x" => hex)
737 // convert to a signed integer
738 bool ToLong(long *val, int base = 10) const;
739 // convert to an unsigned integer
740 bool ToULong(unsigned long *val, int base = 10) const;
741 // convert to a double
742 bool ToDouble(double *val) const;
743
744 // formated input/output
745 // as sprintf(), returns the number of characters written or < 0 on error
746 // (take 'this' into account in attribute parameter count)
747 int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
748 // as vprintf(), returns the number of characters written or < 0 on error
749 int PrintfV(const wxChar* pszFormat, va_list argptr);
750
751 // returns the string containing the result of Printf() to it
752 static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
753 // the same as above, but takes a va_list
754 static wxString FormatV(const wxChar *pszFormat, va_list argptr);
755
756 // raw access to string memory
757 // ensure that string has space for at least nLen characters
758 // only works if the data of this string is not shared
759 bool Alloc(size_t nLen);
760 // minimize the string's memory
761 // only works if the data of this string is not shared
762 bool Shrink();
763 // get writable buffer of at least nLen bytes. Unget() *must* be called
764 // a.s.a.p. to put string back in a reasonable state!
765 wxChar *GetWriteBuf(size_t nLen);
766 // call this immediately after GetWriteBuf() has been used
767 void UngetWriteBuf();
768 void UngetWriteBuf(size_t nLen);
769
770 // wxWindows version 1 compatibility functions
771
772 // use Mid()
773 wxString SubString(size_t from, size_t to) const
774 { return Mid(from, (to - from + 1)); }
775 // values for second parameter of CompareTo function
776 enum caseCompare {exact, ignoreCase};
777 // values for first parameter of Strip function
778 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
779
780 // use Printf()
781 // (take 'this' into account in attribute parameter count)
782 int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
783
784 // use Cmp()
785 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
786 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
787
788 // use Len
789 size_t Length() const { return Len(); }
790 // Count the number of characters
791 int Freq(wxChar ch) const;
792 // use MakeLower
793 void LowerCase() { MakeLower(); }
794 // use MakeUpper
795 void UpperCase() { MakeUpper(); }
796 // use Trim except that it doesn't change this string
797 wxString Strip(stripType w = trailing) const;
798
799 // use Find (more general variants not yet supported)
800 size_t Index(const wxChar* psz) const { return Find(psz); }
801 size_t Index(wxChar ch) const { return Find(ch); }
802 // use Truncate
803 wxString& Remove(size_t pos) { return Truncate(pos); }
804 wxString& RemoveLast(size_t n = 1) { return Truncate(Len() - n); }
805
806 wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); }
807
808 // use Find()
809 int First( const wxChar ch ) const { return Find(ch); }
810 int First( const wxChar* psz ) const { return Find(psz); }
811 int First( const wxString &str ) const { return Find(str); }
812 int Last( const wxChar ch ) const { return Find(ch, TRUE); }
813 bool Contains(const wxString& str) const { return Find(str) != -1; }
814
815 // use IsEmpty()
816 bool IsNull() const { return IsEmpty(); }
817
818 #ifdef wxSTD_STRING_COMPATIBILITY
819 // std::string compatibility functions
820
821 // standard types
822 typedef wxChar value_type;
823 typedef size_t size_type;
824 typedef value_type *iterator;
825 typedef const value_type *const_iterator;
826
827 // an 'invalid' value for string index
828 static const size_t npos;
829
830 // constructors
831 // take nLen chars starting at nPos
832 wxString(const wxString& str, size_t nPos, size_t nLen)
833 : m_pchData(NULL)
834 {
835 wxASSERT_MSG( str.GetStringData()->IsValid(),
836 _T("did you forget to call UngetWriteBuf()?") );
837
838 InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen);
839 }
840 // take all characters from pStart to pEnd
841 wxString(const void *pStart, const void *pEnd);
842
843 // lib.string.capacity
844 // return the length of the string
845 size_t size() const { return Len(); }
846 // return the length of the string
847 size_t length() const { return Len(); }
848 // return the maximum size of the string
849 size_t max_size() const { return wxSTRING_MAXLEN; }
850 // resize the string, filling the space with c if c != 0
851 void resize(size_t nSize, wxChar ch = wxT('\0'));
852 // delete the contents of the string
853 void clear() { Empty(); }
854 // returns true if the string is empty
855 bool empty() const { return IsEmpty(); }
856 // inform string about planned change in size
857 void reserve(size_t sz) { Alloc(sz); }
858
859 // lib.string.access
860 // return the character at position n
861 wxChar at(size_t n) const { return GetChar(n); }
862 // returns the writable character at position n
863 wxChar& at(size_t n) { return GetWritableChar(n); }
864
865 // first valid index position
866 const_iterator begin() const { return wx_str(); }
867 // position one after the last valid one
868 const_iterator end() const { return wx_str() + length(); }
869
870 // first valid index position
871 iterator begin() { CopyBeforeWrite(); return m_pchData; }
872 // position one after the last valid one
873 iterator end() { CopyBeforeWrite(); return m_pchData + length(); }
874
875 // lib.string.modifiers
876 // append a string
877 wxString& append(const wxString& str)
878 { *this += str; return *this; }
879 // append elements str[pos], ..., str[pos+n]
880 wxString& append(const wxString& str, size_t pos, size_t n)
881 { ConcatSelf(n, str.c_str() + pos); return *this; }
882 // append first n (or all if n == npos) characters of sz
883 wxString& append(const wxChar *sz, size_t n = npos)
884 { ConcatSelf(n == npos ? wxStrlen(sz) : n, sz); return *this; }
885
886 // append n copies of ch
887 wxString& append(size_t n, wxChar ch) { return Pad(n, ch); }
888
889 // same as `this_string = str'
890 wxString& assign(const wxString& str)
891 { return *this = str; }
892 // same as ` = str[pos..pos + n]
893 wxString& assign(const wxString& str, size_t pos, size_t n)
894 { Empty(); return Append(str.c_str() + pos, n); }
895 // same as `= first n (or all if n == npos) characters of sz'
896 wxString& assign(const wxChar *sz, size_t n = npos)
897 { Empty(); return Append(sz, n == npos ? wxStrlen(sz) : n); }
898 // same as `= n copies of ch'
899 wxString& assign(size_t n, wxChar ch)
900 { Empty(); return Append(ch, n); }
901
902 // insert another string
903 wxString& insert(size_t nPos, const wxString& str);
904 // insert n chars of str starting at nStart (in str)
905 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
906 { return insert(nPos, wxString((const wxChar *)str + nStart, n)); }
907
908 // insert first n (or all if n == npos) characters of sz
909 wxString& insert(size_t nPos, const wxChar *sz, size_t n = npos)
910 { return insert(nPos, wxString(sz, n)); }
911 // insert n copies of ch
912 wxString& insert(size_t nPos, size_t n, wxChar ch)
913 { return insert(nPos, wxString(ch, n)); }
914
915 // delete characters from nStart to nStart + nLen
916 wxString& erase(size_t nStart = 0, size_t nLen = npos);
917
918 // replaces the substring of length nLen starting at nStart
919 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz);
920 // replaces the substring with nCount copies of ch
921 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch);
922 // replaces a substring with another substring
923 wxString& replace(size_t nStart, size_t nLen,
924 const wxString& str, size_t nStart2, size_t nLen2);
925 // replaces the substring with first nCount chars of sz
926 wxString& replace(size_t nStart, size_t nLen,
927 const wxChar* sz, size_t nCount);
928
929 // swap two strings
930 void swap(wxString& str);
931
932 // All find() functions take the nStart argument which specifies the
933 // position to start the search on, the default value is 0. All functions
934 // return npos if there were no match.
935
936 // find a substring
937 size_t find(const wxString& str, size_t nStart = 0) const;
938
939 // VC++ 1.5 can't cope with this syntax.
940 #if !defined(__VISUALC__) || defined(__WIN32__)
941 // find first n characters of sz
942 size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;
943 #endif // VC++ 1.5
944
945 // Gives a duplicate symbol (presumably a case-insensitivity problem)
946 #if !defined(__BORLANDC__)
947 // find the first occurence of character ch after nStart
948 size_t find(wxChar ch, size_t nStart = 0) const;
949 #endif
950 // rfind() family is exactly like find() but works right to left
951
952 // as find, but from the end
953 size_t rfind(const wxString& str, size_t nStart = npos) const;
954
955 // VC++ 1.5 can't cope with this syntax.
956 #if !defined(__VISUALC__) || defined(__WIN32__)
957 // as find, but from the end
958 size_t rfind(const wxChar* sz, size_t nStart = npos,
959 size_t n = npos) const;
960 // as find, but from the end
961 size_t rfind(wxChar ch, size_t nStart = npos) const;
962 #endif // VC++ 1.5
963
964 // find first/last occurence of any character in the set
965
966 // as strpbrk() but starts at nStart, returns npos if not found
967 size_t find_first_of(const wxString& str, size_t nStart = 0) const
968 { return find_first_of(str.c_str(), nStart); }
969 // same as above
970 size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
971 // same as find(char, size_t)
972 size_t find_first_of(wxChar c, size_t nStart = 0) const
973 { return find(c, nStart); }
974 // find the last (starting from nStart) char from str in this string
975 size_t find_last_of (const wxString& str, size_t nStart = npos) const
976 { return find_last_of(str.c_str(), nStart); }
977 // same as above
978 size_t find_last_of (const wxChar* sz, size_t nStart = npos) const;
979 // same as above
980 size_t find_last_of(wxChar c, size_t nStart = npos) const
981 { return rfind(c, nStart); }
982
983 // find first/last occurence of any character not in the set
984
985 // as strspn() (starting from nStart), returns npos on failure
986 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
987 { return find_first_not_of(str.c_str(), nStart); }
988 // same as above
989 size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const;
990 // same as above
991 size_t find_first_not_of(wxChar ch, size_t nStart = 0) const;
992 // as strcspn()
993 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
994 { return find_first_not_of(str.c_str(), nStart); }
995 // same as above
996 size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const;
997 // same as above
998 size_t find_last_not_of(wxChar ch, size_t nStart = npos) const;
999
1000 // All compare functions return -1, 0 or 1 if the [sub]string is less,
1001 // equal or greater than the compare() argument.
1002
1003 // just like strcmp()
1004 int compare(const wxString& str) const { return Cmp(str); }
1005 // comparison with a substring
1006 int compare(size_t nStart, size_t nLen, const wxString& str) const
1007 { return Mid(nStart, nLen).Cmp(str); }
1008 // comparison of 2 substrings
1009 int compare(size_t nStart, size_t nLen,
1010 const wxString& str, size_t nStart2, size_t nLen2) const
1011 { return Mid(nStart, nLen).Cmp(str.Mid(nStart2, nLen2)); }
1012 // just like strcmp()
1013 int compare(const wxChar* sz) const { return Cmp(sz); }
1014 // substring comparison with first nCount characters of sz
1015 int compare(size_t nStart, size_t nLen,
1016 const wxChar* sz, size_t nCount = npos) const
1017 { return Mid(nStart, nLen).Cmp(wxString(sz, nCount)); }
1018
1019 // substring extraction
1020 wxString substr(size_t nStart = 0, size_t nLen = npos) const
1021 { return Mid(nStart, nLen); }
1022 #endif // wxSTD_STRING_COMPATIBILITY
1023 };
1024
1025 // define wxArrayString, for compatibility
1026 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1027 #include "wx/arrstr.h"
1028 #endif
1029
1030 // ----------------------------------------------------------------------------
1031 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1032 // ----------------------------------------------------------------------------
1033
1034 class WXDLLIMPEXP_BASE wxStringBuffer
1035 {
1036 public:
1037 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1038 : m_str(str), m_buf(NULL)
1039 { m_buf = m_str.GetWriteBuf(lenWanted); }
1040
1041 ~wxStringBuffer() { m_str.UngetWriteBuf(); }
1042
1043 operator wxChar*() const { return m_buf; }
1044
1045 private:
1046 wxString& m_str;
1047 wxChar *m_buf;
1048
1049 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1050 };
1051
1052 // ---------------------------------------------------------------------------
1053 // wxString comparison functions: operator versions are always case sensitive
1054 // ---------------------------------------------------------------------------
1055
1056 inline bool operator==(const wxString& s1, const wxString& s2)
1057 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
1058 inline bool operator==(const wxString& s1, const wxChar * s2)
1059 { return s1.Cmp(s2) == 0; }
1060 inline bool operator==(const wxChar * s1, const wxString& s2)
1061 { return s2.Cmp(s1) == 0; }
1062 inline bool operator!=(const wxString& s1, const wxString& s2)
1063 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
1064 inline bool operator!=(const wxString& s1, const wxChar * s2)
1065 { return s1.Cmp(s2) != 0; }
1066 inline bool operator!=(const wxChar * s1, const wxString& s2)
1067 { return s2.Cmp(s1) != 0; }
1068 inline bool operator< (const wxString& s1, const wxString& s2)
1069 { return s1.Cmp(s2) < 0; }
1070 inline bool operator< (const wxString& s1, const wxChar * s2)
1071 { return s1.Cmp(s2) < 0; }
1072 inline bool operator< (const wxChar * s1, const wxString& s2)
1073 { return s2.Cmp(s1) > 0; }
1074 inline bool operator> (const wxString& s1, const wxString& s2)
1075 { return s1.Cmp(s2) > 0; }
1076 inline bool operator> (const wxString& s1, const wxChar * s2)
1077 { return s1.Cmp(s2) > 0; }
1078 inline bool operator> (const wxChar * s1, const wxString& s2)
1079 { return s2.Cmp(s1) < 0; }
1080 inline bool operator<=(const wxString& s1, const wxString& s2)
1081 { return s1.Cmp(s2) <= 0; }
1082 inline bool operator<=(const wxString& s1, const wxChar * s2)
1083 { return s1.Cmp(s2) <= 0; }
1084 inline bool operator<=(const wxChar * s1, const wxString& s2)
1085 { return s2.Cmp(s1) >= 0; }
1086 inline bool operator>=(const wxString& s1, const wxString& s2)
1087 { return s1.Cmp(s2) >= 0; }
1088 inline bool operator>=(const wxString& s1, const wxChar * s2)
1089 { return s1.Cmp(s2) >= 0; }
1090 inline bool operator>=(const wxChar * s1, const wxString& s2)
1091 { return s2.Cmp(s1) <= 0; }
1092
1093 // comparison with char
1094 inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); }
1095 inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); }
1096 inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); }
1097 inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); }
1098
1099 #if wxUSE_UNICODE
1100 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
1101 { return (s1.Cmp((const wchar_t *)s2) == 0); }
1102 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
1103 { return (s2.Cmp((const wchar_t *)s1) == 0); }
1104 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
1105 { return (s1.Cmp((const wchar_t *)s2) != 0); }
1106 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
1107 { return (s2.Cmp((const wchar_t *)s1) != 0); }
1108 #else // !wxUSE_UNICODE
1109 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
1110 { return (s1.Cmp((const char *)s2) == 0); }
1111 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
1112 { return (s2.Cmp((const char *)s1) == 0); }
1113 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
1114 { return (s1.Cmp((const char *)s2) != 0); }
1115 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
1116 { return (s2.Cmp((const char *)s1) != 0); }
1117 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1118
1119 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
1120 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
1121 wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
1122 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
1123 wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
1124 #if wxUSE_UNICODE
1125 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
1126 { return string + (const wchar_t *)buf; }
1127 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
1128 { return (const wchar_t *)buf + string; }
1129 #else // !wxUSE_UNICODE
1130 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
1131 { return string + (const char *)buf; }
1132 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
1133 { return (const char *)buf + string; }
1134 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1135
1136 // ---------------------------------------------------------------------------
1137 // Implementation only from here until the end of file
1138 // ---------------------------------------------------------------------------
1139
1140 // don't pollute the library user's name space
1141 #undef wxASSERT_VALID_INDEX
1142
1143 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
1144
1145 #include "wx/iosfwrap.h"
1146
1147 WXDLLIMPEXP_BASE wxSTD istream& operator>>(wxSTD istream&, wxString&);
1148 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
1149
1150 #endif // wxSTD_STRING_COMPATIBILITY
1151
1152 #endif // _WX_WXSTRINGH__