]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/string.h | |
3 | // Purpose: wxString and wxArrayString classes | |
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> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | /* | |
13 | Efficient string class [more or less] compatible with MFC CString, | |
14 | wxWidgets version 1 wxString and std::string and some handy functions | |
15 | missing from string.h. | |
16 | */ | |
17 | ||
18 | #ifndef _WX_WXSTRINGH__ | |
19 | #define _WX_WXSTRINGH__ | |
20 | ||
21 | // ---------------------------------------------------------------------------- | |
22 | // headers | |
23 | // ---------------------------------------------------------------------------- | |
24 | ||
25 | #include "wx/defs.h" // everybody should include this | |
26 | ||
27 | #if defined(__WXMAC__) || defined(__VISAGECPP__) | |
28 | #include <ctype.h> | |
29 | #endif | |
30 | ||
31 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 | |
32 | // problem in VACPP V4 with including stdlib.h multiple times | |
33 | // strconv includes it anyway | |
34 | # include <stdio.h> | |
35 | # include <string.h> | |
36 | # include <stdarg.h> | |
37 | # include <limits.h> | |
38 | #else | |
39 | # include <string.h> | |
40 | # include <stdio.h> | |
41 | # include <stdarg.h> | |
42 | # include <limits.h> | |
43 | # include <stdlib.h> | |
44 | #endif | |
45 | ||
46 | #ifdef HAVE_STRCASECMP_IN_STRINGS_H | |
47 | #include <strings.h> // for strcasecmp() | |
48 | #endif // HAVE_STRCASECMP_IN_STRINGS_H | |
49 | ||
50 | #ifdef __WXPALMOS__ | |
51 | #include <StringMgr.h> | |
52 | #endif | |
53 | ||
54 | #include "wx/wxchar.h" // for wxChar, wxStrlen() etc. | |
55 | #include "wx/strvararg.h" | |
56 | #include "wx/buffer.h" // for wxCharBuffer | |
57 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
58 | ||
59 | class WXDLLIMPEXP_BASE wxString; | |
60 | ||
61 | // --------------------------------------------------------------------------- | |
62 | // macros | |
63 | // --------------------------------------------------------------------------- | |
64 | ||
65 | // casts [unfortunately!] needed to call some broken functions which require | |
66 | // "char *" instead of "const char *" | |
67 | #define WXSTRINGCAST (wxChar *)(const wxChar *) | |
68 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) | |
69 | #define wxMBSTRINGCAST (char *)(const char *) | |
70 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
71 | ||
72 | // implementation only | |
73 | #define wxASSERT_VALID_INDEX(i) \ | |
74 | wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") ) | |
75 | ||
76 | // ---------------------------------------------------------------------------- | |
77 | // constants | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | #if WXWIN_COMPATIBILITY_2_6 | |
81 | ||
82 | // deprecated in favour of wxString::npos, don't use in new code | |
83 | // | |
84 | // maximum possible length for a string means "take all string" everywhere | |
85 | #define wxSTRING_MAXLEN wxString::npos | |
86 | ||
87 | #endif // WXWIN_COMPATIBILITY_2_6 | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // global data | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | // global pointer to empty string | |
94 | extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString; | |
95 | ||
96 | // --------------------------------------------------------------------------- | |
97 | // global functions complementing standard C string library replacements for | |
98 | // strlen() and portable strcasecmp() | |
99 | //--------------------------------------------------------------------------- | |
100 | ||
101 | #if WXWIN_COMPATIBILITY_2_8 | |
102 | // Use wxXXX() functions from wxcrt.h instead! These functions are for | |
103 | // backwards compatibility only. | |
104 | ||
105 | // checks whether the passed in pointer is NULL and if the string is empty | |
106 | wxDEPRECATED( inline bool IsEmpty(const char *p) ); | |
107 | inline bool IsEmpty(const char *p) { return (!p || !*p); } | |
108 | ||
109 | // safe version of strlen() (returns 0 if passed NULL pointer) | |
110 | wxDEPRECATED( inline size_t Strlen(const char *psz) ); | |
111 | inline size_t Strlen(const char *psz) | |
112 | { return psz ? strlen(psz) : 0; } | |
113 | ||
114 | // portable strcasecmp/_stricmp | |
115 | wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) ); | |
116 | inline int Stricmp(const char *psz1, const char *psz2) | |
117 | { | |
118 | #if defined(__VISUALC__) && defined(__WXWINCE__) | |
119 | register char c1, c2; | |
120 | do { | |
121 | c1 = tolower(*psz1++); | |
122 | c2 = tolower(*psz2++); | |
123 | } while ( c1 && (c1 == c2) ); | |
124 | ||
125 | return c1 - c2; | |
126 | #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) | |
127 | return _stricmp(psz1, psz2); | |
128 | #elif defined(__SC__) | |
129 | return _stricmp(psz1, psz2); | |
130 | #elif defined(__SALFORDC__) | |
131 | return stricmp(psz1, psz2); | |
132 | #elif defined(__BORLANDC__) | |
133 | return stricmp(psz1, psz2); | |
134 | #elif defined(__WATCOMC__) | |
135 | return stricmp(psz1, psz2); | |
136 | #elif defined(__DJGPP__) | |
137 | return stricmp(psz1, psz2); | |
138 | #elif defined(__EMX__) | |
139 | return stricmp(psz1, psz2); | |
140 | #elif defined(__WXPM__) | |
141 | return stricmp(psz1, psz2); | |
142 | #elif defined(__WXPALMOS__) || \ | |
143 | defined(HAVE_STRCASECMP_IN_STRING_H) || \ | |
144 | defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ | |
145 | defined(__GNUWIN32__) | |
146 | return strcasecmp(psz1, psz2); | |
147 | #elif defined(__MWERKS__) && !defined(__INTEL__) | |
148 | register char c1, c2; | |
149 | do { | |
150 | c1 = tolower(*psz1++); | |
151 | c2 = tolower(*psz2++); | |
152 | } while ( c1 && (c1 == c2) ); | |
153 | ||
154 | return c1 - c2; | |
155 | #else | |
156 | // almost all compilers/libraries provide this function (unfortunately under | |
157 | // different names), that's why we don't implement our own which will surely | |
158 | // be more efficient than this code (uncomment to use): | |
159 | /* | |
160 | register char c1, c2; | |
161 | do { | |
162 | c1 = tolower(*psz1++); | |
163 | c2 = tolower(*psz2++); | |
164 | } while ( c1 && (c1 == c2) ); | |
165 | ||
166 | return c1 - c2; | |
167 | */ | |
168 | ||
169 | #error "Please define string case-insensitive compare for your OS/compiler" | |
170 | #endif // OS/compiler | |
171 | } | |
172 | ||
173 | #endif // WXWIN_COMPATIBILITY_2_8 | |
174 | ||
175 | // ---------------------------------------------------------------------------- | |
176 | // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
179 | // FIXME-UTF8: using std::string as wxString base class is currently broken, | |
180 | // so we use the standard wxString with std::string conversion | |
181 | // enabled, this is API-compatible. | |
182 | #if 1 | |
183 | #define wxUSE_STL_BASED_WXSTRING 0 | |
184 | #if wxUSE_STL | |
185 | #undef wxUSE_STD_STRING | |
186 | #define wxUSE_STD_STRING 1 | |
187 | #endif | |
188 | #else | |
189 | #define wxUSE_STL_BASED_WXSTRING wxUSE_STL | |
190 | #endif | |
191 | ||
192 | // in both cases we need to define wxStdString | |
193 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING | |
194 | ||
195 | #include "wx/beforestd.h" | |
196 | #include <string> | |
197 | #include "wx/afterstd.h" | |
198 | ||
199 | #if wxUSE_UNICODE_WCHAR | |
200 | #ifdef HAVE_STD_WSTRING | |
201 | typedef std::wstring wxStdString; | |
202 | #else | |
203 | typedef std::basic_string<wxChar> wxStdString; | |
204 | #endif | |
205 | #else | |
206 | typedef std::string wxStdString; | |
207 | #endif | |
208 | ||
209 | #endif // need <string> | |
210 | ||
211 | #if wxUSE_STL_BASED_WXSTRING | |
212 | ||
213 | // we always want ctor from std::string when using std::string internally | |
214 | #undef wxUSE_STD_STRING | |
215 | #define wxUSE_STD_STRING 1 | |
216 | ||
217 | #if (defined(__GNUG__) && (__GNUG__ < 3)) || \ | |
218 | (defined(_MSC_VER) && (_MSC_VER <= 1200)) | |
219 | #define wxSTRING_BASE_HASNT_CLEAR | |
220 | #endif | |
221 | ||
222 | typedef wxStdString wxStringImpl; | |
223 | #else // if !wxUSE_STL_BASED_WXSTRING | |
224 | ||
225 | // in non-STL mode, compare() is implemented in wxString and not wxStringImpl | |
226 | #undef HAVE_STD_STRING_COMPARE | |
227 | ||
228 | // --------------------------------------------------------------------------- | |
229 | // string data prepended with some housekeeping info (used by wxString class), | |
230 | // is never used directly (but had to be put here to allow inlining) | |
231 | // --------------------------------------------------------------------------- | |
232 | ||
233 | struct WXDLLIMPEXP_BASE wxStringData | |
234 | { | |
235 | int nRefs; // reference count | |
236 | size_t nDataLength, // actual string length | |
237 | nAllocLength; // allocated memory size | |
238 | ||
239 | // mimics declaration 'wxChar data[nAllocLength]' | |
240 | wxChar* data() const { return (wxChar*)(this + 1); } | |
241 | ||
242 | // empty string has a special ref count so it's never deleted | |
243 | bool IsEmpty() const { return (nRefs == -1); } | |
244 | bool IsShared() const { return (nRefs > 1); } | |
245 | ||
246 | // lock/unlock | |
247 | void Lock() { if ( !IsEmpty() ) nRefs++; } | |
248 | ||
249 | // VC++ will refuse to inline Unlock but profiling shows that it is wrong | |
250 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) | |
251 | __forceinline | |
252 | #endif | |
253 | // VC++ free must take place in same DLL as allocation when using non dll | |
254 | // run-time library (e.g. Multithreaded instead of Multithreaded DLL) | |
255 | #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL) | |
256 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); } | |
257 | // we must not inline deallocation since allocation is not inlined | |
258 | void Free(); | |
259 | #else | |
260 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); } | |
261 | #endif | |
262 | ||
263 | // if we had taken control over string memory (GetWriteBuf), it's | |
264 | // intentionally put in invalid state | |
265 | void Validate(bool b) { nRefs = (b ? 1 : 0); } | |
266 | bool IsValid() const { return (nRefs != 0); } | |
267 | }; | |
268 | ||
269 | class WXDLLIMPEXP_BASE wxStringImpl | |
270 | { | |
271 | public: | |
272 | // an 'invalid' value for string index, moved to this place due to a CW bug | |
273 | static const size_t npos; | |
274 | ||
275 | protected: | |
276 | // points to data preceded by wxStringData structure with ref count info | |
277 | wxStringCharType *m_pchData; | |
278 | ||
279 | // accessor to string data | |
280 | wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; } | |
281 | ||
282 | // string (re)initialization functions | |
283 | // initializes the string to the empty value (must be called only from | |
284 | // ctors, use Reinit() otherwise) | |
285 | void Init() { m_pchData = (wxStringCharType *)wxEmptyString; } | |
286 | // initializes the string with (a part of) C-string | |
287 | void InitWith(const wxStringCharType *psz, size_t nPos = 0, size_t nLen = npos); | |
288 | // as Init, but also frees old data | |
289 | void Reinit() { GetStringData()->Unlock(); Init(); } | |
290 | ||
291 | // memory allocation | |
292 | // allocates memory for string of length nLen | |
293 | bool AllocBuffer(size_t nLen); | |
294 | // effectively copies data to string | |
295 | bool AssignCopy(size_t, const wxStringCharType *); | |
296 | ||
297 | // append a (sub)string | |
298 | bool ConcatSelf(size_t nLen, const wxStringCharType *src, size_t nMaxLen); | |
299 | bool ConcatSelf(size_t nLen, const wxStringCharType *src) | |
300 | { return ConcatSelf(nLen, src, nLen); } | |
301 | ||
302 | // functions called before writing to the string: they copy it if there | |
303 | // are other references to our data (should be the only owner when writing) | |
304 | bool CopyBeforeWrite(); | |
305 | bool AllocBeforeWrite(size_t); | |
306 | ||
307 | // compatibility with wxString | |
308 | bool Alloc(size_t nLen); | |
309 | ||
310 | public: | |
311 | // standard types | |
312 | typedef wxStringCharType value_type; | |
313 | typedef wxStringCharType char_type; | |
314 | typedef size_t size_type; | |
315 | typedef value_type& reference; | |
316 | typedef const value_type& const_reference; | |
317 | typedef value_type* pointer; | |
318 | typedef const value_type* const_pointer; | |
319 | typedef value_type *iterator; | |
320 | typedef const value_type *const_iterator; | |
321 | ||
322 | // constructors and destructor | |
323 | // ctor for an empty string | |
324 | wxStringImpl() { Init(); } | |
325 | // copy ctor | |
326 | wxStringImpl(const wxStringImpl& stringSrc) | |
327 | { | |
328 | wxASSERT_MSG( stringSrc.GetStringData()->IsValid(), | |
329 | _T("did you forget to call UngetWriteBuf()?") ); | |
330 | ||
331 | if ( stringSrc.empty() ) { | |
332 | // nothing to do for an empty string | |
333 | Init(); | |
334 | } | |
335 | else { | |
336 | m_pchData = stringSrc.m_pchData; // share same data | |
337 | GetStringData()->Lock(); // => one more copy | |
338 | } | |
339 | } | |
340 | // string containing nRepeat copies of ch | |
341 | wxStringImpl(size_type nRepeat, wxStringCharType ch); | |
342 | // ctor takes first nLength characters from C string | |
343 | // (default value of npos means take all the string) | |
344 | wxStringImpl(const wxStringCharType *psz) | |
345 | { InitWith(psz, 0, npos); } | |
346 | wxStringImpl(const wxStringCharType *psz, size_t nLength) | |
347 | { InitWith(psz, 0, nLength); } | |
348 | // take nLen chars starting at nPos | |
349 | wxStringImpl(const wxStringImpl& str, size_t nPos, size_t nLen) | |
350 | { | |
351 | wxASSERT_MSG( str.GetStringData()->IsValid(), | |
352 | _T("did you forget to call UngetWriteBuf()?") ); | |
353 | Init(); | |
354 | size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen; | |
355 | InitWith(str.c_str(), nPos, nLen); | |
356 | } | |
357 | // take all characters from pStart to pEnd | |
358 | wxStringImpl(const void *pStart, const void *pEnd); | |
359 | ||
360 | // dtor is not virtual, this class must not be inherited from! | |
361 | ~wxStringImpl() | |
362 | { | |
363 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) | |
364 | //RN - according to the above VC++ does indeed inline this, | |
365 | //even though it spits out two warnings | |
366 | #pragma warning (disable:4714) | |
367 | #endif | |
368 | ||
369 | GetStringData()->Unlock(); | |
370 | } | |
371 | ||
372 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) | |
373 | //re-enable inlining warning | |
374 | #pragma warning (default:4714) | |
375 | #endif | |
376 | // overloaded assignment | |
377 | // from another wxString | |
378 | wxStringImpl& operator=(const wxStringImpl& stringSrc); | |
379 | // from a character | |
380 | wxStringImpl& operator=(wxStringCharType ch); | |
381 | // from a C string | |
382 | wxStringImpl& operator=(const wxStringCharType *psz); | |
383 | ||
384 | // return the length of the string | |
385 | size_type length() const { return GetStringData()->nDataLength; } | |
386 | // return the length of the string | |
387 | size_type size() const { return length(); } | |
388 | // return the maximum size of the string | |
389 | size_type max_size() const { return npos; } | |
390 | // resize the string, filling the space with c if c != 0 | |
391 | void resize(size_t nSize, wxStringCharType ch = '\0'); | |
392 | // delete the contents of the string | |
393 | void clear() { erase(0, npos); } | |
394 | // returns true if the string is empty | |
395 | bool empty() const { return length() == 0; } | |
396 | // inform string about planned change in size | |
397 | void reserve(size_t sz) { Alloc(sz); } | |
398 | size_type capacity() const { return GetStringData()->nAllocLength; } | |
399 | ||
400 | // lib.string.access | |
401 | // return the character at position n | |
402 | value_type at(size_type n) const | |
403 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } | |
404 | // returns the writable character at position n | |
405 | reference at(size_type n) | |
406 | { | |
407 | wxASSERT_VALID_INDEX( n ); | |
408 | CopyBeforeWrite(); | |
409 | return m_pchData[n]; | |
410 | } // FIXME-UTF8: not useful for us...? | |
411 | ||
412 | // lib.string.modifiers | |
413 | // append elements str[pos], ..., str[pos+n] | |
414 | wxStringImpl& append(const wxStringImpl& str, size_t pos, size_t n) | |
415 | { | |
416 | wxASSERT(pos <= str.length()); | |
417 | ConcatSelf(n, str.c_str() + pos, str.length() - pos); | |
418 | return *this; | |
419 | } | |
420 | // append a string | |
421 | wxStringImpl& append(const wxStringImpl& str) | |
422 | { ConcatSelf(str.length(), str.c_str()); return *this; } | |
423 | // append first n (or all if n == npos) characters of sz | |
424 | wxStringImpl& append(const wxStringCharType *sz) | |
425 | { ConcatSelf(wxStrlen(sz), sz); return *this; } | |
426 | wxStringImpl& append(const wxStringCharType *sz, size_t n) | |
427 | { ConcatSelf(n, sz); return *this; } | |
428 | // append n copies of ch | |
429 | wxStringImpl& append(size_t n, wxStringCharType ch); | |
430 | // append from first to last | |
431 | wxStringImpl& append(const_iterator first, const_iterator last) | |
432 | { ConcatSelf(last - first, first); return *this; } | |
433 | ||
434 | // same as `this_string = str' | |
435 | wxStringImpl& assign(const wxStringImpl& str) | |
436 | { return *this = str; } | |
437 | // same as ` = str[pos..pos + n] | |
438 | wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n) | |
439 | { clear(); return append(str, pos, n); } | |
440 | // same as `= first n (or all if n == npos) characters of sz' | |
441 | wxStringImpl& assign(const wxStringCharType *sz) | |
442 | { clear(); return append(sz, wxStrlen(sz)); } | |
443 | wxStringImpl& assign(const wxStringCharType *sz, size_t n) | |
444 | { clear(); return append(sz, n); } | |
445 | // same as `= n copies of ch' | |
446 | wxStringImpl& assign(size_t n, wxStringCharType ch) | |
447 | { clear(); return append(n, ch); } | |
448 | // assign from first to last | |
449 | wxStringImpl& assign(const_iterator first, const_iterator last) | |
450 | { clear(); return append(first, last); } | |
451 | ||
452 | // first valid index position | |
453 | const_iterator begin() const { return m_pchData; } | |
454 | iterator begin(); | |
455 | // position one after the last valid one | |
456 | const_iterator end() const { return m_pchData + length(); } | |
457 | iterator end(); | |
458 | ||
459 | // insert another string | |
460 | wxStringImpl& insert(size_t nPos, const wxStringImpl& str) | |
461 | { | |
462 | wxASSERT( str.GetStringData()->IsValid() ); | |
463 | return insert(nPos, str.c_str(), str.length()); | |
464 | } | |
465 | // insert n chars of str starting at nStart (in str) | |
466 | wxStringImpl& insert(size_t nPos, const wxStringImpl& str, size_t nStart, size_t n) | |
467 | { | |
468 | wxASSERT( str.GetStringData()->IsValid() ); | |
469 | wxASSERT( nStart < str.length() ); | |
470 | size_t strLen = str.length() - nStart; | |
471 | n = strLen < n ? strLen : n; | |
472 | return insert(nPos, str.c_str() + nStart, n); | |
473 | } | |
474 | // insert first n (or all if n == npos) characters of sz | |
475 | wxStringImpl& insert(size_t nPos, const wxStringCharType *sz, size_t n = npos); | |
476 | // insert n copies of ch | |
477 | wxStringImpl& insert(size_t nPos, size_t n, wxStringCharType ch)// FIXME-UTF8: tricky | |
478 | { return insert(nPos, wxStringImpl(n, ch)); } | |
479 | iterator insert(iterator it, wxStringCharType ch) // FIXME-UTF8: tricky | |
480 | { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; } | |
481 | void insert(iterator it, const_iterator first, const_iterator last) | |
482 | { insert(it - begin(), first, last - first); } | |
483 | void insert(iterator it, size_type n, wxStringCharType ch) | |
484 | { insert(it - begin(), n, ch); } | |
485 | ||
486 | // delete characters from nStart to nStart + nLen | |
487 | wxStringImpl& erase(size_type pos = 0, size_type n = npos); | |
488 | iterator erase(iterator first, iterator last) | |
489 | { | |
490 | size_t idx = first - begin(); | |
491 | erase(idx, last - first); | |
492 | return begin() + idx; | |
493 | } | |
494 | iterator erase(iterator first); | |
495 | ||
496 | // explicit conversion to C string (use this with printf()!) | |
497 | const wxStringCharType* c_str() const { return m_pchData; } | |
498 | const wxStringCharType* data() const { return m_pchData; } | |
499 | ||
500 | // replaces the substring of length nLen starting at nStart | |
501 | wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz); | |
502 | // replaces the substring of length nLen starting at nStart | |
503 | wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str) | |
504 | { return replace(nStart, nLen, str.c_str()); } | |
505 | // replaces the substring with nCount copies of ch | |
506 | wxStringImpl& replace(size_t nStart, size_t nLen, size_t nCount, wxStringCharType ch); | |
507 | // replaces a substring with another substring | |
508 | wxStringImpl& replace(size_t nStart, size_t nLen, | |
509 | const wxStringImpl& str, size_t nStart2, size_t nLen2); | |
510 | // replaces the substring with first nCount chars of sz | |
511 | wxStringImpl& replace(size_t nStart, size_t nLen, | |
512 | const wxStringCharType* sz, size_t nCount); | |
513 | wxStringImpl& replace(iterator first, iterator last, const_pointer s) | |
514 | { return replace(first - begin(), last - first, s); } | |
515 | wxStringImpl& replace(iterator first, iterator last, const_pointer s, | |
516 | size_type n) | |
517 | { return replace(first - begin(), last - first, s, n); } | |
518 | wxStringImpl& replace(iterator first, iterator last, const wxStringImpl& s) | |
519 | { return replace(first - begin(), last - first, s); } | |
520 | wxStringImpl& replace(iterator first, iterator last, size_type n, wxStringCharType c) | |
521 | { return replace(first - begin(), last - first, n, c); } | |
522 | wxStringImpl& replace(iterator first, iterator last, | |
523 | const_iterator first1, const_iterator last1) | |
524 | { return replace(first - begin(), last - first, first1, last1 - first1); } | |
525 | ||
526 | // swap two strings | |
527 | void swap(wxStringImpl& str); | |
528 | ||
529 | // All find() functions take the nStart argument which specifies the | |
530 | // position to start the search on, the default value is 0. All functions | |
531 | // return npos if there were no match. | |
532 | ||
533 | // find a substring | |
534 | size_t find(const wxStringImpl& str, size_t nStart = 0) const; | |
535 | ||
536 | // find first n characters of sz | |
537 | size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const; | |
538 | ||
539 | // find the first occurence of character ch after nStart | |
540 | size_t find(wxStringCharType ch, size_t nStart = 0) const; | |
541 | ||
542 | // rfind() family is exactly like find() but works right to left | |
543 | ||
544 | // as find, but from the end | |
545 | size_t rfind(const wxStringImpl& str, size_t nStart = npos) const; | |
546 | ||
547 | // as find, but from the end | |
548 | size_t rfind(const wxStringCharType* sz, size_t nStart = npos, | |
549 | size_t n = npos) const; | |
550 | // as find, but from the end | |
551 | size_t rfind(wxStringCharType ch, size_t nStart = npos) const; | |
552 | ||
553 | size_type copy(wxStringCharType* s, size_type n, size_type pos = 0); | |
554 | ||
555 | // substring extraction | |
556 | wxStringImpl substr(size_t nStart = 0, size_t nLen = npos) const; | |
557 | ||
558 | // string += string | |
559 | wxStringImpl& operator+=(const wxStringImpl& s) { return append(s); } | |
560 | // string += C string | |
561 | wxStringImpl& operator+=(const wxStringCharType *psz) { return append(psz); } | |
562 | // string += char | |
563 | wxStringImpl& operator+=(wxStringCharType ch) { return append(1, ch); } | |
564 | ||
565 | #if !wxUSE_UNICODE_UTF8 | |
566 | // helpers for wxStringBuffer and wxStringBufferLength | |
567 | wxStringCharType *DoGetWriteBuf(size_t nLen); | |
568 | void DoUngetWriteBuf(); | |
569 | void DoUngetWriteBuf(size_t nLen); | |
570 | #endif | |
571 | ||
572 | friend class WXDLLIMPEXP_BASE wxString; | |
573 | }; | |
574 | ||
575 | #endif // !wxUSE_STL_BASED_WXSTRING | |
576 | ||
577 | // don't pollute the library user's name space | |
578 | #undef wxASSERT_VALID_INDEX | |
579 | ||
580 | // wx/unichar.h needs wxStringImpl, so it's only possible to include it here | |
581 | // (it includes string.h if not included from string.h): | |
582 | #include "wx/unichar.h" | |
583 | ||
584 | // ---------------------------------------------------------------------------- | |
585 | // wxCStrData | |
586 | // ---------------------------------------------------------------------------- | |
587 | ||
588 | // Lightweight object returned by wxString::c_str() and implicitly convertible | |
589 | // to either const char* or const wchar_t*. | |
590 | class wxCStrData | |
591 | { | |
592 | private: | |
593 | // Ctors; for internal use by wxString and wxCStrData only | |
594 | wxCStrData(const wxString *str, size_t offset = 0, bool owned = false) | |
595 | : m_str(str), m_offset(offset), m_owned(owned) {} | |
596 | ||
597 | public: | |
598 | // Ctor constructs the object from char literal; they are needed to make | |
599 | // operator?: compile and they intentionally take char*, not const char* | |
600 | wxCStrData(char *buf); | |
601 | wxCStrData(wchar_t *buf); | |
602 | ||
603 | ~wxCStrData(); | |
604 | ||
605 | // FIXME: we'll need convertors for both char* and wchar_t* and NONE | |
606 | // for wxChar*, but that's after completing the transition to | |
607 | // "smart" wxUniChar class. For now, just have conversion to | |
608 | // char* in ANSI build and wchar_t in Unicode build. | |
609 | #if wxUSE_UNICODE | |
610 | const wchar_t* AsWChar() const; | |
611 | operator const wchar_t*() const { return AsWChar(); } | |
612 | #else | |
613 | const char* AsChar() const; | |
614 | const unsigned char* AsUnsignedChar() const | |
615 | { return (const unsigned char *) AsChar(); } | |
616 | operator const void*() const { return AsChar(); } | |
617 | operator const char*() const { return AsChar(); } | |
618 | operator const unsigned char*() const { return AsUnsignedChar(); } | |
619 | #endif | |
620 | ||
621 | wxString AsString() const; | |
622 | operator wxString() const; | |
623 | ||
624 | // allow expressions like "c_str()[0]": | |
625 | wxUniChar operator[](int n) const { return operator[](size_t(n)); } | |
626 | wxUniChar operator[](size_t n) const; | |
627 | wxUniChar operator[](long n) const { return operator[](size_t(n)); } | |
628 | #ifndef wxSIZE_T_IS_UINT | |
629 | wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); } | |
630 | #endif // size_t != unsigned int | |
631 | ||
632 | // these operators are needed to emulate the pointer semantics of c_str(): | |
633 | // expressions like "wxChar *p = str.c_str() + 1;" should continue to work | |
634 | // (we need both versions to resolve ambiguities): | |
635 | wxCStrData operator+(int n) const | |
636 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
637 | wxCStrData operator+(long n) const | |
638 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
639 | wxCStrData operator+(size_t n) const | |
640 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
641 | ||
642 | // this operator is needed to make expressions like "*c_str()" or | |
643 | // "*(c_str() + 2)" work | |
644 | wxUniChar operator*() const; | |
645 | ||
646 | private: | |
647 | const wxString *m_str; | |
648 | size_t m_offset; | |
649 | bool m_owned; | |
650 | ||
651 | friend class WXDLLIMPEXP_BASE wxString; | |
652 | }; | |
653 | ||
654 | // ---------------------------------------------------------------------------- | |
655 | // wxStringPrintfMixin | |
656 | // --------------------------------------------------------------------------- | |
657 | ||
658 | // NB: VC6 has a bug that causes linker errors if you have template methods | |
659 | // in a class using __declspec(dllimport). The solution is to split such | |
660 | // class into two classes, one that contains the template methods and does | |
661 | // *not* use WXDLLIMPEXP_BASE and another class that contains the rest | |
662 | // (with DLL linkage). | |
663 | // | |
664 | // We only do this for VC6 here, because the code is less efficient | |
665 | // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler | |
666 | // cannot compile this code. | |
667 | ||
668 | #if defined(__VISUALC__) && __VISUALC__ < 1300 | |
669 | #define wxNEEDS_WXSTRING_PRINTF_MIXIN | |
670 | #endif | |
671 | ||
672 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
673 | // this class contains implementation of wxString's vararg methods, it's | |
674 | // exported from wxBase DLL | |
675 | class WXDLLIMPEXP_BASE wxStringPrintfMixinBase | |
676 | { | |
677 | protected: | |
678 | wxStringPrintfMixinBase() {} | |
679 | ||
680 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
681 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
682 | }; | |
683 | ||
684 | // this class contains template wrappers for wxString's vararg methods, it's | |
685 | // intentionally *not* exported from the DLL in order to fix the VC6 bug | |
686 | // described above | |
687 | class wxStringPrintfMixin : public wxStringPrintfMixinBase | |
688 | { | |
689 | private: | |
690 | // to further complicate things, we can't return wxString from | |
691 | // wxStringPrintfMixin::Format() because wxString is not yet declared at | |
692 | // this point; the solution is to use this fake type trait template - this | |
693 | // way the compiler won't know the return type until Format() is used | |
694 | // (this doesn't compile with Watcom, but VC6 compiles it just fine): | |
695 | template<typename T> struct StringReturnType | |
696 | { | |
697 | typedef wxString type; | |
698 | }; | |
699 | ||
700 | public: | |
701 | // these are duplicated wxString methods, they're also declared below | |
702 | // if !wxNEEDS_WXSTRING_PRINTF_MIXIN: | |
703 | ||
704 | // int Printf(const wxChar *pszFormat, ...); | |
705 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
706 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; | |
707 | WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type, | |
708 | Format, DoFormat) | |
709 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
710 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
711 | ||
712 | protected: | |
713 | wxStringPrintfMixin() : wxStringPrintfMixinBase() {} | |
714 | }; | |
715 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
716 | ||
717 | ||
718 | // ---------------------------------------------------------------------------- | |
719 | // wxString: string class trying to be compatible with std::string, MFC | |
720 | // CString and wxWindows 1.x wxString all at once | |
721 | // --------------------------------------------------------------------------- | |
722 | ||
723 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
724 | // "non dll-interface class 'wxStringPrintfMixin' used as base interface | |
725 | // for dll-interface class 'wxString'" -- this is OK in our case | |
726 | #pragma warning (disable:4275) | |
727 | #endif | |
728 | ||
729 | class WXDLLIMPEXP_BASE wxString | |
730 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
731 | : public wxStringPrintfMixin | |
732 | #endif | |
733 | { | |
734 | // NB: special care was taken in arranging the member functions in such order | |
735 | // that all inline functions can be effectively inlined, verify that all | |
736 | // performance critical functions are still inlined if you change order! | |
737 | public: | |
738 | #if !wxUSE_STL_BASED_WXSTRING | |
739 | // an 'invalid' value for string index, moved to this place due to a CW bug | |
740 | static const size_t npos; | |
741 | #endif | |
742 | ||
743 | private: | |
744 | // if we hadn't made these operators private, it would be possible to | |
745 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
746 | // converted to char in C and we do have operator=(char) | |
747 | // | |
748 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
749 | // to assign another numeric type to wxString will now result in | |
750 | // ambiguity between operator=(char) and operator=(int) | |
751 | wxString& operator=(int); | |
752 | ||
753 | // these methods are not implemented - there is _no_ conversion from int to | |
754 | // string, you're doing something wrong if the compiler wants to call it! | |
755 | // | |
756 | // try `s << i' or `s.Printf("%d", i)' instead | |
757 | wxString(int); | |
758 | ||
759 | ||
760 | // buffer for holding temporary substring when using any of the methods | |
761 | // that take (char*,size_t) or (wchar_t*,size_t) arguments: | |
762 | // FIXME-UTF8: This will need changes when UTF8 build is introduced | |
763 | template<typename T> | |
764 | struct SubstrBufFromType | |
765 | { | |
766 | T data; | |
767 | size_t len; | |
768 | ||
769 | SubstrBufFromType() {} | |
770 | SubstrBufFromType(const T& data_, size_t len_) | |
771 | : data(data_), len(len_) {} | |
772 | }; | |
773 | ||
774 | #if wxUSE_UNICODE_UTF8 | |
775 | // FIXME-UTF8: this will have to use slightly different type | |
776 | #elif wxUSE_UNICODE_WCHAR | |
777 | typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC; | |
778 | typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB; | |
779 | typedef SubstrBufFromWC SubstrBufFrom; | |
780 | #else | |
781 | typedef SubstrBufFromType<const char*> SubstrBufFromMB; | |
782 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC; | |
783 | typedef SubstrBufFromMB SubstrBufFrom; | |
784 | #endif | |
785 | ||
786 | ||
787 | // Functions implementing primitive operations on string data; wxString | |
788 | // methods and iterators are implemented in terms of it. The differences | |
789 | // between UTF-8 and wchar_t* representations of the string are mostly | |
790 | // contained here. | |
791 | ||
792 | #if wxUSE_UNICODE | |
793 | // FIXME-UTF8: This will need changes when UTF8 build is introduced | |
794 | static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength, | |
795 | const wxMBConv& conv); | |
796 | #else | |
797 | static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength, | |
798 | const wxMBConv& conv); | |
799 | #endif | |
800 | ||
801 | #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE | |
802 | // returns C string encoded as the implementation expects (version for | |
803 | // the same char type as used internally) | |
804 | static const wxStringCharType* ImplStr(const wxStringCharType* str) | |
805 | { return str; } | |
806 | static const SubstrBufFrom ImplStr(const wxStringCharType* str, size_t n) | |
807 | { return SubstrBufFrom(str, n); } | |
808 | #if wxUSE_UNICODE | |
809 | // returns C string encoded as the implementation expects (version for | |
810 | // the other char type than the one used internally) | |
811 | static wxWCharBuffer ImplStr(const char* str) | |
812 | { return ConvertStr(str, npos, wxConvLibc).data; } | |
813 | static SubstrBufFromMB ImplStr(const char* str, size_t n) | |
814 | { return ConvertStr(str, n, wxConvLibc); } | |
815 | #else | |
816 | static wxCharBuffer ImplStr(const wchar_t* str) | |
817 | { return ConvertStr(str, npos, wxConvLibc).data; } | |
818 | static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
819 | { return ConvertStr(str, n, wxConvLibc); } | |
820 | #endif | |
821 | ||
822 | // moves the iterator to the next Unicode character | |
823 | static void IncIter(wxStringImpl::iterator& i) { ++i; } | |
824 | static void IncIter(wxStringImpl::const_iterator& i) { ++i; } | |
825 | // moves the iterator to the previous Unicode character | |
826 | static void DecIter(wxStringImpl::iterator& i) { --i; } | |
827 | static void DecIter(wxStringImpl::const_iterator& i) { --i; } | |
828 | // moves the iterator by n Unicode characters | |
829 | static wxStringImpl::iterator AddToIter(wxStringImpl::iterator i, int n) | |
830 | { return i + n; } | |
831 | static wxStringImpl::const_iterator AddToIter(wxStringImpl::const_iterator i, int n) | |
832 | { return i + n; } | |
833 | // returns distance of the two iterators in Unicode characters | |
834 | static int DiffIters(wxStringImpl::iterator i1, wxStringImpl::iterator i2) | |
835 | { return i1 - i2; } | |
836 | static int DiffIters(wxStringImpl::const_iterator i1, wxStringImpl::const_iterator i2) | |
837 | { return i1 - i2; } | |
838 | ||
839 | // encodes the character to a form used to represent it in internal | |
840 | // representation (returns a string in UTF8 version) | |
841 | static wxChar EncodeChar(wxUniChar ch) { return (wxChar)ch; } | |
842 | ||
843 | // translates position index in wxString to/from index in underlying | |
844 | // wxStringImpl: | |
845 | static size_t PosToImpl(size_t pos) { return pos; } | |
846 | static void PosLenToImpl(size_t pos, size_t len, | |
847 | size_t *implPos, size_t *implLen) | |
848 | { *implPos = pos; *implLen = len; } | |
849 | static size_t PosFromImpl(size_t pos) { return pos; } | |
850 | ||
851 | #else // wxUSE_UNICODE_UTF8 | |
852 | ||
853 | typedef char Utf8CharBuffer[5]; | |
854 | static Utf8CharBuffer EncodeChar(wxUniChar ch); | |
855 | // returns n copies of ch encoded in UTF-8 string | |
856 | static wxCharBuffer EncodeNChars(size_t n, wxUniChar ch); | |
857 | ||
858 | size_t PosToImpl(size_t pos) const | |
859 | { | |
860 | if ( pos == 0 || pos == npos ) | |
861 | return pos; | |
862 | else | |
863 | return wxStringImpl::const_iterator(begin() + pos) - m_impl.begin(); | |
864 | } | |
865 | ||
866 | size_t PosFromImpl(size_t pos) const | |
867 | { | |
868 | if ( pos == 0 || pos == npos ) | |
869 | return pos; | |
870 | else | |
871 | return const_iterator(m_impl.begin() + pos) - begin(); | |
872 | } | |
873 | ||
874 | // FIXME: return as-is without copying under UTF8 locale, return | |
875 | // converted string under other locales - needs wxCharBuffer | |
876 | // changes | |
877 | static wxCharBuffer ImplStr(const char* str); | |
878 | ||
879 | static wxCharBuffer ImplStr(const wchar_t* str) | |
880 | { return wxConvUTF8.cWC2MB(str); } | |
881 | #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8 | |
882 | ||
883 | ||
884 | public: | |
885 | // constructors and destructor | |
886 | // ctor for an empty string | |
887 | wxString() {} | |
888 | // copy ctor | |
889 | wxString(const wxStringImpl& stringSrc) : m_impl(stringSrc) { } | |
890 | wxString(const wxString& stringSrc) : m_impl(stringSrc) { } | |
891 | // string containing nRepeat copies of ch | |
892 | wxString(wxUniChar ch, size_t nRepeat = 1) | |
893 | : m_impl(nRepeat, ch) { } | |
894 | wxString(size_t nRepeat, wxUniChar ch) | |
895 | : m_impl(nRepeat, ch) { } | |
896 | wxString(wxUniCharRef ch, size_t nRepeat = 1) | |
897 | : m_impl(nRepeat, ch) { } | |
898 | wxString(size_t nRepeat, wxUniCharRef ch) | |
899 | : m_impl(nRepeat, ch) { } | |
900 | wxString(char ch, size_t nRepeat = 1) | |
901 | : m_impl(nRepeat, ch) { } | |
902 | wxString(size_t nRepeat, char ch) | |
903 | : m_impl(nRepeat, ch) { } | |
904 | wxString(wchar_t ch, size_t nRepeat = 1) | |
905 | : m_impl(nRepeat, ch) { } | |
906 | wxString(size_t nRepeat, wchar_t ch) | |
907 | : m_impl(nRepeat, ch) { } | |
908 | // ctor takes first nLength characters from C string | |
909 | // (default value of npos means take all the string) | |
910 | wxString(const wxChar *psz) | |
911 | : m_impl(psz ? psz : wxT("")) { } | |
912 | wxString(const wxChar *psz, size_t nLength) | |
913 | : m_impl(psz, nLength) { } | |
914 | wxString(const wxChar *psz, | |
915 | const wxMBConv& WXUNUSED(conv), | |
916 | size_t nLength = npos) | |
917 | : m_impl(psz, nLength == npos ? wxStrlen(psz) : nLength) { } | |
918 | ||
919 | // even if we're not built with wxUSE_STL == 1 it is very convenient to allow | |
920 | // implicit conversions from std::string to wxString as this allows to use | |
921 | // the same strings in non-GUI and GUI code, however we don't want to | |
922 | // unconditionally add this ctor as it would make wx lib dependent on | |
923 | // libstdc++ on some Linux versions which is bad, so instead we ask the | |
924 | // client code to define this wxUSE_STD_STRING symbol if they need it | |
925 | #if wxUSE_STD_STRING | |
926 | wxString(const wxStdString& s) | |
927 | : m_impl(s.c_str()) { } | |
928 | #endif // wxUSE_STD_STRING | |
929 | ||
930 | #if wxUSE_UNICODE | |
931 | // from multibyte string | |
932 | wxString(const char *psz, | |
933 | const wxMBConv& conv = wxConvLibc, | |
934 | size_t nLength = npos); | |
935 | // from multibyte string for ANSI compatibility, with wxConvLibc | |
936 | wxString(const char *psz, size_t nLength); | |
937 | // from wxWCharBuffer (i.e. return from wxGetString) | |
938 | wxString(const wxWCharBuffer& psz) : m_impl(psz.data()) { } | |
939 | #else // ANSI | |
940 | // from C string (for compilers using unsigned char) | |
941 | wxString(const unsigned char* psz) | |
942 | : m_impl((const char*)psz) { } | |
943 | // from part of C string (for compilers using unsigned char) | |
944 | wxString(const unsigned char* psz, size_t nLength) | |
945 | : m_impl((const char*)psz, nLength) { } | |
946 | ||
947 | #if wxUSE_WCHAR_T | |
948 | // from wide (Unicode) string | |
949 | wxString(const wchar_t *pwz, | |
950 | const wxMBConv& conv = wxConvLibc, | |
951 | size_t nLength = npos); | |
952 | // from wide string for Unicode compatibility, with wxConvLibc | |
953 | wxString(const wchar_t *pwz, size_t nLength); | |
954 | #endif // !wxUSE_WCHAR_T | |
955 | ||
956 | // from wxCharBuffer | |
957 | wxString(const wxCharBuffer& psz) | |
958 | : m_impl(psz) { } | |
959 | #endif // Unicode/ANSI | |
960 | ||
961 | // as we provide both ctors with this signature for both char and unsigned | |
962 | // char string, we need to provide one for wxCStrData to resolve ambiguity | |
963 | wxString(const wxCStrData& cstr, size_t nLength) | |
964 | { assign(cstr.AsString(), nLength); } | |
965 | ||
966 | // and because wxString is convertible to wxCStrData and const wxChar * | |
967 | // we also need to provide this one | |
968 | wxString(const wxString& str, size_t nLength) | |
969 | { assign(str, nLength); } | |
970 | ||
971 | public: | |
972 | // standard types | |
973 | typedef wxUniChar value_type; | |
974 | typedef wxUniChar char_type; | |
975 | typedef wxUniCharRef reference; | |
976 | typedef wxChar* pointer; | |
977 | typedef const wxChar* const_pointer; | |
978 | ||
979 | typedef size_t size_type; | |
980 | typedef wxUniChar const_reference; | |
981 | ||
982 | #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \ | |
983 | reference_type, reference_ctor) \ | |
984 | private: \ | |
985 | typedef wxStringImpl::iterator_name underlying_iterator; \ | |
986 | public: \ | |
987 | typedef wxUniChar value_type; \ | |
988 | typedef reference_type reference; \ | |
989 | typedef pointer_type pointer; \ | |
990 | \ | |
991 | iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \ | |
992 | \ | |
993 | reference operator*() const { return reference_ctor; } \ | |
994 | \ | |
995 | iterator_name& operator++() \ | |
996 | { wxString::IncIter(m_cur); return *this; } \ | |
997 | iterator_name& operator--() \ | |
998 | { wxString::DecIter(m_cur); return *this; } \ | |
999 | iterator_name operator++(int) \ | |
1000 | { \ | |
1001 | iterator_name tmp = *this; \ | |
1002 | wxString::IncIter(m_cur); \ | |
1003 | return tmp; \ | |
1004 | } \ | |
1005 | iterator_name operator--(int) \ | |
1006 | { \ | |
1007 | iterator_name tmp = *this; \ | |
1008 | wxString::DecIter(m_cur); \ | |
1009 | return tmp; \ | |
1010 | } \ | |
1011 | \ | |
1012 | iterator_name operator+(int n) const \ | |
1013 | { return iterator_name(wxString::AddToIter(m_cur, n)); } \ | |
1014 | iterator_name operator+(size_t n) const \ | |
1015 | { return iterator_name(wxString::AddToIter(m_cur, (int)n)); } \ | |
1016 | iterator_name operator-(int n) const \ | |
1017 | { return iterator_name(wxString::AddToIter(m_cur, -n)); } \ | |
1018 | iterator_name operator-(size_t n) const \ | |
1019 | { return iterator_name(wxString::AddToIter(m_cur, -(int)n)); } \ | |
1020 | iterator_name operator+=(int n) \ | |
1021 | { m_cur = wxString::AddToIter(m_cur, n); return *this; } \ | |
1022 | iterator_name operator+=(size_t n) \ | |
1023 | { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \ | |
1024 | iterator_name operator-=(int n) \ | |
1025 | { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \ | |
1026 | iterator_name operator-=(size_t n) \ | |
1027 | { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \ | |
1028 | \ | |
1029 | unsigned operator-(const iterator_name& i) const \ | |
1030 | { return wxString::DiffIters(m_cur, i.m_cur); } \ | |
1031 | \ | |
1032 | bool operator==(const iterator_name&i) const \ | |
1033 | { return m_cur == i.m_cur; } \ | |
1034 | bool operator!=(const iterator_name& i) const \ | |
1035 | { return m_cur != i.m_cur; } \ | |
1036 | \ | |
1037 | bool operator<(const iterator_name& i) const \ | |
1038 | { return m_cur < i.m_cur; } \ | |
1039 | bool operator>(const iterator_name& i) const \ | |
1040 | { return m_cur > i.m_cur; } \ | |
1041 | bool operator<=(const iterator_name& i) const \ | |
1042 | { return m_cur <= i.m_cur; } \ | |
1043 | bool operator>=(const iterator_name& i) const \ | |
1044 | { return m_cur >= i.m_cur; } \ | |
1045 | \ | |
1046 | private: \ | |
1047 | /* for internal wxString use only: */ \ | |
1048 | iterator_name(underlying_iterator ptr) : m_cur(ptr) {} \ | |
1049 | operator underlying_iterator() const { return m_cur; } \ | |
1050 | \ | |
1051 | friend class WXDLLIMPEXP_BASE wxString; \ | |
1052 | friend class WXDLLIMPEXP_BASE wxStringImpl; \ | |
1053 | friend class WXDLLIMPEXP_BASE wxCStrData; \ | |
1054 | \ | |
1055 | private: \ | |
1056 | underlying_iterator m_cur; | |
1057 | ||
1058 | class const_iterator; | |
1059 | ||
1060 | class iterator | |
1061 | { | |
1062 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, | |
1063 | wxUniCharRef::CreateForString(m_cur)) | |
1064 | ||
1065 | friend class const_iterator; | |
1066 | }; | |
1067 | ||
1068 | class const_iterator | |
1069 | { | |
1070 | // NB: reference_type is intentionally value, not reference, the character | |
1071 | // may be encoded differently in wxString data: | |
1072 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar, | |
1073 | wxUniChar(*m_cur)) | |
1074 | ||
1075 | public: | |
1076 | const_iterator(const iterator& i) : m_cur(i.m_cur) {} | |
1077 | }; | |
1078 | ||
1079 | #undef WX_STR_ITERATOR_IMPL | |
1080 | ||
1081 | friend class iterator; | |
1082 | friend class const_iterator; | |
1083 | ||
1084 | template <typename T> | |
1085 | class reverse_iterator_impl | |
1086 | { | |
1087 | public: | |
1088 | typedef T iterator_type; | |
1089 | typedef typename T::value_type value_type; | |
1090 | typedef typename T::reference reference; | |
1091 | typedef typename T::pointer *pointer; | |
1092 | ||
1093 | reverse_iterator_impl(iterator_type i) : m_cur(i) {} | |
1094 | reverse_iterator_impl(const reverse_iterator_impl& ri) | |
1095 | : m_cur(ri.m_cur) {} | |
1096 | ||
1097 | iterator_type base() const { return m_cur; } | |
1098 | ||
1099 | reference operator*() const { return *(m_cur-1); } | |
1100 | ||
1101 | reverse_iterator_impl& operator++() | |
1102 | { --m_cur; return *this; } | |
1103 | reverse_iterator_impl operator++(int) | |
1104 | { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } | |
1105 | reverse_iterator_impl& operator--() | |
1106 | { ++m_cur; return *this; } | |
1107 | reverse_iterator_impl operator--(int) | |
1108 | { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } | |
1109 | ||
1110 | reverse_iterator_impl operator+(int n) const | |
1111 | { return reverse_iterator_impl(m_cur - n); } | |
1112 | reverse_iterator_impl operator+(size_t n) const | |
1113 | { return reverse_iterator_impl(m_cur - n); } | |
1114 | reverse_iterator_impl operator-(int n) const | |
1115 | { return reverse_iterator_impl(m_cur + n); } | |
1116 | reverse_iterator_impl operator-(size_t n) const | |
1117 | { return reverse_iterator_impl(m_cur + n); } | |
1118 | reverse_iterator_impl operator+=(int n) | |
1119 | { m_cur -= n; return *this; } | |
1120 | reverse_iterator_impl operator+=(size_t n) | |
1121 | { m_cur -= n; return *this; } | |
1122 | reverse_iterator_impl operator-=(int n) | |
1123 | { m_cur += n; return *this; } | |
1124 | reverse_iterator_impl operator-=(size_t n) | |
1125 | { m_cur += n; return *this; } | |
1126 | ||
1127 | unsigned operator-(const reverse_iterator_impl& i) const | |
1128 | { return i.m_cur - m_cur; } | |
1129 | ||
1130 | bool operator==(const reverse_iterator_impl& ri) const | |
1131 | { return m_cur == ri.m_cur; } | |
1132 | bool operator!=(const reverse_iterator_impl& ri) const | |
1133 | { return !(*this == ri); } | |
1134 | ||
1135 | bool operator<(const reverse_iterator_impl& i) const | |
1136 | { return m_cur > i.m_cur; } | |
1137 | bool operator>(const reverse_iterator_impl& i) const | |
1138 | { return m_cur < i.m_cur; } | |
1139 | bool operator<=(const reverse_iterator_impl& i) const | |
1140 | { return m_cur >= i.m_cur; } | |
1141 | bool operator>=(const reverse_iterator_impl& i) const | |
1142 | { return m_cur <= i.m_cur; } | |
1143 | ||
1144 | private: | |
1145 | iterator_type m_cur; | |
1146 | }; | |
1147 | ||
1148 | typedef reverse_iterator_impl<iterator> reverse_iterator; | |
1149 | typedef reverse_iterator_impl<const_iterator> const_reverse_iterator; | |
1150 | ||
1151 | // first valid index position | |
1152 | const_iterator begin() const { return const_iterator(m_impl.begin()); } | |
1153 | iterator begin() { return iterator(m_impl.begin()); } | |
1154 | // position one after the last valid one | |
1155 | const_iterator end() const { return const_iterator(m_impl.end()); } | |
1156 | iterator end() { return iterator(m_impl.end()); } | |
1157 | ||
1158 | // first element of the reversed string | |
1159 | const_reverse_iterator rbegin() const | |
1160 | { return const_reverse_iterator(end()); } | |
1161 | reverse_iterator rbegin() | |
1162 | { return reverse_iterator(end()); } | |
1163 | // one beyond the end of the reversed string | |
1164 | const_reverse_iterator rend() const | |
1165 | { return const_reverse_iterator(begin()); } | |
1166 | reverse_iterator rend() | |
1167 | { return reverse_iterator(begin()); } | |
1168 | ||
1169 | // std::string methods: | |
1170 | #if wxUSE_UNICODE_UTF8 | |
1171 | size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize! | |
1172 | #else | |
1173 | size_t length() const { return m_impl.length(); } | |
1174 | #endif | |
1175 | ||
1176 | size_type size() const { return length(); } | |
1177 | size_type max_size() const { return npos; } | |
1178 | ||
1179 | bool empty() const { return m_impl.empty(); } | |
1180 | ||
1181 | size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8 | |
1182 | void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8 | |
1183 | ||
1184 | void resize(size_t nSize, wxUniChar ch = wxT('\0')) | |
1185 | { | |
1186 | #if wxUSE_UNICODE_UTF8 | |
1187 | if ( !ch.IsAscii() ) | |
1188 | { | |
1189 | size_t len = length(); | |
1190 | if ( nSize == len) | |
1191 | return; | |
1192 | else if ( nSize < len ) | |
1193 | erase(nSize); | |
1194 | else | |
1195 | append(nSize - len, ch); | |
1196 | } | |
1197 | else | |
1198 | #endif | |
1199 | m_impl.resize(nSize, (wxStringCharType)ch); | |
1200 | } | |
1201 | ||
1202 | wxString substr(size_t nStart = 0, size_t nLen = npos) const | |
1203 | { | |
1204 | size_t pos, len; | |
1205 | PosLenToImpl(nStart, nLen, &pos, &len); | |
1206 | return m_impl.substr(pos, len); | |
1207 | } | |
1208 | ||
1209 | // generic attributes & operations | |
1210 | // as standard strlen() | |
1211 | size_t Len() const { return length(); } | |
1212 | // string contains any characters? | |
1213 | bool IsEmpty() const { return empty(); } | |
1214 | // empty string is "false", so !str will return true | |
1215 | bool operator!() const { return empty(); } | |
1216 | // truncate the string to given length | |
1217 | wxString& Truncate(size_t uiLen); | |
1218 | // empty string contents | |
1219 | void Empty() | |
1220 | { | |
1221 | Truncate(0); | |
1222 | ||
1223 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); | |
1224 | } | |
1225 | // empty the string and free memory | |
1226 | void Clear() | |
1227 | { | |
1228 | wxString tmp(wxEmptyString); | |
1229 | swap(tmp); | |
1230 | } | |
1231 | ||
1232 | // contents test | |
1233 | // Is an ascii value | |
1234 | bool IsAscii() const; | |
1235 | // Is a number | |
1236 | bool IsNumber() const; | |
1237 | // Is a word | |
1238 | bool IsWord() const; | |
1239 | ||
1240 | // data access (all indexes are 0 based) | |
1241 | // read access | |
1242 | wxUniChar at(size_t n) const | |
1243 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
1244 | wxUniChar GetChar(size_t n) const | |
1245 | { return at(n); } | |
1246 | // read/write access | |
1247 | wxUniCharRef at(size_t n) | |
1248 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
1249 | wxUniCharRef GetWritableChar(size_t n) | |
1250 | { return at(n); } | |
1251 | // write access | |
1252 | void SetChar(size_t n, wxUniChar ch) | |
1253 | { at(n) = ch; } | |
1254 | ||
1255 | // get last character | |
1256 | wxUniChar Last() const | |
1257 | { | |
1258 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); | |
1259 | ||
1260 | return at(length() - 1); | |
1261 | } | |
1262 | ||
1263 | // get writable last character | |
1264 | wxUniCharRef Last() | |
1265 | { | |
1266 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); | |
1267 | return at(length() - 1); | |
1268 | } | |
1269 | ||
1270 | /* | |
1271 | Note that we we must define all of the overloads below to avoid | |
1272 | ambiguity when using str[0]. | |
1273 | */ | |
1274 | wxUniChar operator[](int n) const | |
1275 | { return at(n); } | |
1276 | wxUniChar operator[](long n) const | |
1277 | { return at(n); } | |
1278 | wxUniChar operator[](size_t n) const | |
1279 | { return at(n); } | |
1280 | #ifndef wxSIZE_T_IS_UINT | |
1281 | wxUniChar operator[](unsigned int n) const | |
1282 | { return at(n); } | |
1283 | #endif // size_t != unsigned int | |
1284 | ||
1285 | // operator versions of GetWriteableChar() | |
1286 | wxUniCharRef operator[](int n) | |
1287 | { return at(n); } | |
1288 | wxUniCharRef operator[](long n) | |
1289 | { return at(n); } | |
1290 | wxUniCharRef operator[](size_t n) | |
1291 | { return at(n); } | |
1292 | #ifndef wxSIZE_T_IS_UINT | |
1293 | wxUniCharRef operator[](unsigned int n) | |
1294 | { return at(n); } | |
1295 | #endif // size_t != unsigned int | |
1296 | ||
1297 | // explicit conversion to C string (use this with printf()!) | |
1298 | wxCStrData c_str() const { return wxCStrData(this); } | |
1299 | wxCStrData data() const { return c_str(); } | |
1300 | ||
1301 | // implicit conversion to C string | |
1302 | operator wxCStrData() const { return c_str(); } | |
1303 | operator const wxChar*() const { return c_str(); } | |
1304 | ||
1305 | // identical to c_str(), for MFC compatibility | |
1306 | const wxCStrData GetData() const { return c_str(); } | |
1307 | ||
1308 | // explicit conversion to C string in internal representation (char*, | |
1309 | // wchar_t*, UTF-8-encoded char*, depending on the build): | |
1310 | const_pointer wx_str() const { return m_impl.c_str(); } | |
1311 | ||
1312 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for | |
1313 | // converting numbers or strings which are certain not to contain special | |
1314 | // chars (typically system functions, X atoms, environment variables etc.) | |
1315 | // | |
1316 | // the behaviour of these functions with the strings containing anything | |
1317 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
1318 | #if wxUSE_UNICODE | |
1319 | static wxString FromAscii(const char *ascii); // string | |
1320 | static wxString FromAscii(const char ascii); // char | |
1321 | const wxCharBuffer ToAscii() const; | |
1322 | #else // ANSI | |
1323 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
1324 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } | |
1325 | const char *ToAscii() const { return c_str(); } | |
1326 | #endif // Unicode/!Unicode | |
1327 | ||
1328 | // conversions with (possible) format conversions: have to return a | |
1329 | // buffer with temporary data | |
1330 | // | |
1331 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
1332 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
1333 | // fn_str() to return a string which should be used with the OS APIs | |
1334 | // accepting the file names. The return value is always the same, but the | |
1335 | // type differs because a function may either return pointer to the buffer | |
1336 | // directly or have to use intermediate buffer for translation. | |
1337 | #if wxUSE_UNICODE | |
1338 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; | |
1339 | ||
1340 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } | |
1341 | ||
1342 | const wxChar* wc_str() const { return c_str(); } | |
1343 | ||
1344 | // for compatibility with !wxUSE_UNICODE version | |
1345 | const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } | |
1346 | ||
1347 | #if wxMBFILES | |
1348 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } | |
1349 | #else // !wxMBFILES | |
1350 | const wxChar* fn_str() const { return c_str(); } | |
1351 | #endif // wxMBFILES/!wxMBFILES | |
1352 | #else // ANSI | |
1353 | const wxChar* mb_str() const { return c_str(); } | |
1354 | ||
1355 | // for compatibility with wxUSE_UNICODE version | |
1356 | const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } | |
1357 | ||
1358 | const wxWX2MBbuf mbc_str() const { return mb_str(); } | |
1359 | ||
1360 | #if wxUSE_WCHAR_T | |
1361 | const wxWCharBuffer wc_str(const wxMBConv& conv) const; | |
1362 | #endif // wxUSE_WCHAR_T | |
1363 | #ifdef __WXOSX__ | |
1364 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
1365 | #else | |
1366 | const wxChar* fn_str() const { return c_str(); } | |
1367 | #endif | |
1368 | #endif // Unicode/ANSI | |
1369 | ||
1370 | // overloaded assignment | |
1371 | // from another wxString | |
1372 | wxString& operator=(const wxStringImpl& stringSrc) | |
1373 | { m_impl = stringSrc; return *this; } | |
1374 | wxString& operator=(const wxCStrData& cstr) | |
1375 | { return *this = cstr.AsString(); } | |
1376 | // from a character | |
1377 | wxString& operator=(wxUniChar ch) | |
1378 | { m_impl = EncodeChar(ch); return *this; } | |
1379 | wxString& operator=(wxUniCharRef ch) | |
1380 | { return operator=((wxUniChar)ch); } | |
1381 | wxString& operator=(char ch) | |
1382 | { return operator=(wxUniChar(ch)); } | |
1383 | wxString& operator=(wchar_t ch) | |
1384 | { return operator=(wxUniChar(ch)); } | |
1385 | // from a C string - STL probably will crash on NULL, | |
1386 | // so we need to compensate in that case | |
1387 | #if wxUSE_STL_BASED_WXSTRING | |
1388 | wxString& operator=(const wxChar *psz) | |
1389 | { if(psz) m_impl = psz; else Clear(); return *this; } | |
1390 | #else | |
1391 | wxString& operator=(const wxChar *psz) | |
1392 | { m_impl = psz; return *this; } | |
1393 | #endif | |
1394 | ||
1395 | #if wxUSE_UNICODE | |
1396 | // from wxWCharBuffer | |
1397 | wxString& operator=(const wxWCharBuffer& s) | |
1398 | { (void) operator=((const wchar_t *)s); return *this; } | |
1399 | // from C string | |
1400 | wxString& operator=(const char* psz) | |
1401 | { return operator=(wxString(psz)); } | |
1402 | #else // ANSI | |
1403 | // from another kind of C string | |
1404 | wxString& operator=(const unsigned char* psz); | |
1405 | #if wxUSE_WCHAR_T | |
1406 | // from a wide string | |
1407 | wxString& operator=(const wchar_t *pwz); | |
1408 | #endif | |
1409 | // from wxCharBuffer | |
1410 | wxString& operator=(const wxCharBuffer& psz) | |
1411 | { (void) operator=((const char *)psz); return *this; } | |
1412 | #endif // Unicode/ANSI | |
1413 | ||
1414 | // string concatenation | |
1415 | // in place concatenation | |
1416 | /* | |
1417 | Concatenate and return the result. Note that the left to right | |
1418 | associativity of << allows to write things like "str << str1 << str2 | |
1419 | << ..." (unlike with +=) | |
1420 | */ | |
1421 | // string += string | |
1422 | wxString& operator<<(const wxString& s) | |
1423 | { | |
1424 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
1425 | wxASSERT_MSG( s.IsValid(), | |
1426 | _T("did you forget to call UngetWriteBuf()?") ); | |
1427 | #endif | |
1428 | ||
1429 | append(s); | |
1430 | return *this; | |
1431 | } | |
1432 | // string += C string | |
1433 | wxString& operator<<(const char *psz) | |
1434 | { append(psz); return *this; } | |
1435 | wxString& operator<<(const wchar_t *pwz) | |
1436 | { append(pwz); return *this; } | |
1437 | wxString& operator<<(const wxCStrData& psz) | |
1438 | { append(psz.AsString()); return *this; } | |
1439 | // string += char | |
1440 | wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } | |
1441 | wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } | |
1442 | wxString& operator<<(char ch) { append(1, ch); return *this; } | |
1443 | wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } | |
1444 | ||
1445 | // string += buffer (i.e. from wxGetString) | |
1446 | wxString& operator<<(const wxWCharBuffer& s) | |
1447 | { return operator<<((const wchar_t *)s); } | |
1448 | wxString& operator+=(const wxWCharBuffer& s) | |
1449 | { return operator<<((const wchar_t *)s); } | |
1450 | ||
1451 | wxString& operator<<(const wxCharBuffer& s) | |
1452 | { return operator<<((const char *)s); } | |
1453 | wxString& operator+=(const wxCharBuffer& s) | |
1454 | { return operator<<((const char *)s); } | |
1455 | ||
1456 | // string += C string | |
1457 | wxString& Append(const wxString& s) | |
1458 | { | |
1459 | // test for empty() to share the string if possible | |
1460 | if ( empty() ) | |
1461 | *this = s; | |
1462 | else | |
1463 | append(s); | |
1464 | return *this; | |
1465 | } | |
1466 | wxString& Append(const wxCStrData& psz) | |
1467 | { append(psz); return *this; } | |
1468 | wxString& Append(const char* psz) | |
1469 | { append(psz); return *this; } | |
1470 | wxString& Append(const wchar_t* pwz) | |
1471 | { append(pwz); return *this; } | |
1472 | // append count copies of given character | |
1473 | wxString& Append(wxUniChar ch, size_t count = 1u) | |
1474 | { append(count, ch); return *this; } | |
1475 | wxString& Append(wxUniCharRef ch, size_t count = 1u) | |
1476 | { append(count, ch); return *this; } | |
1477 | wxString& Append(char ch, size_t count = 1u) | |
1478 | { append(count, ch); return *this; } | |
1479 | wxString& Append(wchar_t ch, size_t count = 1u) | |
1480 | { append(count, ch); return *this; } | |
1481 | wxString& Append(const char* psz, size_t nLen) | |
1482 | { append(psz, nLen); return *this; } | |
1483 | wxString& Append(const wchar_t* pwz, size_t nLen) | |
1484 | { append(pwz, nLen); return *this; } | |
1485 | ||
1486 | // prepend a string, return the string itself | |
1487 | wxString& Prepend(const wxString& str) | |
1488 | { *this = str + *this; return *this; } | |
1489 | ||
1490 | // non-destructive concatenation | |
1491 | // two strings | |
1492 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
1493 | const wxString& string2); | |
1494 | // string with a single char | |
1495 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); | |
1496 | // char with a string | |
1497 | friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
1498 | // string with C string | |
1499 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1500 | const char *psz); | |
1501 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1502 | const wchar_t *pwz); | |
1503 | // C string with string | |
1504 | friend wxString WXDLLIMPEXP_BASE operator+(const char *psz, | |
1505 | const wxString& string); | |
1506 | friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, | |
1507 | const wxString& string); | |
1508 | ||
1509 | // stream-like functions | |
1510 | // insert an int into string | |
1511 | wxString& operator<<(int i) | |
1512 | { return (*this) << Format(_T("%d"), i); } | |
1513 | // insert an unsigned int into string | |
1514 | wxString& operator<<(unsigned int ui) | |
1515 | { return (*this) << Format(_T("%u"), ui); } | |
1516 | // insert a long into string | |
1517 | wxString& operator<<(long l) | |
1518 | { return (*this) << Format(_T("%ld"), l); } | |
1519 | // insert an unsigned long into string | |
1520 | wxString& operator<<(unsigned long ul) | |
1521 | { return (*this) << Format(_T("%lu"), ul); } | |
1522 | #if defined wxLongLong_t && !defined wxLongLongIsLong | |
1523 | // insert a long long if they exist and aren't longs | |
1524 | wxString& operator<<(wxLongLong_t ll) | |
1525 | { | |
1526 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
1527 | return (*this) << Format(fmt, ll); | |
1528 | } | |
1529 | // insert an unsigned long long | |
1530 | wxString& operator<<(wxULongLong_t ull) | |
1531 | { | |
1532 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
1533 | return (*this) << Format(fmt , ull); | |
1534 | } | |
1535 | #endif | |
1536 | // insert a float into string | |
1537 | wxString& operator<<(float f) | |
1538 | { return (*this) << Format(_T("%f"), f); } | |
1539 | // insert a double into string | |
1540 | wxString& operator<<(double d) | |
1541 | { return (*this) << Format(_T("%g"), d); } | |
1542 | ||
1543 | // string comparison | |
1544 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) | |
1545 | int Cmp(const char *psz) const | |
1546 | { return compare(psz); } | |
1547 | int Cmp(const wchar_t *pwz) const | |
1548 | { return compare(pwz); } | |
1549 | int Cmp(const wxString& s) const | |
1550 | { return compare(s); } | |
1551 | // same as Cmp() but not case-sensitive | |
1552 | int CmpNoCase(const wxString& s) const; | |
1553 | int CmpNoCase(const char *psz) const | |
1554 | { return CmpNoCase(wxString(psz)); } | |
1555 | int CmpNoCase(const wchar_t *pwz) const | |
1556 | { return CmpNoCase(wxString(pwz)); } | |
1557 | // test for the string equality, either considering case or not | |
1558 | // (if compareWithCase then the case matters) | |
1559 | bool IsSameAs(const char *psz, bool compareWithCase = true) const | |
1560 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } | |
1561 | bool IsSameAs(const wchar_t *pwz, bool compareWithCase = true) const | |
1562 | { return (compareWithCase ? Cmp(pwz) : CmpNoCase(pwz)) == 0; } | |
1563 | // comparison with a single character: returns true if equal | |
1564 | bool IsSameAs(wxUniChar c, bool compareWithCase = true) const | |
1565 | { | |
1566 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c | |
1567 | : wxToupper(GetChar(0u)) == wxToupper(c)); | |
1568 | } | |
1569 | ||
1570 | // simple sub-string extraction | |
1571 | // return substring starting at nFirst of length nCount (or till the end | |
1572 | // if nCount = default value) | |
1573 | wxString Mid(size_t nFirst, size_t nCount = npos) const; | |
1574 | ||
1575 | // operator version of Mid() | |
1576 | wxString operator()(size_t start, size_t len) const | |
1577 | { return Mid(start, len); } | |
1578 | ||
1579 | // check if the string starts with the given prefix and return the rest | |
1580 | // of the string in the provided pointer if it is not NULL; otherwise | |
1581 | // return false | |
1582 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; | |
1583 | // check if the string ends with the given suffix and return the | |
1584 | // beginning of the string before the suffix in the provided pointer if | |
1585 | // it is not NULL; otherwise return false | |
1586 | bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const; | |
1587 | ||
1588 | // get first nCount characters | |
1589 | wxString Left(size_t nCount) const; | |
1590 | // get last nCount characters | |
1591 | wxString Right(size_t nCount) const; | |
1592 | // get all characters before the first occurance of ch | |
1593 | // (returns the whole string if ch not found) | |
1594 | wxString BeforeFirst(wxUniChar ch) const; | |
1595 | // get all characters before the last occurence of ch | |
1596 | // (returns empty string if ch not found) | |
1597 | wxString BeforeLast(wxUniChar ch) const; | |
1598 | // get all characters after the first occurence of ch | |
1599 | // (returns empty string if ch not found) | |
1600 | wxString AfterFirst(wxUniChar ch) const; | |
1601 | // get all characters after the last occurence of ch | |
1602 | // (returns the whole string if ch not found) | |
1603 | wxString AfterLast(wxUniChar ch) const; | |
1604 | ||
1605 | // for compatibility only, use more explicitly named functions above | |
1606 | wxString Before(wxUniChar ch) const { return BeforeLast(ch); } | |
1607 | wxString After(wxUniChar ch) const { return AfterFirst(ch); } | |
1608 | ||
1609 | // case conversion | |
1610 | // convert to upper case in place, return the string itself | |
1611 | wxString& MakeUpper(); | |
1612 | // convert to upper case, return the copy of the string | |
1613 | // Here's something to remember: BC++ doesn't like returns in inlines. | |
1614 | wxString Upper() const ; | |
1615 | // convert to lower case in place, return the string itself | |
1616 | wxString& MakeLower(); | |
1617 | // convert to lower case, return the copy of the string | |
1618 | wxString Lower() const ; | |
1619 | ||
1620 | // trimming/padding whitespace (either side) and truncating | |
1621 | // remove spaces from left or from right (default) side | |
1622 | wxString& Trim(bool bFromRight = true); | |
1623 | // add nCount copies chPad in the beginning or at the end (default) | |
1624 | wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); | |
1625 | ||
1626 | // searching and replacing | |
1627 | // searching (return starting index, or -1 if not found) | |
1628 | int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr | |
1629 | // searching (return starting index, or -1 if not found) | |
1630 | int Find(const wxChar *pszSub) const; // like strstr | |
1631 | // replace first (or all of bReplaceAll) occurences of substring with | |
1632 | // another string, returns the number of replacements made | |
1633 | size_t Replace(const wxChar *szOld, | |
1634 | const wxChar *szNew, | |
1635 | bool bReplaceAll = true); | |
1636 | ||
1637 | // check if the string contents matches a mask containing '*' and '?' | |
1638 | bool Matches(const wxChar *szMask) const; | |
1639 | ||
1640 | // conversion to numbers: all functions return true only if the whole | |
1641 | // string is a number and put the value of this number into the pointer | |
1642 | // provided, the base is the numeric base in which the conversion should be | |
1643 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1644 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
1645 | // convert to a signed integer | |
1646 | bool ToLong(long *val, int base = 10) const; | |
1647 | // convert to an unsigned integer | |
1648 | bool ToULong(unsigned long *val, int base = 10) const; | |
1649 | // convert to wxLongLong | |
1650 | #if defined(wxLongLong_t) | |
1651 | bool ToLongLong(wxLongLong_t *val, int base = 10) const; | |
1652 | // convert to wxULongLong | |
1653 | bool ToULongLong(wxULongLong_t *val, int base = 10) const; | |
1654 | #endif // wxLongLong_t | |
1655 | // convert to a double | |
1656 | bool ToDouble(double *val) const; | |
1657 | ||
1658 | ||
1659 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1660 | // formatted input/output | |
1661 | // as sprintf(), returns the number of characters written or < 0 on error | |
1662 | // (take 'this' into account in attribute parameter count) | |
1663 | // int Printf(const wxChar *pszFormat, ...); | |
1664 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
1665 | #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1666 | // as vprintf(), returns the number of characters written or < 0 on error | |
1667 | int PrintfV(const wxString& format, va_list argptr); | |
1668 | ||
1669 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1670 | // returns the string containing the result of Printf() to it | |
1671 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; | |
1672 | WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat) | |
1673 | #endif | |
1674 | // the same as above, but takes a va_list | |
1675 | static wxString FormatV(const wxString& format, va_list argptr); | |
1676 | ||
1677 | // raw access to string memory | |
1678 | // ensure that string has space for at least nLen characters | |
1679 | // only works if the data of this string is not shared | |
1680 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } | |
1681 | // minimize the string's memory | |
1682 | // only works if the data of this string is not shared | |
1683 | bool Shrink(); | |
1684 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
1685 | // These are deprecated, use wxStringBuffer or wxStringBufferLength instead | |
1686 | // | |
1687 | // get writable buffer of at least nLen bytes. Unget() *must* be called | |
1688 | // a.s.a.p. to put string back in a reasonable state! | |
1689 | wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) ); | |
1690 | // call this immediately after GetWriteBuf() has been used | |
1691 | wxDEPRECATED( void UngetWriteBuf() ); | |
1692 | wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); | |
1693 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8 | |
1694 | ||
1695 | // wxWidgets version 1 compatibility functions | |
1696 | ||
1697 | // use Mid() | |
1698 | wxString SubString(size_t from, size_t to) const | |
1699 | { return Mid(from, (to - from + 1)); } | |
1700 | // values for second parameter of CompareTo function | |
1701 | enum caseCompare {exact, ignoreCase}; | |
1702 | // values for first parameter of Strip function | |
1703 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; | |
1704 | ||
1705 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1706 | // use Printf() | |
1707 | // (take 'this' into account in attribute parameter count) | |
1708 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
1709 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
1710 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1711 | ||
1712 | // use Cmp() | |
1713 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const | |
1714 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } | |
1715 | ||
1716 | // use length() | |
1717 | size_t Length() const { return length(); } | |
1718 | // Count the number of characters | |
1719 | int Freq(wxUniChar ch) const; | |
1720 | // use MakeLower | |
1721 | void LowerCase() { MakeLower(); } | |
1722 | // use MakeUpper | |
1723 | void UpperCase() { MakeUpper(); } | |
1724 | // use Trim except that it doesn't change this string | |
1725 | wxString Strip(stripType w = trailing) const; | |
1726 | ||
1727 | // use Find (more general variants not yet supported) | |
1728 | size_t Index(const wxChar* psz) const { return Find(psz); } | |
1729 | size_t Index(wxUniChar ch) const { return Find(ch); } | |
1730 | // use Truncate | |
1731 | wxString& Remove(size_t pos) { return Truncate(pos); } | |
1732 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } | |
1733 | ||
1734 | wxString& Remove(size_t nStart, size_t nLen) | |
1735 | { return (wxString&)erase( nStart, nLen ); } | |
1736 | ||
1737 | // use Find() | |
1738 | int First( wxUniChar ch ) const { return Find(ch); } | |
1739 | int First( char ch ) const { return Find(ch); } | |
1740 | int First( wchar_t ch ) const { return Find(ch); } | |
1741 | int First( const wxChar* psz ) const { return Find(psz); } | |
1742 | int First( const wxString &str ) const { return Find(str); } | |
1743 | int Last( wxUniChar ch ) const { return Find(ch, true); } | |
1744 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } | |
1745 | ||
1746 | // use empty() | |
1747 | bool IsNull() const { return empty(); } | |
1748 | ||
1749 | // std::string compatibility functions | |
1750 | ||
1751 | // take nLen chars starting at nPos | |
1752 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
1753 | : m_impl(str.m_impl, nPos, nLen) { } | |
1754 | // take all characters from pStart to pEnd | |
1755 | wxString(const void *pStart, const void *pEnd) | |
1756 | : m_impl((const wxChar*)pStart, (const wxChar*)pEnd) { } | |
1757 | wxString(const_iterator first, const_iterator last) | |
1758 | : m_impl(first, last) { } | |
1759 | wxString(iterator first, iterator last) | |
1760 | : m_impl(first, last) { } | |
1761 | ||
1762 | // lib.string.modifiers | |
1763 | // append elements str[pos], ..., str[pos+n] | |
1764 | wxString& append(const wxString& str, size_t pos, size_t n) | |
1765 | { | |
1766 | size_t from, len; | |
1767 | str.PosLenToImpl(pos, n, &from, &len); | |
1768 | m_impl.append(str.m_impl, from, len); | |
1769 | return *this; | |
1770 | } | |
1771 | // append a string | |
1772 | wxString& append(const wxString& str) | |
1773 | { m_impl.append(str.m_impl); return *this; } | |
1774 | wxString& append(const wxCStrData& str) | |
1775 | { m_impl.append(str.AsString().m_impl); return *this; } | |
1776 | // append first n (or all if n == npos) characters of sz | |
1777 | wxString& append(const char *sz) | |
1778 | { m_impl.append(ImplStr(sz)); return *this; } | |
1779 | wxString& append(const wchar_t *sz) | |
1780 | { m_impl.append(ImplStr(sz)); return *this; } | |
1781 | wxString& append(const char *sz, size_t n) | |
1782 | { | |
1783 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1784 | m_impl.append(str.data, str.len); | |
1785 | return *this; | |
1786 | } | |
1787 | wxString& append(const wchar_t *sz, size_t n) | |
1788 | { | |
1789 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1790 | m_impl.append(str.data, str.len); | |
1791 | return *this; | |
1792 | } | |
1793 | // append n copies of ch | |
1794 | wxString& append(size_t n, wxUniChar ch) | |
1795 | { | |
1796 | #if wxUSE_UNICODE_UTF8 | |
1797 | if ( !ch.IsAscii() ) | |
1798 | m_impl.append(EncodeNChars(n, ch)); | |
1799 | else | |
1800 | #endif | |
1801 | m_impl.append(n, (wxStringCharType)ch); | |
1802 | return *this; | |
1803 | } | |
1804 | // append from first to last | |
1805 | wxString& append(const_iterator first, const_iterator last) | |
1806 | { m_impl.append(first, last); return *this; } | |
1807 | ||
1808 | // same as `this_string = str' | |
1809 | wxString& assign(const wxString& str) | |
1810 | { m_impl = str.m_impl; return *this; } | |
1811 | // same as ` = str[pos..pos + n] | |
1812 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
1813 | { | |
1814 | size_t from, len; | |
1815 | str.PosLenToImpl(pos, n, &from, &len); | |
1816 | m_impl.assign(str.m_impl, from, len); | |
1817 | return *this; | |
1818 | } | |
1819 | // same as `= first n (or all if n == npos) characters of sz' | |
1820 | wxString& assign(const char *sz) | |
1821 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1822 | wxString& assign(const wchar_t *sz) | |
1823 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1824 | wxString& assign(const char *sz, size_t n) | |
1825 | { | |
1826 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1827 | m_impl.assign(str.data, str.len); | |
1828 | return *this; | |
1829 | } | |
1830 | wxString& assign(const wchar_t *sz, size_t n) | |
1831 | { | |
1832 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1833 | m_impl.assign(str.data, str.len); | |
1834 | return *this; | |
1835 | } | |
1836 | // same as `= n copies of ch' | |
1837 | wxString& assign(size_t n, wxUniChar ch) | |
1838 | { | |
1839 | #if wxUSE_UNICODE_UTF8 | |
1840 | if ( !ch.IsAscii() ) | |
1841 | m_impl.assign(EncodeNChars(n, ch)); | |
1842 | else | |
1843 | #endif | |
1844 | m_impl.assign(n, (wxStringCharType)ch); | |
1845 | return *this; | |
1846 | } | |
1847 | // assign from first to last | |
1848 | wxString& assign(const_iterator first, const_iterator last) | |
1849 | { m_impl.assign(first, last); return *this; } | |
1850 | ||
1851 | // string comparison | |
1852 | int compare(const wxString& str) const; | |
1853 | // comparison with a substring | |
1854 | int compare(size_t nStart, size_t nLen, const wxString& str) const; | |
1855 | // comparison of 2 substrings | |
1856 | int compare(size_t nStart, size_t nLen, | |
1857 | const wxString& str, size_t nStart2, size_t nLen2) const; | |
1858 | // just like strcmp() | |
1859 | int compare(const char* sz) const; | |
1860 | int compare(const wchar_t* sz) const; | |
1861 | // substring comparison with first nCount characters of sz | |
1862 | int compare(size_t nStart, size_t nLen, | |
1863 | const char* sz, size_t nCount = npos) const; | |
1864 | int compare(size_t nStart, size_t nLen, | |
1865 | const wchar_t* sz, size_t nCount = npos) const; | |
1866 | ||
1867 | // insert another string | |
1868 | wxString& insert(size_t nPos, const wxString& str) | |
1869 | { insert(begin() + nPos, str.begin(), str.end()); return *this; } | |
1870 | // insert n chars of str starting at nStart (in str) | |
1871 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
1872 | { | |
1873 | size_t from, len; | |
1874 | str.PosLenToImpl(nStart, n, &from, &len); | |
1875 | m_impl.insert(PosToImpl(nPos), str.m_impl, from, len); | |
1876 | return *this; | |
1877 | } | |
1878 | // insert first n (or all if n == npos) characters of sz | |
1879 | wxString& insert(size_t nPos, const char *sz) | |
1880 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1881 | wxString& insert(size_t nPos, const wchar_t *sz) | |
1882 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1883 | wxString& insert(size_t nPos, const char *sz, size_t n) | |
1884 | { | |
1885 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1886 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1887 | return *this; | |
1888 | } | |
1889 | wxString& insert(size_t nPos, const wchar_t *sz, size_t n) | |
1890 | { | |
1891 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1892 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1893 | return *this; | |
1894 | } | |
1895 | // insert n copies of ch | |
1896 | wxString& insert(size_t nPos, size_t n, wxUniChar ch) | |
1897 | { | |
1898 | #if wxUSE_UNICODE_UTF8 | |
1899 | if ( !ch.IsAscii() ) | |
1900 | m_impl.insert(begin() + nPos, EncodeNChars(n, ch)); | |
1901 | else | |
1902 | #endif | |
1903 | m_impl.insert(begin() + nPos, n, (wxStringCharType)ch); | |
1904 | return *this; | |
1905 | } | |
1906 | iterator insert(iterator it, wxUniChar ch) | |
1907 | { return iterator(m_impl.insert(it, EncodeChar(ch))); } | |
1908 | void insert(iterator it, const_iterator first, const_iterator last) | |
1909 | { m_impl.insert(it, first, last); } | |
1910 | void insert(iterator it, size_type n, wxUniChar ch) | |
1911 | { | |
1912 | #if wxUSE_UNICODE_UTF8 | |
1913 | if ( !ch.IsAscii() ) | |
1914 | m_impl.insert(it, EncodeNChars(n, ch)); | |
1915 | else | |
1916 | #endif | |
1917 | m_impl.insert(it, n, (wxStringCharType)ch); | |
1918 | } | |
1919 | ||
1920 | // delete characters from nStart to nStart + nLen | |
1921 | wxString& erase(size_type pos = 0, size_type n = npos) | |
1922 | { | |
1923 | size_t from, len; | |
1924 | PosLenToImpl(pos, n, &from, &len); | |
1925 | m_impl.erase(from, len); | |
1926 | return *this; | |
1927 | } | |
1928 | iterator erase(iterator first, iterator last) | |
1929 | { return iterator(m_impl.erase(first, last)); } | |
1930 | iterator erase(iterator first) | |
1931 | { return iterator(m_impl.erase(first)); } | |
1932 | ||
1933 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
1934 | void clear() { erase(); } | |
1935 | #else | |
1936 | void clear() { m_impl.clear(); } | |
1937 | #endif | |
1938 | ||
1939 | // replaces the substring of length nLen starting at nStart | |
1940 | wxString& replace(size_t nStart, size_t nLen, const char* sz) | |
1941 | { | |
1942 | size_t from, len; | |
1943 | PosLenToImpl(nStart, nLen, &from, &len); | |
1944 | m_impl.replace(from, len, ImplStr(sz)); | |
1945 | return *this; | |
1946 | } | |
1947 | wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz) | |
1948 | { | |
1949 | size_t from, len; | |
1950 | PosLenToImpl(nStart, nLen, &from, &len); | |
1951 | m_impl.replace(from, len, ImplStr(sz)); | |
1952 | return *this; | |
1953 | } | |
1954 | // replaces the substring of length nLen starting at nStart | |
1955 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
1956 | { | |
1957 | size_t from, len; | |
1958 | PosLenToImpl(nStart, nLen, &from, &len); | |
1959 | m_impl.replace(from, len, str.m_impl); | |
1960 | return *this; | |
1961 | } | |
1962 | // replaces the substring with nCount copies of ch | |
1963 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) | |
1964 | { | |
1965 | size_t from, len; | |
1966 | PosLenToImpl(nStart, nLen, &from, &len); | |
1967 | #if wxUSE_UNICODE_UTF8 | |
1968 | if ( !ch.IsAscii() ) | |
1969 | m_impl.replace(from, len, EncodeNChars(nCount, ch)); | |
1970 | else | |
1971 | #endif | |
1972 | m_impl.replace(from, len, nCount, (wxStringCharType)ch); | |
1973 | return *this; | |
1974 | } | |
1975 | // replaces a substring with another substring | |
1976 | wxString& replace(size_t nStart, size_t nLen, | |
1977 | const wxString& str, size_t nStart2, size_t nLen2) | |
1978 | { | |
1979 | size_t from, len; | |
1980 | PosLenToImpl(nStart, nLen, &from, &len); | |
1981 | ||
1982 | size_t from2, len2; | |
1983 | str.PosLenToImpl(nStart2, nLen2, &from2, &len2); | |
1984 | ||
1985 | m_impl.replace(from, len, str.m_impl, from2, len2); | |
1986 | return *this; | |
1987 | } | |
1988 | // replaces the substring with first nCount chars of sz | |
1989 | wxString& replace(size_t nStart, size_t nLen, | |
1990 | const char* sz, size_t nCount) | |
1991 | { | |
1992 | size_t from, len; | |
1993 | PosLenToImpl(nStart, nLen, &from, &len); | |
1994 | ||
1995 | SubstrBufFromMB str(ImplStr(sz, nCount)); | |
1996 | ||
1997 | m_impl.replace(from, len, str.data, str.len); | |
1998 | return *this; | |
1999 | } | |
2000 | wxString& replace(size_t nStart, size_t nLen, | |
2001 | const wchar_t* sz, size_t nCount) | |
2002 | { | |
2003 | size_t from, len; | |
2004 | PosLenToImpl(nStart, nLen, &from, &len); | |
2005 | ||
2006 | SubstrBufFromWC str(ImplStr(sz, nCount)); | |
2007 | ||
2008 | m_impl.replace(from, len, str.data, str.len); | |
2009 | return *this; | |
2010 | } | |
2011 | wxString& replace(iterator first, iterator last, const char* s) | |
2012 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
2013 | wxString& replace(iterator first, iterator last, const wchar_t* s) | |
2014 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
2015 | wxString& replace(iterator first, iterator last, const char* s, size_type n) | |
2016 | { | |
2017 | SubstrBufFromMB str(ImplStr(s, n)); | |
2018 | m_impl.replace(first, last, str.data, str.len); | |
2019 | return *this; | |
2020 | } | |
2021 | wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n) | |
2022 | { | |
2023 | SubstrBufFromWC str(ImplStr(s, n)); | |
2024 | m_impl.replace(first, last, str.data, str.len); | |
2025 | return *this; | |
2026 | } | |
2027 | wxString& replace(iterator first, iterator last, const wxString& s) | |
2028 | { m_impl.replace(first, last, s.m_impl); return *this; } | |
2029 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch) | |
2030 | { | |
2031 | #if wxUSE_UNICODE_UTF8 | |
2032 | if ( !ch.IsAscii() ) | |
2033 | m_impl.replace(first, last, EncodeNChars(n, ch)); | |
2034 | else | |
2035 | #endif | |
2036 | m_impl.replace(first, last, n, (wxStringCharType)ch); | |
2037 | return *this; | |
2038 | } | |
2039 | wxString& replace(iterator first, iterator last, | |
2040 | const_iterator first1, const_iterator last1) | |
2041 | { m_impl.replace(first, last, first1, last1); return *this; } | |
2042 | ||
2043 | // swap two strings | |
2044 | void swap(wxString& str) | |
2045 | { m_impl.swap(str.m_impl); } | |
2046 | ||
2047 | // find a substring | |
2048 | size_t find(const wxString& str, size_t nStart = 0) const | |
2049 | { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); } | |
2050 | ||
2051 | // find first n characters of sz | |
2052 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const | |
2053 | { | |
2054 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2055 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
2056 | } | |
2057 | size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const | |
2058 | { | |
2059 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2060 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
2061 | } | |
2062 | ||
2063 | // find the first occurence of character ch after nStart | |
2064 | size_t find(wxUniChar ch, size_t nStart = 0) const | |
2065 | { return PosFromImpl(m_impl.find(EncodeChar(ch), PosToImpl(nStart))); } | |
2066 | size_t find(wxUniCharRef ch, size_t nStart = 0) const | |
2067 | { return find(wxUniChar(ch), nStart); } | |
2068 | size_t find(char ch, size_t nStart = 0) const | |
2069 | { return find(wxUniChar(ch), nStart); } | |
2070 | size_t find(wchar_t ch, size_t nStart = 0) const | |
2071 | { return find(wxUniChar(ch), nStart); } | |
2072 | ||
2073 | // rfind() family is exactly like find() but works right to left | |
2074 | ||
2075 | // as find, but from the end | |
2076 | size_t rfind(const wxString& str, size_t nStart = npos) const | |
2077 | { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); } | |
2078 | ||
2079 | // as find, but from the end | |
2080 | size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const | |
2081 | { | |
2082 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2083 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
2084 | } | |
2085 | size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const | |
2086 | { | |
2087 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2088 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
2089 | } | |
2090 | // as find, but from the end | |
2091 | size_t rfind(wxUniChar ch, size_t nStart = npos) const | |
2092 | { return PosFromImpl(m_impl.rfind(EncodeChar(ch), PosToImpl(nStart))); } | |
2093 | size_t rfind(wxUniCharRef ch, size_t nStart = npos) const | |
2094 | { return rfind(wxUniChar(ch), nStart); } | |
2095 | size_t rfind(char ch, size_t nStart = npos) const | |
2096 | { return rfind(wxUniChar(ch), nStart); } | |
2097 | size_t rfind(wchar_t ch, size_t nStart = npos) const | |
2098 | { return rfind(wxUniChar(ch), nStart); } | |
2099 | ||
2100 | // find first/last occurence of any character (not) in the set: | |
2101 | #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2102 | // FIXME-UTF8: this is not entirely correct, because it doesn't work if | |
2103 | // sizeof(wchar_t)==2 and surrogates are present in the string; | |
2104 | // should we care? Probably not. | |
2105 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
2106 | { return m_impl.find_first_of(str.impl, nStart); } | |
2107 | size_t find_first_of(const char* sz, size_t nStart = 0) const | |
2108 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
2109 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const | |
2110 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
2111 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const; | |
2112 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } | |
2113 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2114 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } | |
2115 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
2116 | { return m_impl.find_first_of((wxChar)c, nStart); } | |
2117 | ||
2118 | size_t find_last_of(const wxStringImpl& str, size_t nStart = npos) const | |
2119 | { return m_impl.find_last_of(str.impl, nStart); } | |
2120 | size_t find_last_of(const char* sz, size_t nStart = npos) const | |
2121 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
2122 | size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const | |
2123 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
2124 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const | |
2125 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
2126 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const | |
2127 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
2128 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
2129 | { return m_impl.find_last_of((wxChar)c, nStart); } | |
2130 | ||
2131 | size_t find_first_not_of(const wxStringImpl& str, size_t nStart = 0) const | |
2132 | { return m_impl.find_first_not_of(str.m_impl, nStart); } | |
2133 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const; | |
2134 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } | |
2135 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const; | |
2136 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } | |
2137 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const; | |
2138 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } | |
2139 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2140 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } | |
2141 | size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const; | |
2142 | { return m_impl.find_first_not_of((wxChar)c, nStart); } | |
2143 | ||
2144 | size_t find_last_not_of(const wxStringImpl& str, size_t nStart = npos) const | |
2145 | { return m_impl.find_last_not_of(str.m_impl, nStart); } | |
2146 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const; | |
2147 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } | |
2148 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const; | |
2149 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } | |
2150 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const; | |
2151 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } | |
2152 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2153 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } | |
2154 | size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const; | |
2155 | { return m_impl.find_last_not_of((wxChar)c, nStart); } | |
2156 | #else | |
2157 | // we can't use std::string implementation in UTF-8 build, because the | |
2158 | // character sets would be interpreted wrongly: | |
2159 | ||
2160 | // as strpbrk() but starts at nStart, returns npos if not found | |
2161 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
2162 | { return find_first_of((const wxChar*)str.c_str(), nStart); } | |
2163 | // same as above | |
2164 | size_t find_first_of(const char* sz, size_t nStart = 0) const; | |
2165 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const; | |
2166 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const; | |
2167 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2168 | // same as find(char, size_t) | |
2169 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
2170 | { return find(c, nStart); } | |
2171 | // find the last (starting from nStart) char from str in this string | |
2172 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
2173 | { return find_last_of((const wxChar*)str.c_str(), nStart); } | |
2174 | // same as above | |
2175 | size_t find_last_of (const char* sz, size_t nStart = npos) const; | |
2176 | size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const; | |
2177 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const; | |
2178 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2179 | // same as above | |
2180 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
2181 | { return rfind(c, nStart); } | |
2182 | ||
2183 | // find first/last occurence of any character not in the set | |
2184 | ||
2185 | // as strspn() (starting from nStart), returns npos on failure | |
2186 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
2187 | { return find_first_not_of((const wxChar*)str.c_str(), nStart); } | |
2188 | // same as above | |
2189 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const; | |
2190 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const; | |
2191 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const; | |
2192 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2193 | // same as above | |
2194 | size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; | |
2195 | // as strcspn() | |
2196 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const | |
2197 | { return find_last_not_of((const wxChar*)str.c_str(), nStart); } | |
2198 | // same as above | |
2199 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const; | |
2200 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const; | |
2201 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const; | |
2202 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2203 | // same as above | |
2204 | size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; | |
2205 | #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not | |
2206 | ||
2207 | // provide char/wchar_t/wxUniCharRef overloads for char-finding functions | |
2208 | // above to resolve ambiguities: | |
2209 | size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const | |
2210 | { return find_first_of(wxUniChar(ch), nStart); } | |
2211 | size_t find_first_of(char ch, size_t nStart = 0) const | |
2212 | { return find_first_of(wxUniChar(ch), nStart); } | |
2213 | size_t find_first_of(wchar_t ch, size_t nStart = 0) const | |
2214 | { return find_first_of(wxUniChar(ch), nStart); } | |
2215 | size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const | |
2216 | { return find_last_of(wxUniChar(ch), nStart); } | |
2217 | size_t find_last_of(char ch, size_t nStart = npos) const | |
2218 | { return find_last_of(wxUniChar(ch), nStart); } | |
2219 | size_t find_last_of(wchar_t ch, size_t nStart = npos) const | |
2220 | { return find_last_of(wxUniChar(ch), nStart); } | |
2221 | size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const | |
2222 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
2223 | size_t find_first_not_of(char ch, size_t nStart = 0) const | |
2224 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
2225 | size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const | |
2226 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
2227 | size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const | |
2228 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
2229 | size_t find_last_not_of(char ch, size_t nStart = npos) const | |
2230 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
2231 | size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const | |
2232 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
2233 | ||
2234 | // string += string | |
2235 | wxString& operator+=(const wxString& s) | |
2236 | { m_impl += s.m_impl; return *this; } | |
2237 | // string += C string | |
2238 | wxString& operator+=(const char *psz) | |
2239 | { m_impl += ImplStr(psz); return *this; } | |
2240 | wxString& operator+=(const wchar_t *pwz) | |
2241 | { m_impl += ImplStr(pwz); return *this; } | |
2242 | wxString& operator+=(const wxCStrData& s) | |
2243 | { m_impl += s.AsString().m_impl; return *this; } | |
2244 | // string += char | |
2245 | wxString& operator+=(wxUniChar ch) | |
2246 | { m_impl += EncodeChar(ch); return *this; } | |
2247 | wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } | |
2248 | wxString& operator+=(char ch) { return *this += wxUniChar(ch); } | |
2249 | wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); } | |
2250 | wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } | |
2251 | ||
2252 | private: | |
2253 | #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2254 | // helpers for wxStringBuffer and wxStringBufferLength | |
2255 | wxStringCharType *DoGetWriteBuf(size_t nLen) | |
2256 | { return m_impl.DoGetWriteBuf(nLen); } | |
2257 | void DoUngetWriteBuf() | |
2258 | { m_impl.DoUngetWriteBuf(); } | |
2259 | void DoUngetWriteBuf(size_t nLen) | |
2260 | { m_impl.DoUngetWriteBuf(nLen); } | |
2261 | ||
2262 | friend class WXDLLIMPEXP_BASE wxStringBuffer; | |
2263 | friend class WXDLLIMPEXP_BASE wxStringBufferLength; | |
2264 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2265 | ||
2266 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
2267 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
2268 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
2269 | #endif | |
2270 | ||
2271 | #if !wxUSE_STL_BASED_WXSTRING | |
2272 | // check string's data validity | |
2273 | bool IsValid() const { return m_impl.GetStringData()->IsValid(); } | |
2274 | #endif | |
2275 | ||
2276 | private: | |
2277 | wxStringImpl m_impl; | |
2278 | }; | |
2279 | ||
2280 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
2281 | #pragma warning (default:4275) | |
2282 | #endif | |
2283 | ||
2284 | // notice that even though for many compilers the friend declarations above are | |
2285 | // enough, from the point of view of C++ standard we must have the declarations | |
2286 | // here as friend ones are not injected in the enclosing namespace and without | |
2287 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
2288 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); | |
2289 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz); | |
2290 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz); | |
2291 | wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string); | |
2292 | wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string); | |
2293 | ||
2294 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); | |
2295 | wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
2296 | ||
2297 | inline wxString operator+(const wxString& string, wxUniCharRef ch) | |
2298 | { return string + (wxUniChar)ch; } | |
2299 | inline wxString operator+(const wxString& string, char ch) | |
2300 | { return string + wxUniChar(ch); } | |
2301 | inline wxString operator+(const wxString& string, wchar_t ch) | |
2302 | { return string + wxUniChar(ch); } | |
2303 | inline wxString operator+(wxUniCharRef ch, const wxString& string) | |
2304 | { return (wxUniChar)ch + string; } | |
2305 | inline wxString operator+(char ch, const wxString& string) | |
2306 | { return wxUniChar(ch) + string; } | |
2307 | inline wxString operator+(wchar_t ch, const wxString& string) | |
2308 | { return wxUniChar(ch) + string; } | |
2309 | ||
2310 | ||
2311 | #if wxUSE_STL_BASED_WXSTRING | |
2312 | // return an empty wxString (not very useful with wxUSE_STL == 1) | |
2313 | inline const wxString wxGetEmptyString() { return wxString(); } | |
2314 | #else // !wxUSE_STL_BASED_WXSTRING | |
2315 | // return an empty wxString (more efficient than wxString() here) | |
2316 | inline const wxString& wxGetEmptyString() | |
2317 | { | |
2318 | return *(wxString *)&wxEmptyString; | |
2319 | } | |
2320 | #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING | |
2321 | ||
2322 | // ---------------------------------------------------------------------------- | |
2323 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
2324 | // ---------------------------------------------------------------------------- | |
2325 | ||
2326 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 | |
2327 | ||
2328 | class WXDLLIMPEXP_BASE wxStringBuffer | |
2329 | { | |
2330 | public: | |
2331 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
2332 | : m_str(str), m_buf(lenWanted) | |
2333 | { } | |
2334 | ||
2335 | ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); } | |
2336 | ||
2337 | operator wxChar*() { return m_buf.data(); } | |
2338 | ||
2339 | private: | |
2340 | wxString& m_str; | |
2341 | #if wxUSE_UNICODE | |
2342 | wxWCharBuffer m_buf; | |
2343 | #else | |
2344 | wxCharBuffer m_buf; | |
2345 | #endif | |
2346 | ||
2347 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
2348 | }; | |
2349 | ||
2350 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
2351 | { | |
2352 | public: | |
2353 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2354 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
2355 | { } | |
2356 | ||
2357 | ~wxStringBufferLength() | |
2358 | { | |
2359 | wxASSERT(m_lenSet); | |
2360 | m_str.assign(m_buf.data(), m_len); | |
2361 | } | |
2362 | ||
2363 | operator wxChar*() { return m_buf.data(); } | |
2364 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2365 | ||
2366 | private: | |
2367 | wxString& m_str; | |
2368 | #if wxUSE_UNICODE | |
2369 | wxWCharBuffer m_buf; | |
2370 | #else | |
2371 | wxCharBuffer m_buf; | |
2372 | #endif | |
2373 | size_t m_len; | |
2374 | bool m_lenSet; | |
2375 | ||
2376 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2377 | }; | |
2378 | ||
2379 | #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2380 | ||
2381 | class WXDLLIMPEXP_BASE wxStringBuffer | |
2382 | { | |
2383 | public: | |
2384 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
2385 | : m_str(str), m_buf(NULL) | |
2386 | { m_buf = m_str.DoGetWriteBuf(lenWanted); } | |
2387 | ||
2388 | ~wxStringBuffer() { m_str.DoUngetWriteBuf(); } | |
2389 | ||
2390 | operator wxChar*() const { return m_buf; } | |
2391 | ||
2392 | private: | |
2393 | wxString& m_str; | |
2394 | wxChar *m_buf; | |
2395 | ||
2396 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
2397 | }; | |
2398 | ||
2399 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
2400 | { | |
2401 | public: | |
2402 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2403 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
2404 | { | |
2405 | m_buf = m_str.DoGetWriteBuf(lenWanted); | |
2406 | wxASSERT(m_buf != NULL); | |
2407 | } | |
2408 | ||
2409 | ~wxStringBufferLength() | |
2410 | { | |
2411 | wxASSERT(m_lenSet); | |
2412 | m_str.DoUngetWriteBuf(m_len); | |
2413 | } | |
2414 | ||
2415 | operator wxChar*() const { return m_buf; } | |
2416 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2417 | ||
2418 | private: | |
2419 | wxString& m_str; | |
2420 | wxChar *m_buf; | |
2421 | size_t m_len; | |
2422 | bool m_lenSet; | |
2423 | ||
2424 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2425 | }; | |
2426 | ||
2427 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2428 | ||
2429 | // --------------------------------------------------------------------------- | |
2430 | // wxString comparison functions: operator versions are always case sensitive | |
2431 | // --------------------------------------------------------------------------- | |
2432 | ||
2433 | // note that when wxUSE_STL_BASED_WXSTRING == 1 the comparison operators taking | |
2434 | // std::string are used and defining them also for wxString would only result | |
2435 | // in compilation ambiguities when comparing std::string and wxString | |
2436 | #if !wxUSE_STL_BASED_WXSTRING | |
2437 | ||
2438 | #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0 | |
2439 | ||
2440 | wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) | |
2441 | ||
2442 | #undef wxCMP_WXCHAR_STRING | |
2443 | ||
2444 | // note that there is an optimization in operator==() and !=(): we (quickly) | |
2445 | // checks the strings length first, before comparing their data | |
2446 | inline bool operator==(const wxString& s1, const wxString& s2) | |
2447 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
2448 | inline bool operator!=(const wxString& s1, const wxString& s2) | |
2449 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
2450 | inline bool operator< (const wxString& s1, const wxString& s2) | |
2451 | { return s1.Cmp(s2) < 0; } | |
2452 | inline bool operator> (const wxString& s1, const wxString& s2) | |
2453 | { return s1.Cmp(s2) > 0; } | |
2454 | inline bool operator<=(const wxString& s1, const wxString& s2) | |
2455 | { return s1.Cmp(s2) <= 0; } | |
2456 | inline bool operator>=(const wxString& s1, const wxString& s2) | |
2457 | { return s1.Cmp(s2) >= 0; } | |
2458 | ||
2459 | #if wxUSE_UNICODE | |
2460 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
2461 | { return (s1.Cmp((const wchar_t *)s2) == 0); } | |
2462 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) | |
2463 | { return (s2.Cmp((const wchar_t *)s1) == 0); } | |
2464 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) | |
2465 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
2466 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
2467 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
2468 | #else // !wxUSE_UNICODE | |
2469 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) | |
2470 | { return (s1.Cmp((const char *)s2) == 0); } | |
2471 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) | |
2472 | { return (s2.Cmp((const char *)s1) == 0); } | |
2473 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) | |
2474 | { return (s1.Cmp((const char *)s2) != 0); } | |
2475 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
2476 | { return (s2.Cmp((const char *)s1) != 0); } | |
2477 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
2478 | ||
2479 | #if wxUSE_UNICODE | |
2480 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) | |
2481 | { return string + (const wchar_t *)buf; } | |
2482 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) | |
2483 | { return (const wchar_t *)buf + string; } | |
2484 | #else // !wxUSE_UNICODE | |
2485 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) | |
2486 | { return string + (const char *)buf; } | |
2487 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) | |
2488 | { return (const char *)buf + string; } | |
2489 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
2490 | ||
2491 | #endif // !wxUSE_STL_BASED_WXSTRING | |
2492 | ||
2493 | // comparison with char (those are not defined by std::[w]string and so should | |
2494 | // be always available) | |
2495 | inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } | |
2496 | inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } | |
2497 | inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } | |
2498 | inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } | |
2499 | inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } | |
2500 | inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } | |
2501 | inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } | |
2502 | inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } | |
2503 | inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } | |
2504 | inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } | |
2505 | inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } | |
2506 | inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } | |
2507 | inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } | |
2508 | inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } | |
2509 | inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } | |
2510 | inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } | |
2511 | inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } | |
2512 | inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } | |
2513 | ||
2514 | // comparison with C string in Unicode build | |
2515 | #if wxUSE_UNICODE | |
2516 | ||
2517 | #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s | |
2518 | ||
2519 | wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) | |
2520 | ||
2521 | #undef wxCMP_CHAR_STRING | |
2522 | ||
2523 | #endif // wxUSE_UNICODE | |
2524 | ||
2525 | // we also need to provide the operators for comparison with wxCStrData to | |
2526 | // resolve ambiguity between operator(const wxChar *,const wxString &) and | |
2527 | // operator(const wxChar *, const wxChar *) for "p == s.c_str()" | |
2528 | // | |
2529 | // notice that these are (shallow) pointer comparisons, not (deep) string ones | |
2530 | #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() | |
2531 | #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() | |
2532 | ||
2533 | // FIXME: these ifdefs must be removed when wxCStrData has both conversions | |
2534 | #if wxUSE_UNICODE | |
2535 | wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) | |
2536 | #else | |
2537 | wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) | |
2538 | #endif | |
2539 | ||
2540 | #undef wxCMP_CHAR_CSTRDATA | |
2541 | #undef wxCMP_WCHAR_CSTRDATA | |
2542 | ||
2543 | // --------------------------------------------------------------------------- | |
2544 | // Implementation only from here until the end of file | |
2545 | // --------------------------------------------------------------------------- | |
2546 | ||
2547 | #if wxUSE_STD_IOSTREAM | |
2548 | ||
2549 | #include "wx/iosfwrap.h" | |
2550 | ||
2551 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); | |
2552 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); | |
2553 | ||
2554 | #endif // wxSTD_STRING_COMPATIBILITY | |
2555 | ||
2556 | // --------------------------------------------------------------------------- | |
2557 | // wxCStrData implementation | |
2558 | // --------------------------------------------------------------------------- | |
2559 | ||
2560 | inline wxCStrData::wxCStrData(char *buf) | |
2561 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2562 | inline wxCStrData::wxCStrData(wchar_t *buf) | |
2563 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2564 | ||
2565 | inline wxCStrData::~wxCStrData() | |
2566 | { | |
2567 | if ( m_owned ) | |
2568 | delete m_str; | |
2569 | } | |
2570 | ||
2571 | #if wxUSE_UNICODE | |
2572 | inline const wchar_t* wxCStrData::AsWChar() const | |
2573 | #else | |
2574 | inline const char* wxCStrData::AsChar() const | |
2575 | #endif | |
2576 | { | |
2577 | if ( m_offset == 0 ) | |
2578 | return m_str->wx_str(); // FIXME-UTF8 | |
2579 | else | |
2580 | return (const wxChar*)(m_str->begin() + m_offset); | |
2581 | } | |
2582 | ||
2583 | inline wxString wxCStrData::AsString() const | |
2584 | { | |
2585 | if ( m_offset == 0 ) | |
2586 | return *m_str; | |
2587 | else | |
2588 | return m_str->Mid(m_offset); | |
2589 | } | |
2590 | ||
2591 | inline wxCStrData::operator wxString() const { return AsString(); } | |
2592 | ||
2593 | inline wxUniChar wxCStrData::operator*() const | |
2594 | { | |
2595 | if ( m_str->empty() ) | |
2596 | return wxUniChar(_T('\0')); | |
2597 | else | |
2598 | return (*m_str)[m_offset]; | |
2599 | } | |
2600 | ||
2601 | inline wxUniChar wxCStrData::operator[](size_t n) const | |
2602 | { | |
2603 | return m_str->at(m_offset + n); | |
2604 | } | |
2605 | ||
2606 | // ---------------------------------------------------------------------------- | |
2607 | // implementation of wx[W]CharBuffer inline methods using wxCStrData | |
2608 | // ---------------------------------------------------------------------------- | |
2609 | ||
2610 | // FIXME-UTF8: move this to buffer.h; provide versions for both variants | |
2611 | inline wxWxCharBuffer::wxWxCharBuffer(const wxCStrData& cstr) | |
2612 | : wxCharTypeBufferBase((const wxChar *)cstr) | |
2613 | { | |
2614 | } | |
2615 | ||
2616 | #endif // _WX_WXSTRINGH__ |