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