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