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