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