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