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