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