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