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