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