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