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