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