]>
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; } |
6b95b20d | 265 | // initializaes 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 | |
3c67202d VZ |
302 | // constructors and destructor |
303 | // ctor for an empty string | |
e87b7833 | 304 | wxStringBase() { Init(); } |
3c67202d | 305 | // copy ctor |
e87b7833 | 306 | wxStringBase(const wxStringBase& stringSrc) |
6b95b20d | 307 | { |
09443a26 VZ |
308 | wxASSERT_MSG( stringSrc.GetStringData()->IsValid(), |
309 | _T("did you forget to call UngetWriteBuf()?") ); | |
6b95b20d | 310 | |
e87b7833 | 311 | if ( stringSrc.empty() ) { |
6b95b20d VZ |
312 | // nothing to do for an empty string |
313 | Init(); | |
314 | } | |
315 | else { | |
316 | m_pchData = stringSrc.m_pchData; // share same data | |
317 | GetStringData()->Lock(); // => one more copy | |
318 | } | |
319 | } | |
3c67202d | 320 | // string containing nRepeat copies of ch |
e87b7833 | 321 | wxStringBase(size_type nRepeat, wxChar ch); |
3c67202d | 322 | // ctor takes first nLength characters from C string |
e87b7833 MB |
323 | // (default value of npos means take all the string) |
324 | wxStringBase(const wxChar *psz) | |
325 | { InitWith(psz, 0, npos); } | |
326 | wxStringBase(const wxChar *psz, size_t nLength) | |
b1801e0e | 327 | { InitWith(psz, 0, nLength); } |
e87b7833 | 328 | wxStringBase(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = npos) |
b1801e0e | 329 | { InitWith(psz, 0, nLength); } |
e87b7833 MB |
330 | // take nLen chars starting at nPos |
331 | wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen) | |
332 | { | |
333 | wxASSERT_MSG( str.GetStringData()->IsValid(), | |
334 | _T("did you forget to call UngetWriteBuf()?") ); | |
335 | Init(); | |
336 | size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen; | |
337 | InitWith(str.c_str(), nPos, nLen); | |
338 | } | |
339 | // take all characters from pStart to pEnd | |
340 | wxStringBase(const void *pStart, const void *pEnd); | |
341 | ||
342 | // dtor is not virtual, this class must not be inherited from! | |
1c2d1459 VZ |
343 | ~wxStringBase() |
344 | { | |
7b758e8f | 345 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
1c2d1459 VZ |
346 | //RN - according to the above VC++ does indeed inline this, |
347 | //even though it spits out two warnings | |
348 | #pragma warning (disable:4714) | |
7b758e8f | 349 | #endif |
e87b7833 | 350 | |
1c2d1459 | 351 | GetStringData()->Unlock(); |
7b758e8f RN |
352 | } |
353 | ||
354 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) | |
1c2d1459 VZ |
355 | //re-enable inlining warning |
356 | #pragma warning (default:4714) | |
357 | #endif | |
e87b7833 MB |
358 | // overloaded assignment |
359 | // from another wxString | |
360 | wxStringBase& operator=(const wxStringBase& stringSrc); | |
361 | // from a character | |
362 | wxStringBase& operator=(wxChar ch); | |
363 | // from a C string | |
364 | wxStringBase& operator=(const wxChar *psz); | |
365 | ||
366 | // return the length of the string | |
367 | size_type size() const { return GetStringData()->nDataLength; } | |
368 | // return the length of the string | |
369 | size_type length() const { return size(); } | |
370 | // return the maximum size of the string | |
371 | size_type max_size() const { return wxSTRING_MAXLEN; } | |
372 | // resize the string, filling the space with c if c != 0 | |
373 | void resize(size_t nSize, wxChar ch = wxT('\0')); | |
374 | // delete the contents of the string | |
375 | void clear() { erase(0, npos); } | |
376 | // returns true if the string is empty | |
377 | bool empty() const { return size() == 0; } | |
378 | // inform string about planned change in size | |
379 | void reserve(size_t sz) { Alloc(sz); } | |
380 | size_type capacity() const { return GetStringData()->nAllocLength; } | |
381 | ||
382 | // lib.string.access | |
383 | // return the character at position n | |
384 | value_type at(size_type n) const | |
385 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } | |
e87b7833 MB |
386 | // returns the writable character at position n |
387 | reference at(size_type n) | |
388 | { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } | |
e87b7833 MB |
389 | |
390 | // lib.string.modifiers | |
391 | // append elements str[pos], ..., str[pos+n] | |
392 | wxStringBase& append(const wxStringBase& str, size_t pos, size_t n) | |
393 | { | |
394 | wxASSERT(pos <= str.length()); | |
395 | ConcatSelf(n, str.c_str() + pos, str.length() - pos); | |
396 | return *this; | |
397 | } | |
398 | // append a string | |
399 | wxStringBase& append(const wxStringBase& str) | |
400 | { ConcatSelf(str.length(), str.c_str()); return *this; } | |
401 | // append first n (or all if n == npos) characters of sz | |
402 | wxStringBase& append(const wxChar *sz) | |
403 | { ConcatSelf(wxStrlen(sz), sz); return *this; } | |
404 | wxStringBase& append(const wxChar *sz, size_t n) | |
405 | { ConcatSelf(n, sz); return *this; } | |
406 | // append n copies of ch | |
407 | wxStringBase& append(size_t n, wxChar ch); | |
408 | // append from first to last | |
409 | wxStringBase& append(const_iterator first, const_iterator last) | |
410 | { ConcatSelf(last - first, first); return *this; } | |
411 | ||
412 | // same as `this_string = str' | |
413 | wxStringBase& assign(const wxStringBase& str) | |
414 | { return *this = str; } | |
415 | // same as ` = str[pos..pos + n] | |
416 | wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n) | |
417 | { clear(); return append(str, pos, n); } | |
418 | // same as `= first n (or all if n == npos) characters of sz' | |
419 | wxStringBase& assign(const wxChar *sz) | |
420 | { clear(); return append(sz, wxStrlen(sz)); } | |
421 | wxStringBase& assign(const wxChar *sz, size_t n) | |
422 | { clear(); return append(sz, n); } | |
423 | // same as `= n copies of ch' | |
424 | wxStringBase& assign(size_t n, wxChar ch) | |
425 | { clear(); return append(n, ch); } | |
426 | // assign from first to last | |
427 | wxStringBase& assign(const_iterator first, const_iterator last) | |
428 | { clear(); return append(first, last); } | |
429 | ||
430 | // first valid index position | |
431 | const_iterator begin() const { return m_pchData; } | |
432 | // position one after the last valid one | |
433 | const_iterator end() const { return m_pchData + length(); } | |
434 | ||
435 | // first valid index position | |
ac3c86ee | 436 | iterator begin(); |
e87b7833 | 437 | // position one after the last valid one |
ac3c86ee | 438 | iterator end(); |
e87b7833 MB |
439 | |
440 | // insert another string | |
441 | wxStringBase& insert(size_t nPos, const wxStringBase& str) | |
442 | { | |
443 | wxASSERT( str.GetStringData()->IsValid() ); | |
444 | return insert(nPos, str.c_str(), str.length()); | |
445 | } | |
446 | // insert n chars of str starting at nStart (in str) | |
447 | wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n) | |
448 | { | |
449 | wxASSERT( str.GetStringData()->IsValid() ); | |
450 | wxASSERT( nStart < str.length() ); | |
451 | size_t strLen = str.length() - nStart; | |
452 | n = strLen < n ? strLen : n; | |
453 | return insert(nPos, str.c_str() + nStart, n); | |
454 | } | |
455 | // insert first n (or all if n == npos) characters of sz | |
456 | wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos); | |
457 | // insert n copies of ch | |
458 | wxStringBase& insert(size_t nPos, size_t n, wxChar ch) | |
459 | { return insert(nPos, wxStringBase(n, ch)); } | |
460 | iterator insert(iterator it, wxChar ch) | |
1c2d1459 | 461 | { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; } |
e87b7833 MB |
462 | void insert(iterator it, const_iterator first, const_iterator last) |
463 | { insert(it - begin(), first, last - first); } | |
464 | void insert(iterator it, size_type n, wxChar ch) | |
465 | { insert(it - begin(), n, ch); } | |
466 | ||
467 | // delete characters from nStart to nStart + nLen | |
468 | wxStringBase& erase(size_type pos = 0, size_type n = npos); | |
469 | iterator erase(iterator first, iterator last) | |
470 | { | |
471 | size_t idx = first - begin(); | |
472 | erase(idx, last - first); | |
473 | return begin() + idx; | |
474 | } | |
475 | iterator erase(iterator first); | |
476 | ||
477 | // explicit conversion to C string (use this with printf()!) | |
478 | const wxChar* c_str() const { return m_pchData; } | |
479 | const wxChar* data() const { return m_pchData; } | |
480 | ||
481 | // replaces the substring of length nLen starting at nStart | |
482 | wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz); | |
483 | // replaces the substring of length nLen starting at nStart | |
484 | wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str) | |
485 | { return replace(nStart, nLen, str.c_str()); } | |
486 | // replaces the substring with nCount copies of ch | |
487 | wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch); | |
488 | // replaces a substring with another substring | |
489 | wxStringBase& replace(size_t nStart, size_t nLen, | |
490 | const wxStringBase& str, size_t nStart2, size_t nLen2); | |
491 | // replaces the substring with first nCount chars of sz | |
492 | wxStringBase& replace(size_t nStart, size_t nLen, | |
493 | const wxChar* sz, size_t nCount); | |
494 | wxStringBase& replace(iterator first, iterator last, const_pointer s) | |
495 | { return replace(first - begin(), last - first, s); } | |
496 | wxStringBase& replace(iterator first, iterator last, const_pointer s, | |
497 | size_type n) | |
498 | { return replace(first - begin(), last - first, s, n); } | |
499 | wxStringBase& replace(iterator first, iterator last, const wxStringBase& s) | |
500 | { return replace(first - begin(), last - first, s); } | |
501 | wxStringBase& replace(iterator first, iterator last, size_type n, wxChar c) | |
502 | { return replace(first - begin(), last - first, n, c); } | |
503 | wxStringBase& replace(iterator first, iterator last, | |
504 | const_iterator first1, const_iterator last1) | |
505 | { return replace(first - begin(), last - first, first1, last1 - first1); } | |
506 | ||
507 | // swap two strings | |
508 | void swap(wxStringBase& str); | |
509 | ||
510 | // All find() functions take the nStart argument which specifies the | |
511 | // position to start the search on, the default value is 0. All functions | |
512 | // return npos if there were no match. | |
513 | ||
514 | // find a substring | |
515 | size_t find(const wxStringBase& str, size_t nStart = 0) const; | |
516 | ||
e87b7833 MB |
517 | // find first n characters of sz |
518 | size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; | |
e87b7833 | 519 | |
e87b7833 MB |
520 | // find the first occurence of character ch after nStart |
521 | size_t find(wxChar ch, size_t nStart = 0) const; | |
1412634b | 522 | |
e87b7833 MB |
523 | // rfind() family is exactly like find() but works right to left |
524 | ||
525 | // as find, but from the end | |
526 | size_t rfind(const wxStringBase& str, size_t nStart = npos) const; | |
527 | ||
e87b7833 MB |
528 | // as find, but from the end |
529 | size_t rfind(const wxChar* sz, size_t nStart = npos, | |
530 | size_t n = npos) const; | |
531 | // as find, but from the end | |
532 | size_t rfind(wxChar ch, size_t nStart = npos) const; | |
533 | ||
534 | // find first/last occurence of any character in the set | |
535 | ||
536 | // as strpbrk() but starts at nStart, returns npos if not found | |
537 | size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const | |
538 | { return find_first_of(str.c_str(), nStart); } | |
539 | // same as above | |
540 | size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; | |
e2101186 | 541 | size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
542 | // same as find(char, size_t) |
543 | size_t find_first_of(wxChar c, size_t nStart = 0) const | |
544 | { return find(c, nStart); } | |
545 | // find the last (starting from nStart) char from str in this string | |
546 | size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const | |
547 | { return find_last_of(str.c_str(), nStart); } | |
548 | // same as above | |
549 | size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; | |
e2101186 | 550 | size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
551 | // same as above |
552 | size_t find_last_of(wxChar c, size_t nStart = npos) const | |
553 | { return rfind(c, nStart); } | |
554 | ||
555 | // find first/last occurence of any character not in the set | |
556 | ||
557 | // as strspn() (starting from nStart), returns npos on failure | |
558 | size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const | |
559 | { return find_first_not_of(str.c_str(), nStart); } | |
560 | // same as above | |
561 | size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; | |
e2101186 | 562 | size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
563 | // same as above |
564 | size_t find_first_not_of(wxChar ch, size_t nStart = 0) const; | |
565 | // as strcspn() | |
566 | size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const | |
e2101186 | 567 | { return find_last_not_of(str.c_str(), nStart); } |
e87b7833 MB |
568 | // same as above |
569 | size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; | |
e2101186 | 570 | size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const; |
e87b7833 MB |
571 | // same as above |
572 | size_t find_last_not_of(wxChar ch, size_t nStart = npos) const; | |
573 | ||
574 | // All compare functions return -1, 0 or 1 if the [sub]string is less, | |
575 | // equal or greater than the compare() argument. | |
576 | ||
dcb68102 RN |
577 | // comparison with another string |
578 | int compare(const wxStringBase& str) const; | |
e87b7833 MB |
579 | // comparison with a substring |
580 | int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; | |
581 | // comparison of 2 substrings | |
582 | int compare(size_t nStart, size_t nLen, | |
583 | const wxStringBase& str, size_t nStart2, size_t nLen2) const; | |
dcb68102 RN |
584 | // comparison with a c string |
585 | int compare(const wxChar* sz) const; | |
e87b7833 MB |
586 | // substring comparison with first nCount characters of sz |
587 | int compare(size_t nStart, size_t nLen, | |
588 | const wxChar* sz, size_t nCount = npos) const; | |
589 | ||
590 | size_type copy(wxChar* s, size_type n, size_type pos = 0); | |
591 | ||
592 | // substring extraction | |
593 | wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const; | |
594 | ||
595 | // string += string | |
596 | wxStringBase& operator+=(const wxStringBase& s) { return append(s); } | |
597 | // string += C string | |
598 | wxStringBase& operator+=(const wxChar *psz) { return append(psz); } | |
599 | // string += char | |
600 | wxStringBase& operator+=(wxChar ch) { return append(1, ch); } | |
601 | }; | |
602 | ||
603 | #endif // !wxUSE_STL | |
604 | ||
b6339dde VZ |
605 | // ---------------------------------------------------------------------------- |
606 | // wxString: string class trying to be compatible with std::string, MFC | |
607 | // CString and wxWindows 1.x wxString all at once | |
e87b7833 MB |
608 | // --------------------------------------------------------------------------- |
609 | ||
610 | class WXDLLIMPEXP_BASE wxString : public wxStringBase | |
611 | { | |
612 | #if !wxUSE_STL | |
613 | friend class WXDLLIMPEXP_BASE wxArrayString; | |
614 | #endif | |
615 | ||
616 | // NB: special care was taken in arranging the member functions in such order | |
617 | // that all inline functions can be effectively inlined, verify that all | |
618 | // performace critical functions are still inlined if you change order! | |
619 | private: | |
620 | // if we hadn't made these operators private, it would be possible to | |
621 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
622 | // converted to char in C and we do have operator=(char) | |
623 | // | |
624 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
625 | // to assign another numeric type to wxString will now result in | |
626 | // ambiguity between operator=(char) and operator=(int) | |
627 | wxString& operator=(int); | |
628 | ||
629 | // these methods are not implemented - there is _no_ conversion from int to | |
630 | // string, you're doing something wrong if the compiler wants to call it! | |
631 | // | |
632 | // try `s << i' or `s.Printf("%d", i)' instead | |
633 | wxString(int); | |
634 | ||
635 | public: | |
636 | // constructors and destructor | |
637 | // ctor for an empty string | |
638 | wxString() : wxStringBase() { } | |
639 | // copy ctor | |
640 | wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { } | |
641 | wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { } | |
642 | // string containing nRepeat copies of ch | |
643 | wxString(wxChar ch, size_t nRepeat = 1) | |
644 | : wxStringBase(nRepeat, ch) { } | |
645 | wxString(size_t nRepeat, wxChar ch) | |
646 | : wxStringBase(nRepeat, ch) { } | |
647 | // ctor takes first nLength characters from C string | |
648 | // (default value of npos means take all the string) | |
649 | wxString(const wxChar *psz) | |
650 | : wxStringBase(psz ? psz : wxT("")) { } | |
651 | wxString(const wxChar *psz, size_t nLength) | |
652 | : wxStringBase(psz, nLength) { } | |
653 | wxString(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = npos) | |
654 | : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { } | |
e90c1d2a | 655 | |
ce76f779 VZ |
656 | // even we're not build with wxUSE_STL == 1 it is very convenient to allow |
657 | // implicit conversions from std::string to wxString as this allows to use | |
658 | // the same strings in non-GUI and GUI code, however we don't want to | |
659 | // unconditionally add this ctor as it would make wx lib dependent on | |
660 | // libstdc++ on some Linux versions which is bad, so instead we ask the | |
661 | // client code to define this wxUSE_STD_STRING symbol if they need it | |
47561b0d | 662 | #if wxUSE_STD_STRING |
ce76f779 VZ |
663 | wxString(const wxStdString& s) |
664 | : wxStringBase(s.c_str()) { } | |
665 | #endif // wxUSE_STD_STRING | |
666 | ||
2bb67b80 OK |
667 | #if wxUSE_UNICODE |
668 | // from multibyte string | |
e87b7833 | 669 | wxString(const char *psz, wxMBConv& conv, size_t nLength = npos); |
2bb67b80 | 670 | // from wxWCharBuffer (i.e. return from wxGetString) |
e87b7833 | 671 | wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { } |
e90c1d2a | 672 | #else // ANSI |
3c67202d | 673 | // from C string (for compilers using unsigned char) |
e87b7833 MB |
674 | wxString(const unsigned char* psz, size_t nLength = npos) |
675 | : wxStringBase((const char*)psz, nLength) { } | |
e90c1d2a | 676 | |
6f841509 | 677 | #if wxUSE_WCHAR_T |
2bb67b80 | 678 | // from wide (Unicode) string |
e87b7833 | 679 | wxString(const wchar_t *pwz, wxMBConv& conv = wxConvLibc, size_t nLength = npos); |
e90c1d2a VZ |
680 | #endif // !wxUSE_WCHAR_T |
681 | ||
2bb67b80 OK |
682 | // from wxCharBuffer |
683 | wxString(const wxCharBuffer& psz) | |
051aa330 | 684 | : wxStringBase(psz) { } |
e90c1d2a VZ |
685 | #endif // Unicode/ANSI |
686 | ||
3c67202d VZ |
687 | // generic attributes & operations |
688 | // as standard strlen() | |
e87b7833 | 689 | size_t Len() const { return length(); } |
3c67202d | 690 | // string contains any characters? |
e87b7833 | 691 | bool IsEmpty() const { return empty(); } |
d775fa82 | 692 | // empty string is "false", so !str will return true |
dcfde592 | 693 | bool operator!() const { return IsEmpty(); } |
735d1db6 VZ |
694 | // truncate the string to given length |
695 | wxString& Truncate(size_t uiLen); | |
3c67202d | 696 | // empty string contents |
dd1eaa89 VZ |
697 | void Empty() |
698 | { | |
735d1db6 | 699 | Truncate(0); |
dd1eaa89 | 700 | |
5a8231ef | 701 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); |
7be07660 | 702 | } |
3c67202d | 703 | // empty the string and free memory |
7be07660 VZ |
704 | void Clear() |
705 | { | |
e87b7833 MB |
706 | wxString tmp(wxEmptyString); |
707 | swap(tmp); | |
dd1eaa89 VZ |
708 | } |
709 | ||
3c67202d VZ |
710 | // contents test |
711 | // Is an ascii value | |
c801d85f | 712 | bool IsAscii() const; |
3c67202d | 713 | // Is a number |
c801d85f | 714 | bool IsNumber() const; |
3c67202d | 715 | // Is a word |
c801d85f | 716 | bool IsWord() const; |
c801d85f | 717 | |
3c67202d VZ |
718 | // data access (all indexes are 0 based) |
719 | // read access | |
2bb67b80 | 720 | wxChar GetChar(size_t n) const |
9d9ad673 | 721 | { return at(n); } |
3c67202d | 722 | // read/write access |
2bb67b80 | 723 | wxChar& GetWritableChar(size_t n) |
9d9ad673 | 724 | { return at(n); } |
3c67202d | 725 | // write access |
2bb67b80 | 726 | void SetChar(size_t n, wxChar ch) |
9d9ad673 | 727 | { at(n) = ch; } |
c801d85f | 728 | |
3c67202d | 729 | // get last character |
2bb67b80 | 730 | wxChar Last() const |
09443a26 | 731 | { |
5a8231ef | 732 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
09443a26 | 733 | |
9d9ad673 | 734 | return at(length() - 1); |
09443a26 VZ |
735 | } |
736 | ||
3c67202d | 737 | // get writable last character |
2bb67b80 | 738 | wxChar& Last() |
09443a26 | 739 | { |
5a8231ef | 740 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
9d9ad673 | 741 | return at(length() - 1); |
09443a26 | 742 | } |
c801d85f | 743 | |
d836ee96 | 744 | /* |
9d9ad673 | 745 | Note that we we must define all of the overloads below to avoid |
01398433 VZ |
746 | ambiguity when using str[0]. Also note that for a conforming compiler we |
747 | don't need const version of operatorp[] at all as indexed access to | |
748 | const string is provided by implicit conversion to "const wxChar *" | |
749 | below and defining them would only result in ambiguities, but some other | |
750 | compilers refuse to compile "str[0]" without them. | |
d836ee96 VZ |
751 | */ |
752 | ||
01398433 VZ |
753 | #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__) |
754 | wxChar operator[](int n) const | |
755 | { return wxStringBase::at(n); } | |
756 | wxChar operator[](size_type n) const | |
757 | { return wxStringBase::at(n); } | |
758 | #ifndef wxSIZE_T_IS_UINT | |
759 | wxChar operator[](unsigned int n) const | |
760 | { return wxStringBase::at(n); } | |
761 | #endif // size_t != unsigned int | |
762 | #endif // broken compiler | |
763 | ||
764 | ||
9d9ad673 VZ |
765 | // operator versions of GetWriteableChar() |
766 | wxChar& operator[](int n) | |
767 | { return wxStringBase::at(n); } | |
e87b7833 | 768 | wxChar& operator[](size_type n) |
9d9ad673 | 769 | { return wxStringBase::at(n); } |
d836ee96 | 770 | #ifndef wxSIZE_T_IS_UINT |
d836ee96 | 771 | wxChar& operator[](unsigned int n) |
9d9ad673 | 772 | { return wxStringBase::at(n); } |
d836ee96 | 773 | #endif // size_t != unsigned int |
c801d85f | 774 | |
3c67202d | 775 | // implicit conversion to C string |
e87b7833 | 776 | operator const wxChar*() const { return c_str(); } |
e015c2a3 | 777 | |
e015c2a3 | 778 | // identical to c_str(), for wxWin 1.6x compatibility |
e87b7833 | 779 | const wxChar* wx_str() const { return c_str(); } |
e015c2a3 | 780 | // identical to c_str(), for MFC compatibility |
e87b7833 | 781 | const wxChar* GetData() const { return c_str(); } |
e90c1d2a | 782 | |
e015c2a3 VZ |
783 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
784 | // converting numbers or strings which are certain not to contain special | |
785 | // chars (typically system functions, X atoms, environment variables etc.) | |
786 | // | |
787 | // the behaviour of these functions with the strings containing anything | |
788 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 789 | #if wxUSE_UNICODE |
2b5f62a0 VZ |
790 | static wxString FromAscii(const char *ascii); // string |
791 | static wxString FromAscii(const char ascii); // char | |
b1ac3b56 | 792 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
793 | #else // ANSI |
794 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
2b5f62a0 | 795 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
796 | const char *ToAscii() const { return c_str(); } |
797 | #endif // Unicode/!Unicode | |
b1ac3b56 | 798 | |
2b5f62a0 | 799 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 800 | // buffer with temporary data |
f6bcfd97 BP |
801 | // |
802 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
803 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
804 | // fn_str() to return a string which should be used with the OS APIs | |
805 | // accepting the file names. The return value is always the same, but the | |
806 | // type differs because a function may either return pointer to the buffer | |
807 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 808 | #if wxUSE_UNICODE |
265d5cce | 809 | const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const; |
f6bcfd97 | 810 | |
e90c1d2a VZ |
811 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
812 | ||
e87b7833 | 813 | const wxChar* wc_str() const { return c_str(); } |
f6bcfd97 BP |
814 | |
815 | // for compatibility with !wxUSE_UNICODE version | |
e87b7833 | 816 | const wxChar* wc_str(wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
e90c1d2a | 817 | |
2bb67b80 | 818 | #if wxMBFILES |
5f709e67 | 819 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 820 | #else // !wxMBFILES |
e87b7833 | 821 | const wxChar* fn_str() const { return c_str(); } |
e90c1d2a VZ |
822 | #endif // wxMBFILES/!wxMBFILES |
823 | #else // ANSI | |
e87b7833 | 824 | const wxChar* mb_str() const { return c_str(); } |
f6bcfd97 BP |
825 | |
826 | // for compatibility with wxUSE_UNICODE version | |
e87b7833 | 827 | const wxChar* mb_str(wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
f6bcfd97 | 828 | |
e90c1d2a | 829 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 830 | |
6f841509 | 831 | #if wxUSE_WCHAR_T |
265d5cce | 832 | const wxWCharBuffer wc_str(wxMBConv& conv) const; |
e90c1d2a | 833 | #endif // wxUSE_WCHAR_T |
d5c8817c SC |
834 | #ifdef __WXOSX__ |
835 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
836 | #else | |
e87b7833 | 837 | const wxChar* fn_str() const { return c_str(); } |
d5c8817c | 838 | #endif |
e90c1d2a | 839 | #endif // Unicode/ANSI |
c801d85f | 840 | |
3c67202d VZ |
841 | // overloaded assignment |
842 | // from another wxString | |
e87b7833 MB |
843 | wxString& operator=(const wxStringBase& stringSrc) |
844 | { return (wxString&)wxStringBase::operator=(stringSrc); } | |
3c67202d | 845 | // from a character |
e87b7833 MB |
846 | wxString& operator=(wxChar ch) |
847 | { return (wxString&)wxStringBase::operator=(ch); } | |
c57e3bd5 RN |
848 | // from a C string - STL probably will crash on NULL, |
849 | // so we need to compensate in that case | |
850 | #if wxUSE_STL | |
851 | wxString& operator=(const wxChar *psz) | |
852 | { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; } | |
853 | #else | |
e87b7833 MB |
854 | wxString& operator=(const wxChar *psz) |
855 | { return (wxString&)wxStringBase::operator=(psz); } | |
c57e3bd5 RN |
856 | #endif |
857 | ||
111bb7f2 OK |
858 | #if wxUSE_UNICODE |
859 | // from wxWCharBuffer | |
b1801e0e | 860 | wxString& operator=(const wxWCharBuffer& psz) |
e87b7833 | 861 | { (void) operator=((const wchar_t *)psz); return *this; } |
e90c1d2a | 862 | #else // ANSI |
3c67202d | 863 | // from another kind of C string |
c801d85f | 864 | wxString& operator=(const unsigned char* psz); |
6f841509 | 865 | #if wxUSE_WCHAR_T |
3c67202d | 866 | // from a wide string |
c801d85f | 867 | wxString& operator=(const wchar_t *pwz); |
6f841509 | 868 | #endif |
111bb7f2 | 869 | // from wxCharBuffer |
b1801e0e | 870 | wxString& operator=(const wxCharBuffer& psz) |
e87b7833 | 871 | { (void) operator=((const char *)psz); return *this; } |
e90c1d2a | 872 | #endif // Unicode/ANSI |
3c67202d VZ |
873 | |
874 | // string concatenation | |
875 | // in place concatenation | |
876 | /* | |
877 | Concatenate and return the result. Note that the left to right | |
878 | associativity of << allows to write things like "str << str1 << str2 | |
879 | << ..." (unlike with +=) | |
880 | */ | |
881 | // string += string | |
dd1eaa89 VZ |
882 | wxString& operator<<(const wxString& s) |
883 | { | |
e87b7833 | 884 | #if !wxUSE_STL |
09443a26 VZ |
885 | wxASSERT_MSG( s.GetStringData()->IsValid(), |
886 | _T("did you forget to call UngetWriteBuf()?") ); | |
e87b7833 | 887 | #endif |
dd1eaa89 | 888 | |
e87b7833 | 889 | append(s); |
dd1eaa89 VZ |
890 | return *this; |
891 | } | |
3c67202d | 892 | // string += C string |
2bb67b80 | 893 | wxString& operator<<(const wxChar *psz) |
e87b7833 | 894 | { append(psz); return *this; } |
3c67202d | 895 | // string += char |
e87b7833 | 896 | wxString& operator<<(wxChar ch) { append(1, ch); return *this; } |
2bb67b80 OK |
897 | |
898 | // string += buffer (i.e. from wxGetString) | |
899 | #if wxUSE_UNICODE | |
09443a26 VZ |
900 | wxString& operator<<(const wxWCharBuffer& s) |
901 | { (void)operator<<((const wchar_t *)s); return *this; } | |
902 | void operator+=(const wxWCharBuffer& s) | |
903 | { (void)operator<<((const wchar_t *)s); } | |
904 | #else // !wxUSE_UNICODE | |
905 | wxString& operator<<(const wxCharBuffer& s) | |
906 | { (void)operator<<((const char *)s); return *this; } | |
907 | void operator+=(const wxCharBuffer& s) | |
908 | { (void)operator<<((const char *)s); } | |
909 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
6b95b20d | 910 | |
3c67202d | 911 | // string += C string |
09443a26 VZ |
912 | wxString& Append(const wxString& s) |
913 | { | |
5a8231ef WS |
914 | // test for empty() to share the string if possible |
915 | if ( empty() ) | |
09443a26 VZ |
916 | *this = s; |
917 | else | |
e87b7833 | 918 | append(s); |
09443a26 VZ |
919 | return *this; |
920 | } | |
2bb67b80 | 921 | wxString& Append(const wxChar* psz) |
e87b7833 | 922 | { append(psz); return *this; } |
3c67202d | 923 | // append count copies of given character |
2bb67b80 | 924 | wxString& Append(wxChar ch, size_t count = 1u) |
e87b7833 | 925 | { append(count, ch); return *this; } |
8f06a017 | 926 | wxString& Append(const wxChar* psz, size_t nLen) |
e87b7833 | 927 | { append(psz, nLen); return *this; } |
3c67202d VZ |
928 | |
929 | // prepend a string, return the string itself | |
930 | wxString& Prepend(const wxString& str) | |
931 | { *this = str + *this; return *this; } | |
932 | ||
933 | // non-destructive concatenation | |
934 | // | |
bddd7a8d | 935 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
3c67202d | 936 | // |
bddd7a8d | 937 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); |
3c67202d | 938 | // |
bddd7a8d | 939 | friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); |
3c67202d | 940 | // |
bddd7a8d | 941 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); |
3c67202d | 942 | // |
bddd7a8d | 943 | friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); |
3c67202d VZ |
944 | |
945 | // stream-like functions | |
946 | // insert an int into string | |
3ce65f6c VZ |
947 | wxString& operator<<(int i) |
948 | { return (*this) << Format(_T("%d"), i); } | |
949 | // insert an unsigned int into string | |
950 | wxString& operator<<(unsigned int ui) | |
951 | { return (*this) << Format(_T("%u"), ui); } | |
952 | // insert a long into string | |
953 | wxString& operator<<(long l) | |
954 | { return (*this) << Format(_T("%ld"), l); } | |
955 | // insert an unsigned long into string | |
956 | wxString& operator<<(unsigned long ul) | |
957 | { return (*this) << Format(_T("%lu"), ul); } | |
3fc1adbd MW |
958 | #if defined wxLongLong_t && !defined wxLongLongIsLong |
959 | // insert a long long if they exist and aren't longs | |
960 | wxString& operator<<(wxLongLong_t ll) | |
b7859132 MW |
961 | { |
962 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
963 | return (*this) << Format(fmt, ll); | |
964 | } | |
3fc1adbd MW |
965 | // insert an unsigned long long |
966 | wxString& operator<<(wxULongLong_t ull) | |
b7859132 MW |
967 | { |
968 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
969 | return (*this) << Format(fmt , ull); | |
970 | } | |
3fc1adbd | 971 | #endif |
3c67202d | 972 | // insert a float into string |
3ce65f6c VZ |
973 | wxString& operator<<(float f) |
974 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 975 | // insert a double into string |
3ce65f6c VZ |
976 | wxString& operator<<(double d) |
977 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 978 | |
3c67202d | 979 | // string comparison |
30b21f9a | 980 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
dcb68102 RN |
981 | int Cmp(const wxChar *psz) const; |
982 | int Cmp(const wxString& s) const; | |
3c67202d | 983 | // same as Cmp() but not case-sensitive |
dcb68102 RN |
984 | int CmpNoCase(const wxChar *psz) const; |
985 | int CmpNoCase(const wxString& s) const; | |
3c67202d VZ |
986 | // test for the string equality, either considering case or not |
987 | // (if compareWithCase then the case matters) | |
d775fa82 | 988 | bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const |
3c67202d | 989 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } |
d775fa82 WS |
990 | // comparison with a signle character: returns true if equal |
991 | bool IsSameAs(wxChar c, bool compareWithCase = true) const | |
f33fee2a | 992 | { |
e87b7833 | 993 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c |
f33fee2a VZ |
994 | : wxToupper(GetChar(0u)) == wxToupper(c)); |
995 | } | |
3c67202d VZ |
996 | |
997 | // simple sub-string extraction | |
998 | // return substring starting at nFirst of length nCount (or till the end | |
999 | // if nCount = default value) | |
e87b7833 | 1000 | wxString Mid(size_t nFirst, size_t nCount = npos) const; |
3c67202d | 1001 | |
f6bcfd97 | 1002 | // operator version of Mid() |
3c67202d VZ |
1003 | wxString operator()(size_t start, size_t len) const |
1004 | { return Mid(start, len); } | |
1005 | ||
c0881dc3 | 1006 | // check that the string starts with prefix and return the rest of the |
f6bcfd97 | 1007 | // string in the provided pointer if it is not NULL, otherwise return |
d775fa82 | 1008 | // false |
f6bcfd97 BP |
1009 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; |
1010 | ||
3c67202d | 1011 | // get first nCount characters |
c801d85f | 1012 | wxString Left(size_t nCount) const; |
3c67202d | 1013 | // get last nCount characters |
c801d85f | 1014 | wxString Right(size_t nCount) const; |
c0881dc3 | 1015 | // get all characters before the first occurance of ch |
3c67202d | 1016 | // (returns the whole string if ch not found) |
2bb67b80 | 1017 | wxString BeforeFirst(wxChar ch) const; |
3c67202d VZ |
1018 | // get all characters before the last occurence of ch |
1019 | // (returns empty string if ch not found) | |
2bb67b80 | 1020 | wxString BeforeLast(wxChar ch) const; |
3c67202d VZ |
1021 | // get all characters after the first occurence of ch |
1022 | // (returns empty string if ch not found) | |
2bb67b80 | 1023 | wxString AfterFirst(wxChar ch) const; |
3c67202d VZ |
1024 | // get all characters after the last occurence of ch |
1025 | // (returns the whole string if ch not found) | |
2bb67b80 | 1026 | wxString AfterLast(wxChar ch) const; |
3c67202d VZ |
1027 | |
1028 | // for compatibility only, use more explicitly named functions above | |
2bb67b80 OK |
1029 | wxString Before(wxChar ch) const { return BeforeLast(ch); } |
1030 | wxString After(wxChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
1031 | |
1032 | // case conversion | |
c84c52de | 1033 | // convert to upper case in place, return the string itself |
c801d85f | 1034 | wxString& MakeUpper(); |
c84c52de | 1035 | // convert to upper case, return the copy of the string |
03ab016d JS |
1036 | // Here's something to remember: BC++ doesn't like returns in inlines. |
1037 | wxString Upper() const ; | |
c84c52de | 1038 | // convert to lower case in place, return the string itself |
c801d85f | 1039 | wxString& MakeLower(); |
c84c52de | 1040 | // convert to lower case, return the copy of the string |
03ab016d | 1041 | wxString Lower() const ; |
c801d85f | 1042 | |
3c67202d VZ |
1043 | // trimming/padding whitespace (either side) and truncating |
1044 | // remove spaces from left or from right (default) side | |
d775fa82 | 1045 | wxString& Trim(bool bFromRight = true); |
3c67202d | 1046 | // add nCount copies chPad in the beginning or at the end (default) |
d775fa82 | 1047 | wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = true); |
dd1eaa89 | 1048 | |
3c67202d VZ |
1049 | // searching and replacing |
1050 | // searching (return starting index, or -1 if not found) | |
d775fa82 | 1051 | int Find(wxChar ch, bool bFromEnd = false) const; // like strchr/strrchr |
3c67202d | 1052 | // searching (return starting index, or -1 if not found) |
2bb67b80 | 1053 | int Find(const wxChar *pszSub) const; // like strstr |
3c67202d VZ |
1054 | // replace first (or all of bReplaceAll) occurences of substring with |
1055 | // another string, returns the number of replacements made | |
2bb67b80 OK |
1056 | size_t Replace(const wxChar *szOld, |
1057 | const wxChar *szNew, | |
d775fa82 | 1058 | bool bReplaceAll = true); |
3c67202d VZ |
1059 | |
1060 | // check if the string contents matches a mask containing '*' and '?' | |
2bb67b80 | 1061 | bool Matches(const wxChar *szMask) const; |
c801d85f | 1062 | |
d775fa82 | 1063 | // conversion to numbers: all functions return true only if the whole |
538f35cc VZ |
1064 | // string is a number and put the value of this number into the pointer |
1065 | // provided, the base is the numeric base in which the conversion should be | |
1066 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1067 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
cd0b1709 | 1068 | // convert to a signed integer |
538f35cc | 1069 | bool ToLong(long *val, int base = 10) const; |
cd0b1709 | 1070 | // convert to an unsigned integer |
538f35cc | 1071 | bool ToULong(unsigned long *val, int base = 10) const; |
cd0b1709 VZ |
1072 | // convert to a double |
1073 | bool ToDouble(double *val) const; | |
1074 | ||
3c67202d VZ |
1075 | // formated input/output |
1076 | // as sprintf(), returns the number of characters written or < 0 on error | |
7357f981 GD |
1077 | // (take 'this' into account in attribute parameter count) |
1078 | int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
3c67202d | 1079 | // as vprintf(), returns the number of characters written or < 0 on error |
2bb67b80 | 1080 | int PrintfV(const wxChar* pszFormat, va_list argptr); |
dd1eaa89 | 1081 | |
341e7d28 | 1082 | // returns the string containing the result of Printf() to it |
7357f981 | 1083 | static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; |
341e7d28 VZ |
1084 | // the same as above, but takes a va_list |
1085 | static wxString FormatV(const wxChar *pszFormat, va_list argptr); | |
1086 | ||
3c67202d VZ |
1087 | // raw access to string memory |
1088 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 1089 | // only works if the data of this string is not shared |
e87b7833 | 1090 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } |
3c67202d | 1091 | // minimize the string's memory |
dd1eaa89 | 1092 | // only works if the data of this string is not shared |
b1801e0e | 1093 | bool Shrink(); |
e87b7833 | 1094 | #if !wxUSE_STL |
3c67202d VZ |
1095 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
1096 | // a.s.a.p. to put string back in a reasonable state! | |
2bb67b80 | 1097 | wxChar *GetWriteBuf(size_t nLen); |
3c67202d | 1098 | // call this immediately after GetWriteBuf() has been used |
8fd0f20b | 1099 | void UngetWriteBuf(); |
8f06a017 | 1100 | void UngetWriteBuf(size_t nLen); |
e87b7833 | 1101 | #endif |
c801d85f | 1102 | |
77ffb593 | 1103 | // wxWidgets version 1 compatibility functions |
3c67202d VZ |
1104 | |
1105 | // use Mid() | |
1106 | wxString SubString(size_t from, size_t to) const | |
1107 | { return Mid(from, (to - from + 1)); } | |
1108 | // values for second parameter of CompareTo function | |
c801d85f | 1109 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 1110 | // values for first parameter of Strip function |
c801d85f | 1111 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 1112 | |
7357f981 GD |
1113 | // use Printf() |
1114 | // (take 'this' into account in attribute parameter count) | |
1115 | int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
c801d85f | 1116 | |
3c67202d | 1117 | // use Cmp() |
2bb67b80 | 1118 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 1119 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 1120 | |
3c67202d | 1121 | // use Len |
e87b7833 | 1122 | size_t Length() const { return length(); } |
3c67202d | 1123 | // Count the number of characters |
2bb67b80 | 1124 | int Freq(wxChar ch) const; |
3c67202d | 1125 | // use MakeLower |
c801d85f | 1126 | void LowerCase() { MakeLower(); } |
3c67202d | 1127 | // use MakeUpper |
c801d85f | 1128 | void UpperCase() { MakeUpper(); } |
3c67202d | 1129 | // use Trim except that it doesn't change this string |
c801d85f KB |
1130 | wxString Strip(stripType w = trailing) const; |
1131 | ||
3c67202d | 1132 | // use Find (more general variants not yet supported) |
2bb67b80 OK |
1133 | size_t Index(const wxChar* psz) const { return Find(psz); } |
1134 | size_t Index(wxChar ch) const { return Find(ch); } | |
3c67202d | 1135 | // use Truncate |
c801d85f | 1136 | wxString& Remove(size_t pos) { return Truncate(pos); } |
e87b7833 | 1137 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } |
c801d85f | 1138 | |
e87b7833 MB |
1139 | wxString& Remove(size_t nStart, size_t nLen) |
1140 | { return (wxString&)erase( nStart, nLen ); } | |
dd1eaa89 | 1141 | |
3c67202d | 1142 | // use Find() |
2bb67b80 OK |
1143 | int First( const wxChar ch ) const { return Find(ch); } |
1144 | int First( const wxChar* psz ) const { return Find(psz); } | |
3ed358cb | 1145 | int First( const wxString &str ) const { return Find(str); } |
d775fa82 WS |
1146 | int Last( const wxChar ch ) const { return Find(ch, true); } |
1147 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } | |
c801d85f | 1148 | |
5a8231ef WS |
1149 | // use empty() |
1150 | bool IsNull() const { return empty(); } | |
c801d85f | 1151 | |
3c67202d | 1152 | // std::string compatibility functions |
dd1eaa89 | 1153 | |
3c67202d VZ |
1154 | // take nLen chars starting at nPos |
1155 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
e87b7833 | 1156 | : wxStringBase(str, nPos, nLen) { } |
3c67202d | 1157 | // take all characters from pStart to pEnd |
e87b7833 | 1158 | wxString(const void *pStart, const void *pEnd) |
6e94fff9 | 1159 | : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { } |
e87b7833 MB |
1160 | #if wxUSE_STL |
1161 | wxString(const_iterator first, const_iterator last) | |
1162 | : wxStringBase(first, last) { } | |
1163 | #endif | |
5460b9e3 | 1164 | |
3c67202d | 1165 | // lib.string.modifiers |
3c67202d VZ |
1166 | // append elements str[pos], ..., str[pos+n] |
1167 | wxString& append(const wxString& str, size_t pos, size_t n) | |
e87b7833 MB |
1168 | { return (wxString&)wxStringBase::append(str, pos, n); } |
1169 | // append a string | |
1170 | wxString& append(const wxString& str) | |
1171 | { return (wxString&)wxStringBase::append(str); } | |
3c67202d | 1172 | // append first n (or all if n == npos) characters of sz |
e87b7833 MB |
1173 | wxString& append(const wxChar *sz) |
1174 | { return (wxString&)wxStringBase::append(sz); } | |
1175 | wxString& append(const wxChar *sz, size_t n) | |
1176 | { return (wxString&)wxStringBase::append(sz, n); } | |
3c67202d | 1177 | // append n copies of ch |
e87b7833 MB |
1178 | wxString& append(size_t n, wxChar ch) |
1179 | { return (wxString&)wxStringBase::append(n, ch); } | |
1180 | // append from first to last | |
1181 | wxString& append(const_iterator first, const_iterator last) | |
1182 | { return (wxString&)wxStringBase::append(first, last); } | |
3c67202d VZ |
1183 | |
1184 | // same as `this_string = str' | |
735d1db6 | 1185 | wxString& assign(const wxString& str) |
e87b7833 | 1186 | { return (wxString&)wxStringBase::assign(str); } |
3c67202d VZ |
1187 | // same as ` = str[pos..pos + n] |
1188 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
e87b7833 | 1189 | { return (wxString&)wxStringBase::assign(str, pos, n); } |
3c67202d | 1190 | // same as `= first n (or all if n == npos) characters of sz' |
e87b7833 MB |
1191 | wxString& assign(const wxChar *sz) |
1192 | { return (wxString&)wxStringBase::assign(sz); } | |
1193 | wxString& assign(const wxChar *sz, size_t n) | |
1194 | { return (wxString&)wxStringBase::assign(sz, n); } | |
3c67202d | 1195 | // same as `= n copies of ch' |
2bb67b80 | 1196 | wxString& assign(size_t n, wxChar ch) |
e87b7833 MB |
1197 | { return (wxString&)wxStringBase::assign(n, ch); } |
1198 | // assign from first to last | |
1199 | wxString& assign(const_iterator first, const_iterator last) | |
1200 | { return (wxString&)wxStringBase::assign(first, last); } | |
1201 | ||
1202 | // string comparison | |
963ad140 | 1203 | #if !defined(HAVE_STD_STRING_COMPARE) |
e87b7833 MB |
1204 | int compare(const wxStringBase& str) const; |
1205 | // comparison with a substring | |
1206 | int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; | |
1207 | // comparison of 2 substrings | |
1208 | int compare(size_t nStart, size_t nLen, | |
1209 | const wxStringBase& str, size_t nStart2, size_t nLen2) const; | |
1210 | // just like strcmp() | |
1211 | int compare(const wxChar* sz) const; | |
1212 | // substring comparison with first nCount characters of sz | |
1213 | int compare(size_t nStart, size_t nLen, | |
1214 | const wxChar* sz, size_t nCount = npos) const; | |
1215 | #endif // !defined HAVE_STD_STRING_COMPARE | |
3c67202d VZ |
1216 | |
1217 | // insert another string | |
e87b7833 MB |
1218 | wxString& insert(size_t nPos, const wxString& str) |
1219 | { return (wxString&)wxStringBase::insert(nPos, str); } | |
3c67202d VZ |
1220 | // insert n chars of str starting at nStart (in str) |
1221 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
e87b7833 | 1222 | { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); } |
3c67202d | 1223 | // insert first n (or all if n == npos) characters of sz |
e87b7833 MB |
1224 | wxString& insert(size_t nPos, const wxChar *sz) |
1225 | { return (wxString&)wxStringBase::insert(nPos, sz); } | |
1226 | wxString& insert(size_t nPos, const wxChar *sz, size_t n) | |
1227 | { return (wxString&)wxStringBase::insert(nPos, sz, n); } | |
3c67202d | 1228 | // insert n copies of ch |
2bb67b80 | 1229 | wxString& insert(size_t nPos, size_t n, wxChar ch) |
e87b7833 MB |
1230 | { return (wxString&)wxStringBase::insert(nPos, n, ch); } |
1231 | iterator insert(iterator it, wxChar ch) | |
1c2d1459 | 1232 | { return wxStringBase::insert(it, ch); } |
e87b7833 MB |
1233 | void insert(iterator it, const_iterator first, const_iterator last) |
1234 | { wxStringBase::insert(it, first, last); } | |
1235 | void insert(iterator it, size_type n, wxChar ch) | |
1236 | { wxStringBase::insert(it, n, ch); } | |
3c67202d VZ |
1237 | |
1238 | // delete characters from nStart to nStart + nLen | |
e87b7833 MB |
1239 | wxString& erase(size_type pos = 0, size_type n = npos) |
1240 | { return (wxString&)wxStringBase::erase(pos, n); } | |
1241 | iterator erase(iterator first, iterator last) | |
1242 | { return wxStringBase::erase(first, last); } | |
1243 | iterator erase(iterator first) | |
1244 | { return wxStringBase::erase(first); } | |
1245 | ||
1246 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
1247 | void clear() { erase(); } | |
1248 | #endif | |
3c67202d VZ |
1249 | |
1250 | // replaces the substring of length nLen starting at nStart | |
e87b7833 MB |
1251 | wxString& replace(size_t nStart, size_t nLen, const wxChar* sz) |
1252 | { return (wxString&)wxStringBase::replace(nStart, nLen, sz); } | |
1253 | // replaces the substring of length nLen starting at nStart | |
1254 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
1255 | { return (wxString&)wxStringBase::replace(nStart, nLen, str); } | |
3c67202d | 1256 | // replaces the substring with nCount copies of ch |
e87b7833 MB |
1257 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch) |
1258 | { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); } | |
3c67202d VZ |
1259 | // replaces a substring with another substring |
1260 | wxString& replace(size_t nStart, size_t nLen, | |
e87b7833 MB |
1261 | const wxString& str, size_t nStart2, size_t nLen2) |
1262 | { return (wxString&)wxStringBase::replace(nStart, nLen, str, | |
1263 | nStart2, nLen2); } | |
1264 | // replaces the substring with first nCount chars of sz | |
fb20fa43 | 1265 | wxString& replace(size_t nStart, size_t nLen, |
e87b7833 MB |
1266 | const wxChar* sz, size_t nCount) |
1267 | { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); } | |
1268 | wxString& replace(iterator first, iterator last, const_pointer s) | |
1269 | { return (wxString&)wxStringBase::replace(first, last, s); } | |
1270 | wxString& replace(iterator first, iterator last, const_pointer s, | |
1271 | size_type n) | |
1272 | { return (wxString&)wxStringBase::replace(first, last, s, n); } | |
1273 | wxString& replace(iterator first, iterator last, const wxString& s) | |
1274 | { return (wxString&)wxStringBase::replace(first, last, s); } | |
1275 | wxString& replace(iterator first, iterator last, size_type n, wxChar c) | |
1276 | { return (wxString&)wxStringBase::replace(first, last, n, c); } | |
1277 | wxString& replace(iterator first, iterator last, | |
1278 | const_iterator first1, const_iterator last1) | |
1279 | { return (wxString&)wxStringBase::replace(first, last, first1, last1); } | |
3c67202d | 1280 | |
e87b7833 MB |
1281 | // string += string |
1282 | wxString& operator+=(const wxString& s) | |
1283 | { return (wxString&)wxStringBase::operator+=(s); } | |
1284 | // string += C string | |
1285 | wxString& operator+=(const wxChar *psz) | |
1286 | { return (wxString&)wxStringBase::operator+=(psz); } | |
1287 | // string += char | |
1288 | wxString& operator+=(wxChar ch) | |
1289 | { return (wxString&)wxStringBase::operator+=(ch); } | |
c801d85f KB |
1290 | }; |
1291 | ||
b7146cbe VZ |
1292 | // IBM xlC compiler needs these operators to be declared in global scope, |
1293 | // although this shouldn't be a problem for the other compilers we prefer to | |
1294 | // only do it for it in stable 2.6 branch | |
1295 | #ifdef __IBMCPP__ | |
1296 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); | |
1297 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); | |
1298 | wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); | |
1299 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); | |
1300 | wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); | |
1301 | #endif // __IBMCPP__ | |
1302 | ||
df5168c4 MB |
1303 | // define wxArrayString, for compatibility |
1304 | #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL | |
1305 | #include "wx/arrstr.h" | |
ba8c1601 | 1306 | #endif |
9d155f50 | 1307 | |
07170120 VZ |
1308 | #if wxUSE_STL |
1309 | // return an empty wxString (not very useful with wxUSE_STL == 1) | |
12f99210 | 1310 | inline const wxString wxGetEmptyString() { return wxString(); } |
07170120 VZ |
1311 | #else // !wxUSE_STL |
1312 | // return an empty wxString (more efficient than wxString() here) | |
1313 | inline const wxString& wxGetEmptyString() | |
1314 | { | |
1315 | return *(wxString *)&wxEmptyString; | |
1316 | } | |
1317 | #endif // wxUSE_STL/!wxUSE_STL | |
1318 | ||
1d218550 VZ |
1319 | // ---------------------------------------------------------------------------- |
1320 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
1321 | // ---------------------------------------------------------------------------- | |
1322 | ||
941b78cc MB |
1323 | #if wxUSE_STL |
1324 | ||
1325 | class WXDLLIMPEXP_BASE wxStringBuffer | |
1326 | { | |
1327 | public: | |
1328 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
e87b7833 | 1329 | : m_str(str), m_buf(lenWanted) |
941b78cc MB |
1330 | { } |
1331 | ||
fe5fc682 | 1332 | ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); } |
941b78cc MB |
1333 | |
1334 | operator wxChar*() { return m_buf.data(); } | |
1335 | ||
1336 | private: | |
1337 | wxString& m_str; | |
1338 | #if wxUSE_UNICODE | |
1339 | wxWCharBuffer m_buf; | |
1340 | #else | |
1341 | wxCharBuffer m_buf; | |
1342 | #endif | |
941b78cc MB |
1343 | |
1344 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1345 | }; | |
1346 | ||
1347 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
1348 | { | |
1349 | public: | |
1350 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
1351 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
1352 | { } | |
1353 | ||
1354 | ~wxStringBufferLength() | |
1355 | { | |
1356 | wxASSERT(m_lenSet); | |
1357 | m_str.assign(m_buf.data(), m_len); | |
1358 | } | |
1359 | ||
1360 | operator wxChar*() { return m_buf.data(); } | |
1361 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
1362 | ||
1363 | private: | |
1364 | wxString& m_str; | |
1365 | #if wxUSE_UNICODE | |
1366 | wxWCharBuffer m_buf; | |
1367 | #else | |
1368 | wxCharBuffer m_buf; | |
1369 | #endif | |
1370 | size_t m_len; | |
1371 | bool m_lenSet; | |
1372 | ||
1373 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
1374 | }; | |
1375 | ||
1376 | #else // if !wxUSE_STL | |
1377 | ||
bddd7a8d | 1378 | class WXDLLIMPEXP_BASE wxStringBuffer |
1d218550 VZ |
1379 | { |
1380 | public: | |
1381 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
b1801e0e GD |
1382 | : m_str(str), m_buf(NULL) |
1383 | { m_buf = m_str.GetWriteBuf(lenWanted); } | |
e015c2a3 | 1384 | |
1d218550 | 1385 | ~wxStringBuffer() { m_str.UngetWriteBuf(); } |
e015c2a3 | 1386 | |
1d218550 | 1387 | operator wxChar*() const { return m_buf; } |
e015c2a3 | 1388 | |
1d218550 VZ |
1389 | private: |
1390 | wxString& m_str; | |
1391 | wxChar *m_buf; | |
e015c2a3 VZ |
1392 | |
1393 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1d218550 VZ |
1394 | }; |
1395 | ||
941b78cc MB |
1396 | class WXDLLIMPEXP_BASE wxStringBufferLength |
1397 | { | |
1398 | public: | |
1399 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
1400 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
4055ed82 WS |
1401 | { |
1402 | m_buf = m_str.GetWriteBuf(lenWanted); | |
494b978f RN |
1403 | wxASSERT(m_buf != NULL); |
1404 | } | |
941b78cc MB |
1405 | |
1406 | ~wxStringBufferLength() | |
1407 | { | |
1408 | wxASSERT(m_lenSet); | |
1409 | m_str.UngetWriteBuf(m_len); | |
1410 | } | |
1411 | ||
1412 | operator wxChar*() const { return m_buf; } | |
1413 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
1414 | ||
1415 | private: | |
1416 | wxString& m_str; | |
1417 | wxChar *m_buf; | |
1418 | size_t m_len; | |
1419 | bool m_lenSet; | |
1420 | ||
1421 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
1422 | }; | |
1423 | ||
1424 | #endif // !wxUSE_STL | |
1425 | ||
c801d85f | 1426 | // --------------------------------------------------------------------------- |
3c67202d | 1427 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 1428 | // --------------------------------------------------------------------------- |
f33fee2a | 1429 | |
b6339dde VZ |
1430 | // note that when wxUSE_STL == 1 the comparison operators taking std::string |
1431 | // are used and defining them also for wxString would only result in | |
1432 | // compilation ambiguities when comparing std::string and wxString | |
1433 | #if !wxUSE_STL | |
e87b7833 | 1434 | |
0cbe5ee3 VZ |
1435 | inline bool operator==(const wxString& s1, const wxString& s2) |
1436 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
1437 | inline bool operator==(const wxString& s1, const wxChar * s2) | |
1438 | { return s1.Cmp(s2) == 0; } | |
1439 | inline bool operator==(const wxChar * s1, const wxString& s2) | |
1440 | { return s2.Cmp(s1) == 0; } | |
1441 | inline bool operator!=(const wxString& s1, const wxString& s2) | |
1442 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
1443 | inline bool operator!=(const wxString& s1, const wxChar * s2) | |
1444 | { return s1.Cmp(s2) != 0; } | |
1445 | inline bool operator!=(const wxChar * s1, const wxString& s2) | |
1446 | { return s2.Cmp(s1) != 0; } | |
1447 | inline bool operator< (const wxString& s1, const wxString& s2) | |
1448 | { return s1.Cmp(s2) < 0; } | |
1449 | inline bool operator< (const wxString& s1, const wxChar * s2) | |
1450 | { return s1.Cmp(s2) < 0; } | |
1451 | inline bool operator< (const wxChar * s1, const wxString& s2) | |
1452 | { return s2.Cmp(s1) > 0; } | |
1453 | inline bool operator> (const wxString& s1, const wxString& s2) | |
1454 | { return s1.Cmp(s2) > 0; } | |
1455 | inline bool operator> (const wxString& s1, const wxChar * s2) | |
1456 | { return s1.Cmp(s2) > 0; } | |
1457 | inline bool operator> (const wxChar * s1, const wxString& s2) | |
1458 | { return s2.Cmp(s1) < 0; } | |
1459 | inline bool operator<=(const wxString& s1, const wxString& s2) | |
1460 | { return s1.Cmp(s2) <= 0; } | |
1461 | inline bool operator<=(const wxString& s1, const wxChar * s2) | |
1462 | { return s1.Cmp(s2) <= 0; } | |
1463 | inline bool operator<=(const wxChar * s1, const wxString& s2) | |
1464 | { return s2.Cmp(s1) >= 0; } | |
1465 | inline bool operator>=(const wxString& s1, const wxString& s2) | |
1466 | { return s1.Cmp(s2) >= 0; } | |
1467 | inline bool operator>=(const wxString& s1, const wxChar * s2) | |
1468 | { return s1.Cmp(s2) >= 0; } | |
1469 | inline bool operator>=(const wxChar * s1, const wxString& s2) | |
1470 | { return s2.Cmp(s1) <= 0; } | |
3c67202d | 1471 | |
5ca07d0f OK |
1472 | #if wxUSE_UNICODE |
1473 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 1474 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 1475 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 1476 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
1477 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
1478 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
1479 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
1480 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
0cbe5ee3 | 1481 | #else // !wxUSE_UNICODE |
5ca07d0f | 1482 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 1483 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 1484 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 1485 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
1486 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
1487 | { return (s1.Cmp((const char *)s2) != 0); } | |
1488 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
1489 | { return (s2.Cmp((const char *)s1) != 0); } | |
0cbe5ee3 | 1490 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
5ca07d0f | 1491 | |
028a2b5d | 1492 | #if wxUSE_UNICODE |
6d56eb5c | 1493 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 1494 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 1495 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 1496 | { return (const wchar_t *)buf + string; } |
0cbe5ee3 | 1497 | #else // !wxUSE_UNICODE |
6d56eb5c | 1498 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 1499 | { return string + (const char *)buf; } |
6d56eb5c | 1500 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 1501 | { return (const char *)buf + string; } |
0cbe5ee3 | 1502 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
f04f3991 | 1503 | |
e51b17a1 VZ |
1504 | #endif // !wxUSE_STL |
1505 | ||
1506 | // comparison with char (those are not defined by std::[w]string and so should | |
1507 | // be always available) | |
1508 | inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); } | |
1509 | inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); } | |
1510 | inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); } | |
1511 | inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } | |
1512 | ||
c801d85f | 1513 | // --------------------------------------------------------------------------- |
3c67202d | 1514 | // Implementation only from here until the end of file |
c801d85f KB |
1515 | // --------------------------------------------------------------------------- |
1516 | ||
e90c1d2a | 1517 | // don't pollute the library user's name space |
5737d05f | 1518 | #undef wxASSERT_VALID_INDEX |
e90c1d2a | 1519 | |
e87b7833 | 1520 | #if wxUSE_STD_IOSTREAM |
c801d85f | 1521 | |
65f19af1 | 1522 | #include "wx/iosfwrap.h" |
c801d85f | 1523 | |
bddd7a8d VZ |
1524 | WXDLLIMPEXP_BASE wxSTD istream& operator>>(wxSTD istream&, wxString&); |
1525 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); | |
c801d85f | 1526 | |
3c67202d | 1527 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 1528 | |
34138703 | 1529 | #endif // _WX_WXSTRINGH__ |