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