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