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