]>
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 | ||
e3f6cbd9 VS |
54 | #include "wx/wxchar.h" // for wxChar, wxStrlen() etc. |
55 | #include "wx/unichar.h" | |
c9f78968 | 56 | #include "wx/strvararg.h" |
e90c1d2a VZ |
57 | #include "wx/buffer.h" // for wxCharBuffer |
58 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
3f4a0c5b | 59 | |
fb3c9042 VZ |
60 | class WXDLLIMPEXP_BASE wxString; |
61 | ||
c801d85f KB |
62 | // --------------------------------------------------------------------------- |
63 | // macros | |
64 | // --------------------------------------------------------------------------- | |
65 | ||
5737d05f VZ |
66 | // casts [unfortunately!] needed to call some broken functions which require |
67 | // "char *" instead of "const char *" | |
2bb67b80 | 68 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
e90c1d2a VZ |
69 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) |
70 | #define wxMBSTRINGCAST (char *)(const char *) | |
71 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 72 | |
3c67202d | 73 | // implementation only |
5737d05f | 74 | #define wxASSERT_VALID_INDEX(i) \ |
e87b7833 | 75 | wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") ) |
c801d85f | 76 | |
e90c1d2a VZ |
77 | // ---------------------------------------------------------------------------- |
78 | // constants | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
13874b5c VZ |
81 | #if WXWIN_COMPATIBILITY_2_6 |
82 | ||
83 | // deprecated in favour of wxString::npos, don't use in new code | |
84 | // | |
e90c1d2a | 85 | // maximum possible length for a string means "take all string" everywhere |
2cbfa061 | 86 | #define wxSTRING_MAXLEN wxStringBase::npos |
66b6b045 | 87 | |
13874b5c VZ |
88 | #endif // WXWIN_COMPATIBILITY_2_6 |
89 | ||
e90c1d2a VZ |
90 | // ---------------------------------------------------------------------------- |
91 | // global data | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
94 | // global pointer to empty string | |
bddd7a8d | 95 | extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString; |
6001e347 | 96 | |
c801d85f | 97 | // --------------------------------------------------------------------------- |
e90c1d2a | 98 | // global functions complementing standard C string library replacements for |
3c67202d VZ |
99 | // strlen() and portable strcasecmp() |
100 | //--------------------------------------------------------------------------- | |
e90c1d2a | 101 | |
c7554da8 | 102 | #if WXWIN_COMPATIBILITY_2_8 |
e3f6cbd9 | 103 | // Use wxXXX() functions from wxcrt.h instead! These functions are for |
e90c1d2a | 104 | // backwards compatibility only. |
88150e60 | 105 | |
3c67202d | 106 | // checks whether the passed in pointer is NULL and if the string is empty |
c7554da8 | 107 | wxDEPRECATED( inline bool IsEmpty(const char *p) ); |
6d56eb5c | 108 | inline bool IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 109 | |
3c67202d | 110 | // safe version of strlen() (returns 0 if passed NULL pointer) |
c7554da8 | 111 | wxDEPRECATED( inline size_t Strlen(const char *psz) ); |
6d56eb5c | 112 | inline size_t Strlen(const char *psz) |
c801d85f KB |
113 | { return psz ? strlen(psz) : 0; } |
114 | ||
3c67202d | 115 | // portable strcasecmp/_stricmp |
c7554da8 | 116 | wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) ); |
6d56eb5c | 117 | inline int Stricmp(const char *psz1, const char *psz2) |
dd1eaa89 | 118 | { |
7f0586ef JS |
119 | #if defined(__VISUALC__) && defined(__WXWINCE__) |
120 | register char c1, c2; | |
121 | do { | |
122 | c1 = tolower(*psz1++); | |
123 | c2 = tolower(*psz2++); | |
124 | } while ( c1 && (c1 == c2) ); | |
125 | ||
126 | return c1 - c2; | |
127 | #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) | |
dd1eaa89 | 128 | return _stricmp(psz1, psz2); |
91b8de8d | 129 | #elif defined(__SC__) |
2432b92d | 130 | return _stricmp(psz1, psz2); |
91b8de8d | 131 | #elif defined(__SALFORDC__) |
a3ef5bf5 | 132 | return stricmp(psz1, psz2); |
dd1eaa89 VZ |
133 | #elif defined(__BORLANDC__) |
134 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
135 | #elif defined(__WATCOMC__) |
136 | return stricmp(psz1, psz2); | |
14704334 VS |
137 | #elif defined(__DJGPP__) |
138 | return stricmp(psz1, psz2); | |
91b8de8d RR |
139 | #elif defined(__EMX__) |
140 | return stricmp(psz1, psz2); | |
e2c87f4c | 141 | #elif defined(__WXPM__) |
1777b9bb | 142 | return stricmp(psz1, psz2); |
a0be6908 WS |
143 | #elif defined(__WXPALMOS__) || \ |
144 | defined(HAVE_STRCASECMP_IN_STRING_H) || \ | |
1c2d1459 VZ |
145 | defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ |
146 | defined(__GNUWIN32__) | |
dd1eaa89 | 147 | return strcasecmp(psz1, psz2); |
8be97d65 | 148 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
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; | |
dd1eaa89 VZ |
156 | #else |
157 | // almost all compilers/libraries provide this function (unfortunately under | |
158 | // different names), that's why we don't implement our own which will surely | |
159 | // be more efficient than this code (uncomment to use): | |
160 | /* | |
161 | register char c1, c2; | |
162 | do { | |
163 | c1 = tolower(*psz1++); | |
164 | c2 = tolower(*psz2++); | |
165 | } while ( c1 && (c1 == c2) ); | |
166 | ||
167 | return c1 - c2; | |
168 | */ | |
169 | ||
170 | #error "Please define string case-insensitive compare for your OS/compiler" | |
171 | #endif // OS/compiler | |
172 | } | |
c801d85f | 173 | |
c7554da8 VS |
174 | #endif // WXWIN_COMPATIBILITY_2_8 |
175 | ||
ce76f779 VZ |
176 | // ---------------------------------------------------------------------------- |
177 | // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING | |
178 | // ---------------------------------------------------------------------------- | |
179 | ||
992527a5 VS |
180 | // FIXME-UTF8: using std::string as wxString base class is currently broken, |
181 | // so we use the standard wxString with std::string conversion | |
182 | // enabled, this is API-compatible. | |
183 | #define wxUSE_STL_BASED_WXSTRING 0 | |
184 | #if wxUSE_STL | |
185 | #undef wxUSE_STD_STRING | |
186 | #define wxUSE_STD_STRING 1 | |
187 | #endif | |
188 | //#define wxUSE_STL_BASED_WXSTRING wxUSE_STL | |
189 | ||
ce76f779 | 190 | // in both cases we need to define wxStdString |
992527a5 | 191 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING |
e87b7833 MB |
192 | |
193 | #include "wx/beforestd.h" | |
194 | #include <string> | |
195 | #include "wx/afterstd.h" | |
196 | ||
197 | #if wxUSE_UNICODE | |
0c7cfd99 | 198 | #ifdef HAVE_STD_WSTRING |
ce76f779 | 199 | typedef std::wstring wxStdString; |
e87b7833 | 200 | #else |
ce76f779 | 201 | typedef std::basic_string<wxChar> wxStdString; |
e87b7833 MB |
202 | #endif |
203 | #else | |
ce76f779 | 204 | typedef std::string wxStdString; |
e87b7833 MB |
205 | #endif |
206 | ||
ce76f779 | 207 | #endif // need <string> |
e87b7833 | 208 | |
992527a5 | 209 | #if wxUSE_STL_BASED_WXSTRING |
ce76f779 VZ |
210 | |
211 | // we don't need an extra ctor from std::string when copy ctor already does | |
212 | // the work | |
213 | #undef wxUSE_STD_STRING | |
7147aa47 | 214 | #define wxUSE_STD_STRING 0 |
ce76f779 VZ |
215 | |
216 | #if (defined(__GNUG__) && (__GNUG__ < 3)) || \ | |
217 | (defined(_MSC_VER) && (_MSC_VER <= 1200)) | |
218 | #define wxSTRING_BASE_HASNT_CLEAR | |
219 | #endif | |
220 | ||
221 | typedef wxStdString wxStringBase; | |
992527a5 | 222 | #else // if !wxUSE_STL_BASED_WXSTRING |
e87b7833 | 223 | |
47561b0d | 224 | #if !defined(HAVE_STD_STRING_COMPARE) && \ |
992527a5 | 225 | (!defined(__WX_SETUP_H__) || wxUSE_STL_BASED_WXSTRING == 0) |
e87b7833 MB |
226 | #define HAVE_STD_STRING_COMPARE |
227 | #endif | |
228 | ||
c801d85f | 229 | // --------------------------------------------------------------------------- |
f04f3991 | 230 | // string data prepended with some housekeeping info (used by wxString class), |
c801d85f KB |
231 | // is never used directly (but had to be put here to allow inlining) |
232 | // --------------------------------------------------------------------------- | |
e90c1d2a | 233 | |
bddd7a8d | 234 | struct WXDLLIMPEXP_BASE wxStringData |
c801d85f KB |
235 | { |
236 | int nRefs; // reference count | |
3c024cc2 | 237 | size_t nDataLength, // actual string length |
c801d85f KB |
238 | nAllocLength; // allocated memory size |
239 | ||
2bb67b80 OK |
240 | // mimics declaration 'wxChar data[nAllocLength]' |
241 | wxChar* data() const { return (wxChar*)(this + 1); } | |
c801d85f KB |
242 | |
243 | // empty string has a special ref count so it's never deleted | |
dbda9e86 JS |
244 | bool IsEmpty() const { return (nRefs == -1); } |
245 | bool IsShared() const { return (nRefs > 1); } | |
c801d85f KB |
246 | |
247 | // lock/unlock | |
dd1eaa89 | 248 | void Lock() { if ( !IsEmpty() ) nRefs++; } |
f6bcfd97 | 249 | |
8ecf21b7 | 250 | // VC++ will refuse to inline Unlock but profiling shows that it is wrong |
f6bcfd97 | 251 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
1c2d1459 | 252 | __forceinline |
f6bcfd97 | 253 | #endif |
ca5e07c7 GD |
254 | // VC++ free must take place in same DLL as allocation when using non dll |
255 | // run-time library (e.g. Multithreaded instead of Multithreaded DLL) | |
8ecf21b7 GD |
256 | #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL) |
257 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); } | |
ca5e07c7 GD |
258 | // we must not inline deallocation since allocation is not inlined |
259 | void Free(); | |
8ecf21b7 | 260 | #else |
f1317a5d | 261 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); } |
8ecf21b7 | 262 | #endif |
8fd0f20b | 263 | |
dd1eaa89 | 264 | // if we had taken control over string memory (GetWriteBuf), it's |
8fd0f20b | 265 | // intentionally put in invalid state |
dbda9e86 JS |
266 | void Validate(bool b) { nRefs = (b ? 1 : 0); } |
267 | bool IsValid() const { return (nRefs != 0); } | |
c801d85f KB |
268 | }; |
269 | ||
e87b7833 | 270 | class WXDLLIMPEXP_BASE wxStringBase |
3c67202d | 271 | { |
5c0217af SC |
272 | public : |
273 | // an 'invalid' value for string index, moved to this place due to a CW bug | |
274 | static const size_t npos; | |
e87b7833 | 275 | protected: |
dd1eaa89 | 276 | // points to data preceded by wxStringData structure with ref count info |
2bb67b80 | 277 | wxChar *m_pchData; |
dd1eaa89 VZ |
278 | |
279 | // accessor to string data | |
280 | wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; } | |
281 | ||
6b95b20d VZ |
282 | // string (re)initialization functions |
283 | // initializes the string to the empty value (must be called only from | |
284 | // ctors, use Reinit() otherwise) | |
e90c1d2a | 285 | void Init() { m_pchData = (wxChar *)wxEmptyString; } |
90e572f1 | 286 | // initializes the string with (a part of) C-string |
e87b7833 | 287 | void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = npos); |
6b95b20d VZ |
288 | // as Init, but also frees old data |
289 | void Reinit() { GetStringData()->Unlock(); Init(); } | |
290 | ||
291 | // memory allocation | |
b1801e0e GD |
292 | // allocates memory for string of length nLen |
293 | bool AllocBuffer(size_t nLen); | |
6b95b20d | 294 | // copies data to another string |
b1801e0e | 295 | bool AllocCopy(wxString&, int, int) const; |
6b95b20d | 296 | // effectively copies data to string |
b1801e0e | 297 | bool AssignCopy(size_t, const wxChar *); |
6b95b20d VZ |
298 | |
299 | // append a (sub)string | |
e87b7833 MB |
300 | bool ConcatSelf(size_t nLen, const wxChar *src, size_t nMaxLen); |
301 | bool ConcatSelf(size_t nLen, const wxChar *src) | |
302 | { return ConcatSelf(nLen, src, nLen); } | |
6b95b20d VZ |
303 | |
304 | // functions called before writing to the string: they copy it if there | |
305 | // are other references to our data (should be the only owner when writing) | |
b1801e0e GD |
306 | bool CopyBeforeWrite(); |
307 | bool AllocBeforeWrite(size_t); | |
6b95b20d | 308 | |
e87b7833 MB |
309 | // compatibility with wxString |
310 | bool Alloc(size_t nLen); | |
311 | public: | |
312 | // standard types | |
c9f78968 VS |
313 | typedef wxUniChar value_type; |
314 | typedef wxUniChar char_type; | |
315 | typedef wxUniCharRef reference; | |
316 | typedef wxChar* pointer; | |
317 | typedef const wxChar* const_pointer; | |
318 | ||
e87b7833 | 319 | typedef size_t size_type; |
c9f78968 VS |
320 | typedef wxUniChar const_reference; |
321 | ||
322 | #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \ | |
323 | reference_type, reference_ctor) \ | |
324 | public: \ | |
325 | typedef wxUniChar value_type; \ | |
326 | typedef reference_type reference; \ | |
327 | typedef pointer_type pointer; \ | |
328 | \ | |
329 | iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \ | |
330 | \ | |
331 | reference operator*() const { return reference_ctor; } \ | |
332 | \ | |
333 | iterator_name& operator++() \ | |
334 | { ++m_cur; return *this; } \ | |
335 | iterator_name operator++(int) \ | |
336 | { iterator_name tmp = *this; ++m_cur; return tmp; } \ | |
337 | iterator_name& operator--() \ | |
338 | { --m_cur; return *this; } \ | |
339 | iterator_name operator--(int) \ | |
340 | { iterator_name tmp = *this; --m_cur; return tmp; } \ | |
341 | \ | |
342 | iterator_name operator+(int n) const \ | |
343 | { return iterator_name(m_cur + n); } \ | |
344 | iterator_name operator+(size_t n) const \ | |
345 | { return iterator_name(m_cur + n); } \ | |
346 | iterator_name operator-(int n) const \ | |
347 | { return iterator_name(m_cur - n); } \ | |
348 | iterator_name operator-(size_t n) const \ | |
349 | { return iterator_name(m_cur - n); } \ | |
350 | iterator_name operator+=(int n) \ | |
351 | { m_cur += n; return *this; } \ | |
352 | iterator_name operator+=(size_t n) \ | |
353 | { m_cur += n; return *this; } \ | |
354 | iterator_name operator-=(int n) \ | |
355 | { m_cur -= n; return *this; } \ | |
356 | iterator_name operator-=(size_t n) \ | |
357 | { m_cur -= n; return *this; } \ | |
358 | \ | |
359 | unsigned operator-(const iterator_name& i) const \ | |
360 | { return m_cur - i.m_cur; } \ | |
361 | \ | |
362 | bool operator==(const iterator_name&i) const \ | |
363 | { return m_cur == i.m_cur; } \ | |
364 | bool operator!=(const iterator_name& i) const \ | |
365 | { return m_cur != i.m_cur; } \ | |
366 | \ | |
367 | bool operator<(const iterator_name& i) const \ | |
368 | { return m_cur < i.m_cur; } \ | |
369 | bool operator>(const iterator_name& i) const \ | |
370 | { return m_cur > i.m_cur; } \ | |
371 | bool operator<=(const iterator_name& i) const \ | |
372 | { return m_cur <= i.m_cur; } \ | |
373 | bool operator>=(const iterator_name& i) const \ | |
374 | { return m_cur >= i.m_cur; } \ | |
375 | \ | |
376 | protected: \ | |
377 | /* for internal wxString use only: */ \ | |
378 | iterator_name(pointer ptr) : m_cur(ptr) {} \ | |
379 | operator pointer() const { return m_cur; } \ | |
380 | \ | |
381 | friend class WXDLLIMPEXP_BASE wxString; \ | |
382 | friend class WXDLLIMPEXP_BASE wxStringBase; \ | |
383 | friend class WXDLLIMPEXP_BASE wxCStrData; \ | |
384 | \ | |
385 | protected: \ | |
386 | pointer m_cur; | |
387 | ||
388 | class const_iterator; | |
389 | ||
390 | class iterator | |
391 | { | |
392 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, | |
393 | wxUniCharRef::CreateForString(m_cur)) | |
394 | ||
395 | friend class const_iterator; | |
396 | }; | |
397 | ||
398 | class const_iterator | |
399 | { | |
400 | // NB: reference_type is intentionally value, not reference, the character | |
401 | // may be encoded differently in wxString data: | |
402 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar, | |
403 | wxUniChar(*m_cur)) | |
404 | ||
405 | public: | |
406 | const_iterator(const iterator& i) : m_cur(i.m_cur) {} | |
407 | }; | |
408 | ||
409 | #undef WX_STR_ITERATOR | |
410 | ||
411 | template <typename T> | |
412 | class reverse_iterator_impl | |
413 | { | |
414 | public: | |
415 | typedef T iterator_type; | |
416 | typedef typename T::value_type value_type; | |
417 | typedef typename T::reference reference; | |
418 | typedef typename T::pointer *pointer; | |
419 | ||
420 | reverse_iterator_impl(iterator_type i) : m_cur(i) {} | |
421 | reverse_iterator_impl(const reverse_iterator_impl& ri) | |
422 | : m_cur(ri.m_cur) {} | |
423 | ||
424 | iterator_type base() const { return m_cur; } | |
425 | ||
426 | reference operator*() const { return *(m_cur-1); } | |
968f291b | 427 | |
c9f78968 VS |
428 | reverse_iterator_impl& operator++() |
429 | { --m_cur; return *this; } | |
430 | reverse_iterator_impl operator++(int) | |
431 | { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } | |
432 | reverse_iterator_impl& operator--() | |
433 | { ++m_cur; return *this; } | |
434 | reverse_iterator_impl operator--(int) | |
435 | { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } | |
968f291b | 436 | |
c9f78968 VS |
437 | bool operator==(const reverse_iterator_impl& ri) const |
438 | { return m_cur == ri.m_cur; } | |
439 | bool operator!=(const reverse_iterator_impl& ri) const | |
440 | { return !(*this == ri); } | |
968f291b | 441 | |
c9f78968 VS |
442 | private: |
443 | iterator_type m_cur; | |
444 | }; | |
445 | ||
446 | typedef reverse_iterator_impl<iterator> reverse_iterator; | |
447 | typedef reverse_iterator_impl<const_iterator> const_reverse_iterator; | |
968f291b VZ |
448 | |
449 | ||
3c67202d VZ |
450 | // constructors and destructor |
451 | // ctor for an empty string | |
e87b7833 | 452 | wxStringBase() { Init(); } |
3c67202d | 453 | // copy ctor |
e87b7833 | 454 | wxStringBase(const wxStringBase& stringSrc) |
6b95b20d | 455 | { |
09443a26 VZ |
456 | wxASSERT_MSG( stringSrc.GetStringData()->IsValid(), |
457 | _T("did you forget to call UngetWriteBuf()?") ); | |
6b95b20d | 458 | |
e87b7833 | 459 | if ( stringSrc.empty() ) { |
6b95b20d VZ |
460 | // nothing to do for an empty string |
461 | Init(); | |
462 | } | |
463 | else { | |
464 | m_pchData = stringSrc.m_pchData; // share same data | |
465 | GetStringData()->Lock(); // => one more copy | |
466 | } | |
467 | } | |
3c67202d | 468 | // string containing nRepeat copies of ch |
c9f78968 | 469 | wxStringBase(size_type nRepeat, wxUniChar ch); |
3c67202d | 470 | // ctor takes first nLength characters from C string |
e87b7833 MB |
471 | // (default value of npos means take all the string) |
472 | wxStringBase(const wxChar *psz) | |
473 | { InitWith(psz, 0, npos); } | |
474 | wxStringBase(const wxChar *psz, size_t nLength) | |
b1801e0e | 475 | { InitWith(psz, 0, nLength); } |
830f8f11 VZ |
476 | wxStringBase(const wxChar *psz, |
477 | const wxMBConv& WXUNUSED(conv), | |
478 | size_t nLength = npos) | |
b1801e0e | 479 | { InitWith(psz, 0, nLength); } |
e87b7833 MB |
480 | // take nLen chars starting at nPos |
481 | wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen) | |
482 | { | |
483 | wxASSERT_MSG( str.GetStringData()->IsValid(), | |
484 | _T("did you forget to call UngetWriteBuf()?") ); | |
485 | Init(); | |
486 | size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen; | |
487 | InitWith(str.c_str(), nPos, nLen); | |
488 | } | |
489 | // take all characters from pStart to pEnd | |
490 | wxStringBase(const void *pStart, const void *pEnd); | |
491 | ||
492 | // dtor is not virtual, this class must not be inherited from! | |
1c2d1459 VZ |
493 | ~wxStringBase() |
494 | { | |
7b758e8f | 495 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
1c2d1459 VZ |
496 | //RN - according to the above VC++ does indeed inline this, |
497 | //even though it spits out two warnings | |
498 | #pragma warning (disable:4714) | |
7b758e8f | 499 | #endif |
e87b7833 | 500 | |
1c2d1459 | 501 | GetStringData()->Unlock(); |
7b758e8f RN |
502 | } |
503 | ||
504 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) | |
1c2d1459 VZ |
505 | //re-enable inlining warning |
506 | #pragma warning (default:4714) | |
507 | #endif | |
e87b7833 MB |
508 | // overloaded assignment |
509 | // from another wxString | |
510 | wxStringBase& operator=(const wxStringBase& stringSrc); | |
511 | // from a character | |
c9f78968 | 512 | wxStringBase& operator=(wxUniChar ch); |
e87b7833 MB |
513 | // from a C string |
514 | wxStringBase& operator=(const wxChar *psz); | |
515 | ||
516 | // return the length of the string | |
20aa779e | 517 | size_type length() const { return GetStringData()->nDataLength; } |
e87b7833 | 518 | // return the length of the string |
20aa779e | 519 | size_type size() const { return length(); } |
e87b7833 | 520 | // return the maximum size of the string |
13874b5c | 521 | size_type max_size() const { return npos; } |
e87b7833 | 522 | // resize the string, filling the space with c if c != 0 |
c9f78968 | 523 | void resize(size_t nSize, wxUniChar ch = wxT('\0')); |
e87b7833 MB |
524 | // delete the contents of the string |
525 | void clear() { erase(0, npos); } | |
526 | // returns true if the string is empty | |
20aa779e | 527 | bool empty() const { return length() == 0; } |
e87b7833 MB |
528 | // inform string about planned change in size |
529 | void reserve(size_t sz) { Alloc(sz); } | |
530 | size_type capacity() const { return GetStringData()->nAllocLength; } | |
531 | ||
532 | // lib.string.access | |
533 | // return the character at position n | |
534 | value_type at(size_type n) const | |
535 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } | |
e87b7833 MB |
536 | // returns the writable character at position n |
537 | reference at(size_type n) | |
c9f78968 VS |
538 | { |
539 | wxASSERT_VALID_INDEX( n ); | |
540 | CopyBeforeWrite(); | |
541 | return wxUniCharRef::CreateForString(&m_pchData[n]); | |
542 | } | |
e87b7833 MB |
543 | |
544 | // lib.string.modifiers | |
545 | // append elements str[pos], ..., str[pos+n] | |
546 | wxStringBase& append(const wxStringBase& str, size_t pos, size_t n) | |
547 | { | |
548 | wxASSERT(pos <= str.length()); | |
549 | ConcatSelf(n, str.c_str() + pos, str.length() - pos); | |
550 | return *this; | |
551 | } | |
552 | // append a string | |
553 | wxStringBase& append(const wxStringBase& str) | |
554 | { ConcatSelf(str.length(), str.c_str()); return *this; } | |
555 | // append first n (or all if n == npos) characters of sz | |
556 | wxStringBase& append(const wxChar *sz) | |
557 | { ConcatSelf(wxStrlen(sz), sz); return *this; } | |
558 | wxStringBase& append(const wxChar *sz, size_t n) | |
559 | { ConcatSelf(n, sz); return *this; } | |
560 | // append n copies of ch | |
c9f78968 | 561 | wxStringBase& append(size_t n, wxUniChar ch); |
e87b7833 MB |
562 | // append from first to last |
563 | wxStringBase& append(const_iterator first, const_iterator last) | |
564 | { ConcatSelf(last - first, first); return *this; } | |
565 | ||
566 | // same as `this_string = str' | |
567 | wxStringBase& assign(const wxStringBase& str) | |
568 | { return *this = str; } | |
569 | // same as ` = str[pos..pos + n] | |
570 | wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n) | |
571 | { clear(); return append(str, pos, n); } | |
572 | // same as `= first n (or all if n == npos) characters of sz' | |
573 | wxStringBase& assign(const wxChar *sz) | |
574 | { clear(); return append(sz, wxStrlen(sz)); } | |
575 | wxStringBase& assign(const wxChar *sz, size_t n) | |
576 | { clear(); return append(sz, n); } | |
577 | // same as `= n copies of ch' | |
c9f78968 | 578 | wxStringBase& assign(size_t n, wxUniChar ch) |
e87b7833 MB |
579 | { clear(); return append(n, ch); } |
580 | // assign from first to last | |
581 | wxStringBase& assign(const_iterator first, const_iterator last) | |
582 | { clear(); return append(first, last); } | |
583 | ||
584 | // first valid index position | |
585 | const_iterator begin() const { return m_pchData; } | |
968f291b | 586 | iterator begin(); |
e87b7833 MB |
587 | // position one after the last valid one |
588 | const_iterator end() const { return m_pchData + length(); } | |
ac3c86ee | 589 | iterator end(); |
e87b7833 | 590 | |
968f291b VZ |
591 | // first element of the reversed string |
592 | const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } | |
593 | reverse_iterator rbegin() { return reverse_iterator(end()); } | |
594 | // one beyond the end of the reversed string | |
595 | const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } | |
596 | reverse_iterator rend() { return reverse_iterator(begin()); } | |
597 | ||
e87b7833 MB |
598 | // insert another string |
599 | wxStringBase& insert(size_t nPos, const wxStringBase& str) | |
600 | { | |
601 | wxASSERT( str.GetStringData()->IsValid() ); | |
602 | return insert(nPos, str.c_str(), str.length()); | |
603 | } | |
604 | // insert n chars of str starting at nStart (in str) | |
605 | wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n) | |
606 | { | |
607 | wxASSERT( str.GetStringData()->IsValid() ); | |
608 | wxASSERT( nStart < str.length() ); | |
609 | size_t strLen = str.length() - nStart; | |
610 | n = strLen < n ? strLen : n; | |
611 | return insert(nPos, str.c_str() + nStart, n); | |
612 | } | |
613 | // insert first n (or all if n == npos) characters of sz | |
614 | wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos); | |
615 | // insert n copies of ch | |
c9f78968 | 616 | wxStringBase& insert(size_t nPos, size_t n, wxUniChar ch) |
e87b7833 | 617 | { return insert(nPos, wxStringBase(n, ch)); } |
c9f78968 | 618 | iterator insert(iterator it, wxUniChar ch) |
1c2d1459 | 619 | { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; } |
e87b7833 MB |
620 | void insert(iterator it, const_iterator first, const_iterator last) |
621 | { insert(it - begin(), first, last - first); } | |
c9f78968 | 622 | void insert(iterator it, size_type n, wxUniChar ch) |
e87b7833 MB |
623 | { insert(it - begin(), n, ch); } |
624 | ||
625 | // delete characters from nStart to nStart + nLen | |
626 | wxStringBase& erase(size_type pos = 0, size_type n = npos); | |
627 | iterator erase(iterator first, iterator last) | |
628 | { | |
629 | size_t idx = first - begin(); | |
630 | erase(idx, last - first); | |
631 | return begin() + idx; | |
632 | } | |
633 | iterator erase(iterator first); | |
634 | ||
635 | // explicit conversion to C string (use this with printf()!) | |
636 | const wxChar* c_str() const { return m_pchData; } | |
637 | const wxChar* data() const { return m_pchData; } | |
638 | ||
639 | // replaces the substring of length nLen starting at nStart | |
640 | wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz); | |
641 | // replaces the substring of length nLen starting at nStart | |
642 | wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str) | |
643 | { return replace(nStart, nLen, str.c_str()); } | |
644 | // replaces the substring with nCount copies of ch | |
c9f78968 | 645 | wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch); |
e87b7833 MB |
646 | // replaces a substring with another substring |
647 | wxStringBase& replace(size_t nStart, size_t nLen, | |
648 | const wxStringBase& str, size_t nStart2, size_t nLen2); | |
649 | // replaces the substring with first nCount chars of sz | |
650 | wxStringBase& replace(size_t nStart, size_t nLen, | |
651 | const wxChar* sz, size_t nCount); | |
652 | wxStringBase& replace(iterator first, iterator last, const_pointer s) | |
653 | { return replace(first - begin(), last - first, s); } | |
654 | wxStringBase& replace(iterator first, iterator last, const_pointer s, | |
655 | size_type n) | |
656 | { return replace(first - begin(), last - first, s, n); } | |
657 | wxStringBase& replace(iterator first, iterator last, const wxStringBase& s) | |
658 | { return replace(first - begin(), last - first, s); } | |
c9f78968 | 659 | wxStringBase& replace(iterator first, iterator last, size_type n, wxUniChar c) |
e87b7833 MB |
660 | { return replace(first - begin(), last - first, n, c); } |
661 | wxStringBase& replace(iterator first, iterator last, | |
662 | const_iterator first1, const_iterator last1) | |
663 | { return replace(first - begin(), last - first, first1, last1 - first1); } | |
664 | ||
665 | // swap two strings | |
666 | void swap(wxStringBase& str); | |
667 | ||
668 | // All find() functions take the nStart argument which specifies the | |
669 | // position to start the search on, the default value is 0. All functions | |
670 | // return npos if there were no match. | |
671 | ||
672 | // find a substring | |
673 | size_t find(const wxStringBase& str, size_t nStart = 0) const; | |
674 | ||
e87b7833 MB |
675 | // find first n characters of sz |
676 | size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; | |
e87b7833 | 677 | |
e87b7833 | 678 | // find the first occurence of character ch after nStart |
c9f78968 | 679 | size_t find(wxUniChar ch, size_t nStart = 0) const; |
1412634b | 680 | |
e87b7833 MB |
681 | // rfind() family is exactly like find() but works right to left |
682 | ||
683 | // as find, but from the end | |
684 | size_t rfind(const wxStringBase& str, size_t nStart = npos) const; | |
685 | ||
e87b7833 MB |
686 | // as find, but from the end |
687 | size_t rfind(const wxChar* sz, size_t nStart = npos, | |
688 | size_t n = npos) const; | |
689 | // as find, but from the end | |
c9f78968 | 690 | size_t rfind(wxUniChar ch, size_t nStart = npos) const; |
e87b7833 MB |
691 | |
692 | // find first/last occurence of any character in the set | |
693 | ||
694 | // as strpbrk() but starts at nStart, returns npos if not found | |
695 | size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const | |
696 | { return find_first_of(str.c_str(), nStart); } | |
697 | // same as above | |
698 | size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; | |
e2101186 | 699 | size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 | 700 | // same as find(char, size_t) |
c9f78968 | 701 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const |
e87b7833 MB |
702 | { return find(c, nStart); } |
703 | // find the last (starting from nStart) char from str in this string | |
704 | size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const | |
705 | { return find_last_of(str.c_str(), nStart); } | |
706 | // same as above | |
707 | size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; | |
e2101186 | 708 | size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 | 709 | // same as above |
c9f78968 | 710 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const |
e87b7833 MB |
711 | { return rfind(c, nStart); } |
712 | ||
713 | // find first/last occurence of any character not in the set | |
714 | ||
715 | // as strspn() (starting from nStart), returns npos on failure | |
716 | size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const | |
717 | { return find_first_not_of(str.c_str(), nStart); } | |
718 | // same as above | |
719 | size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; | |
e2101186 | 720 | size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 | 721 | // same as above |
c9f78968 | 722 | size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; |
e87b7833 MB |
723 | // as strcspn() |
724 | size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const | |
e2101186 | 725 | { return find_last_not_of(str.c_str(), nStart); } |
e87b7833 MB |
726 | // same as above |
727 | size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; | |
e2101186 | 728 | size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 | 729 | // same as above |
c9f78968 | 730 | size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; |
e87b7833 MB |
731 | |
732 | // All compare functions return -1, 0 or 1 if the [sub]string is less, | |
733 | // equal or greater than the compare() argument. | |
734 | ||
dcb68102 RN |
735 | // comparison with another string |
736 | int compare(const wxStringBase& str) const; | |
e87b7833 MB |
737 | // comparison with a substring |
738 | int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; | |
739 | // comparison of 2 substrings | |
740 | int compare(size_t nStart, size_t nLen, | |
741 | const wxStringBase& str, size_t nStart2, size_t nLen2) const; | |
dcb68102 RN |
742 | // comparison with a c string |
743 | int compare(const wxChar* sz) const; | |
e87b7833 MB |
744 | // substring comparison with first nCount characters of sz |
745 | int compare(size_t nStart, size_t nLen, | |
746 | const wxChar* sz, size_t nCount = npos) const; | |
747 | ||
748 | size_type copy(wxChar* s, size_type n, size_type pos = 0); | |
749 | ||
750 | // substring extraction | |
751 | wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const; | |
752 | ||
753 | // string += string | |
754 | wxStringBase& operator+=(const wxStringBase& s) { return append(s); } | |
755 | // string += C string | |
756 | wxStringBase& operator+=(const wxChar *psz) { return append(psz); } | |
757 | // string += char | |
c9f78968 VS |
758 | wxStringBase& operator+=(wxUniChar ch) { return append(1, ch); } |
759 | wxStringBase& operator+=(wxUniCharRef ch) { return append(1, ch); } | |
760 | wxStringBase& operator+=(char ch) { return append(1, ch); } | |
761 | wxStringBase& operator+=(wchar_t ch) { return append(1, ch); } | |
e87b7833 MB |
762 | }; |
763 | ||
fb52c2b6 VZ |
764 | // don't pollute the library user's name space |
765 | #undef wxASSERT_VALID_INDEX | |
766 | ||
992527a5 | 767 | #endif // !wxUSE_STL_BASED_WXSTRING |
e87b7833 | 768 | |
c9f78968 VS |
769 | // ---------------------------------------------------------------------------- |
770 | // wxCStrData | |
771 | // ---------------------------------------------------------------------------- | |
772 | ||
773 | // Lightweight object returned by wxString::c_str() and implicitly convertible | |
774 | // to either const char* or const wchar_t*. | |
bb650010 | 775 | class wxCStrData |
c9f78968 VS |
776 | { |
777 | private: | |
778 | // Ctors; for internal use by wxString and wxCStrData only | |
779 | wxCStrData(const wxString *str, size_t offset = 0, bool owned = false) | |
780 | : m_str(str), m_offset(offset), m_owned(owned) {} | |
781 | ||
782 | public: | |
783 | // Ctor constructs the object from char literal; they are needed to make | |
784 | // operator?: compile and they intentionally take char*, not const char* | |
785 | wxCStrData(char *buf); | |
786 | wxCStrData(wchar_t *buf); | |
787 | ||
788 | ~wxCStrData(); | |
789 | ||
790 | // FIXME: we'll need convertors for both char* and wchar_t* and NONE | |
791 | // for wxChar*, but that's after completing the transition to | |
792 | // "smart" wxUniChar class. For now, just have conversion to | |
793 | // char* in ANSI build and wchar_t in Unicode build. | |
794 | #if wxUSE_UNICODE | |
795 | const wchar_t* AsWChar() const; | |
796 | operator const wchar_t*() const { return AsWChar(); } | |
797 | #else | |
798 | const char* AsChar() const; | |
dbea442a VZ |
799 | const unsigned char* AsUnsignedChar() const |
800 | { return (const unsigned char *) AsChar(); } | |
17709275 | 801 | operator const void*() const { return AsChar(); } |
c9f78968 | 802 | operator const char*() const { return AsChar(); } |
dbea442a | 803 | operator const unsigned char*() const { return AsUnsignedChar(); } |
c9f78968 VS |
804 | #endif |
805 | ||
806 | wxString AsString() const; | |
807 | operator wxString() const; | |
808 | ||
809 | // allow expressions like "c_str()[0]": | |
810 | wxUniChar operator[](int n) const { return operator[](size_t(n)); } | |
811 | wxUniChar operator[](size_t n) const; | |
c1df0a3b | 812 | wxUniChar operator[](long n) const { return operator[](size_t(n)); } |
c9f78968 VS |
813 | #ifndef wxSIZE_T_IS_UINT |
814 | wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); } | |
815 | #endif // size_t != unsigned int | |
816 | ||
acf0c9ac | 817 | // these operators are needed to emulate the pointer semantics of c_str(): |
c9f78968 VS |
818 | // expressions like "wxChar *p = str.c_str() + 1;" should continue to work |
819 | // (we need both versions to resolve ambiguities): | |
820 | wxCStrData operator+(int n) const | |
821 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
acf0c9ac VZ |
822 | wxCStrData operator+(long n) const |
823 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
c9f78968 VS |
824 | wxCStrData operator+(size_t n) const |
825 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
826 | ||
b77afb41 | 827 | // this operator is needed to make expressions like "*c_str()" or |
c9f78968 VS |
828 | // "*(c_str() + 2)" work |
829 | wxUniChar operator*() const; | |
830 | ||
831 | private: | |
832 | const wxString *m_str; | |
833 | size_t m_offset; | |
834 | bool m_owned; | |
835 | ||
836 | friend class WXDLLIMPEXP_BASE wxString; | |
837 | }; | |
838 | ||
839 | // ---------------------------------------------------------------------------- | |
840 | // wxStringPrintfMixin | |
841 | // --------------------------------------------------------------------------- | |
842 | ||
843 | // NB: VC6 has a bug that causes linker errors if you have template methods | |
844 | // in a class using __declspec(dllimport). The solution is to split such | |
845 | // class into two classes, one that contains the template methods and does | |
846 | // *not* use WXDLLIMPEXP_BASE and another class that contains the rest | |
847 | // (with DLL linkage). | |
848 | // | |
849 | // We only do this for VC6 here, because the code is less efficient | |
850 | // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler | |
851 | // cannot compile this code. | |
852 | ||
853 | #if defined(__VISUALC__) && __VISUALC__ < 1300 | |
854 | #define wxNEEDS_WXSTRING_PRINTF_MIXIN | |
855 | #endif | |
856 | ||
857 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
858 | // this class contains implementation of wxString's vararg methods, it's | |
859 | // exported from wxBase DLL | |
860 | class WXDLLIMPEXP_BASE wxStringPrintfMixinBase | |
861 | { | |
862 | protected: | |
863 | wxStringPrintfMixinBase() {} | |
864 | ||
865 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
866 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
867 | }; | |
868 | ||
869 | // this class contains template wrappers for wxString's vararg methods, it's | |
870 | // intentionally *not* exported from the DLL in order to fix the VC6 bug | |
871 | // described above | |
872 | class wxStringPrintfMixin : public wxStringPrintfMixinBase | |
873 | { | |
874 | private: | |
875 | // to further complicate things, we can't return wxString from | |
876 | // wxStringPrintfMixin::Format() because wxString is not yet declared at | |
877 | // this point; the solution is to use this fake type trait template - this | |
878 | // way the compiler won't know the return type until Format() is used | |
879 | // (this doesn't compile with Watcom, but VC6 compiles it just fine): | |
880 | template<typename T> struct StringReturnType | |
881 | { | |
882 | typedef wxString type; | |
883 | }; | |
884 | ||
885 | public: | |
886 | // these are duplicated wxString methods, they're also declared below | |
887 | // if !wxNEEDS_WXSTRING_PRINTF_MIXIN: | |
888 | ||
889 | // int Printf(const wxChar *pszFormat, ...); | |
890 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
891 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; | |
892 | WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type, | |
893 | Format, DoFormat) | |
894 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
895 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
896 | ||
897 | protected: | |
898 | wxStringPrintfMixin() : wxStringPrintfMixinBase() {} | |
899 | }; | |
900 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
901 | ||
902 | ||
b6339dde VZ |
903 | // ---------------------------------------------------------------------------- |
904 | // wxString: string class trying to be compatible with std::string, MFC | |
905 | // CString and wxWindows 1.x wxString all at once | |
e87b7833 MB |
906 | // --------------------------------------------------------------------------- |
907 | ||
c9f78968 VS |
908 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
909 | // "non dll-interface class 'wxStringPrintfMixin' used as base interface | |
910 | // for dll-interface class 'wxString'" -- this is OK in our case | |
911 | #pragma warning (disable:4275) | |
e87b7833 MB |
912 | #endif |
913 | ||
c9f78968 VS |
914 | class WXDLLIMPEXP_BASE wxString : public wxStringBase |
915 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
916 | ,public wxStringPrintfMixin | |
917 | #endif | |
918 | { | |
e87b7833 MB |
919 | // NB: special care was taken in arranging the member functions in such order |
920 | // that all inline functions can be effectively inlined, verify that all | |
20aa779e | 921 | // performance critical functions are still inlined if you change order! |
e87b7833 MB |
922 | private: |
923 | // if we hadn't made these operators private, it would be possible to | |
924 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
925 | // converted to char in C and we do have operator=(char) | |
926 | // | |
927 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
928 | // to assign another numeric type to wxString will now result in | |
929 | // ambiguity between operator=(char) and operator=(int) | |
930 | wxString& operator=(int); | |
931 | ||
932 | // these methods are not implemented - there is _no_ conversion from int to | |
933 | // string, you're doing something wrong if the compiler wants to call it! | |
934 | // | |
935 | // try `s << i' or `s.Printf("%d", i)' instead | |
936 | wxString(int); | |
937 | ||
938 | public: | |
939 | // constructors and destructor | |
940 | // ctor for an empty string | |
941 | wxString() : wxStringBase() { } | |
942 | // copy ctor | |
943 | wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { } | |
944 | wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { } | |
945 | // string containing nRepeat copies of ch | |
c9f78968 VS |
946 | wxString(wxUniChar ch, size_t nRepeat = 1) |
947 | : wxStringBase(nRepeat, ch) { } | |
948 | wxString(size_t nRepeat, wxUniChar ch) | |
949 | : wxStringBase(nRepeat, ch) { } | |
950 | wxString(wxUniCharRef ch, size_t nRepeat = 1) | |
951 | : wxStringBase(nRepeat, ch) { } | |
952 | wxString(size_t nRepeat, wxUniCharRef ch) | |
953 | : wxStringBase(nRepeat, ch) { } | |
954 | wxString(char ch, size_t nRepeat = 1) | |
955 | : wxStringBase(nRepeat, ch) { } | |
956 | wxString(size_t nRepeat, char ch) | |
e87b7833 | 957 | : wxStringBase(nRepeat, ch) { } |
c9f78968 VS |
958 | wxString(wchar_t ch, size_t nRepeat = 1) |
959 | : wxStringBase(nRepeat, ch) { } | |
960 | wxString(size_t nRepeat, wchar_t ch) | |
e87b7833 MB |
961 | : wxStringBase(nRepeat, ch) { } |
962 | // ctor takes first nLength characters from C string | |
963 | // (default value of npos means take all the string) | |
964 | wxString(const wxChar *psz) | |
965 | : wxStringBase(psz ? psz : wxT("")) { } | |
966 | wxString(const wxChar *psz, size_t nLength) | |
967 | : wxStringBase(psz, nLength) { } | |
830f8f11 VZ |
968 | wxString(const wxChar *psz, |
969 | const wxMBConv& WXUNUSED(conv), | |
970 | size_t nLength = npos) | |
e87b7833 | 971 | : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { } |
e90c1d2a | 972 | |
20aa779e | 973 | // even if we're not built with wxUSE_STL == 1 it is very convenient to allow |
ce76f779 VZ |
974 | // implicit conversions from std::string to wxString as this allows to use |
975 | // the same strings in non-GUI and GUI code, however we don't want to | |
976 | // unconditionally add this ctor as it would make wx lib dependent on | |
977 | // libstdc++ on some Linux versions which is bad, so instead we ask the | |
978 | // client code to define this wxUSE_STD_STRING symbol if they need it | |
47561b0d | 979 | #if wxUSE_STD_STRING |
ce76f779 VZ |
980 | wxString(const wxStdString& s) |
981 | : wxStringBase(s.c_str()) { } | |
982 | #endif // wxUSE_STD_STRING | |
983 | ||
2bb67b80 OK |
984 | #if wxUSE_UNICODE |
985 | // from multibyte string | |
d7330233 VS |
986 | wxString(const char *psz, |
987 | const wxMBConv& conv = wxConvLibc, | |
988 | size_t nLength = npos); | |
2bb67b80 | 989 | // from wxWCharBuffer (i.e. return from wxGetString) |
e87b7833 | 990 | wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { } |
e90c1d2a | 991 | #else // ANSI |
3c67202d | 992 | // from C string (for compilers using unsigned char) |
153958f7 VZ |
993 | wxString(const unsigned char* psz) |
994 | : wxStringBase((const char*)psz) { } | |
995 | // from part of C string (for compilers using unsigned char) | |
996 | wxString(const unsigned char* psz, size_t nLength) | |
e87b7833 | 997 | : wxStringBase((const char*)psz, nLength) { } |
e90c1d2a | 998 | |
b77afb41 VZ |
999 | // as we provide both ctors with this signature for both char and unsigned |
1000 | // char string, we need to provide one for wxCStrData to resolve ambiguity | |
1001 | wxString(const wxCStrData& cstr, size_t nLength) | |
1002 | : wxStringBase(cstr.AsChar(), nLength) { } | |
1003 | ||
1004 | // and because wxString is convertible to wxCStrData and const wxChar * | |
1005 | // we also need to provide this one | |
1006 | wxString(const wxString& str, size_t nLength) | |
1007 | : wxStringBase(str, 0, nLength) { } | |
1008 | ||
6f841509 | 1009 | #if wxUSE_WCHAR_T |
2bb67b80 | 1010 | // from wide (Unicode) string |
830f8f11 VZ |
1011 | wxString(const wchar_t *pwz, |
1012 | const wxMBConv& conv = wxConvLibc, | |
1013 | size_t nLength = npos); | |
e90c1d2a VZ |
1014 | #endif // !wxUSE_WCHAR_T |
1015 | ||
2bb67b80 OK |
1016 | // from wxCharBuffer |
1017 | wxString(const wxCharBuffer& psz) | |
051aa330 | 1018 | : wxStringBase(psz) { } |
e90c1d2a VZ |
1019 | #endif // Unicode/ANSI |
1020 | ||
3c67202d VZ |
1021 | // generic attributes & operations |
1022 | // as standard strlen() | |
e87b7833 | 1023 | size_t Len() const { return length(); } |
3c67202d | 1024 | // string contains any characters? |
e87b7833 | 1025 | bool IsEmpty() const { return empty(); } |
d775fa82 | 1026 | // empty string is "false", so !str will return true |
20aa779e | 1027 | bool operator!() const { return empty(); } |
735d1db6 VZ |
1028 | // truncate the string to given length |
1029 | wxString& Truncate(size_t uiLen); | |
3c67202d | 1030 | // empty string contents |
dd1eaa89 VZ |
1031 | void Empty() |
1032 | { | |
735d1db6 | 1033 | Truncate(0); |
dd1eaa89 | 1034 | |
5a8231ef | 1035 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); |
7be07660 | 1036 | } |
3c67202d | 1037 | // empty the string and free memory |
7be07660 VZ |
1038 | void Clear() |
1039 | { | |
e87b7833 MB |
1040 | wxString tmp(wxEmptyString); |
1041 | swap(tmp); | |
dd1eaa89 VZ |
1042 | } |
1043 | ||
3c67202d VZ |
1044 | // contents test |
1045 | // Is an ascii value | |
c801d85f | 1046 | bool IsAscii() const; |
3c67202d | 1047 | // Is a number |
c801d85f | 1048 | bool IsNumber() const; |
3c67202d | 1049 | // Is a word |
c801d85f | 1050 | bool IsWord() const; |
c801d85f | 1051 | |
3c67202d VZ |
1052 | // data access (all indexes are 0 based) |
1053 | // read access | |
c9f78968 | 1054 | wxUniChar GetChar(size_t n) const |
9d9ad673 | 1055 | { return at(n); } |
3c67202d | 1056 | // read/write access |
c9f78968 | 1057 | wxUniCharRef GetWritableChar(size_t n) |
9d9ad673 | 1058 | { return at(n); } |
3c67202d | 1059 | // write access |
c9f78968 | 1060 | void SetChar(size_t n, wxUniChar ch) |
9d9ad673 | 1061 | { at(n) = ch; } |
c801d85f | 1062 | |
3c67202d | 1063 | // get last character |
c9f78968 | 1064 | wxUniChar Last() const |
09443a26 | 1065 | { |
5a8231ef | 1066 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
09443a26 | 1067 | |
9d9ad673 | 1068 | return at(length() - 1); |
09443a26 VZ |
1069 | } |
1070 | ||
3c67202d | 1071 | // get writable last character |
c9f78968 | 1072 | wxUniCharRef Last() |
09443a26 | 1073 | { |
5a8231ef | 1074 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
9d9ad673 | 1075 | return at(length() - 1); |
09443a26 | 1076 | } |
c801d85f | 1077 | |
d836ee96 | 1078 | /* |
9d9ad673 | 1079 | Note that we we must define all of the overloads below to avoid |
c9f78968 | 1080 | ambiguity when using str[0]. |
d836ee96 | 1081 | */ |
c9f78968 | 1082 | wxUniChar operator[](int n) const |
01398433 | 1083 | { return wxStringBase::at(n); } |
c1df0a3b VZ |
1084 | wxUniChar operator[](long n) const |
1085 | { return wxStringBase::at(n); } | |
c9f78968 | 1086 | wxUniChar operator[](size_t n) const |
01398433 VZ |
1087 | { return wxStringBase::at(n); } |
1088 | #ifndef wxSIZE_T_IS_UINT | |
c9f78968 | 1089 | wxUniChar operator[](unsigned int n) const |
01398433 VZ |
1090 | { return wxStringBase::at(n); } |
1091 | #endif // size_t != unsigned int | |
01398433 | 1092 | |
9d9ad673 | 1093 | // operator versions of GetWriteableChar() |
c9f78968 | 1094 | wxUniCharRef operator[](int n) |
9d9ad673 | 1095 | { return wxStringBase::at(n); } |
c1df0a3b VZ |
1096 | wxUniCharRef operator[](long n) |
1097 | { return wxStringBase::at(n); } | |
c9f78968 | 1098 | wxUniCharRef operator[](size_t n) |
9d9ad673 | 1099 | { return wxStringBase::at(n); } |
d836ee96 | 1100 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 1101 | wxUniCharRef operator[](unsigned int n) |
9d9ad673 | 1102 | { return wxStringBase::at(n); } |
d836ee96 | 1103 | #endif // size_t != unsigned int |
c801d85f | 1104 | |
c9f78968 VS |
1105 | // explicit conversion to C string (use this with printf()!) |
1106 | wxCStrData c_str() const { return wxCStrData(this); } | |
1107 | ||
3c67202d | 1108 | // implicit conversion to C string |
c9f78968 | 1109 | operator wxCStrData() const { return c_str(); } |
e87b7833 | 1110 | operator const wxChar*() const { return c_str(); } |
e015c2a3 | 1111 | |
e015c2a3 | 1112 | // identical to c_str(), for MFC compatibility |
c9f78968 VS |
1113 | const wxCStrData GetData() const { return c_str(); } |
1114 | ||
1115 | // explicit conversion to C string in internal representation (char*, | |
1116 | // wchar_t*, UTF-8-encoded char*, depending on the build): | |
1117 | const_pointer wx_str() const { return data(); } | |
e90c1d2a | 1118 | |
e015c2a3 VZ |
1119 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
1120 | // converting numbers or strings which are certain not to contain special | |
1121 | // chars (typically system functions, X atoms, environment variables etc.) | |
1122 | // | |
1123 | // the behaviour of these functions with the strings containing anything | |
1124 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 1125 | #if wxUSE_UNICODE |
2b5f62a0 VZ |
1126 | static wxString FromAscii(const char *ascii); // string |
1127 | static wxString FromAscii(const char ascii); // char | |
b1ac3b56 | 1128 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
1129 | #else // ANSI |
1130 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
2b5f62a0 | 1131 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
1132 | const char *ToAscii() const { return c_str(); } |
1133 | #endif // Unicode/!Unicode | |
b1ac3b56 | 1134 | |
2b5f62a0 | 1135 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 1136 | // buffer with temporary data |
f6bcfd97 BP |
1137 | // |
1138 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
1139 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
1140 | // fn_str() to return a string which should be used with the OS APIs | |
1141 | // accepting the file names. The return value is always the same, but the | |
1142 | // type differs because a function may either return pointer to the buffer | |
1143 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 1144 | #if wxUSE_UNICODE |
830f8f11 | 1145 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; |
f6bcfd97 | 1146 | |
e90c1d2a VZ |
1147 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
1148 | ||
e87b7833 | 1149 | const wxChar* wc_str() const { return c_str(); } |
f6bcfd97 BP |
1150 | |
1151 | // for compatibility with !wxUSE_UNICODE version | |
830f8f11 | 1152 | const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
e90c1d2a | 1153 | |
2bb67b80 | 1154 | #if wxMBFILES |
5f709e67 | 1155 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 1156 | #else // !wxMBFILES |
e87b7833 | 1157 | const wxChar* fn_str() const { return c_str(); } |
e90c1d2a VZ |
1158 | #endif // wxMBFILES/!wxMBFILES |
1159 | #else // ANSI | |
e87b7833 | 1160 | const wxChar* mb_str() const { return c_str(); } |
f6bcfd97 BP |
1161 | |
1162 | // for compatibility with wxUSE_UNICODE version | |
830f8f11 | 1163 | const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
f6bcfd97 | 1164 | |
e90c1d2a | 1165 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 1166 | |
6f841509 | 1167 | #if wxUSE_WCHAR_T |
830f8f11 | 1168 | const wxWCharBuffer wc_str(const wxMBConv& conv) const; |
e90c1d2a | 1169 | #endif // wxUSE_WCHAR_T |
d5c8817c SC |
1170 | #ifdef __WXOSX__ |
1171 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
1172 | #else | |
e87b7833 | 1173 | const wxChar* fn_str() const { return c_str(); } |
d5c8817c | 1174 | #endif |
e90c1d2a | 1175 | #endif // Unicode/ANSI |
c801d85f | 1176 | |
3c67202d VZ |
1177 | // overloaded assignment |
1178 | // from another wxString | |
e87b7833 MB |
1179 | wxString& operator=(const wxStringBase& stringSrc) |
1180 | { return (wxString&)wxStringBase::operator=(stringSrc); } | |
c8eb8172 | 1181 | wxString& operator=(const wxCStrData& cstr); |
3c67202d | 1182 | // from a character |
c9f78968 | 1183 | wxString& operator=(wxUniChar ch) |
e87b7833 | 1184 | { return (wxString&)wxStringBase::operator=(ch); } |
c9f78968 VS |
1185 | wxString& operator=(wxUniCharRef ch) |
1186 | { return (wxString&)wxStringBase::operator=((wxUniChar)ch); } | |
1187 | wxString& operator=(char ch) | |
1188 | { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); } | |
1189 | wxString& operator=(wchar_t ch) | |
1190 | { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); } | |
c57e3bd5 RN |
1191 | // from a C string - STL probably will crash on NULL, |
1192 | // so we need to compensate in that case | |
992527a5 | 1193 | #if wxUSE_STL_BASED_WXSTRING |
c57e3bd5 RN |
1194 | wxString& operator=(const wxChar *psz) |
1195 | { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; } | |
1196 | #else | |
e87b7833 MB |
1197 | wxString& operator=(const wxChar *psz) |
1198 | { return (wxString&)wxStringBase::operator=(psz); } | |
c57e3bd5 RN |
1199 | #endif |
1200 | ||
111bb7f2 OK |
1201 | #if wxUSE_UNICODE |
1202 | // from wxWCharBuffer | |
b1801e0e | 1203 | wxString& operator=(const wxWCharBuffer& psz) |
e87b7833 | 1204 | { (void) operator=((const wchar_t *)psz); return *this; } |
d7330233 VS |
1205 | // from C string |
1206 | wxString& operator=(const char* psz) | |
1207 | { return operator=(wxString(psz)); } | |
e90c1d2a | 1208 | #else // ANSI |
3c67202d | 1209 | // from another kind of C string |
c801d85f | 1210 | wxString& operator=(const unsigned char* psz); |
6f841509 | 1211 | #if wxUSE_WCHAR_T |
3c67202d | 1212 | // from a wide string |
c801d85f | 1213 | wxString& operator=(const wchar_t *pwz); |
6f841509 | 1214 | #endif |
111bb7f2 | 1215 | // from wxCharBuffer |
b1801e0e | 1216 | wxString& operator=(const wxCharBuffer& psz) |
e87b7833 | 1217 | { (void) operator=((const char *)psz); return *this; } |
e90c1d2a | 1218 | #endif // Unicode/ANSI |
3c67202d VZ |
1219 | |
1220 | // string concatenation | |
1221 | // in place concatenation | |
1222 | /* | |
1223 | Concatenate and return the result. Note that the left to right | |
1224 | associativity of << allows to write things like "str << str1 << str2 | |
1225 | << ..." (unlike with +=) | |
1226 | */ | |
1227 | // string += string | |
dd1eaa89 VZ |
1228 | wxString& operator<<(const wxString& s) |
1229 | { | |
d8a4b666 | 1230 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL |
09443a26 VZ |
1231 | wxASSERT_MSG( s.GetStringData()->IsValid(), |
1232 | _T("did you forget to call UngetWriteBuf()?") ); | |
e87b7833 | 1233 | #endif |
dd1eaa89 | 1234 | |
e87b7833 | 1235 | append(s); |
dd1eaa89 VZ |
1236 | return *this; |
1237 | } | |
3c67202d | 1238 | // string += C string |
2bb67b80 | 1239 | wxString& operator<<(const wxChar *psz) |
c9f78968 VS |
1240 | { append(psz); return *this; } |
1241 | wxString& operator<<(const wxCStrData& psz) | |
e87b7833 | 1242 | { append(psz); return *this; } |
3c67202d | 1243 | // string += char |
c9f78968 VS |
1244 | wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } |
1245 | wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } | |
1246 | wxString& operator<<(char ch) { append(1, ch); return *this; } | |
1247 | wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } | |
2bb67b80 OK |
1248 | |
1249 | // string += buffer (i.e. from wxGetString) | |
1250 | #if wxUSE_UNICODE | |
09443a26 | 1251 | wxString& operator<<(const wxWCharBuffer& s) |
d7330233 VS |
1252 | { return operator<<((const wchar_t *)s); } |
1253 | wxString& operator+=(const wxWCharBuffer& s) | |
1254 | { return operator<<((const wchar_t *)s); } | |
09443a26 VZ |
1255 | #else // !wxUSE_UNICODE |
1256 | wxString& operator<<(const wxCharBuffer& s) | |
d7330233 VS |
1257 | { return operator<<((const char *)s); } |
1258 | wxString& operator+=(const wxCharBuffer& s) | |
1259 | { return operator<<((const char *)s); } | |
09443a26 | 1260 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
6b95b20d | 1261 | |
d7330233 VS |
1262 | #if wxUSE_UNICODE |
1263 | // string += C string in Unicode build (with conversion) | |
1264 | wxString& operator<<(const char *s) | |
1265 | { return operator<<(wxString(s)); } | |
1266 | wxString& operator+=(const char *s) | |
1267 | { return operator+=(wxString(s)); } | |
1268 | #endif // wxUSE_UNICODE | |
1269 | ||
3c67202d | 1270 | // string += C string |
09443a26 VZ |
1271 | wxString& Append(const wxString& s) |
1272 | { | |
5a8231ef WS |
1273 | // test for empty() to share the string if possible |
1274 | if ( empty() ) | |
09443a26 VZ |
1275 | *this = s; |
1276 | else | |
e87b7833 | 1277 | append(s); |
09443a26 VZ |
1278 | return *this; |
1279 | } | |
c9f78968 VS |
1280 | wxString& Append(const wxCStrData& psz) |
1281 | { append(psz); return *this; } | |
2bb67b80 | 1282 | wxString& Append(const wxChar* psz) |
e87b7833 | 1283 | { append(psz); return *this; } |
3c67202d | 1284 | // append count copies of given character |
c9f78968 VS |
1285 | wxString& Append(wxUniChar ch, size_t count = 1u) |
1286 | { append(count, ch); return *this; } | |
1287 | wxString& Append(wxUniCharRef ch, size_t count = 1u) | |
1288 | { append(count, ch); return *this; } | |
1289 | wxString& Append(char ch, size_t count = 1u) | |
1290 | { append(count, ch); return *this; } | |
1291 | wxString& Append(wchar_t ch, size_t count = 1u) | |
e87b7833 | 1292 | { append(count, ch); return *this; } |
8f06a017 | 1293 | wxString& Append(const wxChar* psz, size_t nLen) |
e87b7833 | 1294 | { append(psz, nLen); return *this; } |
3c67202d VZ |
1295 | |
1296 | // prepend a string, return the string itself | |
1297 | wxString& Prepend(const wxString& str) | |
1298 | { *this = str + *this; return *this; } | |
1299 | ||
1300 | // non-destructive concatenation | |
1fc8cfbd VZ |
1301 | // two strings |
1302 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
1303 | const wxString& string2); | |
1304 | // string with a single char | |
c9f78968 | 1305 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
1fc8cfbd | 1306 | // char with a string |
c9f78968 | 1307 | friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); |
1fc8cfbd VZ |
1308 | // string with C string |
1309 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1310 | const wxChar *psz); | |
1311 | // C string with string | |
1312 | friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, | |
1313 | const wxString& string); | |
3c67202d VZ |
1314 | |
1315 | // stream-like functions | |
1316 | // insert an int into string | |
3ce65f6c VZ |
1317 | wxString& operator<<(int i) |
1318 | { return (*this) << Format(_T("%d"), i); } | |
1319 | // insert an unsigned int into string | |
1320 | wxString& operator<<(unsigned int ui) | |
1321 | { return (*this) << Format(_T("%u"), ui); } | |
1322 | // insert a long into string | |
1323 | wxString& operator<<(long l) | |
1324 | { return (*this) << Format(_T("%ld"), l); } | |
1325 | // insert an unsigned long into string | |
1326 | wxString& operator<<(unsigned long ul) | |
1327 | { return (*this) << Format(_T("%lu"), ul); } | |
3fc1adbd MW |
1328 | #if defined wxLongLong_t && !defined wxLongLongIsLong |
1329 | // insert a long long if they exist and aren't longs | |
1330 | wxString& operator<<(wxLongLong_t ll) | |
b7859132 MW |
1331 | { |
1332 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
1333 | return (*this) << Format(fmt, ll); | |
1334 | } | |
3fc1adbd MW |
1335 | // insert an unsigned long long |
1336 | wxString& operator<<(wxULongLong_t ull) | |
b7859132 MW |
1337 | { |
1338 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
1339 | return (*this) << Format(fmt , ull); | |
1340 | } | |
3fc1adbd | 1341 | #endif |
3c67202d | 1342 | // insert a float into string |
3ce65f6c VZ |
1343 | wxString& operator<<(float f) |
1344 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 1345 | // insert a double into string |
3ce65f6c VZ |
1346 | wxString& operator<<(double d) |
1347 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 1348 | |
3c67202d | 1349 | // string comparison |
30b21f9a | 1350 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
dcb68102 RN |
1351 | int Cmp(const wxChar *psz) const; |
1352 | int Cmp(const wxString& s) const; | |
3c67202d | 1353 | // same as Cmp() but not case-sensitive |
dcb68102 RN |
1354 | int CmpNoCase(const wxChar *psz) const; |
1355 | int CmpNoCase(const wxString& s) const; | |
3c67202d VZ |
1356 | // test for the string equality, either considering case or not |
1357 | // (if compareWithCase then the case matters) | |
d775fa82 | 1358 | bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const |
3c67202d | 1359 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } |
20aa779e | 1360 | // comparison with a single character: returns true if equal |
c9f78968 | 1361 | bool IsSameAs(wxUniChar c, bool compareWithCase = true) const |
f33fee2a | 1362 | { |
e87b7833 | 1363 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c |
f33fee2a VZ |
1364 | : wxToupper(GetChar(0u)) == wxToupper(c)); |
1365 | } | |
3c67202d VZ |
1366 | |
1367 | // simple sub-string extraction | |
1368 | // return substring starting at nFirst of length nCount (or till the end | |
1369 | // if nCount = default value) | |
e87b7833 | 1370 | wxString Mid(size_t nFirst, size_t nCount = npos) const; |
3c67202d | 1371 | |
f6bcfd97 | 1372 | // operator version of Mid() |
3c67202d VZ |
1373 | wxString operator()(size_t start, size_t len) const |
1374 | { return Mid(start, len); } | |
1375 | ||
3affcd07 VZ |
1376 | // check if the string starts with the given prefix and return the rest |
1377 | // of the string in the provided pointer if it is not NULL; otherwise | |
1378 | // return false | |
f6bcfd97 | 1379 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; |
3affcd07 VZ |
1380 | // check if the string ends with the given suffix and return the |
1381 | // beginning of the string before the suffix in the provided pointer if | |
1382 | // it is not NULL; otherwise return false | |
1383 | bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const; | |
f6bcfd97 | 1384 | |
3c67202d | 1385 | // get first nCount characters |
c801d85f | 1386 | wxString Left(size_t nCount) const; |
3c67202d | 1387 | // get last nCount characters |
c801d85f | 1388 | wxString Right(size_t nCount) const; |
c0881dc3 | 1389 | // get all characters before the first occurance of ch |
3c67202d | 1390 | // (returns the whole string if ch not found) |
c9f78968 | 1391 | wxString BeforeFirst(wxUniChar ch) const; |
3c67202d VZ |
1392 | // get all characters before the last occurence of ch |
1393 | // (returns empty string if ch not found) | |
c9f78968 | 1394 | wxString BeforeLast(wxUniChar ch) const; |
3c67202d VZ |
1395 | // get all characters after the first occurence of ch |
1396 | // (returns empty string if ch not found) | |
c9f78968 | 1397 | wxString AfterFirst(wxUniChar ch) const; |
3c67202d VZ |
1398 | // get all characters after the last occurence of ch |
1399 | // (returns the whole string if ch not found) | |
c9f78968 | 1400 | wxString AfterLast(wxUniChar ch) const; |
3c67202d VZ |
1401 | |
1402 | // for compatibility only, use more explicitly named functions above | |
c9f78968 VS |
1403 | wxString Before(wxUniChar ch) const { return BeforeLast(ch); } |
1404 | wxString After(wxUniChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
1405 | |
1406 | // case conversion | |
c84c52de | 1407 | // convert to upper case in place, return the string itself |
c801d85f | 1408 | wxString& MakeUpper(); |
c84c52de | 1409 | // convert to upper case, return the copy of the string |
03ab016d JS |
1410 | // Here's something to remember: BC++ doesn't like returns in inlines. |
1411 | wxString Upper() const ; | |
c84c52de | 1412 | // convert to lower case in place, return the string itself |
c801d85f | 1413 | wxString& MakeLower(); |
c84c52de | 1414 | // convert to lower case, return the copy of the string |
03ab016d | 1415 | wxString Lower() const ; |
c801d85f | 1416 | |
3c67202d VZ |
1417 | // trimming/padding whitespace (either side) and truncating |
1418 | // remove spaces from left or from right (default) side | |
d775fa82 | 1419 | wxString& Trim(bool bFromRight = true); |
3c67202d | 1420 | // add nCount copies chPad in the beginning or at the end (default) |
c9f78968 | 1421 | wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); |
dd1eaa89 | 1422 | |
3c67202d VZ |
1423 | // searching and replacing |
1424 | // searching (return starting index, or -1 if not found) | |
c9f78968 | 1425 | int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr |
3c67202d | 1426 | // searching (return starting index, or -1 if not found) |
2bb67b80 | 1427 | int Find(const wxChar *pszSub) const; // like strstr |
3c67202d VZ |
1428 | // replace first (or all of bReplaceAll) occurences of substring with |
1429 | // another string, returns the number of replacements made | |
2bb67b80 OK |
1430 | size_t Replace(const wxChar *szOld, |
1431 | const wxChar *szNew, | |
d775fa82 | 1432 | bool bReplaceAll = true); |
3c67202d VZ |
1433 | |
1434 | // check if the string contents matches a mask containing '*' and '?' | |
2bb67b80 | 1435 | bool Matches(const wxChar *szMask) const; |
c801d85f | 1436 | |
d775fa82 | 1437 | // conversion to numbers: all functions return true only if the whole |
538f35cc VZ |
1438 | // string is a number and put the value of this number into the pointer |
1439 | // provided, the base is the numeric base in which the conversion should be | |
1440 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1441 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
cd0b1709 | 1442 | // convert to a signed integer |
538f35cc | 1443 | bool ToLong(long *val, int base = 10) const; |
cd0b1709 | 1444 | // convert to an unsigned integer |
538f35cc | 1445 | bool ToULong(unsigned long *val, int base = 10) const; |
d6718dd1 VZ |
1446 | // convert to wxLongLong |
1447 | #if defined(wxLongLong_t) | |
1448 | bool ToLongLong(wxLongLong_t *val, int base = 10) const; | |
1449 | // convert to wxULongLong | |
1450 | bool ToULongLong(wxULongLong_t *val, int base = 10) const; | |
1451 | #endif // wxLongLong_t | |
cd0b1709 VZ |
1452 | // convert to a double |
1453 | bool ToDouble(double *val) const; | |
1454 | ||
d6718dd1 | 1455 | |
c9f78968 | 1456 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
90e572f1 | 1457 | // formatted input/output |
3c67202d | 1458 | // as sprintf(), returns the number of characters written or < 0 on error |
7357f981 | 1459 | // (take 'this' into account in attribute parameter count) |
c9f78968 VS |
1460 | // int Printf(const wxChar *pszFormat, ...); |
1461 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
1462 | #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN | |
3c67202d | 1463 | // as vprintf(), returns the number of characters written or < 0 on error |
c9f78968 | 1464 | int PrintfV(const wxString& format, va_list argptr); |
dd1eaa89 | 1465 | |
c9f78968 | 1466 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
341e7d28 | 1467 | // returns the string containing the result of Printf() to it |
c9f78968 VS |
1468 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; |
1469 | WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat) | |
1470 | #endif | |
341e7d28 | 1471 | // the same as above, but takes a va_list |
c9f78968 | 1472 | static wxString FormatV(const wxString& format, va_list argptr); |
341e7d28 | 1473 | |
3c67202d VZ |
1474 | // raw access to string memory |
1475 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 1476 | // only works if the data of this string is not shared |
e87b7833 | 1477 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } |
3c67202d | 1478 | // minimize the string's memory |
dd1eaa89 | 1479 | // only works if the data of this string is not shared |
b1801e0e | 1480 | bool Shrink(); |
76046d73 | 1481 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING |
d8a4b666 VS |
1482 | // These are deprecated, use wxStringBuffer or wxStringBufferLength instead |
1483 | // | |
3c67202d VZ |
1484 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
1485 | // a.s.a.p. to put string back in a reasonable state! | |
d8a4b666 | 1486 | wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) ); |
3c67202d | 1487 | // call this immediately after GetWriteBuf() has been used |
d8a4b666 VS |
1488 | wxDEPRECATED( void UngetWriteBuf() ); |
1489 | wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); | |
76046d73 | 1490 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING |
c801d85f | 1491 | |
77ffb593 | 1492 | // wxWidgets version 1 compatibility functions |
3c67202d VZ |
1493 | |
1494 | // use Mid() | |
1495 | wxString SubString(size_t from, size_t to) const | |
1496 | { return Mid(from, (to - from + 1)); } | |
1497 | // values for second parameter of CompareTo function | |
c801d85f | 1498 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 1499 | // values for first parameter of Strip function |
c801d85f | 1500 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 1501 | |
c9f78968 | 1502 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
7357f981 GD |
1503 | // use Printf() |
1504 | // (take 'this' into account in attribute parameter count) | |
c9f78968 VS |
1505 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; |
1506 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
1507 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
c801d85f | 1508 | |
3c67202d | 1509 | // use Cmp() |
2bb67b80 | 1510 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 1511 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 1512 | |
3c67202d | 1513 | // use Len |
e87b7833 | 1514 | size_t Length() const { return length(); } |
3c67202d | 1515 | // Count the number of characters |
c9f78968 | 1516 | int Freq(wxUniChar ch) const; |
3c67202d | 1517 | // use MakeLower |
c801d85f | 1518 | void LowerCase() { MakeLower(); } |
3c67202d | 1519 | // use MakeUpper |
c801d85f | 1520 | void UpperCase() { MakeUpper(); } |
3c67202d | 1521 | // use Trim except that it doesn't change this string |
c801d85f KB |
1522 | wxString Strip(stripType w = trailing) const; |
1523 | ||
3c67202d | 1524 | // use Find (more general variants not yet supported) |
2bb67b80 | 1525 | size_t Index(const wxChar* psz) const { return Find(psz); } |
c9f78968 | 1526 | size_t Index(wxUniChar ch) const { return Find(ch); } |
3c67202d | 1527 | // use Truncate |
c801d85f | 1528 | wxString& Remove(size_t pos) { return Truncate(pos); } |
e87b7833 | 1529 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } |
c801d85f | 1530 | |
e87b7833 MB |
1531 | wxString& Remove(size_t nStart, size_t nLen) |
1532 | { return (wxString&)erase( nStart, nLen ); } | |
dd1eaa89 | 1533 | |
3c67202d | 1534 | // use Find() |
c9f78968 VS |
1535 | int First( const wxUniChar ch ) const { return Find(ch); } |
1536 | int First( char ch ) const { return Find(ch); } | |
1537 | int First( wchar_t ch ) const { return Find(ch); } | |
2bb67b80 | 1538 | int First( const wxChar* psz ) const { return Find(psz); } |
3ed358cb | 1539 | int First( const wxString &str ) const { return Find(str); } |
c9f78968 | 1540 | int Last( const wxUniChar ch ) const { return Find(ch, true); } |
d775fa82 | 1541 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } |
c801d85f | 1542 | |
5a8231ef WS |
1543 | // use empty() |
1544 | bool IsNull() const { return empty(); } | |
c801d85f | 1545 | |
3c67202d | 1546 | // std::string compatibility functions |
dd1eaa89 | 1547 | |
3c67202d VZ |
1548 | // take nLen chars starting at nPos |
1549 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
e87b7833 | 1550 | : wxStringBase(str, nPos, nLen) { } |
3c67202d | 1551 | // take all characters from pStart to pEnd |
e87b7833 | 1552 | wxString(const void *pStart, const void *pEnd) |
6e94fff9 | 1553 | : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { } |
e87b7833 MB |
1554 | wxString(const_iterator first, const_iterator last) |
1555 | : wxStringBase(first, last) { } | |
c9f78968 VS |
1556 | wxString(iterator first, iterator last) |
1557 | : wxStringBase(first, last) { } | |
5460b9e3 | 1558 | |
3c67202d | 1559 | // lib.string.modifiers |
3c67202d VZ |
1560 | // append elements str[pos], ..., str[pos+n] |
1561 | wxString& append(const wxString& str, size_t pos, size_t n) | |
e87b7833 MB |
1562 | { return (wxString&)wxStringBase::append(str, pos, n); } |
1563 | // append a string | |
1564 | wxString& append(const wxString& str) | |
1565 | { return (wxString&)wxStringBase::append(str); } | |
c9f78968 VS |
1566 | wxString& append(const wxCStrData& str) |
1567 | { return (wxString&)wxStringBase::append(str.AsString()); } | |
3c67202d | 1568 | // append first n (or all if n == npos) characters of sz |
e87b7833 MB |
1569 | wxString& append(const wxChar *sz) |
1570 | { return (wxString&)wxStringBase::append(sz); } | |
1571 | wxString& append(const wxChar *sz, size_t n) | |
1572 | { return (wxString&)wxStringBase::append(sz, n); } | |
3c67202d | 1573 | // append n copies of ch |
c9f78968 | 1574 | wxString& append(size_t n, wxUniChar ch) |
e87b7833 MB |
1575 | { return (wxString&)wxStringBase::append(n, ch); } |
1576 | // append from first to last | |
1577 | wxString& append(const_iterator first, const_iterator last) | |
1578 | { return (wxString&)wxStringBase::append(first, last); } | |
3c67202d VZ |
1579 | |
1580 | // same as `this_string = str' | |
735d1db6 | 1581 | wxString& assign(const wxString& str) |
e87b7833 | 1582 | { return (wxString&)wxStringBase::assign(str); } |
3c67202d VZ |
1583 | // same as ` = str[pos..pos + n] |
1584 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
e87b7833 | 1585 | { return (wxString&)wxStringBase::assign(str, pos, n); } |
3c67202d | 1586 | // same as `= first n (or all if n == npos) characters of sz' |
e87b7833 MB |
1587 | wxString& assign(const wxChar *sz) |
1588 | { return (wxString&)wxStringBase::assign(sz); } | |
1589 | wxString& assign(const wxChar *sz, size_t n) | |
1590 | { return (wxString&)wxStringBase::assign(sz, n); } | |
3c67202d | 1591 | // same as `= n copies of ch' |
c9f78968 | 1592 | wxString& assign(size_t n, wxUniChar ch) |
e87b7833 MB |
1593 | { return (wxString&)wxStringBase::assign(n, ch); } |
1594 | // assign from first to last | |
1595 | wxString& assign(const_iterator first, const_iterator last) | |
1596 | { return (wxString&)wxStringBase::assign(first, last); } | |
1597 | ||
1598 | // string comparison | |
963ad140 | 1599 | #if !defined(HAVE_STD_STRING_COMPARE) |
e87b7833 MB |
1600 | int compare(const wxStringBase& str) const; |
1601 | // comparison with a substring | |
1602 | int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; | |
1603 | // comparison of 2 substrings | |
1604 | int compare(size_t nStart, size_t nLen, | |
1605 | const wxStringBase& str, size_t nStart2, size_t nLen2) const; | |
1606 | // just like strcmp() | |
1607 | int compare(const wxChar* sz) const; | |
1608 | // substring comparison with first nCount characters of sz | |
1609 | int compare(size_t nStart, size_t nLen, | |
1610 | const wxChar* sz, size_t nCount = npos) const; | |
1611 | #endif // !defined HAVE_STD_STRING_COMPARE | |
3c67202d VZ |
1612 | |
1613 | // insert another string | |
e87b7833 MB |
1614 | wxString& insert(size_t nPos, const wxString& str) |
1615 | { return (wxString&)wxStringBase::insert(nPos, str); } | |
3c67202d VZ |
1616 | // insert n chars of str starting at nStart (in str) |
1617 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
e87b7833 | 1618 | { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); } |
3c67202d | 1619 | // insert first n (or all if n == npos) characters of sz |
e87b7833 MB |
1620 | wxString& insert(size_t nPos, const wxChar *sz) |
1621 | { return (wxString&)wxStringBase::insert(nPos, sz); } | |
1622 | wxString& insert(size_t nPos, const wxChar *sz, size_t n) | |
1623 | { return (wxString&)wxStringBase::insert(nPos, sz, n); } | |
3c67202d | 1624 | // insert n copies of ch |
c9f78968 | 1625 | wxString& insert(size_t nPos, size_t n, wxUniChar ch) |
e87b7833 | 1626 | { return (wxString&)wxStringBase::insert(nPos, n, ch); } |
c9f78968 | 1627 | iterator insert(iterator it, wxUniChar ch) |
1c2d1459 | 1628 | { return wxStringBase::insert(it, ch); } |
e87b7833 MB |
1629 | void insert(iterator it, const_iterator first, const_iterator last) |
1630 | { wxStringBase::insert(it, first, last); } | |
c9f78968 | 1631 | void insert(iterator it, size_type n, wxUniChar ch) |
e87b7833 | 1632 | { wxStringBase::insert(it, n, ch); } |
3c67202d VZ |
1633 | |
1634 | // delete characters from nStart to nStart + nLen | |
e87b7833 MB |
1635 | wxString& erase(size_type pos = 0, size_type n = npos) |
1636 | { return (wxString&)wxStringBase::erase(pos, n); } | |
1637 | iterator erase(iterator first, iterator last) | |
1638 | { return wxStringBase::erase(first, last); } | |
1639 | iterator erase(iterator first) | |
1640 | { return wxStringBase::erase(first); } | |
1641 | ||
1642 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
1643 | void clear() { erase(); } | |
1644 | #endif | |
3c67202d VZ |
1645 | |
1646 | // replaces the substring of length nLen starting at nStart | |
e87b7833 MB |
1647 | wxString& replace(size_t nStart, size_t nLen, const wxChar* sz) |
1648 | { return (wxString&)wxStringBase::replace(nStart, nLen, sz); } | |
1649 | // replaces the substring of length nLen starting at nStart | |
1650 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
1651 | { return (wxString&)wxStringBase::replace(nStart, nLen, str); } | |
3c67202d | 1652 | // replaces the substring with nCount copies of ch |
c9f78968 | 1653 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) |
e87b7833 | 1654 | { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); } |
3c67202d VZ |
1655 | // replaces a substring with another substring |
1656 | wxString& replace(size_t nStart, size_t nLen, | |
e87b7833 MB |
1657 | const wxString& str, size_t nStart2, size_t nLen2) |
1658 | { return (wxString&)wxStringBase::replace(nStart, nLen, str, | |
1659 | nStart2, nLen2); } | |
1660 | // replaces the substring with first nCount chars of sz | |
fb20fa43 | 1661 | wxString& replace(size_t nStart, size_t nLen, |
e87b7833 MB |
1662 | const wxChar* sz, size_t nCount) |
1663 | { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); } | |
1664 | wxString& replace(iterator first, iterator last, const_pointer s) | |
1665 | { return (wxString&)wxStringBase::replace(first, last, s); } | |
1666 | wxString& replace(iterator first, iterator last, const_pointer s, | |
1667 | size_type n) | |
1668 | { return (wxString&)wxStringBase::replace(first, last, s, n); } | |
1669 | wxString& replace(iterator first, iterator last, const wxString& s) | |
1670 | { return (wxString&)wxStringBase::replace(first, last, s); } | |
c9f78968 | 1671 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar c) |
e87b7833 MB |
1672 | { return (wxString&)wxStringBase::replace(first, last, n, c); } |
1673 | wxString& replace(iterator first, iterator last, | |
1674 | const_iterator first1, const_iterator last1) | |
1675 | { return (wxString&)wxStringBase::replace(first, last, first1, last1); } | |
3c67202d | 1676 | |
e87b7833 MB |
1677 | // string += string |
1678 | wxString& operator+=(const wxString& s) | |
1679 | { return (wxString&)wxStringBase::operator+=(s); } | |
1680 | // string += C string | |
1681 | wxString& operator+=(const wxChar *psz) | |
1682 | { return (wxString&)wxStringBase::operator+=(psz); } | |
c9f78968 VS |
1683 | wxString& operator+=(const wxCStrData& s) |
1684 | { return (wxString&)wxStringBase::operator+=(s.AsString()); } | |
e87b7833 | 1685 | // string += char |
c9f78968 | 1686 | wxString& operator+=(wxUniChar ch) |
e87b7833 | 1687 | { return (wxString&)wxStringBase::operator+=(ch); } |
c9f78968 VS |
1688 | wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } |
1689 | wxString& operator+=(char ch) { return *this += wxUniChar(ch); } | |
b77afb41 | 1690 | wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); } |
c9f78968 | 1691 | wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } |
d8a4b666 VS |
1692 | |
1693 | private: | |
992527a5 | 1694 | #if !wxUSE_STL_BASED_WXSTRING |
d8a4b666 VS |
1695 | // helpers for wxStringBuffer and wxStringBufferLength |
1696 | wxChar *DoGetWriteBuf(size_t nLen); | |
1697 | void DoUngetWriteBuf(); | |
1698 | void DoUngetWriteBuf(size_t nLen); | |
1699 | ||
1700 | friend class WXDLLIMPEXP_BASE wxStringBuffer; | |
1701 | friend class WXDLLIMPEXP_BASE wxStringBufferLength; | |
1702 | #endif | |
c9f78968 VS |
1703 | |
1704 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1705 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
1706 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
1707 | #endif | |
c801d85f KB |
1708 | }; |
1709 | ||
c9f78968 VS |
1710 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
1711 | #pragma warning (default:4275) | |
1712 | #endif | |
1713 | ||
1fc8cfbd VZ |
1714 | // notice that even though for many compilers the friend declarations above are |
1715 | // enough, from the point of view of C++ standard we must have the declarations | |
1716 | // here as friend ones are not injected in the enclosing namespace and without | |
1717 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
c9f78968 | 1718 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
b7146cbe VZ |
1719 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); |
1720 | wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); | |
1fc8cfbd | 1721 | |
c9f78968 VS |
1722 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
1723 | wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
1724 | ||
1725 | inline wxString operator+(const wxString& string, wxUniCharRef ch) | |
1726 | { return string + (wxUniChar)ch; } | |
1727 | inline wxString operator+(const wxString& string, char ch) | |
1728 | { return string + wxUniChar(ch); } | |
1729 | inline wxString operator+(const wxString& string, wchar_t ch) | |
1730 | { return string + wxUniChar(ch); } | |
1731 | inline wxString operator+(wxUniCharRef ch, const wxString& string) | |
1732 | { return (wxUniChar)ch + string; } | |
1733 | inline wxString operator+(char ch, const wxString& string) | |
1734 | { return wxUniChar(ch) + string; } | |
1735 | inline wxString operator+(wchar_t ch, const wxString& string) | |
1736 | { return wxUniChar(ch) + string; } | |
1737 | ||
b7146cbe | 1738 | |
992527a5 | 1739 | #if wxUSE_STL_BASED_WXSTRING |
07170120 | 1740 | // return an empty wxString (not very useful with wxUSE_STL == 1) |
12f99210 | 1741 | inline const wxString wxGetEmptyString() { return wxString(); } |
992527a5 | 1742 | #else // !wxUSE_STL_BASED_WXSTRING |
07170120 VZ |
1743 | // return an empty wxString (more efficient than wxString() here) |
1744 | inline const wxString& wxGetEmptyString() | |
1745 | { | |
1746 | return *(wxString *)&wxEmptyString; | |
1747 | } | |
992527a5 | 1748 | #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING |
07170120 | 1749 | |
1d218550 VZ |
1750 | // ---------------------------------------------------------------------------- |
1751 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
1752 | // ---------------------------------------------------------------------------- | |
1753 | ||
992527a5 | 1754 | #if wxUSE_STL_BASED_WXSTRING |
941b78cc MB |
1755 | |
1756 | class WXDLLIMPEXP_BASE wxStringBuffer | |
1757 | { | |
1758 | public: | |
1759 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
e87b7833 | 1760 | : m_str(str), m_buf(lenWanted) |
941b78cc MB |
1761 | { } |
1762 | ||
fe5fc682 | 1763 | ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); } |
941b78cc MB |
1764 | |
1765 | operator wxChar*() { return m_buf.data(); } | |
1766 | ||
1767 | private: | |
1768 | wxString& m_str; | |
1769 | #if wxUSE_UNICODE | |
1770 | wxWCharBuffer m_buf; | |
1771 | #else | |
1772 | wxCharBuffer m_buf; | |
1773 | #endif | |
941b78cc MB |
1774 | |
1775 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1776 | }; | |
1777 | ||
1778 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
1779 | { | |
1780 | public: | |
1781 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
1782 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
1783 | { } | |
1784 | ||
1785 | ~wxStringBufferLength() | |
1786 | { | |
1787 | wxASSERT(m_lenSet); | |
1788 | m_str.assign(m_buf.data(), m_len); | |
1789 | } | |
1790 | ||
1791 | operator wxChar*() { return m_buf.data(); } | |
1792 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
1793 | ||
1794 | private: | |
1795 | wxString& m_str; | |
1796 | #if wxUSE_UNICODE | |
1797 | wxWCharBuffer m_buf; | |
1798 | #else | |
1799 | wxCharBuffer m_buf; | |
1800 | #endif | |
1801 | size_t m_len; | |
1802 | bool m_lenSet; | |
1803 | ||
1804 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
1805 | }; | |
1806 | ||
992527a5 | 1807 | #else // if !wxUSE_STL_BASED_WXSTRING |
941b78cc | 1808 | |
bddd7a8d | 1809 | class WXDLLIMPEXP_BASE wxStringBuffer |
1d218550 VZ |
1810 | { |
1811 | public: | |
1812 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
b1801e0e | 1813 | : m_str(str), m_buf(NULL) |
d8a4b666 | 1814 | { m_buf = m_str.DoGetWriteBuf(lenWanted); } |
e015c2a3 | 1815 | |
d8a4b666 | 1816 | ~wxStringBuffer() { m_str.DoUngetWriteBuf(); } |
e015c2a3 | 1817 | |
1d218550 | 1818 | operator wxChar*() const { return m_buf; } |
e015c2a3 | 1819 | |
1d218550 VZ |
1820 | private: |
1821 | wxString& m_str; | |
1822 | wxChar *m_buf; | |
e015c2a3 VZ |
1823 | |
1824 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1d218550 VZ |
1825 | }; |
1826 | ||
941b78cc MB |
1827 | class WXDLLIMPEXP_BASE wxStringBufferLength |
1828 | { | |
1829 | public: | |
1830 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
1831 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
4055ed82 | 1832 | { |
d8a4b666 | 1833 | m_buf = m_str.DoGetWriteBuf(lenWanted); |
494b978f RN |
1834 | wxASSERT(m_buf != NULL); |
1835 | } | |
941b78cc MB |
1836 | |
1837 | ~wxStringBufferLength() | |
1838 | { | |
1839 | wxASSERT(m_lenSet); | |
d8a4b666 | 1840 | m_str.DoUngetWriteBuf(m_len); |
941b78cc MB |
1841 | } |
1842 | ||
1843 | operator wxChar*() const { return m_buf; } | |
1844 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
1845 | ||
1846 | private: | |
1847 | wxString& m_str; | |
1848 | wxChar *m_buf; | |
1849 | size_t m_len; | |
1850 | bool m_lenSet; | |
1851 | ||
1852 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
1853 | }; | |
1854 | ||
992527a5 | 1855 | #endif // !wxUSE_STL_BASED_WXSTRING |
941b78cc | 1856 | |
c801d85f | 1857 | // --------------------------------------------------------------------------- |
3c67202d | 1858 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 1859 | // --------------------------------------------------------------------------- |
f33fee2a | 1860 | |
992527a5 VS |
1861 | // note that when wxUSE_STL_BASED_WXSTRING == 1 the comparison operators taking |
1862 | // std::string are used and defining them also for wxString would only result | |
1863 | // in compilation ambiguities when comparing std::string and wxString | |
1864 | #if !wxUSE_STL_BASED_WXSTRING | |
e87b7833 | 1865 | |
fb52c2b6 VZ |
1866 | #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0 |
1867 | ||
1868 | wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) | |
1869 | ||
1870 | #undef wxCMP_WXCHAR_STRING | |
1871 | ||
1872 | // note that there is an optimization in operator==() and !=(): we (quickly) | |
1873 | // checks the strings length first, before comparing their data | |
0cbe5ee3 VZ |
1874 | inline bool operator==(const wxString& s1, const wxString& s2) |
1875 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
0cbe5ee3 VZ |
1876 | inline bool operator!=(const wxString& s1, const wxString& s2) |
1877 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
0cbe5ee3 VZ |
1878 | inline bool operator< (const wxString& s1, const wxString& s2) |
1879 | { return s1.Cmp(s2) < 0; } | |
0cbe5ee3 VZ |
1880 | inline bool operator> (const wxString& s1, const wxString& s2) |
1881 | { return s1.Cmp(s2) > 0; } | |
0cbe5ee3 VZ |
1882 | inline bool operator<=(const wxString& s1, const wxString& s2) |
1883 | { return s1.Cmp(s2) <= 0; } | |
0cbe5ee3 VZ |
1884 | inline bool operator>=(const wxString& s1, const wxString& s2) |
1885 | { return s1.Cmp(s2) >= 0; } | |
3c67202d | 1886 | |
5ca07d0f OK |
1887 | #if wxUSE_UNICODE |
1888 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 1889 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 1890 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 1891 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
1892 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
1893 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
1894 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
1895 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
0cbe5ee3 | 1896 | #else // !wxUSE_UNICODE |
5ca07d0f | 1897 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 1898 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 1899 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 1900 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
1901 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
1902 | { return (s1.Cmp((const char *)s2) != 0); } | |
1903 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
1904 | { return (s2.Cmp((const char *)s1) != 0); } | |
0cbe5ee3 | 1905 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
5ca07d0f | 1906 | |
028a2b5d | 1907 | #if wxUSE_UNICODE |
6d56eb5c | 1908 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 1909 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 1910 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 1911 | { return (const wchar_t *)buf + string; } |
0cbe5ee3 | 1912 | #else // !wxUSE_UNICODE |
6d56eb5c | 1913 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 1914 | { return string + (const char *)buf; } |
6d56eb5c | 1915 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 1916 | { return (const char *)buf + string; } |
0cbe5ee3 | 1917 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
f04f3991 | 1918 | |
992527a5 | 1919 | #endif // !wxUSE_STL_BASED_WXSTRING |
e51b17a1 VZ |
1920 | |
1921 | // comparison with char (those are not defined by std::[w]string and so should | |
1922 | // be always available) | |
c9f78968 VS |
1923 | inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } |
1924 | inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } | |
1925 | inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } | |
1926 | inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } | |
1927 | inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } | |
1928 | inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } | |
1929 | inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } | |
1930 | inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } | |
1931 | inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } | |
1932 | inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } | |
1933 | inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } | |
1934 | inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } | |
1935 | inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } | |
1936 | inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } | |
1937 | inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } | |
1938 | inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } | |
1939 | inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } | |
1940 | inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } | |
e51b17a1 | 1941 | |
d7330233 VS |
1942 | // comparison with C string in Unicode build |
1943 | #if wxUSE_UNICODE | |
fb52c2b6 VZ |
1944 | |
1945 | #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s | |
1946 | ||
1947 | wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) | |
1948 | ||
1949 | #undef wxCMP_CHAR_STRING | |
1950 | ||
d7330233 VS |
1951 | #endif // wxUSE_UNICODE |
1952 | ||
fb52c2b6 VZ |
1953 | // we also need to provide the operators for comparison with wxCStrData to |
1954 | // resolve ambiguity between operator(const wxChar *,const wxString &) and | |
1955 | // operator(const wxChar *, const wxChar *) for "p == s.c_str()" | |
1956 | // | |
1957 | // notice that these are (shallow) pointer comparisons, not (deep) string ones | |
1958 | #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() | |
1959 | #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() | |
1960 | ||
1961 | // FIXME: these ifdefs must be removed when wxCStrData has both conversions | |
1962 | #if wxUSE_UNICODE | |
1963 | wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) | |
1964 | #else | |
1965 | wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) | |
1966 | #endif | |
1967 | ||
1968 | #undef wxCMP_CHAR_CSTRDATA | |
1969 | #undef wxCMP_WCHAR_CSTRDATA | |
1970 | ||
c801d85f | 1971 | // --------------------------------------------------------------------------- |
3c67202d | 1972 | // Implementation only from here until the end of file |
c801d85f KB |
1973 | // --------------------------------------------------------------------------- |
1974 | ||
e87b7833 | 1975 | #if wxUSE_STD_IOSTREAM |
c801d85f | 1976 | |
65f19af1 | 1977 | #include "wx/iosfwrap.h" |
c801d85f | 1978 | |
bddd7a8d | 1979 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); |
c9f78968 | 1980 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); |
c801d85f | 1981 | |
3c67202d | 1982 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 1983 | |
c9f78968 VS |
1984 | // --------------------------------------------------------------------------- |
1985 | // wxCStrData implementation | |
1986 | // --------------------------------------------------------------------------- | |
1987 | ||
1988 | inline wxCStrData::wxCStrData(char *buf) | |
1989 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
1990 | inline wxCStrData::wxCStrData(wchar_t *buf) | |
1991 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
1992 | ||
1993 | inline wxCStrData::~wxCStrData() | |
1994 | { | |
1995 | if ( m_owned ) | |
1996 | delete m_str; | |
1997 | } | |
1998 | ||
1999 | #if wxUSE_UNICODE | |
2000 | inline const wchar_t* wxCStrData::AsWChar() const | |
2001 | #else | |
2002 | inline const char* wxCStrData::AsChar() const | |
2003 | #endif | |
2004 | { | |
2005 | if ( m_offset == 0 ) | |
2006 | return m_str->wx_str(); // FIXME | |
2007 | else | |
2008 | return (const wxChar*)(m_str->begin() + m_offset); | |
2009 | } | |
2010 | ||
2011 | inline wxString wxCStrData::AsString() const | |
2012 | { | |
2013 | if ( m_offset == 0 ) | |
2014 | return *m_str; | |
2015 | else | |
2016 | return m_str->Mid(m_offset); | |
2017 | } | |
2018 | ||
2019 | inline wxCStrData::operator wxString() const { return AsString(); } | |
2020 | ||
2021 | inline wxUniChar wxCStrData::operator*() const | |
2022 | { | |
2023 | if ( m_str->empty() ) | |
2024 | return wxUniChar(_T('\0')); | |
2025 | else | |
2026 | return (*m_str)[m_offset]; | |
2027 | } | |
2028 | ||
2029 | inline wxUniChar wxCStrData::operator[](size_t n) const | |
2030 | { | |
2031 | return m_str->at(m_offset + n); | |
2032 | } | |
2033 | ||
c8eb8172 VZ |
2034 | // ---------------------------------------------------------------------------- |
2035 | // implementation of wxString inline methods using wxCStrData | |
2036 | // ---------------------------------------------------------------------------- | |
2037 | ||
2038 | inline wxString& wxString::operator=(const wxCStrData& cstr) | |
2039 | { | |
2040 | return *this = cstr.AsString(); | |
2041 | } | |
2042 | ||
414b7b40 VZ |
2043 | // ---------------------------------------------------------------------------- |
2044 | // implementation of wx[W]CharBuffer inline methods using wxCStrData | |
2045 | // ---------------------------------------------------------------------------- | |
2046 | ||
d18c8d3d | 2047 | // FIXME-UTF8: move this to buffer.h; provide versions for both variants |
ccd4deab VZ |
2048 | inline wxWxCharBuffer::wxWxCharBuffer(const wxCStrData& cstr) |
2049 | : wxCharTypeBufferBase((const wxChar *)cstr) | |
414b7b40 VZ |
2050 | { |
2051 | } | |
2052 | ||
34138703 | 2053 | #endif // _WX_WXSTRINGH__ |