]>
Commit | Line | Data |
---|---|---|
3c67202d | 1 | /////////////////////////////////////////////////////////////////////////////// |
1c2d1459 | 2 | // Name: wx/string.h |
3c67202d | 3 | // Purpose: wxString and wxArrayString classes |
c801d85f KB |
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> | |
65571936 | 9 | // Licence: wxWindows licence |
3c67202d | 10 | /////////////////////////////////////////////////////////////////////////////// |
c801d85f | 11 | |
e90c1d2a VZ |
12 | /* |
13 | Efficient string class [more or less] compatible with MFC CString, | |
77ffb593 | 14 | wxWidgets version 1 wxString and std::string and some handy functions |
e90c1d2a VZ |
15 | missing from string.h. |
16 | */ | |
17 | ||
34138703 JS |
18 | #ifndef _WX_WXSTRINGH__ |
19 | #define _WX_WXSTRINGH__ | |
c801d85f | 20 | |
e90c1d2a VZ |
21 | // ---------------------------------------------------------------------------- |
22 | // headers | |
23 | // ---------------------------------------------------------------------------- | |
24 | ||
5737d05f VZ |
25 | #include "wx/defs.h" // everybody should include this |
26 | ||
9dea36ef | 27 | #if defined(__WXMAC__) || defined(__VISAGECPP__) |
3f4a0c5b | 28 | #include <ctype.h> |
17dff81c | 29 | #endif |
3f4a0c5b | 30 | |
9dea36ef DW |
31 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
32 | // problem in VACPP V4 with including stdlib.h multiple times | |
33 | // strconv includes it anyway | |
34 | # include <stdio.h> | |
35 | # include <string.h> | |
36 | # include <stdarg.h> | |
37 | # include <limits.h> | |
38 | #else | |
39 | # include <string.h> | |
40 | # include <stdio.h> | |
41 | # include <stdarg.h> | |
42 | # include <limits.h> | |
43 | # include <stdlib.h> | |
44 | #endif | |
c801d85f | 45 | |
1c2d1459 | 46 | #ifdef HAVE_STRCASECMP_IN_STRINGS_H |
1bfcb0b6 | 47 | #include <strings.h> // for strcasecmp() |
1c2d1459 | 48 | #endif // HAVE_STRCASECMP_IN_STRINGS_H |
1bfcb0b6 | 49 | |
4055ed82 | 50 | #ifdef __WXPALMOS__ |
ffecfa5a JS |
51 | #include <StringMgr.h> |
52 | #endif | |
53 | ||
e90c1d2a VZ |
54 | #include "wx/wxchar.h" // for wxChar |
55 | #include "wx/buffer.h" // for wxCharBuffer | |
56 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
3f4a0c5b | 57 | |
fb3c9042 VZ |
58 | class WXDLLIMPEXP_BASE wxString; |
59 | ||
c801d85f KB |
60 | // --------------------------------------------------------------------------- |
61 | // macros | |
62 | // --------------------------------------------------------------------------- | |
63 | ||
5737d05f VZ |
64 | // casts [unfortunately!] needed to call some broken functions which require |
65 | // "char *" instead of "const char *" | |
2bb67b80 | 66 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
e90c1d2a VZ |
67 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) |
68 | #define wxMBSTRINGCAST (char *)(const char *) | |
69 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 70 | |
3c67202d | 71 | // implementation only |
5737d05f | 72 | #define wxASSERT_VALID_INDEX(i) \ |
e87b7833 | 73 | wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") ) |
c801d85f | 74 | |
e90c1d2a VZ |
75 | // ---------------------------------------------------------------------------- |
76 | // constants | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
79 | // maximum possible length for a string means "take all string" everywhere | |
2cbfa061 | 80 | #define wxSTRING_MAXLEN wxStringBase::npos |
66b6b045 | 81 | |
e90c1d2a VZ |
82 | // ---------------------------------------------------------------------------- |
83 | // global data | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | // global pointer to empty string | |
bddd7a8d | 87 | extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString; |
6001e347 | 88 | |
c801d85f | 89 | // --------------------------------------------------------------------------- |
e90c1d2a | 90 | // global functions complementing standard C string library replacements for |
3c67202d VZ |
91 | // strlen() and portable strcasecmp() |
92 | //--------------------------------------------------------------------------- | |
e90c1d2a VZ |
93 | |
94 | // Use wxXXX() functions from wxchar.h instead! These functions are for | |
95 | // backwards compatibility only. | |
88150e60 | 96 | |
3c67202d | 97 | // checks whether the passed in pointer is NULL and if the string is empty |
6d56eb5c | 98 | inline bool IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 99 | |
3c67202d | 100 | // safe version of strlen() (returns 0 if passed NULL pointer) |
6d56eb5c | 101 | inline size_t Strlen(const char *psz) |
c801d85f KB |
102 | { return psz ? strlen(psz) : 0; } |
103 | ||
3c67202d | 104 | // portable strcasecmp/_stricmp |
6d56eb5c | 105 | inline int Stricmp(const char *psz1, const char *psz2) |
dd1eaa89 | 106 | { |
7f0586ef JS |
107 | #if defined(__VISUALC__) && defined(__WXWINCE__) |
108 | register char c1, c2; | |
109 | do { | |
110 | c1 = tolower(*psz1++); | |
111 | c2 = tolower(*psz2++); | |
112 | } while ( c1 && (c1 == c2) ); | |
113 | ||
114 | return c1 - c2; | |
115 | #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) | |
dd1eaa89 | 116 | return _stricmp(psz1, psz2); |
91b8de8d | 117 | #elif defined(__SC__) |
2432b92d | 118 | return _stricmp(psz1, psz2); |
91b8de8d | 119 | #elif defined(__SALFORDC__) |
a3ef5bf5 | 120 | return stricmp(psz1, psz2); |
dd1eaa89 VZ |
121 | #elif defined(__BORLANDC__) |
122 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
123 | #elif defined(__WATCOMC__) |
124 | return stricmp(psz1, psz2); | |
14704334 VS |
125 | #elif defined(__DJGPP__) |
126 | return stricmp(psz1, psz2); | |
91b8de8d RR |
127 | #elif defined(__EMX__) |
128 | return stricmp(psz1, psz2); | |
e2c87f4c | 129 | #elif defined(__WXPM__) |
1777b9bb | 130 | return stricmp(psz1, psz2); |
a0be6908 WS |
131 | #elif defined(__WXPALMOS__) || \ |
132 | defined(HAVE_STRCASECMP_IN_STRING_H) || \ | |
1c2d1459 VZ |
133 | defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ |
134 | defined(__GNUWIN32__) | |
dd1eaa89 | 135 | return strcasecmp(psz1, psz2); |
8be97d65 | 136 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
137 | register char c1, c2; |
138 | do { | |
139 | c1 = tolower(*psz1++); | |
140 | c2 = tolower(*psz2++); | |
141 | } while ( c1 && (c1 == c2) ); | |
142 | ||
143 | return c1 - c2; | |
dd1eaa89 VZ |
144 | #else |
145 | // almost all compilers/libraries provide this function (unfortunately under | |
146 | // different names), that's why we don't implement our own which will surely | |
147 | // be more efficient than this code (uncomment to use): | |
148 | /* | |
149 | register char c1, c2; | |
150 | do { | |
151 | c1 = tolower(*psz1++); | |
152 | c2 = tolower(*psz2++); | |
153 | } while ( c1 && (c1 == c2) ); | |
154 | ||
155 | return c1 - c2; | |
156 | */ | |
157 | ||
158 | #error "Please define string case-insensitive compare for your OS/compiler" | |
159 | #endif // OS/compiler | |
160 | } | |
c801d85f | 161 | |
ce76f779 VZ |
162 | // ---------------------------------------------------------------------------- |
163 | // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
166 | // in both cases we need to define wxStdString | |
47561b0d | 167 | #if wxUSE_STL || wxUSE_STD_STRING |
e87b7833 MB |
168 | |
169 | #include "wx/beforestd.h" | |
170 | #include <string> | |
171 | #include "wx/afterstd.h" | |
172 | ||
173 | #if wxUSE_UNICODE | |
0c7cfd99 | 174 | #ifdef HAVE_STD_WSTRING |
ce76f779 | 175 | typedef std::wstring wxStdString; |
e87b7833 | 176 | #else |
ce76f779 | 177 | typedef std::basic_string<wxChar> wxStdString; |
e87b7833 MB |
178 | #endif |
179 | #else | |
ce76f779 | 180 | typedef std::string wxStdString; |
e87b7833 MB |
181 | #endif |
182 | ||
ce76f779 | 183 | #endif // need <string> |
e87b7833 | 184 | |
ce76f779 VZ |
185 | #if wxUSE_STL |
186 | ||
187 | // we don't need an extra ctor from std::string when copy ctor already does | |
188 | // the work | |
189 | #undef wxUSE_STD_STRING | |
7147aa47 | 190 | #define wxUSE_STD_STRING 0 |
ce76f779 VZ |
191 | |
192 | #if (defined(__GNUG__) && (__GNUG__ < 3)) || \ | |
193 | (defined(_MSC_VER) && (_MSC_VER <= 1200)) | |
194 | #define wxSTRING_BASE_HASNT_CLEAR | |
195 | #endif | |
196 | ||
197 | typedef wxStdString wxStringBase; | |
e87b7833 MB |
198 | #else // if !wxUSE_STL |
199 | ||
47561b0d MB |
200 | #if !defined(HAVE_STD_STRING_COMPARE) && \ |
201 | (!defined(__WX_SETUP_H__) || wxUSE_STL == 0) | |
e87b7833 MB |
202 | #define HAVE_STD_STRING_COMPARE |
203 | #endif | |
204 | ||
c801d85f | 205 | // --------------------------------------------------------------------------- |
f04f3991 | 206 | // string data prepended with some housekeeping info (used by wxString class), |
c801d85f KB |
207 | // is never used directly (but had to be put here to allow inlining) |
208 | // --------------------------------------------------------------------------- | |
e90c1d2a | 209 | |
bddd7a8d | 210 | struct WXDLLIMPEXP_BASE wxStringData |
c801d85f KB |
211 | { |
212 | int nRefs; // reference count | |
3c024cc2 | 213 | size_t nDataLength, // actual string length |
c801d85f KB |
214 | nAllocLength; // allocated memory size |
215 | ||
2bb67b80 OK |
216 | // mimics declaration 'wxChar data[nAllocLength]' |
217 | wxChar* data() const { return (wxChar*)(this + 1); } | |
c801d85f KB |
218 | |
219 | // empty string has a special ref count so it's never deleted | |
dbda9e86 JS |
220 | bool IsEmpty() const { return (nRefs == -1); } |
221 | bool IsShared() const { return (nRefs > 1); } | |
c801d85f KB |
222 | |
223 | // lock/unlock | |
dd1eaa89 | 224 | void Lock() { if ( !IsEmpty() ) nRefs++; } |
f6bcfd97 | 225 | |
8ecf21b7 | 226 | // VC++ will refuse to inline Unlock but profiling shows that it is wrong |
f6bcfd97 | 227 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
1c2d1459 | 228 | __forceinline |
f6bcfd97 | 229 | #endif |
ca5e07c7 GD |
230 | // VC++ free must take place in same DLL as allocation when using non dll |
231 | // run-time library (e.g. Multithreaded instead of Multithreaded DLL) | |
8ecf21b7 GD |
232 | #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL) |
233 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); } | |
ca5e07c7 GD |
234 | // we must not inline deallocation since allocation is not inlined |
235 | void Free(); | |
8ecf21b7 | 236 | #else |
f1317a5d | 237 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); } |
8ecf21b7 | 238 | #endif |
8fd0f20b | 239 | |
dd1eaa89 | 240 | // if we had taken control over string memory (GetWriteBuf), it's |
8fd0f20b | 241 | // intentionally put in invalid state |
dbda9e86 JS |
242 | void Validate(bool b) { nRefs = (b ? 1 : 0); } |
243 | bool IsValid() const { return (nRefs != 0); } | |
c801d85f KB |
244 | }; |
245 | ||
e87b7833 | 246 | class WXDLLIMPEXP_BASE wxStringBase |
3c67202d | 247 | { |
df5168c4 | 248 | #if !wxUSE_STL |
bddd7a8d | 249 | friend class WXDLLIMPEXP_BASE wxArrayString; |
df5168c4 | 250 | #endif |
5c0217af SC |
251 | public : |
252 | // an 'invalid' value for string index, moved to this place due to a CW bug | |
253 | static const size_t npos; | |
e87b7833 | 254 | protected: |
dd1eaa89 | 255 | // points to data preceded by wxStringData structure with ref count info |
2bb67b80 | 256 | wxChar *m_pchData; |
dd1eaa89 VZ |
257 | |
258 | // accessor to string data | |
259 | wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; } | |
260 | ||
6b95b20d VZ |
261 | // string (re)initialization functions |
262 | // initializes the string to the empty value (must be called only from | |
263 | // ctors, use Reinit() otherwise) | |
e90c1d2a | 264 | void Init() { m_pchData = (wxChar *)wxEmptyString; } |
90e572f1 | 265 | // initializes the string with (a part of) C-string |
e87b7833 | 266 | void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = npos); |
6b95b20d VZ |
267 | // as Init, but also frees old data |
268 | void Reinit() { GetStringData()->Unlock(); Init(); } | |
269 | ||
270 | // memory allocation | |
b1801e0e GD |
271 | // allocates memory for string of length nLen |
272 | bool AllocBuffer(size_t nLen); | |
6b95b20d | 273 | // copies data to another string |
b1801e0e | 274 | bool AllocCopy(wxString&, int, int) const; |
6b95b20d | 275 | // effectively copies data to string |
b1801e0e | 276 | bool AssignCopy(size_t, const wxChar *); |
6b95b20d VZ |
277 | |
278 | // append a (sub)string | |
e87b7833 MB |
279 | bool ConcatSelf(size_t nLen, const wxChar *src, size_t nMaxLen); |
280 | bool ConcatSelf(size_t nLen, const wxChar *src) | |
281 | { return ConcatSelf(nLen, src, nLen); } | |
6b95b20d VZ |
282 | |
283 | // functions called before writing to the string: they copy it if there | |
284 | // are other references to our data (should be the only owner when writing) | |
b1801e0e GD |
285 | bool CopyBeforeWrite(); |
286 | bool AllocBeforeWrite(size_t); | |
6b95b20d | 287 | |
e87b7833 MB |
288 | // compatibility with wxString |
289 | bool Alloc(size_t nLen); | |
290 | public: | |
291 | // standard types | |
292 | typedef wxChar value_type; | |
293 | typedef wxChar char_type; | |
294 | typedef size_t size_type; | |
295 | typedef value_type& reference; | |
296 | typedef const value_type& const_reference; | |
297 | typedef value_type* pointer; | |
298 | typedef const value_type* const_pointer; | |
299 | typedef value_type *iterator; | |
300 | typedef const value_type *const_iterator; | |
00b4a13e | 301 | |
968f291b VZ |
302 | #define wxSTRING_REVERSE_ITERATOR(name, const_or_not) \ |
303 | class name \ | |
304 | { \ | |
305 | public: \ | |
306 | typedef wxChar value_type; \ | |
307 | typedef const_or_not value_type& reference; \ | |
308 | typedef const_or_not value_type *pointer; \ | |
309 | typedef const_or_not value_type *iterator_type; \ | |
310 | \ | |
311 | name(iterator_type i) : m_cur(i) { } \ | |
312 | name(const name& ri) : m_cur(ri.m_cur) { } \ | |
313 | \ | |
314 | iterator_type base() const { return m_cur; } \ | |
315 | \ | |
316 | reference operator*() const { return *(m_cur - 1); } \ | |
968f291b VZ |
317 | \ |
318 | name& operator++() { --m_cur; return *this; } \ | |
319 | name operator++(int) { name tmp = *this; --m_cur; return tmp; } \ | |
320 | name& operator--() { ++m_cur; return *this; } \ | |
321 | name operator--(int) { name tmp = *this; ++m_cur; return tmp; } \ | |
322 | \ | |
323 | bool operator==(name ri) const { return m_cur == ri.m_cur; } \ | |
324 | bool operator!=(name ri) const { return !(*this == ri); } \ | |
325 | \ | |
326 | private: \ | |
327 | iterator_type m_cur; \ | |
328 | } | |
329 | ||
330 | wxSTRING_REVERSE_ITERATOR(const_reverse_iterator, const); | |
331 | ||
332 | #define wxSTRING_CONST | |
333 | wxSTRING_REVERSE_ITERATOR(reverse_iterator, wxSTRING_CONST); | |
334 | #undef wxSTRING_CONST | |
335 | ||
336 | #undef wxSTRING_REVERSE_ITERATOR | |
337 | ||
338 | ||
3c67202d VZ |
339 | // constructors and destructor |
340 | // ctor for an empty string | |
e87b7833 | 341 | wxStringBase() { Init(); } |
3c67202d | 342 | // copy ctor |
e87b7833 | 343 | wxStringBase(const wxStringBase& stringSrc) |
6b95b20d | 344 | { |
09443a26 VZ |
345 | wxASSERT_MSG( stringSrc.GetStringData()->IsValid(), |
346 | _T("did you forget to call UngetWriteBuf()?") ); | |
6b95b20d | 347 | |
e87b7833 | 348 | if ( stringSrc.empty() ) { |
6b95b20d VZ |
349 | // nothing to do for an empty string |
350 | Init(); | |
351 | } | |
352 | else { | |
353 | m_pchData = stringSrc.m_pchData; // share same data | |
354 | GetStringData()->Lock(); // => one more copy | |
355 | } | |
356 | } | |
3c67202d | 357 | // string containing nRepeat copies of ch |
e87b7833 | 358 | wxStringBase(size_type nRepeat, wxChar ch); |
3c67202d | 359 | // ctor takes first nLength characters from C string |
e87b7833 MB |
360 | // (default value of npos means take all the string) |
361 | wxStringBase(const wxChar *psz) | |
362 | { InitWith(psz, 0, npos); } | |
363 | wxStringBase(const wxChar *psz, size_t nLength) | |
b1801e0e | 364 | { InitWith(psz, 0, nLength); } |
830f8f11 VZ |
365 | wxStringBase(const wxChar *psz, |
366 | const wxMBConv& WXUNUSED(conv), | |
367 | size_t nLength = npos) | |
b1801e0e | 368 | { InitWith(psz, 0, nLength); } |
e87b7833 MB |
369 | // take nLen chars starting at nPos |
370 | wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen) | |
371 | { | |
372 | wxASSERT_MSG( str.GetStringData()->IsValid(), | |
373 | _T("did you forget to call UngetWriteBuf()?") ); | |
374 | Init(); | |
375 | size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen; | |
376 | InitWith(str.c_str(), nPos, nLen); | |
377 | } | |
378 | // take all characters from pStart to pEnd | |
379 | wxStringBase(const void *pStart, const void *pEnd); | |
380 | ||
381 | // dtor is not virtual, this class must not be inherited from! | |
1c2d1459 VZ |
382 | ~wxStringBase() |
383 | { | |
7b758e8f | 384 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
1c2d1459 VZ |
385 | //RN - according to the above VC++ does indeed inline this, |
386 | //even though it spits out two warnings | |
387 | #pragma warning (disable:4714) | |
7b758e8f | 388 | #endif |
e87b7833 | 389 | |
1c2d1459 | 390 | GetStringData()->Unlock(); |
7b758e8f RN |
391 | } |
392 | ||
393 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) | |
1c2d1459 VZ |
394 | //re-enable inlining warning |
395 | #pragma warning (default:4714) | |
396 | #endif | |
e87b7833 MB |
397 | // overloaded assignment |
398 | // from another wxString | |
399 | wxStringBase& operator=(const wxStringBase& stringSrc); | |
400 | // from a character | |
401 | wxStringBase& operator=(wxChar ch); | |
402 | // from a C string | |
403 | wxStringBase& operator=(const wxChar *psz); | |
404 | ||
405 | // return the length of the string | |
20aa779e | 406 | size_type length() const { return GetStringData()->nDataLength; } |
e87b7833 | 407 | // return the length of the string |
20aa779e | 408 | size_type size() const { return length(); } |
e87b7833 MB |
409 | // return the maximum size of the string |
410 | size_type max_size() const { return wxSTRING_MAXLEN; } | |
411 | // resize the string, filling the space with c if c != 0 | |
412 | void resize(size_t nSize, wxChar ch = wxT('\0')); | |
413 | // delete the contents of the string | |
414 | void clear() { erase(0, npos); } | |
415 | // returns true if the string is empty | |
20aa779e | 416 | bool empty() const { return length() == 0; } |
e87b7833 MB |
417 | // inform string about planned change in size |
418 | void reserve(size_t sz) { Alloc(sz); } | |
419 | size_type capacity() const { return GetStringData()->nAllocLength; } | |
420 | ||
421 | // lib.string.access | |
422 | // return the character at position n | |
423 | value_type at(size_type n) const | |
424 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } | |
e87b7833 MB |
425 | // returns the writable character at position n |
426 | reference at(size_type n) | |
427 | { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } | |
e87b7833 MB |
428 | |
429 | // lib.string.modifiers | |
430 | // append elements str[pos], ..., str[pos+n] | |
431 | wxStringBase& append(const wxStringBase& str, size_t pos, size_t n) | |
432 | { | |
433 | wxASSERT(pos <= str.length()); | |
434 | ConcatSelf(n, str.c_str() + pos, str.length() - pos); | |
435 | return *this; | |
436 | } | |
437 | // append a string | |
438 | wxStringBase& append(const wxStringBase& str) | |
439 | { ConcatSelf(str.length(), str.c_str()); return *this; } | |
440 | // append first n (or all if n == npos) characters of sz | |
441 | wxStringBase& append(const wxChar *sz) | |
442 | { ConcatSelf(wxStrlen(sz), sz); return *this; } | |
443 | wxStringBase& append(const wxChar *sz, size_t n) | |
444 | { ConcatSelf(n, sz); return *this; } | |
445 | // append n copies of ch | |
446 | wxStringBase& append(size_t n, wxChar ch); | |
447 | // append from first to last | |
448 | wxStringBase& append(const_iterator first, const_iterator last) | |
449 | { ConcatSelf(last - first, first); return *this; } | |
450 | ||
451 | // same as `this_string = str' | |
452 | wxStringBase& assign(const wxStringBase& str) | |
453 | { return *this = str; } | |
454 | // same as ` = str[pos..pos + n] | |
455 | wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n) | |
456 | { clear(); return append(str, pos, n); } | |
457 | // same as `= first n (or all if n == npos) characters of sz' | |
458 | wxStringBase& assign(const wxChar *sz) | |
459 | { clear(); return append(sz, wxStrlen(sz)); } | |
460 | wxStringBase& assign(const wxChar *sz, size_t n) | |
461 | { clear(); return append(sz, n); } | |
462 | // same as `= n copies of ch' | |
463 | wxStringBase& assign(size_t n, wxChar ch) | |
464 | { clear(); return append(n, ch); } | |
465 | // assign from first to last | |
466 | wxStringBase& assign(const_iterator first, const_iterator last) | |
467 | { clear(); return append(first, last); } | |
468 | ||
469 | // first valid index position | |
470 | const_iterator begin() const { return m_pchData; } | |
968f291b | 471 | iterator begin(); |
e87b7833 MB |
472 | // position one after the last valid one |
473 | const_iterator end() const { return m_pchData + length(); } | |
ac3c86ee | 474 | iterator end(); |
e87b7833 | 475 | |
968f291b VZ |
476 | // first element of the reversed string |
477 | const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } | |
478 | reverse_iterator rbegin() { return reverse_iterator(end()); } | |
479 | // one beyond the end of the reversed string | |
480 | const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } | |
481 | reverse_iterator rend() { return reverse_iterator(begin()); } | |
482 | ||
e87b7833 MB |
483 | // insert another string |
484 | wxStringBase& insert(size_t nPos, const wxStringBase& str) | |
485 | { | |
486 | wxASSERT( str.GetStringData()->IsValid() ); | |
487 | return insert(nPos, str.c_str(), str.length()); | |
488 | } | |
489 | // insert n chars of str starting at nStart (in str) | |
490 | wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n) | |
491 | { | |
492 | wxASSERT( str.GetStringData()->IsValid() ); | |
493 | wxASSERT( nStart < str.length() ); | |
494 | size_t strLen = str.length() - nStart; | |
495 | n = strLen < n ? strLen : n; | |
496 | return insert(nPos, str.c_str() + nStart, n); | |
497 | } | |
498 | // insert first n (or all if n == npos) characters of sz | |
499 | wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos); | |
500 | // insert n copies of ch | |
501 | wxStringBase& insert(size_t nPos, size_t n, wxChar ch) | |
502 | { return insert(nPos, wxStringBase(n, ch)); } | |
503 | iterator insert(iterator it, wxChar ch) | |
1c2d1459 | 504 | { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; } |
e87b7833 MB |
505 | void insert(iterator it, const_iterator first, const_iterator last) |
506 | { insert(it - begin(), first, last - first); } | |
507 | void insert(iterator it, size_type n, wxChar ch) | |
508 | { insert(it - begin(), n, ch); } | |
509 | ||
510 | // delete characters from nStart to nStart + nLen | |
511 | wxStringBase& erase(size_type pos = 0, size_type n = npos); | |
512 | iterator erase(iterator first, iterator last) | |
513 | { | |
514 | size_t idx = first - begin(); | |
515 | erase(idx, last - first); | |
516 | return begin() + idx; | |
517 | } | |
518 | iterator erase(iterator first); | |
519 | ||
520 | // explicit conversion to C string (use this with printf()!) | |
521 | const wxChar* c_str() const { return m_pchData; } | |
522 | const wxChar* data() const { return m_pchData; } | |
523 | ||
524 | // replaces the substring of length nLen starting at nStart | |
525 | wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz); | |
526 | // replaces the substring of length nLen starting at nStart | |
527 | wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str) | |
528 | { return replace(nStart, nLen, str.c_str()); } | |
529 | // replaces the substring with nCount copies of ch | |
530 | wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch); | |
531 | // replaces a substring with another substring | |
532 | wxStringBase& replace(size_t nStart, size_t nLen, | |
533 | const wxStringBase& str, size_t nStart2, size_t nLen2); | |
534 | // replaces the substring with first nCount chars of sz | |
535 | wxStringBase& replace(size_t nStart, size_t nLen, | |
536 | const wxChar* sz, size_t nCount); | |
537 | wxStringBase& replace(iterator first, iterator last, const_pointer s) | |
538 | { return replace(first - begin(), last - first, s); } | |
539 | wxStringBase& replace(iterator first, iterator last, const_pointer s, | |
540 | size_type n) | |
541 | { return replace(first - begin(), last - first, s, n); } | |
542 | wxStringBase& replace(iterator first, iterator last, const wxStringBase& s) | |
543 | { return replace(first - begin(), last - first, s); } | |
544 | wxStringBase& replace(iterator first, iterator last, size_type n, wxChar c) | |
545 | { return replace(first - begin(), last - first, n, c); } | |
546 | wxStringBase& replace(iterator first, iterator last, | |
547 | const_iterator first1, const_iterator last1) | |
548 | { return replace(first - begin(), last - first, first1, last1 - first1); } | |
549 | ||
550 | // swap two strings | |
551 | void swap(wxStringBase& str); | |
552 | ||
553 | // All find() functions take the nStart argument which specifies the | |
554 | // position to start the search on, the default value is 0. All functions | |
555 | // return npos if there were no match. | |
556 | ||
557 | // find a substring | |
558 | size_t find(const wxStringBase& str, size_t nStart = 0) const; | |
559 | ||
e87b7833 MB |
560 | // find first n characters of sz |
561 | size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; | |
e87b7833 | 562 | |
e87b7833 MB |
563 | // find the first occurence of character ch after nStart |
564 | size_t find(wxChar ch, size_t nStart = 0) const; | |
1412634b | 565 | |
e87b7833 MB |
566 | // rfind() family is exactly like find() but works right to left |
567 | ||
568 | // as find, but from the end | |
569 | size_t rfind(const wxStringBase& str, size_t nStart = npos) const; | |
570 | ||
e87b7833 MB |
571 | // as find, but from the end |
572 | size_t rfind(const wxChar* sz, size_t nStart = npos, | |
573 | size_t n = npos) const; | |
574 | // as find, but from the end | |
575 | size_t rfind(wxChar ch, size_t nStart = npos) const; | |
576 | ||
577 | // find first/last occurence of any character in the set | |
578 | ||
579 | // as strpbrk() but starts at nStart, returns npos if not found | |
580 | size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const | |
581 | { return find_first_of(str.c_str(), nStart); } | |
582 | // same as above | |
583 | size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; | |
e2101186 | 584 | size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
585 | // same as find(char, size_t) |
586 | size_t find_first_of(wxChar c, size_t nStart = 0) const | |
587 | { return find(c, nStart); } | |
588 | // find the last (starting from nStart) char from str in this string | |
589 | size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const | |
590 | { return find_last_of(str.c_str(), nStart); } | |
591 | // same as above | |
592 | size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; | |
e2101186 | 593 | size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
594 | // same as above |
595 | size_t find_last_of(wxChar c, size_t nStart = npos) const | |
596 | { return rfind(c, nStart); } | |
597 | ||
598 | // find first/last occurence of any character not in the set | |
599 | ||
600 | // as strspn() (starting from nStart), returns npos on failure | |
601 | size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const | |
602 | { return find_first_not_of(str.c_str(), nStart); } | |
603 | // same as above | |
604 | size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; | |
e2101186 | 605 | size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
606 | // same as above |
607 | size_t find_first_not_of(wxChar ch, size_t nStart = 0) const; | |
608 | // as strcspn() | |
609 | size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const | |
e2101186 | 610 | { return find_last_not_of(str.c_str(), nStart); } |
e87b7833 MB |
611 | // same as above |
612 | size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; | |
e2101186 | 613 | size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
614 | // same as above |
615 | size_t find_last_not_of(wxChar ch, size_t nStart = npos) const; | |
616 | ||
617 | // All compare functions return -1, 0 or 1 if the [sub]string is less, | |
618 | // equal or greater than the compare() argument. | |
619 | ||
dcb68102 RN |
620 | // comparison with another string |
621 | int compare(const wxStringBase& str) const; | |
e87b7833 MB |
622 | // comparison with a substring |
623 | int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; | |
624 | // comparison of 2 substrings | |
625 | int compare(size_t nStart, size_t nLen, | |
626 | const wxStringBase& str, size_t nStart2, size_t nLen2) const; | |
dcb68102 RN |
627 | // comparison with a c string |
628 | int compare(const wxChar* sz) const; | |
e87b7833 MB |
629 | // substring comparison with first nCount characters of sz |
630 | int compare(size_t nStart, size_t nLen, | |
631 | const wxChar* sz, size_t nCount = npos) const; | |
632 | ||
633 | size_type copy(wxChar* s, size_type n, size_type pos = 0); | |
634 | ||
635 | // substring extraction | |
636 | wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const; | |
637 | ||
638 | // string += string | |
639 | wxStringBase& operator+=(const wxStringBase& s) { return append(s); } | |
640 | // string += C string | |
641 | wxStringBase& operator+=(const wxChar *psz) { return append(psz); } | |
642 | // string += char | |
643 | wxStringBase& operator+=(wxChar ch) { return append(1, ch); } | |
644 | }; | |
645 | ||
646 | #endif // !wxUSE_STL | |
647 | ||
b6339dde VZ |
648 | // ---------------------------------------------------------------------------- |
649 | // wxString: string class trying to be compatible with std::string, MFC | |
650 | // CString and wxWindows 1.x wxString all at once | |
e87b7833 MB |
651 | // --------------------------------------------------------------------------- |
652 | ||
653 | class WXDLLIMPEXP_BASE wxString : public wxStringBase | |
654 | { | |
655 | #if !wxUSE_STL | |
656 | friend class WXDLLIMPEXP_BASE wxArrayString; | |
657 | #endif | |
658 | ||
659 | // NB: special care was taken in arranging the member functions in such order | |
660 | // that all inline functions can be effectively inlined, verify that all | |
20aa779e | 661 | // performance critical functions are still inlined if you change order! |
e87b7833 MB |
662 | private: |
663 | // if we hadn't made these operators private, it would be possible to | |
664 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
665 | // converted to char in C and we do have operator=(char) | |
666 | // | |
667 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
668 | // to assign another numeric type to wxString will now result in | |
669 | // ambiguity between operator=(char) and operator=(int) | |
670 | wxString& operator=(int); | |
671 | ||
672 | // these methods are not implemented - there is _no_ conversion from int to | |
673 | // string, you're doing something wrong if the compiler wants to call it! | |
674 | // | |
675 | // try `s << i' or `s.Printf("%d", i)' instead | |
676 | wxString(int); | |
677 | ||
678 | public: | |
679 | // constructors and destructor | |
680 | // ctor for an empty string | |
681 | wxString() : wxStringBase() { } | |
682 | // copy ctor | |
683 | wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { } | |
684 | wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { } | |
685 | // string containing nRepeat copies of ch | |
686 | wxString(wxChar ch, size_t nRepeat = 1) | |
687 | : wxStringBase(nRepeat, ch) { } | |
688 | wxString(size_t nRepeat, wxChar ch) | |
689 | : wxStringBase(nRepeat, ch) { } | |
690 | // ctor takes first nLength characters from C string | |
691 | // (default value of npos means take all the string) | |
692 | wxString(const wxChar *psz) | |
693 | : wxStringBase(psz ? psz : wxT("")) { } | |
694 | wxString(const wxChar *psz, size_t nLength) | |
695 | : wxStringBase(psz, nLength) { } | |
830f8f11 VZ |
696 | wxString(const wxChar *psz, |
697 | const wxMBConv& WXUNUSED(conv), | |
698 | size_t nLength = npos) | |
e87b7833 | 699 | : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { } |
e90c1d2a | 700 | |
20aa779e | 701 | // even if we're not built with wxUSE_STL == 1 it is very convenient to allow |
ce76f779 VZ |
702 | // implicit conversions from std::string to wxString as this allows to use |
703 | // the same strings in non-GUI and GUI code, however we don't want to | |
704 | // unconditionally add this ctor as it would make wx lib dependent on | |
705 | // libstdc++ on some Linux versions which is bad, so instead we ask the | |
706 | // client code to define this wxUSE_STD_STRING symbol if they need it | |
47561b0d | 707 | #if wxUSE_STD_STRING |
ce76f779 VZ |
708 | wxString(const wxStdString& s) |
709 | : wxStringBase(s.c_str()) { } | |
710 | #endif // wxUSE_STD_STRING | |
711 | ||
2bb67b80 OK |
712 | #if wxUSE_UNICODE |
713 | // from multibyte string | |
830f8f11 | 714 | wxString(const char *psz, const wxMBConv& conv, size_t nLength = npos); |
2bb67b80 | 715 | // from wxWCharBuffer (i.e. return from wxGetString) |
e87b7833 | 716 | wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { } |
e90c1d2a | 717 | #else // ANSI |
3c67202d | 718 | // from C string (for compilers using unsigned char) |
153958f7 VZ |
719 | wxString(const unsigned char* psz) |
720 | : wxStringBase((const char*)psz) { } | |
721 | // from part of C string (for compilers using unsigned char) | |
722 | wxString(const unsigned char* psz, size_t nLength) | |
e87b7833 | 723 | : wxStringBase((const char*)psz, nLength) { } |
e90c1d2a | 724 | |
6f841509 | 725 | #if wxUSE_WCHAR_T |
2bb67b80 | 726 | // from wide (Unicode) string |
830f8f11 VZ |
727 | wxString(const wchar_t *pwz, |
728 | const wxMBConv& conv = wxConvLibc, | |
729 | size_t nLength = npos); | |
e90c1d2a VZ |
730 | #endif // !wxUSE_WCHAR_T |
731 | ||
2bb67b80 OK |
732 | // from wxCharBuffer |
733 | wxString(const wxCharBuffer& psz) | |
051aa330 | 734 | : wxStringBase(psz) { } |
e90c1d2a VZ |
735 | #endif // Unicode/ANSI |
736 | ||
3c67202d VZ |
737 | // generic attributes & operations |
738 | // as standard strlen() | |
e87b7833 | 739 | size_t Len() const { return length(); } |
3c67202d | 740 | // string contains any characters? |
e87b7833 | 741 | bool IsEmpty() const { return empty(); } |
d775fa82 | 742 | // empty string is "false", so !str will return true |
20aa779e | 743 | bool operator!() const { return empty(); } |
735d1db6 VZ |
744 | // truncate the string to given length |
745 | wxString& Truncate(size_t uiLen); | |
3c67202d | 746 | // empty string contents |
dd1eaa89 VZ |
747 | void Empty() |
748 | { | |
735d1db6 | 749 | Truncate(0); |
dd1eaa89 | 750 | |
5a8231ef | 751 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); |
7be07660 | 752 | } |
3c67202d | 753 | // empty the string and free memory |
7be07660 VZ |
754 | void Clear() |
755 | { | |
e87b7833 MB |
756 | wxString tmp(wxEmptyString); |
757 | swap(tmp); | |
dd1eaa89 VZ |
758 | } |
759 | ||
3c67202d VZ |
760 | // contents test |
761 | // Is an ascii value | |
c801d85f | 762 | bool IsAscii() const; |
3c67202d | 763 | // Is a number |
c801d85f | 764 | bool IsNumber() const; |
3c67202d | 765 | // Is a word |
c801d85f | 766 | bool IsWord() const; |
c801d85f | 767 | |
3c67202d VZ |
768 | // data access (all indexes are 0 based) |
769 | // read access | |
2bb67b80 | 770 | wxChar GetChar(size_t n) const |
9d9ad673 | 771 | { return at(n); } |
3c67202d | 772 | // read/write access |
2bb67b80 | 773 | wxChar& GetWritableChar(size_t n) |
9d9ad673 | 774 | { return at(n); } |
3c67202d | 775 | // write access |
2bb67b80 | 776 | void SetChar(size_t n, wxChar ch) |
9d9ad673 | 777 | { at(n) = ch; } |
c801d85f | 778 | |
3c67202d | 779 | // get last character |
2bb67b80 | 780 | wxChar Last() const |
09443a26 | 781 | { |
5a8231ef | 782 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
09443a26 | 783 | |
9d9ad673 | 784 | return at(length() - 1); |
09443a26 VZ |
785 | } |
786 | ||
3c67202d | 787 | // get writable last character |
2bb67b80 | 788 | wxChar& Last() |
09443a26 | 789 | { |
5a8231ef | 790 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
9d9ad673 | 791 | return at(length() - 1); |
09443a26 | 792 | } |
c801d85f | 793 | |
d836ee96 | 794 | /* |
9d9ad673 | 795 | Note that we we must define all of the overloads below to avoid |
01398433 VZ |
796 | ambiguity when using str[0]. Also note that for a conforming compiler we |
797 | don't need const version of operatorp[] at all as indexed access to | |
798 | const string is provided by implicit conversion to "const wxChar *" | |
799 | below and defining them would only result in ambiguities, but some other | |
800 | compilers refuse to compile "str[0]" without them. | |
d836ee96 VZ |
801 | */ |
802 | ||
01398433 VZ |
803 | #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__) |
804 | wxChar operator[](int n) const | |
805 | { return wxStringBase::at(n); } | |
806 | wxChar operator[](size_type n) const | |
807 | { return wxStringBase::at(n); } | |
808 | #ifndef wxSIZE_T_IS_UINT | |
809 | wxChar operator[](unsigned int n) const | |
810 | { return wxStringBase::at(n); } | |
811 | #endif // size_t != unsigned int | |
812 | #endif // broken compiler | |
813 | ||
814 | ||
9d9ad673 VZ |
815 | // operator versions of GetWriteableChar() |
816 | wxChar& operator[](int n) | |
817 | { return wxStringBase::at(n); } | |
e87b7833 | 818 | wxChar& operator[](size_type n) |
9d9ad673 | 819 | { return wxStringBase::at(n); } |
d836ee96 | 820 | #ifndef wxSIZE_T_IS_UINT |
d836ee96 | 821 | wxChar& operator[](unsigned int n) |
9d9ad673 | 822 | { return wxStringBase::at(n); } |
d836ee96 | 823 | #endif // size_t != unsigned int |
c801d85f | 824 | |
3c67202d | 825 | // implicit conversion to C string |
e87b7833 | 826 | operator const wxChar*() const { return c_str(); } |
e015c2a3 | 827 | |
e015c2a3 | 828 | // identical to c_str(), for wxWin 1.6x compatibility |
e87b7833 | 829 | const wxChar* wx_str() const { return c_str(); } |
e015c2a3 | 830 | // identical to c_str(), for MFC compatibility |
e87b7833 | 831 | const wxChar* GetData() const { return c_str(); } |
e90c1d2a | 832 | |
e015c2a3 VZ |
833 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
834 | // converting numbers or strings which are certain not to contain special | |
835 | // chars (typically system functions, X atoms, environment variables etc.) | |
836 | // | |
837 | // the behaviour of these functions with the strings containing anything | |
838 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 839 | #if wxUSE_UNICODE |
2b5f62a0 VZ |
840 | static wxString FromAscii(const char *ascii); // string |
841 | static wxString FromAscii(const char ascii); // char | |
b1ac3b56 | 842 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
843 | #else // ANSI |
844 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
2b5f62a0 | 845 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
846 | const char *ToAscii() const { return c_str(); } |
847 | #endif // Unicode/!Unicode | |
b1ac3b56 | 848 | |
2b5f62a0 | 849 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 850 | // buffer with temporary data |
f6bcfd97 BP |
851 | // |
852 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
853 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
854 | // fn_str() to return a string which should be used with the OS APIs | |
855 | // accepting the file names. The return value is always the same, but the | |
856 | // type differs because a function may either return pointer to the buffer | |
857 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 858 | #if wxUSE_UNICODE |
830f8f11 | 859 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; |
f6bcfd97 | 860 | |
e90c1d2a VZ |
861 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
862 | ||
e87b7833 | 863 | const wxChar* wc_str() const { return c_str(); } |
f6bcfd97 BP |
864 | |
865 | // for compatibility with !wxUSE_UNICODE version | |
830f8f11 | 866 | const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
e90c1d2a | 867 | |
2bb67b80 | 868 | #if wxMBFILES |
5f709e67 | 869 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 870 | #else // !wxMBFILES |
e87b7833 | 871 | const wxChar* fn_str() const { return c_str(); } |
e90c1d2a VZ |
872 | #endif // wxMBFILES/!wxMBFILES |
873 | #else // ANSI | |
e87b7833 | 874 | const wxChar* mb_str() const { return c_str(); } |
f6bcfd97 BP |
875 | |
876 | // for compatibility with wxUSE_UNICODE version | |
830f8f11 | 877 | const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
f6bcfd97 | 878 | |
e90c1d2a | 879 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 880 | |
6f841509 | 881 | #if wxUSE_WCHAR_T |
830f8f11 | 882 | const wxWCharBuffer wc_str(const wxMBConv& conv) const; |
e90c1d2a | 883 | #endif // wxUSE_WCHAR_T |
d5c8817c SC |
884 | #ifdef __WXOSX__ |
885 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
886 | #else | |
e87b7833 | 887 | const wxChar* fn_str() const { return c_str(); } |
d5c8817c | 888 | #endif |
e90c1d2a | 889 | #endif // Unicode/ANSI |
c801d85f | 890 | |
3c67202d VZ |
891 | // overloaded assignment |
892 | // from another wxString | |
e87b7833 MB |
893 | wxString& operator=(const wxStringBase& stringSrc) |
894 | { return (wxString&)wxStringBase::operator=(stringSrc); } | |
3c67202d | 895 | // from a character |
e87b7833 MB |
896 | wxString& operator=(wxChar ch) |
897 | { return (wxString&)wxStringBase::operator=(ch); } | |
c57e3bd5 RN |
898 | // from a C string - STL probably will crash on NULL, |
899 | // so we need to compensate in that case | |
900 | #if wxUSE_STL | |
901 | wxString& operator=(const wxChar *psz) | |
902 | { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; } | |
903 | #else | |
e87b7833 MB |
904 | wxString& operator=(const wxChar *psz) |
905 | { return (wxString&)wxStringBase::operator=(psz); } | |
c57e3bd5 RN |
906 | #endif |
907 | ||
111bb7f2 OK |
908 | #if wxUSE_UNICODE |
909 | // from wxWCharBuffer | |
b1801e0e | 910 | wxString& operator=(const wxWCharBuffer& psz) |
e87b7833 | 911 | { (void) operator=((const wchar_t *)psz); return *this; } |
e90c1d2a | 912 | #else // ANSI |
3c67202d | 913 | // from another kind of C string |
c801d85f | 914 | wxString& operator=(const unsigned char* psz); |
6f841509 | 915 | #if wxUSE_WCHAR_T |
3c67202d | 916 | // from a wide string |
c801d85f | 917 | wxString& operator=(const wchar_t *pwz); |
6f841509 | 918 | #endif |
111bb7f2 | 919 | // from wxCharBuffer |
b1801e0e | 920 | wxString& operator=(const wxCharBuffer& psz) |
e87b7833 | 921 | { (void) operator=((const char *)psz); return *this; } |
e90c1d2a | 922 | #endif // Unicode/ANSI |
3c67202d VZ |
923 | |
924 | // string concatenation | |
925 | // in place concatenation | |
926 | /* | |
927 | Concatenate and return the result. Note that the left to right | |
928 | associativity of << allows to write things like "str << str1 << str2 | |
929 | << ..." (unlike with +=) | |
930 | */ | |
931 | // string += string | |
dd1eaa89 VZ |
932 | wxString& operator<<(const wxString& s) |
933 | { | |
e87b7833 | 934 | #if !wxUSE_STL |
09443a26 VZ |
935 | wxASSERT_MSG( s.GetStringData()->IsValid(), |
936 | _T("did you forget to call UngetWriteBuf()?") ); | |
e87b7833 | 937 | #endif |
dd1eaa89 | 938 | |
e87b7833 | 939 | append(s); |
dd1eaa89 VZ |
940 | return *this; |
941 | } | |
3c67202d | 942 | // string += C string |
2bb67b80 | 943 | wxString& operator<<(const wxChar *psz) |
e87b7833 | 944 | { append(psz); return *this; } |
3c67202d | 945 | // string += char |
e87b7833 | 946 | wxString& operator<<(wxChar ch) { append(1, ch); return *this; } |
2bb67b80 OK |
947 | |
948 | // string += buffer (i.e. from wxGetString) | |
949 | #if wxUSE_UNICODE | |
09443a26 VZ |
950 | wxString& operator<<(const wxWCharBuffer& s) |
951 | { (void)operator<<((const wchar_t *)s); return *this; } | |
952 | void operator+=(const wxWCharBuffer& s) | |
953 | { (void)operator<<((const wchar_t *)s); } | |
954 | #else // !wxUSE_UNICODE | |
955 | wxString& operator<<(const wxCharBuffer& s) | |
956 | { (void)operator<<((const char *)s); return *this; } | |
957 | void operator+=(const wxCharBuffer& s) | |
958 | { (void)operator<<((const char *)s); } | |
959 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
6b95b20d | 960 | |
3c67202d | 961 | // string += C string |
09443a26 VZ |
962 | wxString& Append(const wxString& s) |
963 | { | |
5a8231ef WS |
964 | // test for empty() to share the string if possible |
965 | if ( empty() ) | |
09443a26 VZ |
966 | *this = s; |
967 | else | |
e87b7833 | 968 | append(s); |
09443a26 VZ |
969 | return *this; |
970 | } | |
2bb67b80 | 971 | wxString& Append(const wxChar* psz) |
e87b7833 | 972 | { append(psz); return *this; } |
3c67202d | 973 | // append count copies of given character |
2bb67b80 | 974 | wxString& Append(wxChar ch, size_t count = 1u) |
e87b7833 | 975 | { append(count, ch); return *this; } |
8f06a017 | 976 | wxString& Append(const wxChar* psz, size_t nLen) |
e87b7833 | 977 | { append(psz, nLen); return *this; } |
3c67202d VZ |
978 | |
979 | // prepend a string, return the string itself | |
980 | wxString& Prepend(const wxString& str) | |
981 | { *this = str + *this; return *this; } | |
982 | ||
983 | // non-destructive concatenation | |
1fc8cfbd VZ |
984 | // two strings |
985 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
986 | const wxString& string2); | |
987 | // string with a single char | |
bddd7a8d | 988 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); |
1fc8cfbd | 989 | // char with a string |
bddd7a8d | 990 | friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); |
1fc8cfbd VZ |
991 | // string with C string |
992 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
993 | const wxChar *psz); | |
994 | // C string with string | |
995 | friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, | |
996 | const wxString& string); | |
3c67202d VZ |
997 | |
998 | // stream-like functions | |
999 | // insert an int into string | |
3ce65f6c VZ |
1000 | wxString& operator<<(int i) |
1001 | { return (*this) << Format(_T("%d"), i); } | |
1002 | // insert an unsigned int into string | |
1003 | wxString& operator<<(unsigned int ui) | |
1004 | { return (*this) << Format(_T("%u"), ui); } | |
1005 | // insert a long into string | |
1006 | wxString& operator<<(long l) | |
1007 | { return (*this) << Format(_T("%ld"), l); } | |
1008 | // insert an unsigned long into string | |
1009 | wxString& operator<<(unsigned long ul) | |
1010 | { return (*this) << Format(_T("%lu"), ul); } | |
3fc1adbd MW |
1011 | #if defined wxLongLong_t && !defined wxLongLongIsLong |
1012 | // insert a long long if they exist and aren't longs | |
1013 | wxString& operator<<(wxLongLong_t ll) | |
b7859132 MW |
1014 | { |
1015 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
1016 | return (*this) << Format(fmt, ll); | |
1017 | } | |
3fc1adbd MW |
1018 | // insert an unsigned long long |
1019 | wxString& operator<<(wxULongLong_t ull) | |
b7859132 MW |
1020 | { |
1021 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
1022 | return (*this) << Format(fmt , ull); | |
1023 | } | |
3fc1adbd | 1024 | #endif |
3c67202d | 1025 | // insert a float into string |
3ce65f6c VZ |
1026 | wxString& operator<<(float f) |
1027 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 1028 | // insert a double into string |
3ce65f6c VZ |
1029 | wxString& operator<<(double d) |
1030 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 1031 | |
3c67202d | 1032 | // string comparison |
30b21f9a | 1033 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
dcb68102 RN |
1034 | int Cmp(const wxChar *psz) const; |
1035 | int Cmp(const wxString& s) const; | |
3c67202d | 1036 | // same as Cmp() but not case-sensitive |
dcb68102 RN |
1037 | int CmpNoCase(const wxChar *psz) const; |
1038 | int CmpNoCase(const wxString& s) const; | |
3c67202d VZ |
1039 | // test for the string equality, either considering case or not |
1040 | // (if compareWithCase then the case matters) | |
d775fa82 | 1041 | bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const |
3c67202d | 1042 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } |
20aa779e | 1043 | // comparison with a single character: returns true if equal |
d775fa82 | 1044 | bool IsSameAs(wxChar c, bool compareWithCase = true) const |
f33fee2a | 1045 | { |
e87b7833 | 1046 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c |
f33fee2a VZ |
1047 | : wxToupper(GetChar(0u)) == wxToupper(c)); |
1048 | } | |
3c67202d VZ |
1049 | |
1050 | // simple sub-string extraction | |
1051 | // return substring starting at nFirst of length nCount (or till the end | |
1052 | // if nCount = default value) | |
e87b7833 | 1053 | wxString Mid(size_t nFirst, size_t nCount = npos) const; |
3c67202d | 1054 | |
f6bcfd97 | 1055 | // operator version of Mid() |
3c67202d VZ |
1056 | wxString operator()(size_t start, size_t len) const |
1057 | { return Mid(start, len); } | |
1058 | ||
3affcd07 VZ |
1059 | // check if the string starts with the given prefix and return the rest |
1060 | // of the string in the provided pointer if it is not NULL; otherwise | |
1061 | // return false | |
f6bcfd97 | 1062 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; |
3affcd07 VZ |
1063 | // check if the string ends with the given suffix and return the |
1064 | // beginning of the string before the suffix in the provided pointer if | |
1065 | // it is not NULL; otherwise return false | |
1066 | bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const; | |
f6bcfd97 | 1067 | |
3c67202d | 1068 | // get first nCount characters |
c801d85f | 1069 | wxString Left(size_t nCount) const; |
3c67202d | 1070 | // get last nCount characters |
c801d85f | 1071 | wxString Right(size_t nCount) const; |
c0881dc3 | 1072 | // get all characters before the first occurance of ch |
3c67202d | 1073 | // (returns the whole string if ch not found) |
2bb67b80 | 1074 | wxString BeforeFirst(wxChar ch) const; |
3c67202d VZ |
1075 | // get all characters before the last occurence of ch |
1076 | // (returns empty string if ch not found) | |
2bb67b80 | 1077 | wxString BeforeLast(wxChar ch) const; |
3c67202d VZ |
1078 | // get all characters after the first occurence of ch |
1079 | // (returns empty string if ch not found) | |
2bb67b80 | 1080 | wxString AfterFirst(wxChar ch) const; |
3c67202d VZ |
1081 | // get all characters after the last occurence of ch |
1082 | // (returns the whole string if ch not found) | |
2bb67b80 | 1083 | wxString AfterLast(wxChar ch) const; |
3c67202d VZ |
1084 | |
1085 | // for compatibility only, use more explicitly named functions above | |
2bb67b80 OK |
1086 | wxString Before(wxChar ch) const { return BeforeLast(ch); } |
1087 | wxString After(wxChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
1088 | |
1089 | // case conversion | |
c84c52de | 1090 | // convert to upper case in place, return the string itself |
c801d85f | 1091 | wxString& MakeUpper(); |
c84c52de | 1092 | // convert to upper case, return the copy of the string |
03ab016d JS |
1093 | // Here's something to remember: BC++ doesn't like returns in inlines. |
1094 | wxString Upper() const ; | |
c84c52de | 1095 | // convert to lower case in place, return the string itself |
c801d85f | 1096 | wxString& MakeLower(); |
c84c52de | 1097 | // convert to lower case, return the copy of the string |
03ab016d | 1098 | wxString Lower() const ; |
c801d85f | 1099 | |
3c67202d VZ |
1100 | // trimming/padding whitespace (either side) and truncating |
1101 | // remove spaces from left or from right (default) side | |
d775fa82 | 1102 | wxString& Trim(bool bFromRight = true); |
3c67202d | 1103 | // add nCount copies chPad in the beginning or at the end (default) |
d775fa82 | 1104 | wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = true); |
dd1eaa89 | 1105 | |
3c67202d VZ |
1106 | // searching and replacing |
1107 | // searching (return starting index, or -1 if not found) | |
d775fa82 | 1108 | int Find(wxChar ch, bool bFromEnd = false) const; // like strchr/strrchr |
3c67202d | 1109 | // searching (return starting index, or -1 if not found) |
2bb67b80 | 1110 | int Find(const wxChar *pszSub) const; // like strstr |
3c67202d VZ |
1111 | // replace first (or all of bReplaceAll) occurences of substring with |
1112 | // another string, returns the number of replacements made | |
2bb67b80 OK |
1113 | size_t Replace(const wxChar *szOld, |
1114 | const wxChar *szNew, | |
d775fa82 | 1115 | bool bReplaceAll = true); |
3c67202d VZ |
1116 | |
1117 | // check if the string contents matches a mask containing '*' and '?' | |
2bb67b80 | 1118 | bool Matches(const wxChar *szMask) const; |
c801d85f | 1119 | |
d775fa82 | 1120 | // conversion to numbers: all functions return true only if the whole |
538f35cc VZ |
1121 | // string is a number and put the value of this number into the pointer |
1122 | // provided, the base is the numeric base in which the conversion should be | |
1123 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1124 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
cd0b1709 | 1125 | // convert to a signed integer |
538f35cc | 1126 | bool ToLong(long *val, int base = 10) const; |
cd0b1709 | 1127 | // convert to an unsigned integer |
538f35cc | 1128 | bool ToULong(unsigned long *val, int base = 10) const; |
cd0b1709 VZ |
1129 | // convert to a double |
1130 | bool ToDouble(double *val) const; | |
1131 | ||
90e572f1 | 1132 | // formatted input/output |
3c67202d | 1133 | // as sprintf(), returns the number of characters written or < 0 on error |
7357f981 GD |
1134 | // (take 'this' into account in attribute parameter count) |
1135 | int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
3c67202d | 1136 | // as vprintf(), returns the number of characters written or < 0 on error |
2bb67b80 | 1137 | int PrintfV(const wxChar* pszFormat, va_list argptr); |
dd1eaa89 | 1138 | |
341e7d28 | 1139 | // returns the string containing the result of Printf() to it |
7357f981 | 1140 | static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; |
341e7d28 VZ |
1141 | // the same as above, but takes a va_list |
1142 | static wxString FormatV(const wxChar *pszFormat, va_list argptr); | |
1143 | ||
3c67202d VZ |
1144 | // raw access to string memory |
1145 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 1146 | // only works if the data of this string is not shared |
e87b7833 | 1147 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } |
3c67202d | 1148 | // minimize the string's memory |
dd1eaa89 | 1149 | // only works if the data of this string is not shared |
b1801e0e | 1150 | bool Shrink(); |
e87b7833 | 1151 | #if !wxUSE_STL |
3c67202d VZ |
1152 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
1153 | // a.s.a.p. to put string back in a reasonable state! | |
2bb67b80 | 1154 | wxChar *GetWriteBuf(size_t nLen); |
3c67202d | 1155 | // call this immediately after GetWriteBuf() has been used |
8fd0f20b | 1156 | void UngetWriteBuf(); |
8f06a017 | 1157 | void UngetWriteBuf(size_t nLen); |
e87b7833 | 1158 | #endif |
c801d85f | 1159 | |
77ffb593 | 1160 | // wxWidgets version 1 compatibility functions |
3c67202d VZ |
1161 | |
1162 | // use Mid() | |
1163 | wxString SubString(size_t from, size_t to) const | |
1164 | { return Mid(from, (to - from + 1)); } | |
1165 | // values for second parameter of CompareTo function | |
c801d85f | 1166 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 1167 | // values for first parameter of Strip function |
c801d85f | 1168 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 1169 | |
7357f981 GD |
1170 | // use Printf() |
1171 | // (take 'this' into account in attribute parameter count) | |
1172 | int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
c801d85f | 1173 | |
3c67202d | 1174 | // use Cmp() |
2bb67b80 | 1175 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 1176 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 1177 | |
3c67202d | 1178 | // use Len |
e87b7833 | 1179 | size_t Length() const { return length(); } |
3c67202d | 1180 | // Count the number of characters |
2bb67b80 | 1181 | int Freq(wxChar ch) const; |
3c67202d | 1182 | // use MakeLower |
c801d85f | 1183 | void LowerCase() { MakeLower(); } |
3c67202d | 1184 | // use MakeUpper |
c801d85f | 1185 | void UpperCase() { MakeUpper(); } |
3c67202d | 1186 | // use Trim except that it doesn't change this string |
c801d85f KB |
1187 | wxString Strip(stripType w = trailing) const; |
1188 | ||
3c67202d | 1189 | // use Find (more general variants not yet supported) |
2bb67b80 OK |
1190 | size_t Index(const wxChar* psz) const { return Find(psz); } |
1191 | size_t Index(wxChar ch) const { return Find(ch); } | |
3c67202d | 1192 | // use Truncate |
c801d85f | 1193 | wxString& Remove(size_t pos) { return Truncate(pos); } |
e87b7833 | 1194 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } |
c801d85f | 1195 | |
e87b7833 MB |
1196 | wxString& Remove(size_t nStart, size_t nLen) |
1197 | { return (wxString&)erase( nStart, nLen ); } | |
dd1eaa89 | 1198 | |
3c67202d | 1199 | // use Find() |
2bb67b80 OK |
1200 | int First( const wxChar ch ) const { return Find(ch); } |
1201 | int First( const wxChar* psz ) const { return Find(psz); } | |
3ed358cb | 1202 | int First( const wxString &str ) const { return Find(str); } |
d775fa82 WS |
1203 | int Last( const wxChar ch ) const { return Find(ch, true); } |
1204 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } | |
c801d85f | 1205 | |
5a8231ef WS |
1206 | // use empty() |
1207 | bool IsNull() const { return empty(); } | |
c801d85f | 1208 | |
3c67202d | 1209 | // std::string compatibility functions |
dd1eaa89 | 1210 | |
3c67202d VZ |
1211 | // take nLen chars starting at nPos |
1212 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
e87b7833 | 1213 | : wxStringBase(str, nPos, nLen) { } |
3c67202d | 1214 | // take all characters from pStart to pEnd |
e87b7833 | 1215 | wxString(const void *pStart, const void *pEnd) |
6e94fff9 | 1216 | : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { } |
e87b7833 MB |
1217 | #if wxUSE_STL |
1218 | wxString(const_iterator first, const_iterator last) | |
1219 | : wxStringBase(first, last) { } | |
1220 | #endif | |
5460b9e3 | 1221 | |
3c67202d | 1222 | // lib.string.modifiers |
3c67202d VZ |
1223 | // append elements str[pos], ..., str[pos+n] |
1224 | wxString& append(const wxString& str, size_t pos, size_t n) | |
e87b7833 MB |
1225 | { return (wxString&)wxStringBase::append(str, pos, n); } |
1226 | // append a string | |
1227 | wxString& append(const wxString& str) | |
1228 | { return (wxString&)wxStringBase::append(str); } | |
3c67202d | 1229 | // append first n (or all if n == npos) characters of sz |
e87b7833 MB |
1230 | wxString& append(const wxChar *sz) |
1231 | { return (wxString&)wxStringBase::append(sz); } | |
1232 | wxString& append(const wxChar *sz, size_t n) | |
1233 | { return (wxString&)wxStringBase::append(sz, n); } | |
3c67202d | 1234 | // append n copies of ch |
e87b7833 MB |
1235 | wxString& append(size_t n, wxChar ch) |
1236 | { return (wxString&)wxStringBase::append(n, ch); } | |
1237 | // append from first to last | |
1238 | wxString& append(const_iterator first, const_iterator last) | |
1239 | { return (wxString&)wxStringBase::append(first, last); } | |
3c67202d VZ |
1240 | |
1241 | // same as `this_string = str' | |
735d1db6 | 1242 | wxString& assign(const wxString& str) |
e87b7833 | 1243 | { return (wxString&)wxStringBase::assign(str); } |
3c67202d VZ |
1244 | // same as ` = str[pos..pos + n] |
1245 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
e87b7833 | 1246 | { return (wxString&)wxStringBase::assign(str, pos, n); } |
3c67202d | 1247 | // same as `= first n (or all if n == npos) characters of sz' |
e87b7833 MB |
1248 | wxString& assign(const wxChar *sz) |
1249 | { return (wxString&)wxStringBase::assign(sz); } | |
1250 | wxString& assign(const wxChar *sz, size_t n) | |
1251 | { return (wxString&)wxStringBase::assign(sz, n); } | |
3c67202d | 1252 | // same as `= n copies of ch' |
2bb67b80 | 1253 | wxString& assign(size_t n, wxChar ch) |
e87b7833 MB |
1254 | { return (wxString&)wxStringBase::assign(n, ch); } |
1255 | // assign from first to last | |
1256 | wxString& assign(const_iterator first, const_iterator last) | |
1257 | { return (wxString&)wxStringBase::assign(first, last); } | |
1258 | ||
1259 | // string comparison | |
963ad140 | 1260 | #if !defined(HAVE_STD_STRING_COMPARE) |
e87b7833 MB |
1261 | int compare(const wxStringBase& str) const; |
1262 | // comparison with a substring | |
1263 | int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; | |
1264 | // comparison of 2 substrings | |
1265 | int compare(size_t nStart, size_t nLen, | |
1266 | const wxStringBase& str, size_t nStart2, size_t nLen2) const; | |
1267 | // just like strcmp() | |
1268 | int compare(const wxChar* sz) const; | |
1269 | // substring comparison with first nCount characters of sz | |
1270 | int compare(size_t nStart, size_t nLen, | |
1271 | const wxChar* sz, size_t nCount = npos) const; | |
1272 | #endif // !defined HAVE_STD_STRING_COMPARE | |
3c67202d VZ |
1273 | |
1274 | // insert another string | |
e87b7833 MB |
1275 | wxString& insert(size_t nPos, const wxString& str) |
1276 | { return (wxString&)wxStringBase::insert(nPos, str); } | |
3c67202d VZ |
1277 | // insert n chars of str starting at nStart (in str) |
1278 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
e87b7833 | 1279 | { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); } |
3c67202d | 1280 | // insert first n (or all if n == npos) characters of sz |
e87b7833 MB |
1281 | wxString& insert(size_t nPos, const wxChar *sz) |
1282 | { return (wxString&)wxStringBase::insert(nPos, sz); } | |
1283 | wxString& insert(size_t nPos, const wxChar *sz, size_t n) | |
1284 | { return (wxString&)wxStringBase::insert(nPos, sz, n); } | |
3c67202d | 1285 | // insert n copies of ch |
2bb67b80 | 1286 | wxString& insert(size_t nPos, size_t n, wxChar ch) |
e87b7833 MB |
1287 | { return (wxString&)wxStringBase::insert(nPos, n, ch); } |
1288 | iterator insert(iterator it, wxChar ch) | |
1c2d1459 | 1289 | { return wxStringBase::insert(it, ch); } |
e87b7833 MB |
1290 | void insert(iterator it, const_iterator first, const_iterator last) |
1291 | { wxStringBase::insert(it, first, last); } | |
1292 | void insert(iterator it, size_type n, wxChar ch) | |
1293 | { wxStringBase::insert(it, n, ch); } | |
3c67202d VZ |
1294 | |
1295 | // delete characters from nStart to nStart + nLen | |
e87b7833 MB |
1296 | wxString& erase(size_type pos = 0, size_type n = npos) |
1297 | { return (wxString&)wxStringBase::erase(pos, n); } | |
1298 | iterator erase(iterator first, iterator last) | |
1299 | { return wxStringBase::erase(first, last); } | |
1300 | iterator erase(iterator first) | |
1301 | { return wxStringBase::erase(first); } | |
1302 | ||
1303 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
1304 | void clear() { erase(); } | |
1305 | #endif | |
3c67202d VZ |
1306 | |
1307 | // replaces the substring of length nLen starting at nStart | |
e87b7833 MB |
1308 | wxString& replace(size_t nStart, size_t nLen, const wxChar* sz) |
1309 | { return (wxString&)wxStringBase::replace(nStart, nLen, sz); } | |
1310 | // replaces the substring of length nLen starting at nStart | |
1311 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
1312 | { return (wxString&)wxStringBase::replace(nStart, nLen, str); } | |
3c67202d | 1313 | // replaces the substring with nCount copies of ch |
e87b7833 MB |
1314 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch) |
1315 | { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); } | |
3c67202d VZ |
1316 | // replaces a substring with another substring |
1317 | wxString& replace(size_t nStart, size_t nLen, | |
e87b7833 MB |
1318 | const wxString& str, size_t nStart2, size_t nLen2) |
1319 | { return (wxString&)wxStringBase::replace(nStart, nLen, str, | |
1320 | nStart2, nLen2); } | |
1321 | // replaces the substring with first nCount chars of sz | |
fb20fa43 | 1322 | wxString& replace(size_t nStart, size_t nLen, |
e87b7833 MB |
1323 | const wxChar* sz, size_t nCount) |
1324 | { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); } | |
1325 | wxString& replace(iterator first, iterator last, const_pointer s) | |
1326 | { return (wxString&)wxStringBase::replace(first, last, s); } | |
1327 | wxString& replace(iterator first, iterator last, const_pointer s, | |
1328 | size_type n) | |
1329 | { return (wxString&)wxStringBase::replace(first, last, s, n); } | |
1330 | wxString& replace(iterator first, iterator last, const wxString& s) | |
1331 | { return (wxString&)wxStringBase::replace(first, last, s); } | |
1332 | wxString& replace(iterator first, iterator last, size_type n, wxChar c) | |
1333 | { return (wxString&)wxStringBase::replace(first, last, n, c); } | |
1334 | wxString& replace(iterator first, iterator last, | |
1335 | const_iterator first1, const_iterator last1) | |
1336 | { return (wxString&)wxStringBase::replace(first, last, first1, last1); } | |
3c67202d | 1337 | |
e87b7833 MB |
1338 | // string += string |
1339 | wxString& operator+=(const wxString& s) | |
1340 | { return (wxString&)wxStringBase::operator+=(s); } | |
1341 | // string += C string | |
1342 | wxString& operator+=(const wxChar *psz) | |
1343 | { return (wxString&)wxStringBase::operator+=(psz); } | |
1344 | // string += char | |
1345 | wxString& operator+=(wxChar ch) | |
1346 | { return (wxString&)wxStringBase::operator+=(ch); } | |
c801d85f KB |
1347 | }; |
1348 | ||
1fc8cfbd VZ |
1349 | // notice that even though for many compilers the friend declarations above are |
1350 | // enough, from the point of view of C++ standard we must have the declarations | |
1351 | // here as friend ones are not injected in the enclosing namespace and without | |
1352 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
b7146cbe VZ |
1353 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
1354 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); | |
1355 | wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); | |
1356 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); | |
1357 | wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); | |
1fc8cfbd | 1358 | |
b7146cbe | 1359 | |
df5168c4 MB |
1360 | // define wxArrayString, for compatibility |
1361 | #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL | |
1362 | #include "wx/arrstr.h" | |
ba8c1601 | 1363 | #endif |
9d155f50 | 1364 | |
07170120 VZ |
1365 | #if wxUSE_STL |
1366 | // return an empty wxString (not very useful with wxUSE_STL == 1) | |
12f99210 | 1367 | inline const wxString wxGetEmptyString() { return wxString(); } |
07170120 VZ |
1368 | #else // !wxUSE_STL |
1369 | // return an empty wxString (more efficient than wxString() here) | |
1370 | inline const wxString& wxGetEmptyString() | |
1371 | { | |
1372 | return *(wxString *)&wxEmptyString; | |
1373 | } | |
1374 | #endif // wxUSE_STL/!wxUSE_STL | |
1375 | ||
1d218550 VZ |
1376 | // ---------------------------------------------------------------------------- |
1377 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
1378 | // ---------------------------------------------------------------------------- | |
1379 | ||
941b78cc MB |
1380 | #if wxUSE_STL |
1381 | ||
1382 | class WXDLLIMPEXP_BASE wxStringBuffer | |
1383 | { | |
1384 | public: | |
1385 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
e87b7833 | 1386 | : m_str(str), m_buf(lenWanted) |
941b78cc MB |
1387 | { } |
1388 | ||
fe5fc682 | 1389 | ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); } |
941b78cc MB |
1390 | |
1391 | operator wxChar*() { return m_buf.data(); } | |
1392 | ||
1393 | private: | |
1394 | wxString& m_str; | |
1395 | #if wxUSE_UNICODE | |
1396 | wxWCharBuffer m_buf; | |
1397 | #else | |
1398 | wxCharBuffer m_buf; | |
1399 | #endif | |
941b78cc MB |
1400 | |
1401 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1402 | }; | |
1403 | ||
1404 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
1405 | { | |
1406 | public: | |
1407 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
1408 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
1409 | { } | |
1410 | ||
1411 | ~wxStringBufferLength() | |
1412 | { | |
1413 | wxASSERT(m_lenSet); | |
1414 | m_str.assign(m_buf.data(), m_len); | |
1415 | } | |
1416 | ||
1417 | operator wxChar*() { return m_buf.data(); } | |
1418 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
1419 | ||
1420 | private: | |
1421 | wxString& m_str; | |
1422 | #if wxUSE_UNICODE | |
1423 | wxWCharBuffer m_buf; | |
1424 | #else | |
1425 | wxCharBuffer m_buf; | |
1426 | #endif | |
1427 | size_t m_len; | |
1428 | bool m_lenSet; | |
1429 | ||
1430 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
1431 | }; | |
1432 | ||
1433 | #else // if !wxUSE_STL | |
1434 | ||
bddd7a8d | 1435 | class WXDLLIMPEXP_BASE wxStringBuffer |
1d218550 VZ |
1436 | { |
1437 | public: | |
1438 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
b1801e0e GD |
1439 | : m_str(str), m_buf(NULL) |
1440 | { m_buf = m_str.GetWriteBuf(lenWanted); } | |
e015c2a3 | 1441 | |
1d218550 | 1442 | ~wxStringBuffer() { m_str.UngetWriteBuf(); } |
e015c2a3 | 1443 | |
1d218550 | 1444 | operator wxChar*() const { return m_buf; } |
e015c2a3 | 1445 | |
1d218550 VZ |
1446 | private: |
1447 | wxString& m_str; | |
1448 | wxChar *m_buf; | |
e015c2a3 VZ |
1449 | |
1450 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1d218550 VZ |
1451 | }; |
1452 | ||
941b78cc MB |
1453 | class WXDLLIMPEXP_BASE wxStringBufferLength |
1454 | { | |
1455 | public: | |
1456 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
1457 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
4055ed82 WS |
1458 | { |
1459 | m_buf = m_str.GetWriteBuf(lenWanted); | |
494b978f RN |
1460 | wxASSERT(m_buf != NULL); |
1461 | } | |
941b78cc MB |
1462 | |
1463 | ~wxStringBufferLength() | |
1464 | { | |
1465 | wxASSERT(m_lenSet); | |
1466 | m_str.UngetWriteBuf(m_len); | |
1467 | } | |
1468 | ||
1469 | operator wxChar*() const { return m_buf; } | |
1470 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
1471 | ||
1472 | private: | |
1473 | wxString& m_str; | |
1474 | wxChar *m_buf; | |
1475 | size_t m_len; | |
1476 | bool m_lenSet; | |
1477 | ||
1478 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
1479 | }; | |
1480 | ||
1481 | #endif // !wxUSE_STL | |
1482 | ||
c801d85f | 1483 | // --------------------------------------------------------------------------- |
3c67202d | 1484 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 1485 | // --------------------------------------------------------------------------- |
f33fee2a | 1486 | |
b6339dde VZ |
1487 | // note that when wxUSE_STL == 1 the comparison operators taking std::string |
1488 | // are used and defining them also for wxString would only result in | |
1489 | // compilation ambiguities when comparing std::string and wxString | |
1490 | #if !wxUSE_STL | |
e87b7833 | 1491 | |
0cbe5ee3 VZ |
1492 | inline bool operator==(const wxString& s1, const wxString& s2) |
1493 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
1494 | inline bool operator==(const wxString& s1, const wxChar * s2) | |
1495 | { return s1.Cmp(s2) == 0; } | |
1496 | inline bool operator==(const wxChar * s1, const wxString& s2) | |
1497 | { return s2.Cmp(s1) == 0; } | |
1498 | inline bool operator!=(const wxString& s1, const wxString& s2) | |
1499 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
1500 | inline bool operator!=(const wxString& s1, const wxChar * s2) | |
1501 | { return s1.Cmp(s2) != 0; } | |
1502 | inline bool operator!=(const wxChar * s1, const wxString& s2) | |
1503 | { return s2.Cmp(s1) != 0; } | |
1504 | inline bool operator< (const wxString& s1, const wxString& s2) | |
1505 | { return s1.Cmp(s2) < 0; } | |
1506 | inline bool operator< (const wxString& s1, const wxChar * s2) | |
1507 | { return s1.Cmp(s2) < 0; } | |
1508 | inline bool operator< (const wxChar * s1, const wxString& s2) | |
1509 | { return s2.Cmp(s1) > 0; } | |
1510 | inline bool operator> (const wxString& s1, const wxString& s2) | |
1511 | { return s1.Cmp(s2) > 0; } | |
1512 | inline bool operator> (const wxString& s1, const wxChar * s2) | |
1513 | { return s1.Cmp(s2) > 0; } | |
1514 | inline bool operator> (const wxChar * s1, const wxString& s2) | |
1515 | { return s2.Cmp(s1) < 0; } | |
1516 | inline bool operator<=(const wxString& s1, const wxString& s2) | |
1517 | { return s1.Cmp(s2) <= 0; } | |
1518 | inline bool operator<=(const wxString& s1, const wxChar * s2) | |
1519 | { return s1.Cmp(s2) <= 0; } | |
1520 | inline bool operator<=(const wxChar * s1, const wxString& s2) | |
1521 | { return s2.Cmp(s1) >= 0; } | |
1522 | inline bool operator>=(const wxString& s1, const wxString& s2) | |
1523 | { return s1.Cmp(s2) >= 0; } | |
1524 | inline bool operator>=(const wxString& s1, const wxChar * s2) | |
1525 | { return s1.Cmp(s2) >= 0; } | |
1526 | inline bool operator>=(const wxChar * s1, const wxString& s2) | |
1527 | { return s2.Cmp(s1) <= 0; } | |
3c67202d | 1528 | |
5ca07d0f OK |
1529 | #if wxUSE_UNICODE |
1530 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 1531 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 1532 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 1533 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
1534 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
1535 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
1536 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
1537 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
0cbe5ee3 | 1538 | #else // !wxUSE_UNICODE |
5ca07d0f | 1539 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 1540 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 1541 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 1542 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
1543 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
1544 | { return (s1.Cmp((const char *)s2) != 0); } | |
1545 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
1546 | { return (s2.Cmp((const char *)s1) != 0); } | |
0cbe5ee3 | 1547 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
5ca07d0f | 1548 | |
028a2b5d | 1549 | #if wxUSE_UNICODE |
6d56eb5c | 1550 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 1551 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 1552 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 1553 | { return (const wchar_t *)buf + string; } |
0cbe5ee3 | 1554 | #else // !wxUSE_UNICODE |
6d56eb5c | 1555 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 1556 | { return string + (const char *)buf; } |
6d56eb5c | 1557 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 1558 | { return (const char *)buf + string; } |
0cbe5ee3 | 1559 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
f04f3991 | 1560 | |
e51b17a1 VZ |
1561 | #endif // !wxUSE_STL |
1562 | ||
1563 | // comparison with char (those are not defined by std::[w]string and so should | |
1564 | // be always available) | |
1565 | inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); } | |
1566 | inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); } | |
1567 | inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); } | |
1568 | inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } | |
1569 | ||
c801d85f | 1570 | // --------------------------------------------------------------------------- |
3c67202d | 1571 | // Implementation only from here until the end of file |
c801d85f KB |
1572 | // --------------------------------------------------------------------------- |
1573 | ||
e90c1d2a | 1574 | // don't pollute the library user's name space |
5737d05f | 1575 | #undef wxASSERT_VALID_INDEX |
e90c1d2a | 1576 | |
e87b7833 | 1577 | #if wxUSE_STD_IOSTREAM |
c801d85f | 1578 | |
65f19af1 | 1579 | #include "wx/iosfwrap.h" |
c801d85f | 1580 | |
bddd7a8d VZ |
1581 | WXDLLIMPEXP_BASE wxSTD istream& operator>>(wxSTD istream&, wxString&); |
1582 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); | |
c801d85f | 1583 | |
3c67202d | 1584 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 1585 | |
34138703 | 1586 | #endif // _WX_WXSTRINGH__ |