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