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