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