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