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