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