]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/string.h | |
3 | // Purpose: wxString class | |
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_WXSTRING_H__ | |
19 | #define _WX_WXSTRING_H__ | |
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/wxcrtbase.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 | #include "wx/stringimpl.h" | |
59 | #include "wx/stringops.h" | |
60 | #include "wx/unichar.h" | |
61 | ||
62 | class WXDLLIMPEXP_FWD_BASE wxString; | |
63 | ||
64 | // unless this symbol is predefined to disable the compatibility functions, do | |
65 | // use them | |
66 | #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
67 | #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1 | |
68 | #endif | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | // macros | |
72 | // --------------------------------------------------------------------------- | |
73 | ||
74 | // casts [unfortunately!] needed to call some broken functions which require | |
75 | // "char *" instead of "const char *" | |
76 | #define WXSTRINGCAST (wxChar *)(const wxChar *) | |
77 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) | |
78 | #define wxMBSTRINGCAST (char *)(const char *) | |
79 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
80 | ||
81 | // like _T(), but for literals in wxString's internal representation, i.e. | |
82 | // char* in UTF-8 build and wxChar* otherwise: | |
83 | #if wxUSE_UNICODE_UTF8 | |
84 | #define wxSTRING_TEXT(str) str | |
85 | #else | |
86 | #define wxSTRING_TEXT(str) _T(str) | |
87 | #endif | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // constants | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | #if WXWIN_COMPATIBILITY_2_6 | |
94 | ||
95 | // deprecated in favour of wxString::npos, don't use in new code | |
96 | // | |
97 | // maximum possible length for a string means "take all string" everywhere | |
98 | #define wxSTRING_MAXLEN wxString::npos | |
99 | ||
100 | #endif // WXWIN_COMPATIBILITY_2_6 | |
101 | ||
102 | // --------------------------------------------------------------------------- | |
103 | // global functions complementing standard C string library replacements for | |
104 | // strlen() and portable strcasecmp() | |
105 | //--------------------------------------------------------------------------- | |
106 | ||
107 | #if WXWIN_COMPATIBILITY_2_8 | |
108 | // Use wxXXX() functions from wxcrt.h instead! These functions are for | |
109 | // backwards compatibility only. | |
110 | ||
111 | // checks whether the passed in pointer is NULL and if the string is empty | |
112 | wxDEPRECATED( inline bool IsEmpty(const char *p) ); | |
113 | inline bool IsEmpty(const char *p) { return (!p || !*p); } | |
114 | ||
115 | // safe version of strlen() (returns 0 if passed NULL pointer) | |
116 | wxDEPRECATED( inline size_t Strlen(const char *psz) ); | |
117 | inline size_t Strlen(const char *psz) | |
118 | { return psz ? strlen(psz) : 0; } | |
119 | ||
120 | // portable strcasecmp/_stricmp | |
121 | wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) ); | |
122 | inline int Stricmp(const char *psz1, const char *psz2) | |
123 | { | |
124 | #if defined(__VISUALC__) && defined(__WXWINCE__) | |
125 | register char c1, c2; | |
126 | do { | |
127 | c1 = tolower(*psz1++); | |
128 | c2 = tolower(*psz2++); | |
129 | } while ( c1 && (c1 == c2) ); | |
130 | ||
131 | return c1 - c2; | |
132 | #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) | |
133 | return _stricmp(psz1, psz2); | |
134 | #elif defined(__SC__) | |
135 | return _stricmp(psz1, psz2); | |
136 | #elif defined(__SALFORDC__) | |
137 | return stricmp(psz1, psz2); | |
138 | #elif defined(__BORLANDC__) | |
139 | return stricmp(psz1, psz2); | |
140 | #elif defined(__WATCOMC__) | |
141 | return stricmp(psz1, psz2); | |
142 | #elif defined(__DJGPP__) | |
143 | return stricmp(psz1, psz2); | |
144 | #elif defined(__EMX__) | |
145 | return stricmp(psz1, psz2); | |
146 | #elif defined(__WXPM__) | |
147 | return stricmp(psz1, psz2); | |
148 | #elif defined(__WXPALMOS__) || \ | |
149 | defined(HAVE_STRCASECMP_IN_STRING_H) || \ | |
150 | defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ | |
151 | defined(__GNUWIN32__) | |
152 | return strcasecmp(psz1, psz2); | |
153 | #elif defined(__MWERKS__) && !defined(__INTEL__) | |
154 | register char c1, c2; | |
155 | do { | |
156 | c1 = tolower(*psz1++); | |
157 | c2 = tolower(*psz2++); | |
158 | } while ( c1 && (c1 == c2) ); | |
159 | ||
160 | return c1 - c2; | |
161 | #else | |
162 | // almost all compilers/libraries provide this function (unfortunately under | |
163 | // different names), that's why we don't implement our own which will surely | |
164 | // be more efficient than this code (uncomment to use): | |
165 | /* | |
166 | register char c1, c2; | |
167 | do { | |
168 | c1 = tolower(*psz1++); | |
169 | c2 = tolower(*psz2++); | |
170 | } while ( c1 && (c1 == c2) ); | |
171 | ||
172 | return c1 - c2; | |
173 | */ | |
174 | ||
175 | #error "Please define string case-insensitive compare for your OS/compiler" | |
176 | #endif // OS/compiler | |
177 | } | |
178 | ||
179 | #endif // WXWIN_COMPATIBILITY_2_8 | |
180 | ||
181 | // ---------------------------------------------------------------------------- | |
182 | // wxCStrData | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
185 | // Lightweight object returned by wxString::c_str() and implicitly convertible | |
186 | // to either const char* or const wchar_t*. | |
187 | class WXDLLIMPEXP_BASE wxCStrData | |
188 | { | |
189 | private: | |
190 | // Ctors; for internal use by wxString and wxCStrData only | |
191 | wxCStrData(const wxString *str, size_t offset = 0, bool owned = false) | |
192 | : m_str(str), m_offset(offset), m_owned(owned) {} | |
193 | ||
194 | public: | |
195 | // Ctor constructs the object from char literal; they are needed to make | |
196 | // operator?: compile and they intentionally take char*, not const char* | |
197 | inline wxCStrData(char *buf); | |
198 | inline wxCStrData(wchar_t *buf); | |
199 | inline wxCStrData(const wxCStrData& data); | |
200 | ||
201 | inline ~wxCStrData(); | |
202 | ||
203 | // methods defined inline below must be declared inline or mingw32 3.4.5 | |
204 | // warns about "<symbol> defined locally after being referenced with | |
205 | // dllimport linkage" | |
206 | #if wxUSE_UNICODE_WCHAR | |
207 | inline | |
208 | #endif | |
209 | const wchar_t* AsWChar() const; | |
210 | operator const wchar_t*() const { return AsWChar(); } | |
211 | ||
212 | #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY | |
213 | inline | |
214 | #endif | |
215 | const char* AsChar() const; | |
216 | const unsigned char* AsUnsignedChar() const | |
217 | { return (const unsigned char *) AsChar(); } | |
218 | operator const char*() const { return AsChar(); } | |
219 | operator const unsigned char*() const { return AsUnsignedChar(); } | |
220 | ||
221 | operator const void*() const { return AsChar(); } | |
222 | ||
223 | inline const wxCharBuffer AsCharBuf() const; | |
224 | inline const wxWCharBuffer AsWCharBuf() const; | |
225 | ||
226 | inline wxString AsString() const; | |
227 | ||
228 | // returns the value as C string in internal representation (equivalent | |
229 | // to AsString().wx_str(), but more efficient) | |
230 | const wxStringCharType *AsInternal() const; | |
231 | ||
232 | // allow expressions like "c_str()[0]": | |
233 | inline wxUniChar operator[](size_t n) const; | |
234 | wxUniChar operator[](int n) const { return operator[](size_t(n)); } | |
235 | wxUniChar operator[](long n) const { return operator[](size_t(n)); } | |
236 | #ifndef wxSIZE_T_IS_UINT | |
237 | wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); } | |
238 | #endif // size_t != unsigned int | |
239 | ||
240 | // these operators are needed to emulate the pointer semantics of c_str(): | |
241 | // expressions like "wxChar *p = str.c_str() + 1;" should continue to work | |
242 | // (we need both versions to resolve ambiguities): | |
243 | wxCStrData operator+(int n) const | |
244 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
245 | wxCStrData operator+(long n) const | |
246 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
247 | wxCStrData operator+(size_t n) const | |
248 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
249 | ||
250 | // and these for "str.c_str() + n - 2": | |
251 | wxCStrData operator-(int n) const | |
252 | { | |
253 | wxASSERT_MSG( n <= (int)m_offset, | |
254 | _T("attempt to construct address before the beginning of the string") ); | |
255 | return wxCStrData(m_str, m_offset - n, m_owned); | |
256 | } | |
257 | wxCStrData operator-(long n) const | |
258 | { | |
259 | wxASSERT_MSG( n <= (int)m_offset, | |
260 | _T("attempt to construct address before the beginning of the string") ); | |
261 | return wxCStrData(m_str, m_offset - n, m_owned); | |
262 | } | |
263 | wxCStrData operator-(size_t n) const | |
264 | { | |
265 | wxASSERT_MSG( n <= m_offset, | |
266 | _T("attempt to construct address before the beginning of the string") ); | |
267 | return wxCStrData(m_str, m_offset - n, m_owned); | |
268 | } | |
269 | ||
270 | // this operator is needed to make expressions like "*c_str()" or | |
271 | // "*(c_str() + 2)" work | |
272 | inline wxUniChar operator*() const; | |
273 | ||
274 | private: | |
275 | const wxString *m_str; | |
276 | size_t m_offset; | |
277 | bool m_owned; | |
278 | ||
279 | friend class WXDLLIMPEXP_FWD_BASE wxString; | |
280 | }; | |
281 | ||
282 | // ---------------------------------------------------------------------------- | |
283 | // wxStringPrintfMixin | |
284 | // --------------------------------------------------------------------------- | |
285 | ||
286 | // NB: VC6 has a bug that causes linker errors if you have template methods | |
287 | // in a class using __declspec(dllimport). The solution is to split such | |
288 | // class into two classes, one that contains the template methods and does | |
289 | // *not* use WXDLLIMPEXP_BASE and another class that contains the rest | |
290 | // (with DLL linkage). | |
291 | // | |
292 | // We only do this for VC6 here, because the code is less efficient | |
293 | // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler | |
294 | // cannot compile this code. | |
295 | ||
296 | #if defined(__VISUALC__) && __VISUALC__ < 1300 | |
297 | #define wxNEEDS_WXSTRING_PRINTF_MIXIN | |
298 | #endif | |
299 | ||
300 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
301 | // this class contains implementation of wxString's vararg methods, it's | |
302 | // exported from wxBase DLL | |
303 | class WXDLLIMPEXP_BASE wxStringPrintfMixinBase | |
304 | { | |
305 | protected: | |
306 | wxStringPrintfMixinBase() {} | |
307 | ||
308 | #if !wxUSE_UTF8_LOCALE_ONLY | |
309 | int DoPrintfWchar(const wxChar *format, ...); | |
310 | static wxString DoFormatWchar(const wxChar *format, ...); | |
311 | #endif | |
312 | #if wxUSE_UNICODE_UTF8 | |
313 | int DoPrintfUtf8(const char *format, ...); | |
314 | static wxString DoFormatUtf8(const char *format, ...); | |
315 | #endif | |
316 | }; | |
317 | ||
318 | // this class contains template wrappers for wxString's vararg methods, it's | |
319 | // intentionally *not* exported from the DLL in order to fix the VC6 bug | |
320 | // described above | |
321 | class wxStringPrintfMixin : public wxStringPrintfMixinBase | |
322 | { | |
323 | private: | |
324 | // to further complicate things, we can't return wxString from | |
325 | // wxStringPrintfMixin::Format() because wxString is not yet declared at | |
326 | // this point; the solution is to use this fake type trait template - this | |
327 | // way the compiler won't know the return type until Format() is used | |
328 | // (this doesn't compile with Watcom, but VC6 compiles it just fine): | |
329 | template<typename T> struct StringReturnType | |
330 | { | |
331 | typedef wxString type; | |
332 | }; | |
333 | ||
334 | public: | |
335 | // these are duplicated wxString methods, they're also declared below | |
336 | // if !wxNEEDS_WXSTRING_PRINTF_MIXIN: | |
337 | ||
338 | // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1; | |
339 | WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type, | |
340 | Format, 1, (const wxFormatString&), | |
341 | DoFormatWchar, DoFormatUtf8) | |
342 | // We have to implement the version without template arguments manually | |
343 | // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC | |
344 | // normally does it itself. It has to be a template so that we can use | |
345 | // the hack, even though there's no real template parameter. We can't move | |
346 | // it to wxStrig, because it would shadow these versions of Format() then. | |
347 | template<typename T> | |
348 | inline static typename StringReturnType<T>::type | |
349 | Format(const T& fmt) | |
350 | { | |
351 | // NB: this doesn't compile if T is not (some form of) a string; | |
352 | // this makes Format's prototype equivalent to | |
353 | // Format(const wxFormatString& fmt) | |
354 | return DoFormatWchar(wxFormatString(fmt)); | |
355 | } | |
356 | ||
357 | // int Printf(const wxString& format, ...); | |
358 | WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&), | |
359 | DoPrintfWchar, DoPrintfUtf8) | |
360 | // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2; | |
361 | WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&), | |
362 | DoPrintfWchar, DoPrintfUtf8) | |
363 | ||
364 | protected: | |
365 | wxStringPrintfMixin() : wxStringPrintfMixinBase() {} | |
366 | }; | |
367 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
368 | ||
369 | ||
370 | // ---------------------------------------------------------------------------- | |
371 | // wxString: string class trying to be compatible with std::string, MFC | |
372 | // CString and wxWindows 1.x wxString all at once | |
373 | // --------------------------------------------------------------------------- | |
374 | ||
375 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
376 | // "non dll-interface class 'wxStringPrintfMixin' used as base interface | |
377 | // for dll-interface class 'wxString'" -- this is OK in our case | |
378 | #pragma warning (disable:4275) | |
379 | #endif | |
380 | ||
381 | #if wxUSE_UNICODE_UTF8 | |
382 | // see the comment near wxString::iterator for why we need this | |
383 | class WXDLLIMPEXP_BASE wxStringIteratorNode | |
384 | { | |
385 | public: | |
386 | wxStringIteratorNode() | |
387 | : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {} | |
388 | wxStringIteratorNode(const wxString *str, | |
389 | wxStringImpl::const_iterator *citer) | |
390 | { DoSet(str, citer, NULL); } | |
391 | wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter) | |
392 | { DoSet(str, NULL, iter); } | |
393 | ~wxStringIteratorNode() | |
394 | { clear(); } | |
395 | ||
396 | inline void set(const wxString *str, wxStringImpl::const_iterator *citer) | |
397 | { clear(); DoSet(str, citer, NULL); } | |
398 | inline void set(const wxString *str, wxStringImpl::iterator *iter) | |
399 | { clear(); DoSet(str, NULL, iter); } | |
400 | ||
401 | const wxString *m_str; | |
402 | wxStringImpl::const_iterator *m_citer; | |
403 | wxStringImpl::iterator *m_iter; | |
404 | wxStringIteratorNode *m_prev, *m_next; | |
405 | ||
406 | private: | |
407 | inline void clear(); | |
408 | inline void DoSet(const wxString *str, | |
409 | wxStringImpl::const_iterator *citer, | |
410 | wxStringImpl::iterator *iter); | |
411 | ||
412 | // the node belongs to a particular iterator instance, it's not copied | |
413 | // when a copy of the iterator is made | |
414 | DECLARE_NO_COPY_CLASS(wxStringIteratorNode) | |
415 | }; | |
416 | #endif // wxUSE_UNICODE_UTF8 | |
417 | ||
418 | class WXDLLIMPEXP_BASE wxString | |
419 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
420 | : public wxStringPrintfMixin | |
421 | #endif | |
422 | { | |
423 | // NB: special care was taken in arranging the member functions in such order | |
424 | // that all inline functions can be effectively inlined, verify that all | |
425 | // performance critical functions are still inlined if you change order! | |
426 | public: | |
427 | // an 'invalid' value for string index, moved to this place due to a CW bug | |
428 | static const size_t npos; | |
429 | ||
430 | private: | |
431 | // if we hadn't made these operators private, it would be possible to | |
432 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
433 | // converted to char in C and we do have operator=(char) | |
434 | // | |
435 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
436 | // to assign another numeric type to wxString will now result in | |
437 | // ambiguity between operator=(char) and operator=(int) | |
438 | wxString& operator=(int); | |
439 | ||
440 | // these methods are not implemented - there is _no_ conversion from int to | |
441 | // string, you're doing something wrong if the compiler wants to call it! | |
442 | // | |
443 | // try `s << i' or `s.Printf("%d", i)' instead | |
444 | wxString(int); | |
445 | ||
446 | ||
447 | // buffer for holding temporary substring when using any of the methods | |
448 | // that take (char*,size_t) or (wchar_t*,size_t) arguments: | |
449 | template<typename T> | |
450 | struct SubstrBufFromType | |
451 | { | |
452 | T data; | |
453 | size_t len; | |
454 | ||
455 | SubstrBufFromType(const T& data_, size_t len_) | |
456 | : data(data_), len(len_) {} | |
457 | }; | |
458 | ||
459 | #if wxUSE_UNICODE_UTF8 | |
460 | // even char* -> char* needs conversion, from locale charset to UTF-8 | |
461 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC; | |
462 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB; | |
463 | #elif wxUSE_UNICODE_WCHAR | |
464 | typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC; | |
465 | typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB; | |
466 | #else | |
467 | typedef SubstrBufFromType<const char*> SubstrBufFromMB; | |
468 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC; | |
469 | #endif | |
470 | ||
471 | ||
472 | // Functions implementing primitive operations on string data; wxString | |
473 | // methods and iterators are implemented in terms of it. The differences | |
474 | // between UTF-8 and wchar_t* representations of the string are mostly | |
475 | // contained here. | |
476 | ||
477 | #if wxUSE_UNICODE_UTF8 | |
478 | static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength, | |
479 | const wxMBConv& conv); | |
480 | static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength, | |
481 | const wxMBConv& conv); | |
482 | #elif wxUSE_UNICODE_WCHAR | |
483 | static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength, | |
484 | const wxMBConv& conv); | |
485 | #else | |
486 | static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength, | |
487 | const wxMBConv& conv); | |
488 | #endif | |
489 | ||
490 | #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE | |
491 | // returns C string encoded as the implementation expects: | |
492 | #if wxUSE_UNICODE | |
493 | static const wchar_t* ImplStr(const wchar_t* str) | |
494 | { return str ? str : wxT(""); } | |
495 | static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
496 | { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); } | |
497 | static wxWCharBuffer ImplStr(const char* str, | |
498 | const wxMBConv& conv = wxConvLibc) | |
499 | { return ConvertStr(str, npos, conv).data; } | |
500 | static SubstrBufFromMB ImplStr(const char* str, size_t n, | |
501 | const wxMBConv& conv = wxConvLibc) | |
502 | { return ConvertStr(str, n, conv); } | |
503 | #else | |
504 | static const char* ImplStr(const char* str, | |
505 | const wxMBConv& WXUNUSED(conv) = wxConvLibc) | |
506 | { return str ? str : ""; } | |
507 | static const SubstrBufFromMB ImplStr(const char* str, size_t n, | |
508 | const wxMBConv& WXUNUSED(conv) = wxConvLibc) | |
509 | { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); } | |
510 | static wxCharBuffer ImplStr(const wchar_t* str) | |
511 | { return ConvertStr(str, npos, wxConvLibc).data; } | |
512 | static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
513 | { return ConvertStr(str, n, wxConvLibc); } | |
514 | #endif | |
515 | ||
516 | // translates position index in wxString to/from index in underlying | |
517 | // wxStringImpl: | |
518 | static size_t PosToImpl(size_t pos) { return pos; } | |
519 | static void PosLenToImpl(size_t pos, size_t len, | |
520 | size_t *implPos, size_t *implLen) | |
521 | { *implPos = pos; *implLen = len; } | |
522 | static size_t LenToImpl(size_t len) { return len; } | |
523 | static size_t PosFromImpl(size_t pos) { return pos; } | |
524 | ||
525 | #else // wxUSE_UNICODE_UTF8 | |
526 | ||
527 | static wxCharBuffer ImplStr(const char* str, | |
528 | const wxMBConv& conv = wxConvLibc) | |
529 | { return ConvertStr(str, npos, conv).data; } | |
530 | static SubstrBufFromMB ImplStr(const char* str, size_t n, | |
531 | const wxMBConv& conv = wxConvLibc) | |
532 | { return ConvertStr(str, n, conv); } | |
533 | ||
534 | static wxCharBuffer ImplStr(const wchar_t* str) | |
535 | { return ConvertStr(str, npos, wxMBConvUTF8()).data; } | |
536 | static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
537 | { return ConvertStr(str, n, wxMBConvUTF8()); } | |
538 | ||
539 | size_t PosToImpl(size_t pos) const | |
540 | { | |
541 | if ( pos == 0 || pos == npos ) | |
542 | return pos; | |
543 | else | |
544 | return (begin() + pos).impl() - m_impl.begin(); | |
545 | } | |
546 | ||
547 | void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const; | |
548 | ||
549 | size_t LenToImpl(size_t len) const | |
550 | { | |
551 | size_t pos, len2; | |
552 | PosLenToImpl(0, len, &pos, &len2); | |
553 | return len2; | |
554 | } | |
555 | ||
556 | size_t PosFromImpl(size_t pos) const | |
557 | { | |
558 | if ( pos == 0 || pos == npos ) | |
559 | return pos; | |
560 | else | |
561 | return const_iterator(this, m_impl.begin() + pos) - begin(); | |
562 | } | |
563 | #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8 | |
564 | ||
565 | public: | |
566 | // standard types | |
567 | typedef wxUniChar value_type; | |
568 | typedef wxUniChar char_type; | |
569 | typedef wxUniCharRef reference; | |
570 | typedef wxChar* pointer; | |
571 | typedef const wxChar* const_pointer; | |
572 | ||
573 | typedef size_t size_type; | |
574 | typedef wxUniChar const_reference; | |
575 | ||
576 | #if wxUSE_STL | |
577 | #if wxUSE_UNICODE_UTF8 | |
578 | // random access is not O(1), as required by Random Access Iterator | |
579 | #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag | |
580 | #else | |
581 | #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag | |
582 | #endif | |
583 | #else | |
584 | #define WX_STR_ITERATOR_TAG void /* dummy type */ | |
585 | #endif | |
586 | ||
587 | #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \ | |
588 | private: \ | |
589 | typedef wxStringImpl::iterator_name underlying_iterator; \ | |
590 | public: \ | |
591 | typedef WX_STR_ITERATOR_TAG iterator_category; \ | |
592 | typedef wxUniChar value_type; \ | |
593 | typedef int difference_type; \ | |
594 | typedef reference_type reference; \ | |
595 | typedef pointer_type pointer; \ | |
596 | \ | |
597 | reference operator[](size_t n) const { return *(*this + n); } \ | |
598 | \ | |
599 | iterator_name& operator++() \ | |
600 | { wxStringOperations::IncIter(m_cur); return *this; } \ | |
601 | iterator_name& operator--() \ | |
602 | { wxStringOperations::DecIter(m_cur); return *this; } \ | |
603 | iterator_name operator++(int) \ | |
604 | { \ | |
605 | iterator_name tmp = *this; \ | |
606 | wxStringOperations::IncIter(m_cur); \ | |
607 | return tmp; \ | |
608 | } \ | |
609 | iterator_name operator--(int) \ | |
610 | { \ | |
611 | iterator_name tmp = *this; \ | |
612 | wxStringOperations::DecIter(m_cur); \ | |
613 | return tmp; \ | |
614 | } \ | |
615 | \ | |
616 | iterator_name& operator+=(int n) \ | |
617 | { \ | |
618 | m_cur = wxStringOperations::AddToIter(m_cur, n); \ | |
619 | return *this; \ | |
620 | } \ | |
621 | iterator_name& operator+=(size_t n) \ | |
622 | { \ | |
623 | m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \ | |
624 | return *this; \ | |
625 | } \ | |
626 | iterator_name& operator-=(int n) \ | |
627 | { \ | |
628 | m_cur = wxStringOperations::AddToIter(m_cur, -n); \ | |
629 | return *this; \ | |
630 | } \ | |
631 | iterator_name& operator-=(size_t n) \ | |
632 | { \ | |
633 | m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \ | |
634 | return *this; \ | |
635 | } \ | |
636 | \ | |
637 | difference_type operator-(const iterator_name& i) const \ | |
638 | { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \ | |
639 | \ | |
640 | bool operator==(const iterator_name& i) const \ | |
641 | { return m_cur == i.m_cur; } \ | |
642 | bool operator!=(const iterator_name& i) const \ | |
643 | { return m_cur != i.m_cur; } \ | |
644 | \ | |
645 | bool operator<(const iterator_name& i) const \ | |
646 | { return m_cur < i.m_cur; } \ | |
647 | bool operator>(const iterator_name& i) const \ | |
648 | { return m_cur > i.m_cur; } \ | |
649 | bool operator<=(const iterator_name& i) const \ | |
650 | { return m_cur <= i.m_cur; } \ | |
651 | bool operator>=(const iterator_name& i) const \ | |
652 | { return m_cur >= i.m_cur; } \ | |
653 | \ | |
654 | private: \ | |
655 | /* for internal wxString use only: */ \ | |
656 | underlying_iterator impl() const { return m_cur; } \ | |
657 | \ | |
658 | friend class wxString; \ | |
659 | friend class wxCStrData; \ | |
660 | \ | |
661 | private: \ | |
662 | underlying_iterator m_cur | |
663 | ||
664 | class WXDLLIMPEXP_FWD_BASE const_iterator; | |
665 | ||
666 | #if wxUSE_UNICODE_UTF8 | |
667 | // NB: In UTF-8 build, (non-const) iterator needs to keep reference | |
668 | // to the underlying wxStringImpl, because UTF-8 is variable-length | |
669 | // encoding and changing the value pointer to by an iterator (using | |
670 | // its operator*) requires calling wxStringImpl::replace() if the old | |
671 | // and new values differ in their encoding's length. | |
672 | // | |
673 | // Furthermore, the replace() call may invalid all iterators for the | |
674 | // string, so we have to keep track of outstanding iterators and update | |
675 | // them if replace() happens. | |
676 | // | |
677 | // This is implemented by maintaining linked list of iterators for every | |
678 | // string and traversing it in wxUniCharRef::operator=(). Head of the | |
679 | // list is stored in wxString. (FIXME-UTF8) | |
680 | ||
681 | class WXDLLIMPEXP_BASE iterator | |
682 | { | |
683 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); | |
684 | ||
685 | public: | |
686 | iterator() {} | |
687 | iterator(const iterator& i) | |
688 | : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} | |
689 | iterator& operator=(const iterator& i) | |
690 | { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } | |
691 | ||
692 | reference operator*() | |
693 | { return wxUniCharRef::CreateForString(m_node, m_cur); } | |
694 | ||
695 | iterator operator+(int n) const | |
696 | { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } | |
697 | iterator operator+(size_t n) const | |
698 | { return iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); } | |
699 | iterator operator-(int n) const | |
700 | { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } | |
701 | iterator operator-(size_t n) const | |
702 | { return iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); } | |
703 | ||
704 | private: | |
705 | iterator(wxString *str, underlying_iterator ptr) | |
706 | : m_cur(ptr), m_node(str, &m_cur) {} | |
707 | ||
708 | wxString* str() const { return wx_const_cast(wxString*, m_node.m_str); } | |
709 | ||
710 | wxStringIteratorNode m_node; | |
711 | ||
712 | friend class const_iterator; | |
713 | }; | |
714 | ||
715 | class WXDLLIMPEXP_BASE const_iterator | |
716 | { | |
717 | // NB: reference_type is intentionally value, not reference, the character | |
718 | // may be encoded differently in wxString data: | |
719 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); | |
720 | ||
721 | public: | |
722 | const_iterator() {} | |
723 | const_iterator(const const_iterator& i) | |
724 | : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} | |
725 | const_iterator(const iterator& i) | |
726 | : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} | |
727 | ||
728 | const_iterator& operator=(const const_iterator& i) | |
729 | { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } | |
730 | const_iterator& operator=(const iterator& i) | |
731 | { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } | |
732 | ||
733 | reference operator*() const | |
734 | { return wxStringOperations::DecodeChar(m_cur); } | |
735 | ||
736 | const_iterator operator+(int n) const | |
737 | { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } | |
738 | const_iterator operator+(size_t n) const | |
739 | { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); } | |
740 | const_iterator operator-(int n) const | |
741 | { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } | |
742 | const_iterator operator-(size_t n) const | |
743 | { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); } | |
744 | ||
745 | private: | |
746 | // for internal wxString use only: | |
747 | const_iterator(const wxString *str, underlying_iterator ptr) | |
748 | : m_cur(ptr), m_node(str, &m_cur) {} | |
749 | ||
750 | const wxString* str() const { return m_node.m_str; } | |
751 | ||
752 | wxStringIteratorNode m_node; | |
753 | }; | |
754 | ||
755 | size_t IterToImplPos(wxString::iterator i) const | |
756 | { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); } | |
757 | ||
758 | #else // !wxUSE_UNICODE_UTF8 | |
759 | ||
760 | class WXDLLIMPEXP_BASE iterator | |
761 | { | |
762 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); | |
763 | ||
764 | public: | |
765 | iterator() {} | |
766 | iterator(const iterator& i) : m_cur(i.m_cur) {} | |
767 | ||
768 | reference operator*() | |
769 | { return wxUniCharRef::CreateForString(m_cur); } | |
770 | ||
771 | iterator operator+(int n) const | |
772 | { return iterator(wxStringOperations::AddToIter(m_cur, n)); } | |
773 | iterator operator+(size_t n) const | |
774 | { return iterator(wxStringOperations::AddToIter(m_cur, (int)n)); } | |
775 | iterator operator-(int n) const | |
776 | { return iterator(wxStringOperations::AddToIter(m_cur, -n)); } | |
777 | iterator operator-(size_t n) const | |
778 | { return iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); } | |
779 | ||
780 | private: | |
781 | // for internal wxString use only: | |
782 | iterator(underlying_iterator ptr) : m_cur(ptr) {} | |
783 | iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {} | |
784 | ||
785 | friend class const_iterator; | |
786 | }; | |
787 | ||
788 | class WXDLLIMPEXP_BASE const_iterator | |
789 | { | |
790 | // NB: reference_type is intentionally value, not reference, the character | |
791 | // may be encoded differently in wxString data: | |
792 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); | |
793 | ||
794 | public: | |
795 | const_iterator() {} | |
796 | const_iterator(const const_iterator& i) : m_cur(i.m_cur) {} | |
797 | const_iterator(const iterator& i) : m_cur(i.m_cur) {} | |
798 | ||
799 | reference operator*() const | |
800 | { return wxStringOperations::DecodeChar(m_cur); } | |
801 | ||
802 | const_iterator operator+(int n) const | |
803 | { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); } | |
804 | const_iterator operator+(size_t n) const | |
805 | { return const_iterator(wxStringOperations::AddToIter(m_cur, (int)n)); } | |
806 | const_iterator operator-(int n) const | |
807 | { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); } | |
808 | const_iterator operator-(size_t n) const | |
809 | { return const_iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); } | |
810 | ||
811 | private: | |
812 | // for internal wxString use only: | |
813 | const_iterator(underlying_iterator ptr) : m_cur(ptr) {} | |
814 | const_iterator(const wxString *WXUNUSED(str), underlying_iterator ptr) | |
815 | : m_cur(ptr) {} | |
816 | }; | |
817 | #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8 | |
818 | ||
819 | #undef WX_STR_ITERATOR_TAG | |
820 | #undef WX_STR_ITERATOR_IMPL | |
821 | ||
822 | friend class iterator; | |
823 | friend class const_iterator; | |
824 | ||
825 | template <typename T> | |
826 | class reverse_iterator_impl | |
827 | { | |
828 | public: | |
829 | typedef T iterator_type; | |
830 | ||
831 | typedef typename T::iterator_category iterator_category; | |
832 | typedef typename T::value_type value_type; | |
833 | typedef typename T::difference_type difference_type; | |
834 | typedef typename T::reference reference; | |
835 | typedef typename T::pointer *pointer; | |
836 | ||
837 | reverse_iterator_impl() {} | |
838 | reverse_iterator_impl(iterator_type i) : m_cur(i) {} | |
839 | reverse_iterator_impl(const reverse_iterator_impl& ri) | |
840 | : m_cur(ri.m_cur) {} | |
841 | ||
842 | iterator_type base() const { return m_cur; } | |
843 | ||
844 | reference operator*() const { return *(m_cur-1); } | |
845 | reference operator[](size_t n) const { return *(*this + n); } | |
846 | ||
847 | reverse_iterator_impl& operator++() | |
848 | { --m_cur; return *this; } | |
849 | reverse_iterator_impl operator++(int) | |
850 | { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } | |
851 | reverse_iterator_impl& operator--() | |
852 | { ++m_cur; return *this; } | |
853 | reverse_iterator_impl operator--(int) | |
854 | { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } | |
855 | ||
856 | // NB: explicit <T> in the functions below is to keep BCC 5.5 happy | |
857 | reverse_iterator_impl operator+(int n) const | |
858 | { return reverse_iterator_impl<T>(m_cur - n); } | |
859 | reverse_iterator_impl operator+(size_t n) const | |
860 | { return reverse_iterator_impl<T>(m_cur - n); } | |
861 | reverse_iterator_impl operator-(int n) const | |
862 | { return reverse_iterator_impl<T>(m_cur + n); } | |
863 | reverse_iterator_impl operator-(size_t n) const | |
864 | { return reverse_iterator_impl<T>(m_cur + n); } | |
865 | reverse_iterator_impl operator+=(int n) | |
866 | { m_cur -= n; return *this; } | |
867 | reverse_iterator_impl operator+=(size_t n) | |
868 | { m_cur -= n; return *this; } | |
869 | reverse_iterator_impl operator-=(int n) | |
870 | { m_cur += n; return *this; } | |
871 | reverse_iterator_impl operator-=(size_t n) | |
872 | { m_cur += n; return *this; } | |
873 | ||
874 | unsigned operator-(const reverse_iterator_impl& i) const | |
875 | { return i.m_cur - m_cur; } | |
876 | ||
877 | bool operator==(const reverse_iterator_impl& ri) const | |
878 | { return m_cur == ri.m_cur; } | |
879 | bool operator!=(const reverse_iterator_impl& ri) const | |
880 | { return !(*this == ri); } | |
881 | ||
882 | bool operator<(const reverse_iterator_impl& i) const | |
883 | { return m_cur > i.m_cur; } | |
884 | bool operator>(const reverse_iterator_impl& i) const | |
885 | { return m_cur < i.m_cur; } | |
886 | bool operator<=(const reverse_iterator_impl& i) const | |
887 | { return m_cur >= i.m_cur; } | |
888 | bool operator>=(const reverse_iterator_impl& i) const | |
889 | { return m_cur <= i.m_cur; } | |
890 | ||
891 | private: | |
892 | iterator_type m_cur; | |
893 | }; | |
894 | ||
895 | typedef reverse_iterator_impl<iterator> reverse_iterator; | |
896 | typedef reverse_iterator_impl<const_iterator> const_reverse_iterator; | |
897 | ||
898 | private: | |
899 | // used to transform an expression built using c_str() (and hence of type | |
900 | // wxCStrData) to an iterator into the string | |
901 | static const_iterator CreateConstIterator(const wxCStrData& data) | |
902 | { | |
903 | return const_iterator(data.m_str, | |
904 | (data.m_str->begin() + data.m_offset).impl()); | |
905 | } | |
906 | ||
907 | // in UTF-8 STL build, creation from std::string requires conversion under | |
908 | // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor; | |
909 | // instead we define dummy type that lets us have wxString ctor for creation | |
910 | // from wxStringImpl that couldn't be used by user code (in all other builds, | |
911 | // "standard" ctors can be used): | |
912 | #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING | |
913 | struct CtorFromStringImplTag {}; | |
914 | ||
915 | wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src) | |
916 | : m_impl(src) {} | |
917 | ||
918 | static wxString FromImpl(const wxStringImpl& src) | |
919 | { return wxString((CtorFromStringImplTag*)NULL, src); } | |
920 | #else | |
921 | #if !wxUSE_STL_BASED_WXSTRING | |
922 | wxString(const wxStringImpl& src) : m_impl(src) { } | |
923 | // else: already defined as wxString(wxStdString) below | |
924 | #endif | |
925 | static wxString FromImpl(const wxStringImpl& src) { return wxString(src); } | |
926 | #endif | |
927 | ||
928 | public: | |
929 | // constructors and destructor | |
930 | // ctor for an empty string | |
931 | wxString() {} | |
932 | ||
933 | // copy ctor | |
934 | wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { } | |
935 | ||
936 | // string containing nRepeat copies of ch | |
937 | wxString(wxUniChar ch, size_t nRepeat = 1 ) | |
938 | { assign(nRepeat, ch); } | |
939 | wxString(size_t nRepeat, wxUniChar ch) | |
940 | { assign(nRepeat, ch); } | |
941 | wxString(wxUniCharRef ch, size_t nRepeat = 1) | |
942 | { assign(nRepeat, ch); } | |
943 | wxString(size_t nRepeat, wxUniCharRef ch) | |
944 | { assign(nRepeat, ch); } | |
945 | wxString(char ch, size_t nRepeat = 1) | |
946 | { assign(nRepeat, ch); } | |
947 | wxString(size_t nRepeat, char ch) | |
948 | { assign(nRepeat, ch); } | |
949 | wxString(wchar_t ch, size_t nRepeat = 1) | |
950 | { assign(nRepeat, ch); } | |
951 | wxString(size_t nRepeat, wchar_t ch) | |
952 | { assign(nRepeat, ch); } | |
953 | ||
954 | // ctors from char* strings: | |
955 | wxString(const char *psz) | |
956 | : m_impl(ImplStr(psz)) {} | |
957 | wxString(const char *psz, const wxMBConv& conv) | |
958 | : m_impl(ImplStr(psz, conv)) {} | |
959 | wxString(const char *psz, size_t nLength) | |
960 | { assign(psz, nLength); } | |
961 | wxString(const char *psz, const wxMBConv& conv, size_t nLength) | |
962 | { | |
963 | SubstrBufFromMB str(ImplStr(psz, nLength, conv)); | |
964 | m_impl.assign(str.data, str.len); | |
965 | } | |
966 | ||
967 | // and unsigned char*: | |
968 | wxString(const unsigned char *psz) | |
969 | : m_impl(ImplStr((const char*)psz)) {} | |
970 | wxString(const unsigned char *psz, const wxMBConv& conv) | |
971 | : m_impl(ImplStr((const char*)psz, conv)) {} | |
972 | wxString(const unsigned char *psz, size_t nLength) | |
973 | { assign((const char*)psz, nLength); } | |
974 | wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength) | |
975 | { | |
976 | SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv)); | |
977 | m_impl.assign(str.data, str.len); | |
978 | } | |
979 | ||
980 | // ctors from wchar_t* strings: | |
981 | wxString(const wchar_t *pwz) | |
982 | : m_impl(ImplStr(pwz)) {} | |
983 | wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv)) | |
984 | : m_impl(ImplStr(pwz)) {} | |
985 | wxString(const wchar_t *pwz, size_t nLength) | |
986 | { assign(pwz, nLength); } | |
987 | wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength) | |
988 | { assign(pwz, nLength); } | |
989 | ||
990 | wxString(const wxCharBuffer& buf) | |
991 | { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length | |
992 | wxString(const wxWCharBuffer& buf) | |
993 | { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length | |
994 | ||
995 | wxString(const wxCStrData& cstr) | |
996 | : m_impl(cstr.AsString().m_impl) { } | |
997 | ||
998 | // as we provide both ctors with this signature for both char and unsigned | |
999 | // char string, we need to provide one for wxCStrData to resolve ambiguity | |
1000 | wxString(const wxCStrData& cstr, size_t nLength) | |
1001 | : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {} | |
1002 | ||
1003 | // and because wxString is convertible to wxCStrData and const wxChar * | |
1004 | // we also need to provide this one | |
1005 | wxString(const wxString& str, size_t nLength) | |
1006 | { assign(str, nLength); } | |
1007 | ||
1008 | // even if we're not built with wxUSE_STL == 1 it is very convenient to allow | |
1009 | // implicit conversions from std::string to wxString and vice verse as this | |
1010 | // allows to use the same strings in non-GUI and GUI code, however we don't | |
1011 | // want to unconditionally add this ctor as it would make wx lib dependent on | |
1012 | // libstdc++ on some Linux versions which is bad, so instead we ask the | |
1013 | // client code to define this wxUSE_STD_STRING symbol if they need it | |
1014 | #if wxUSE_STD_STRING | |
1015 | #if wxUSE_UNICODE_WCHAR | |
1016 | wxString(const wxStdWideString& str) : m_impl(str) {} | |
1017 | #else // UTF-8 or ANSI | |
1018 | wxString(const wxStdWideString& str) | |
1019 | { assign(str.c_str(), str.length()); } | |
1020 | #endif | |
1021 | ||
1022 | #if !wxUSE_UNICODE // ANSI build | |
1023 | // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too | |
1024 | wxString(const std::string& str) : m_impl(str) {} | |
1025 | #else // Unicode | |
1026 | wxString(const std::string& str) | |
1027 | { assign(str.c_str(), str.length()); } | |
1028 | #endif | |
1029 | #endif // wxUSE_STD_STRING | |
1030 | ||
1031 | // Unlike ctor from std::string, we provide conversion to std::string only | |
1032 | // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default), | |
1033 | // because it conflicts with operator const char/wchar_t*: | |
1034 | #if wxUSE_STL | |
1035 | #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING | |
1036 | // wxStringImpl is std::string in the encoding we want | |
1037 | operator const wxStdWideString&() const { return m_impl; } | |
1038 | #else | |
1039 | // wxStringImpl is either not std::string or needs conversion | |
1040 | operator wxStdWideString() const | |
1041 | // FIXME-UTF8: broken for embedded NULs | |
1042 | { return wxStdWideString(wc_str()); } | |
1043 | #endif | |
1044 | ||
1045 | #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING | |
1046 | // wxStringImpl is std::string in the encoding we want | |
1047 | operator const std::string&() const { return m_impl; } | |
1048 | #else | |
1049 | // wxStringImpl is either not std::string or needs conversion | |
1050 | operator std::string() const | |
1051 | // FIXME-UTF8: broken for embedded NULs | |
1052 | { return std::string(mb_str()); } | |
1053 | #endif | |
1054 | #endif // wxUSE_STL | |
1055 | ||
1056 | // first valid index position | |
1057 | const_iterator begin() const { return const_iterator(this, m_impl.begin()); } | |
1058 | iterator begin() { return iterator(this, m_impl.begin()); } | |
1059 | // position one after the last valid one | |
1060 | const_iterator end() const { return const_iterator(this, m_impl.end()); } | |
1061 | iterator end() { return iterator(this, m_impl.end()); } | |
1062 | ||
1063 | // first element of the reversed string | |
1064 | const_reverse_iterator rbegin() const | |
1065 | { return const_reverse_iterator(end()); } | |
1066 | reverse_iterator rbegin() | |
1067 | { return reverse_iterator(end()); } | |
1068 | // one beyond the end of the reversed string | |
1069 | const_reverse_iterator rend() const | |
1070 | { return const_reverse_iterator(begin()); } | |
1071 | reverse_iterator rend() | |
1072 | { return reverse_iterator(begin()); } | |
1073 | ||
1074 | // std::string methods: | |
1075 | #if wxUSE_UNICODE_UTF8 | |
1076 | size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize! | |
1077 | #else | |
1078 | size_t length() const { return m_impl.length(); } | |
1079 | #endif | |
1080 | ||
1081 | size_type size() const { return length(); } | |
1082 | size_type max_size() const { return npos; } | |
1083 | ||
1084 | bool empty() const { return m_impl.empty(); } | |
1085 | ||
1086 | size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8 | |
1087 | void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8 | |
1088 | ||
1089 | void resize(size_t nSize, wxUniChar ch = wxT('\0')) | |
1090 | { | |
1091 | const size_t len = length(); | |
1092 | if ( nSize == len) | |
1093 | return; | |
1094 | ||
1095 | #if wxUSE_UNICODE_UTF8 | |
1096 | if ( nSize < len ) | |
1097 | { | |
1098 | // we can't use wxStringImpl::resize() for truncating the string as it | |
1099 | // counts in bytes, not characters | |
1100 | erase(nSize); | |
1101 | return; | |
1102 | } | |
1103 | ||
1104 | // we also can't use (presumably more efficient) resize() if we have to | |
1105 | // append characters taking more than one byte | |
1106 | if ( !ch.IsAscii() ) | |
1107 | append(nSize - len, ch); | |
1108 | else | |
1109 | #endif // wxUSE_UNICODE_UTF8 | |
1110 | m_impl.resize(nSize, (wxStringCharType)ch); | |
1111 | } | |
1112 | ||
1113 | wxString substr(size_t nStart = 0, size_t nLen = npos) const | |
1114 | { | |
1115 | size_t pos, len; | |
1116 | PosLenToImpl(nStart, nLen, &pos, &len); | |
1117 | return FromImpl(m_impl.substr(pos, len)); | |
1118 | } | |
1119 | ||
1120 | // generic attributes & operations | |
1121 | // as standard strlen() | |
1122 | size_t Len() const { return length(); } | |
1123 | // string contains any characters? | |
1124 | bool IsEmpty() const { return empty(); } | |
1125 | // empty string is "false", so !str will return true | |
1126 | bool operator!() const { return empty(); } | |
1127 | // truncate the string to given length | |
1128 | wxString& Truncate(size_t uiLen); | |
1129 | // empty string contents | |
1130 | void Empty() | |
1131 | { | |
1132 | Truncate(0); | |
1133 | ||
1134 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); | |
1135 | } | |
1136 | // empty the string and free memory | |
1137 | void Clear() | |
1138 | { | |
1139 | wxString tmp(wxEmptyString); | |
1140 | swap(tmp); | |
1141 | } | |
1142 | ||
1143 | // contents test | |
1144 | // Is an ascii value | |
1145 | bool IsAscii() const; | |
1146 | // Is a number | |
1147 | bool IsNumber() const; | |
1148 | // Is a word | |
1149 | bool IsWord() const; | |
1150 | ||
1151 | // data access (all indexes are 0 based) | |
1152 | // read access | |
1153 | wxUniChar at(size_t n) const | |
1154 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
1155 | wxUniChar GetChar(size_t n) const | |
1156 | { return at(n); } | |
1157 | // read/write access | |
1158 | wxUniCharRef at(size_t n) | |
1159 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
1160 | wxUniCharRef GetWritableChar(size_t n) | |
1161 | { return at(n); } | |
1162 | // write access | |
1163 | void SetChar(size_t n, wxUniChar ch) | |
1164 | { at(n) = ch; } | |
1165 | ||
1166 | // get last character | |
1167 | wxUniChar Last() const | |
1168 | { | |
1169 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); | |
1170 | return *rbegin(); | |
1171 | } | |
1172 | ||
1173 | // get writable last character | |
1174 | wxUniCharRef Last() | |
1175 | { | |
1176 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); | |
1177 | return *rbegin(); | |
1178 | } | |
1179 | ||
1180 | /* | |
1181 | Note that we we must define all of the overloads below to avoid | |
1182 | ambiguity when using str[0]. | |
1183 | */ | |
1184 | wxUniChar operator[](int n) const | |
1185 | { return at(n); } | |
1186 | wxUniChar operator[](long n) const | |
1187 | { return at(n); } | |
1188 | wxUniChar operator[](size_t n) const | |
1189 | { return at(n); } | |
1190 | #ifndef wxSIZE_T_IS_UINT | |
1191 | wxUniChar operator[](unsigned int n) const | |
1192 | { return at(n); } | |
1193 | #endif // size_t != unsigned int | |
1194 | ||
1195 | // operator versions of GetWriteableChar() | |
1196 | wxUniCharRef operator[](int n) | |
1197 | { return at(n); } | |
1198 | wxUniCharRef operator[](long n) | |
1199 | { return at(n); } | |
1200 | wxUniCharRef operator[](size_t n) | |
1201 | { return at(n); } | |
1202 | #ifndef wxSIZE_T_IS_UINT | |
1203 | wxUniCharRef operator[](unsigned int n) | |
1204 | { return at(n); } | |
1205 | #endif // size_t != unsigned int | |
1206 | ||
1207 | // explicit conversion to C string (use this with printf()!) | |
1208 | wxCStrData c_str() const { return wxCStrData(this); } | |
1209 | wxCStrData data() const { return c_str(); } | |
1210 | ||
1211 | // implicit conversion to C string | |
1212 | operator wxCStrData() const { return c_str(); } | |
1213 | ||
1214 | // the first two operators conflict with operators for conversion to | |
1215 | // std::string and they must be disabled in STL build; the next one only | |
1216 | // makes sense if conversions to char* are also defined and not defining it | |
1217 | // in STL build also helps us to get more clear error messages for the code | |
1218 | // which relies on implicit conversion to char* in STL build | |
1219 | #if !wxUSE_STL | |
1220 | operator const char*() const { return c_str(); } | |
1221 | operator const wchar_t*() const { return c_str(); } | |
1222 | ||
1223 | // implicit conversion to untyped pointer for compatibility with previous | |
1224 | // wxWidgets versions: this is the same as conversion to const char * so it | |
1225 | // may fail! | |
1226 | operator const void*() const { return c_str(); } | |
1227 | #endif // wxUSE_STL | |
1228 | ||
1229 | // identical to c_str(), for MFC compatibility | |
1230 | const wxCStrData GetData() const { return c_str(); } | |
1231 | ||
1232 | // explicit conversion to C string in internal representation (char*, | |
1233 | // wchar_t*, UTF-8-encoded char*, depending on the build): | |
1234 | const wxStringCharType *wx_str() const { return m_impl.c_str(); } | |
1235 | ||
1236 | // conversion to *non-const* multibyte or widestring buffer; modifying | |
1237 | // returned buffer won't affect the string, these methods are only useful | |
1238 | // for passing values to const-incorrect functions | |
1239 | wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const | |
1240 | { return mb_str(conv); } | |
1241 | wxWritableWCharBuffer wchar_str() const { return wc_str(); } | |
1242 | ||
1243 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for | |
1244 | // converting numbers or strings which are certain not to contain special | |
1245 | // chars (typically system functions, X atoms, environment variables etc.) | |
1246 | // | |
1247 | // the behaviour of these functions with the strings containing anything | |
1248 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
1249 | #if wxUSE_UNICODE | |
1250 | static wxString FromAscii(const char *ascii, size_t len); | |
1251 | static wxString FromAscii(const char *ascii); | |
1252 | static wxString FromAscii(char ascii); | |
1253 | const wxCharBuffer ToAscii() const; | |
1254 | #else // ANSI | |
1255 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
1256 | static wxString FromAscii(const char *ascii, size_t len) | |
1257 | { return wxString( ascii, len ); } | |
1258 | static wxString FromAscii(char ascii) { return wxString( ascii ); } | |
1259 | const char *ToAscii() const { return c_str(); } | |
1260 | #endif // Unicode/!Unicode | |
1261 | ||
1262 | // also provide unsigned char overloads as signed/unsigned doesn't matter | |
1263 | // for 7 bit ASCII characters | |
1264 | static wxString FromAscii(const unsigned char *ascii) | |
1265 | { return FromAscii((const char *)ascii); } | |
1266 | static wxString FromAscii(const unsigned char *ascii, size_t len) | |
1267 | { return FromAscii((const char *)ascii, len); } | |
1268 | ||
1269 | // conversion to/from UTF-8: | |
1270 | #if wxUSE_UNICODE_UTF8 | |
1271 | static wxString FromUTF8(const char *utf8) | |
1272 | { | |
1273 | if ( !utf8 ) | |
1274 | return wxEmptyString; | |
1275 | ||
1276 | wxASSERT( wxStringOperations::IsValidUtf8String(utf8) ); | |
1277 | return FromImpl(wxStringImpl(utf8)); | |
1278 | } | |
1279 | static wxString FromUTF8(const char *utf8, size_t len) | |
1280 | { | |
1281 | if ( !utf8 ) | |
1282 | return wxEmptyString; | |
1283 | if ( len == npos ) | |
1284 | return FromUTF8(utf8); | |
1285 | ||
1286 | wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) ); | |
1287 | return FromImpl(wxStringImpl(utf8, len)); | |
1288 | } | |
1289 | const char* utf8_str() const { return wx_str(); } | |
1290 | const char* ToUTF8() const { return wx_str(); } | |
1291 | #elif wxUSE_UNICODE_WCHAR | |
1292 | static wxString FromUTF8(const char *utf8) | |
1293 | { return wxString(utf8, wxMBConvUTF8()); } | |
1294 | static wxString FromUTF8(const char *utf8, size_t len) | |
1295 | { return wxString(utf8, wxMBConvUTF8(), len); } | |
1296 | const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); } | |
1297 | const wxCharBuffer ToUTF8() const { return utf8_str(); } | |
1298 | #else // ANSI | |
1299 | static wxString FromUTF8(const char *utf8) | |
1300 | { return wxString(wxMBConvUTF8().cMB2WC(utf8)); } | |
1301 | static wxString FromUTF8(const char *utf8, size_t len) | |
1302 | { | |
1303 | size_t wlen; | |
1304 | wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen)); | |
1305 | return wxString(buf.data(), wlen); | |
1306 | } | |
1307 | const wxCharBuffer utf8_str() const | |
1308 | { return wxMBConvUTF8().cWC2MB(wc_str()); } | |
1309 | const wxCharBuffer ToUTF8() const { return utf8_str(); } | |
1310 | #endif | |
1311 | ||
1312 | // functions for storing binary data in wxString: | |
1313 | #if wxUSE_UNICODE | |
1314 | static wxString From8BitData(const char *data, size_t len) | |
1315 | { return wxString(data, wxConvISO8859_1, len); } | |
1316 | // version for NUL-terminated data: | |
1317 | static wxString From8BitData(const char *data) | |
1318 | { return wxString(data, wxConvISO8859_1); } | |
1319 | const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); } | |
1320 | #else // ANSI | |
1321 | static wxString From8BitData(const char *data, size_t len) | |
1322 | { return wxString(data, len); } | |
1323 | // version for NUL-terminated data: | |
1324 | static wxString From8BitData(const char *data) | |
1325 | { return wxString(data); } | |
1326 | const char *To8BitData() const { return c_str(); } | |
1327 | #endif // Unicode/ANSI | |
1328 | ||
1329 | // conversions with (possible) format conversions: have to return a | |
1330 | // buffer with temporary data | |
1331 | // | |
1332 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
1333 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
1334 | // fn_str() to return a string which should be used with the OS APIs | |
1335 | // accepting the file names. The return value is always the same, but the | |
1336 | // type differs because a function may either return pointer to the buffer | |
1337 | // directly or have to use intermediate buffer for translation. | |
1338 | #if wxUSE_UNICODE | |
1339 | ||
1340 | #if wxUSE_UTF8_LOCALE_ONLY | |
1341 | const char* mb_str() const { return wx_str(); } | |
1342 | const wxCharBuffer mb_str(const wxMBConv& conv) const; | |
1343 | #else | |
1344 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; | |
1345 | #endif | |
1346 | ||
1347 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } | |
1348 | ||
1349 | #if wxUSE_UNICODE_WCHAR | |
1350 | const wxChar* wc_str() const { return wx_str(); } | |
1351 | #elif wxUSE_UNICODE_UTF8 | |
1352 | const wxWCharBuffer wc_str() const; | |
1353 | #endif | |
1354 | // for compatibility with !wxUSE_UNICODE version | |
1355 | const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const | |
1356 | { return wc_str(); } | |
1357 | ||
1358 | #if wxMBFILES | |
1359 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } | |
1360 | #else // !wxMBFILES | |
1361 | const wxWX2WCbuf fn_str() const { return wc_str(); } | |
1362 | #endif // wxMBFILES/!wxMBFILES | |
1363 | ||
1364 | #else // ANSI | |
1365 | const wxChar* mb_str() const { return wx_str(); } | |
1366 | ||
1367 | // for compatibility with wxUSE_UNICODE version | |
1368 | const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); } | |
1369 | ||
1370 | const wxWX2MBbuf mbc_str() const { return mb_str(); } | |
1371 | ||
1372 | #if wxUSE_WCHAR_T | |
1373 | const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const; | |
1374 | #endif // wxUSE_WCHAR_T | |
1375 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
1376 | #endif // Unicode/ANSI | |
1377 | ||
1378 | // overloaded assignment | |
1379 | // from another wxString | |
1380 | wxString& operator=(const wxString& stringSrc) | |
1381 | { m_impl = stringSrc.m_impl; return *this; } | |
1382 | wxString& operator=(const wxCStrData& cstr) | |
1383 | { return *this = cstr.AsString(); } | |
1384 | // from a character | |
1385 | wxString& operator=(wxUniChar ch) | |
1386 | { m_impl = wxStringOperations::EncodeChar(ch); return *this; } | |
1387 | wxString& operator=(wxUniCharRef ch) | |
1388 | { return operator=((wxUniChar)ch); } | |
1389 | wxString& operator=(char ch) | |
1390 | { return operator=(wxUniChar(ch)); } | |
1391 | wxString& operator=(unsigned char ch) | |
1392 | { return operator=(wxUniChar(ch)); } | |
1393 | wxString& operator=(wchar_t ch) | |
1394 | { return operator=(wxUniChar(ch)); } | |
1395 | // from a C string - STL probably will crash on NULL, | |
1396 | // so we need to compensate in that case | |
1397 | #if wxUSE_STL_BASED_WXSTRING | |
1398 | wxString& operator=(const char *psz) | |
1399 | { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; } | |
1400 | wxString& operator=(const wchar_t *pwz) | |
1401 | { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; } | |
1402 | #else | |
1403 | wxString& operator=(const char *psz) | |
1404 | { m_impl = ImplStr(psz); return *this; } | |
1405 | wxString& operator=(const wchar_t *pwz) | |
1406 | { m_impl = ImplStr(pwz); return *this; } | |
1407 | #endif | |
1408 | wxString& operator=(const unsigned char *psz) | |
1409 | { return operator=((const char*)psz); } | |
1410 | ||
1411 | // from wxWCharBuffer | |
1412 | wxString& operator=(const wxWCharBuffer& s) | |
1413 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs | |
1414 | // from wxCharBuffer | |
1415 | wxString& operator=(const wxCharBuffer& s) | |
1416 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs | |
1417 | ||
1418 | // string concatenation | |
1419 | // in place concatenation | |
1420 | /* | |
1421 | Concatenate and return the result. Note that the left to right | |
1422 | associativity of << allows to write things like "str << str1 << str2 | |
1423 | << ..." (unlike with +=) | |
1424 | */ | |
1425 | // string += string | |
1426 | wxString& operator<<(const wxString& s) | |
1427 | { | |
1428 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
1429 | wxASSERT_MSG( s.IsValid(), | |
1430 | _T("did you forget to call UngetWriteBuf()?") ); | |
1431 | #endif | |
1432 | ||
1433 | append(s); | |
1434 | return *this; | |
1435 | } | |
1436 | // string += C string | |
1437 | wxString& operator<<(const char *psz) | |
1438 | { append(psz); return *this; } | |
1439 | wxString& operator<<(const wchar_t *pwz) | |
1440 | { append(pwz); return *this; } | |
1441 | wxString& operator<<(const wxCStrData& psz) | |
1442 | { append(psz.AsString()); return *this; } | |
1443 | // string += char | |
1444 | wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } | |
1445 | wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } | |
1446 | wxString& operator<<(char ch) { append(1, ch); return *this; } | |
1447 | wxString& operator<<(unsigned char ch) { append(1, ch); return *this; } | |
1448 | wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } | |
1449 | ||
1450 | // string += buffer (i.e. from wxGetString) | |
1451 | wxString& operator<<(const wxWCharBuffer& s) | |
1452 | { return operator<<((const wchar_t *)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 char* psz) | |
1467 | { append(psz); return *this; } | |
1468 | wxString& Append(const wchar_t* pwz) | |
1469 | { append(pwz); return *this; } | |
1470 | wxString& Append(const wxCStrData& psz) | |
1471 | { append(psz); return *this; } | |
1472 | wxString& Append(const wxCharBuffer& psz) | |
1473 | { append(psz); return *this; } | |
1474 | wxString& Append(const wxWCharBuffer& psz) | |
1475 | { append(psz); return *this; } | |
1476 | // append count copies of given character | |
1477 | wxString& Append(wxUniChar ch, size_t count = 1u) | |
1478 | { append(count, ch); return *this; } | |
1479 | wxString& Append(wxUniCharRef ch, size_t count = 1u) | |
1480 | { append(count, ch); return *this; } | |
1481 | wxString& Append(char ch, size_t count = 1u) | |
1482 | { append(count, ch); return *this; } | |
1483 | wxString& Append(unsigned char ch, size_t count = 1u) | |
1484 | { append(count, ch); return *this; } | |
1485 | wxString& Append(wchar_t ch, size_t count = 1u) | |
1486 | { append(count, ch); return *this; } | |
1487 | wxString& Append(const char* psz, size_t nLen) | |
1488 | { append(psz, nLen); return *this; } | |
1489 | wxString& Append(const wchar_t* pwz, size_t nLen) | |
1490 | { append(pwz, nLen); return *this; } | |
1491 | ||
1492 | // prepend a string, return the string itself | |
1493 | wxString& Prepend(const wxString& str) | |
1494 | { *this = str + *this; return *this; } | |
1495 | ||
1496 | // non-destructive concatenation | |
1497 | // two strings | |
1498 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
1499 | const wxString& string2); | |
1500 | // string with a single char | |
1501 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); | |
1502 | // char with a string | |
1503 | friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
1504 | // string with C string | |
1505 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1506 | const char *psz); | |
1507 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1508 | const wchar_t *pwz); | |
1509 | // C string with string | |
1510 | friend wxString WXDLLIMPEXP_BASE operator+(const char *psz, | |
1511 | const wxString& string); | |
1512 | friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, | |
1513 | const wxString& string); | |
1514 | ||
1515 | // stream-like functions | |
1516 | // insert an int into string | |
1517 | wxString& operator<<(int i) | |
1518 | { return (*this) << Format(_T("%d"), i); } | |
1519 | // insert an unsigned int into string | |
1520 | wxString& operator<<(unsigned int ui) | |
1521 | { return (*this) << Format(_T("%u"), ui); } | |
1522 | // insert a long into string | |
1523 | wxString& operator<<(long l) | |
1524 | { return (*this) << Format(_T("%ld"), l); } | |
1525 | // insert an unsigned long into string | |
1526 | wxString& operator<<(unsigned long ul) | |
1527 | { return (*this) << Format(_T("%lu"), ul); } | |
1528 | #if defined wxLongLong_t && !defined wxLongLongIsLong | |
1529 | // insert a long long if they exist and aren't longs | |
1530 | wxString& operator<<(wxLongLong_t ll) | |
1531 | { | |
1532 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
1533 | return (*this) << Format(fmt, ll); | |
1534 | } | |
1535 | // insert an unsigned long long | |
1536 | wxString& operator<<(wxULongLong_t ull) | |
1537 | { | |
1538 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
1539 | return (*this) << Format(fmt , ull); | |
1540 | } | |
1541 | #endif | |
1542 | // insert a float into string | |
1543 | wxString& operator<<(float f) | |
1544 | { return (*this) << Format(_T("%f"), f); } | |
1545 | // insert a double into string | |
1546 | wxString& operator<<(double d) | |
1547 | { return (*this) << Format(_T("%g"), d); } | |
1548 | ||
1549 | // string comparison | |
1550 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) | |
1551 | int Cmp(const char *psz) const | |
1552 | { return compare(psz); } | |
1553 | int Cmp(const wchar_t *pwz) const | |
1554 | { return compare(pwz); } | |
1555 | int Cmp(const wxString& s) const | |
1556 | { return compare(s); } | |
1557 | int Cmp(const wxCStrData& s) const | |
1558 | { return compare(s); } | |
1559 | int Cmp(const wxCharBuffer& s) const | |
1560 | { return compare(s); } | |
1561 | int Cmp(const wxWCharBuffer& s) const | |
1562 | { return compare(s); } | |
1563 | // same as Cmp() but not case-sensitive | |
1564 | int CmpNoCase(const wxString& s) const; | |
1565 | // test for the string equality, either considering case or not | |
1566 | // (if compareWithCase then the case matters) | |
1567 | bool IsSameAs(const wxString& str, bool compareWithCase = true) const | |
1568 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
1569 | bool IsSameAs(const char *str, bool compareWithCase = true) const | |
1570 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
1571 | bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const | |
1572 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
1573 | bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const | |
1574 | { return IsSameAs(str.AsString(), compareWithCase); } | |
1575 | bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const | |
1576 | { return IsSameAs(str.data(), compareWithCase); } | |
1577 | bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const | |
1578 | { return IsSameAs(str.data(), compareWithCase); } | |
1579 | // comparison with a single character: returns true if equal | |
1580 | bool IsSameAs(wxUniChar c, bool compareWithCase = true) const; | |
1581 | // FIXME-UTF8: remove these overloads | |
1582 | bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const | |
1583 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1584 | bool IsSameAs(char c, bool compareWithCase = true) const | |
1585 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1586 | bool IsSameAs(unsigned char c, bool compareWithCase = true) const | |
1587 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1588 | bool IsSameAs(wchar_t c, bool compareWithCase = true) const | |
1589 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1590 | bool IsSameAs(int c, bool compareWithCase = true) const | |
1591 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1592 | ||
1593 | // simple sub-string extraction | |
1594 | // return substring starting at nFirst of length nCount (or till the end | |
1595 | // if nCount = default value) | |
1596 | wxString Mid(size_t nFirst, size_t nCount = npos) const; | |
1597 | ||
1598 | // operator version of Mid() | |
1599 | wxString operator()(size_t start, size_t len) const | |
1600 | { return Mid(start, len); } | |
1601 | ||
1602 | // check if the string starts with the given prefix and return the rest | |
1603 | // of the string in the provided pointer if it is not NULL; otherwise | |
1604 | // return false | |
1605 | bool StartsWith(const wxString& prefix, wxString *rest = NULL) const; | |
1606 | // check if the string ends with the given suffix and return the | |
1607 | // beginning of the string before the suffix in the provided pointer if | |
1608 | // it is not NULL; otherwise return false | |
1609 | bool EndsWith(const wxString& suffix, wxString *rest = NULL) const; | |
1610 | ||
1611 | // get first nCount characters | |
1612 | wxString Left(size_t nCount) const; | |
1613 | // get last nCount characters | |
1614 | wxString Right(size_t nCount) const; | |
1615 | // get all characters before the first occurance of ch | |
1616 | // (returns the whole string if ch not found) | |
1617 | wxString BeforeFirst(wxUniChar ch) const; | |
1618 | // get all characters before the last occurence of ch | |
1619 | // (returns empty string if ch not found) | |
1620 | wxString BeforeLast(wxUniChar ch) const; | |
1621 | // get all characters after the first occurence of ch | |
1622 | // (returns empty string if ch not found) | |
1623 | wxString AfterFirst(wxUniChar ch) const; | |
1624 | // get all characters after the last occurence of ch | |
1625 | // (returns the whole string if ch not found) | |
1626 | wxString AfterLast(wxUniChar ch) const; | |
1627 | ||
1628 | // for compatibility only, use more explicitly named functions above | |
1629 | wxString Before(wxUniChar ch) const { return BeforeLast(ch); } | |
1630 | wxString After(wxUniChar ch) const { return AfterFirst(ch); } | |
1631 | ||
1632 | // case conversion | |
1633 | // convert to upper case in place, return the string itself | |
1634 | wxString& MakeUpper(); | |
1635 | // convert to upper case, return the copy of the string | |
1636 | // Here's something to remember: BC++ doesn't like returns in inlines. | |
1637 | wxString Upper() const ; | |
1638 | // convert to lower case in place, return the string itself | |
1639 | wxString& MakeLower(); | |
1640 | // convert to lower case, return the copy of the string | |
1641 | wxString Lower() const ; | |
1642 | ||
1643 | // trimming/padding whitespace (either side) and truncating | |
1644 | // remove spaces from left or from right (default) side | |
1645 | wxString& Trim(bool bFromRight = true); | |
1646 | // add nCount copies chPad in the beginning or at the end (default) | |
1647 | wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); | |
1648 | ||
1649 | // searching and replacing | |
1650 | // searching (return starting index, or -1 if not found) | |
1651 | int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr | |
1652 | int Find(wxUniCharRef ch, bool bFromEnd = false) const | |
1653 | { return Find(wxUniChar(ch), bFromEnd); } | |
1654 | int Find(char ch, bool bFromEnd = false) const | |
1655 | { return Find(wxUniChar(ch), bFromEnd); } | |
1656 | int Find(unsigned char ch, bool bFromEnd = false) const | |
1657 | { return Find(wxUniChar(ch), bFromEnd); } | |
1658 | int Find(wchar_t ch, bool bFromEnd = false) const | |
1659 | { return Find(wxUniChar(ch), bFromEnd); } | |
1660 | // searching (return starting index, or -1 if not found) | |
1661 | int Find(const wxString& sub) const // like strstr | |
1662 | { | |
1663 | size_type idx = find(sub); | |
1664 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1665 | } | |
1666 | int Find(const char *sub) const // like strstr | |
1667 | { | |
1668 | size_type idx = find(sub); | |
1669 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1670 | } | |
1671 | int Find(const wchar_t *sub) const // like strstr | |
1672 | { | |
1673 | size_type idx = find(sub); | |
1674 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1675 | } | |
1676 | ||
1677 | int Find(const wxCStrData& sub) const | |
1678 | { return Find(sub.AsString()); } | |
1679 | int Find(const wxCharBuffer& sub) const | |
1680 | { return Find(sub.data()); } | |
1681 | int Find(const wxWCharBuffer& sub) const | |
1682 | { return Find(sub.data()); } | |
1683 | ||
1684 | // replace first (or all of bReplaceAll) occurences of substring with | |
1685 | // another string, returns the number of replacements made | |
1686 | size_t Replace(const wxString& strOld, | |
1687 | const wxString& strNew, | |
1688 | bool bReplaceAll = true); | |
1689 | ||
1690 | // check if the string contents matches a mask containing '*' and '?' | |
1691 | bool Matches(const wxString& mask) const; | |
1692 | ||
1693 | // conversion to numbers: all functions return true only if the whole | |
1694 | // string is a number and put the value of this number into the pointer | |
1695 | // provided, the base is the numeric base in which the conversion should be | |
1696 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1697 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
1698 | // convert to a signed integer | |
1699 | bool ToLong(long *val, int base = 10) const; | |
1700 | // convert to an unsigned integer | |
1701 | bool ToULong(unsigned long *val, int base = 10) const; | |
1702 | // convert to wxLongLong | |
1703 | #if defined(wxLongLong_t) | |
1704 | bool ToLongLong(wxLongLong_t *val, int base = 10) const; | |
1705 | // convert to wxULongLong | |
1706 | bool ToULongLong(wxULongLong_t *val, int base = 10) const; | |
1707 | #endif // wxLongLong_t | |
1708 | // convert to a double | |
1709 | bool ToDouble(double *val) const; | |
1710 | ||
1711 | ||
1712 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1713 | // formatted input/output | |
1714 | // as sprintf(), returns the number of characters written or < 0 on error | |
1715 | // (take 'this' into account in attribute parameter count) | |
1716 | // int Printf(const wxString& format, ...); | |
1717 | WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&), | |
1718 | DoPrintfWchar, DoPrintfUtf8) | |
1719 | #ifdef __WATCOMC__ | |
1720 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
1721 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&), | |
1722 | (wxFormatString(f1))); | |
1723 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&), | |
1724 | (wxFormatString(f1))); | |
1725 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*), | |
1726 | (wxFormatString(f1))); | |
1727 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*), | |
1728 | (wxFormatString(f1))); | |
1729 | #endif | |
1730 | #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1731 | // as vprintf(), returns the number of characters written or < 0 on error | |
1732 | int PrintfV(const wxString& format, va_list argptr); | |
1733 | ||
1734 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1735 | // returns the string containing the result of Printf() to it | |
1736 | // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1; | |
1737 | WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&), | |
1738 | DoFormatWchar, DoFormatUtf8) | |
1739 | #ifdef __WATCOMC__ | |
1740 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
1741 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&), | |
1742 | (wxFormatString(f1))); | |
1743 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&), | |
1744 | (wxFormatString(f1))); | |
1745 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*), | |
1746 | (wxFormatString(f1))); | |
1747 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*), | |
1748 | (wxFormatString(f1))); | |
1749 | #endif | |
1750 | #endif | |
1751 | // the same as above, but takes a va_list | |
1752 | static wxString FormatV(const wxString& format, va_list argptr); | |
1753 | ||
1754 | // raw access to string memory | |
1755 | // ensure that string has space for at least nLen characters | |
1756 | // only works if the data of this string is not shared | |
1757 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } | |
1758 | // minimize the string's memory | |
1759 | // only works if the data of this string is not shared | |
1760 | bool Shrink(); | |
1761 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
1762 | // These are deprecated, use wxStringBuffer or wxStringBufferLength instead | |
1763 | // | |
1764 | // get writable buffer of at least nLen bytes. Unget() *must* be called | |
1765 | // a.s.a.p. to put string back in a reasonable state! | |
1766 | wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) ); | |
1767 | // call this immediately after GetWriteBuf() has been used | |
1768 | wxDEPRECATED( void UngetWriteBuf() ); | |
1769 | wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); | |
1770 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8 | |
1771 | ||
1772 | // wxWidgets version 1 compatibility functions | |
1773 | ||
1774 | // use Mid() | |
1775 | wxString SubString(size_t from, size_t to) const | |
1776 | { return Mid(from, (to - from + 1)); } | |
1777 | // values for second parameter of CompareTo function | |
1778 | enum caseCompare {exact, ignoreCase}; | |
1779 | // values for first parameter of Strip function | |
1780 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; | |
1781 | ||
1782 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1783 | // use Printf() | |
1784 | // (take 'this' into account in attribute parameter count) | |
1785 | // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2; | |
1786 | WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&), | |
1787 | DoPrintfWchar, DoPrintfUtf8) | |
1788 | #ifdef __WATCOMC__ | |
1789 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
1790 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&), | |
1791 | (wxFormatString(f1))); | |
1792 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&), | |
1793 | (wxFormatString(f1))); | |
1794 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*), | |
1795 | (wxFormatString(f1))); | |
1796 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*), | |
1797 | (wxFormatString(f1))); | |
1798 | #endif | |
1799 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1800 | ||
1801 | // use Cmp() | |
1802 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const | |
1803 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } | |
1804 | ||
1805 | // use length() | |
1806 | size_t Length() const { return length(); } | |
1807 | // Count the number of characters | |
1808 | int Freq(wxUniChar ch) const; | |
1809 | // use MakeLower | |
1810 | void LowerCase() { MakeLower(); } | |
1811 | // use MakeUpper | |
1812 | void UpperCase() { MakeUpper(); } | |
1813 | // use Trim except that it doesn't change this string | |
1814 | wxString Strip(stripType w = trailing) const; | |
1815 | ||
1816 | // use Find (more general variants not yet supported) | |
1817 | size_t Index(const wxChar* psz) const { return Find(psz); } | |
1818 | size_t Index(wxUniChar ch) const { return Find(ch); } | |
1819 | // use Truncate | |
1820 | wxString& Remove(size_t pos) { return Truncate(pos); } | |
1821 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } | |
1822 | ||
1823 | wxString& Remove(size_t nStart, size_t nLen) | |
1824 | { return (wxString&)erase( nStart, nLen ); } | |
1825 | ||
1826 | // use Find() | |
1827 | int First( wxUniChar ch ) const { return Find(ch); } | |
1828 | int First( wxUniCharRef ch ) const { return Find(ch); } | |
1829 | int First( char ch ) const { return Find(ch); } | |
1830 | int First( unsigned char ch ) const { return Find(ch); } | |
1831 | int First( wchar_t ch ) const { return Find(ch); } | |
1832 | int First( const wxString& str ) const { return Find(str); } | |
1833 | int Last( wxUniChar ch ) const { return Find(ch, true); } | |
1834 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } | |
1835 | ||
1836 | // use empty() | |
1837 | bool IsNull() const { return empty(); } | |
1838 | ||
1839 | // std::string compatibility functions | |
1840 | ||
1841 | // take nLen chars starting at nPos | |
1842 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
1843 | { assign(str, nPos, nLen); } | |
1844 | // take all characters from first to last | |
1845 | wxString(const_iterator first, const_iterator last) | |
1846 | : m_impl(first.impl(), last.impl()) { } | |
1847 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
1848 | // the 2 overloads below are for compatibility with the existing code using | |
1849 | // pointers instead of iterators | |
1850 | wxString(const char *first, const char *last) | |
1851 | { | |
1852 | SubstrBufFromMB str(ImplStr(first, last - first)); | |
1853 | m_impl.assign(str.data, str.len); | |
1854 | } | |
1855 | wxString(const wchar_t *first, const wchar_t *last) | |
1856 | { | |
1857 | SubstrBufFromWC str(ImplStr(first, last - first)); | |
1858 | m_impl.assign(str.data, str.len); | |
1859 | } | |
1860 | // and this one is needed to compile code adding offsets to c_str() result | |
1861 | wxString(const wxCStrData& first, const wxCStrData& last) | |
1862 | : m_impl(CreateConstIterator(first).impl(), | |
1863 | CreateConstIterator(last).impl()) | |
1864 | { | |
1865 | wxASSERT_MSG( first.m_str == last.m_str, | |
1866 | _T("pointers must be into the same string") ); | |
1867 | } | |
1868 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
1869 | ||
1870 | // lib.string.modifiers | |
1871 | // append elements str[pos], ..., str[pos+n] | |
1872 | wxString& append(const wxString& str, size_t pos, size_t n) | |
1873 | { | |
1874 | size_t from, len; | |
1875 | str.PosLenToImpl(pos, n, &from, &len); | |
1876 | m_impl.append(str.m_impl, from, len); | |
1877 | return *this; | |
1878 | } | |
1879 | // append a string | |
1880 | wxString& append(const wxString& str) | |
1881 | { m_impl.append(str.m_impl); return *this; } | |
1882 | // append first n (or all if n == npos) characters of sz | |
1883 | wxString& append(const char *sz) | |
1884 | { m_impl.append(ImplStr(sz)); return *this; } | |
1885 | wxString& append(const wchar_t *sz) | |
1886 | { m_impl.append(ImplStr(sz)); return *this; } | |
1887 | wxString& append(const char *sz, size_t n) | |
1888 | { | |
1889 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1890 | m_impl.append(str.data, str.len); | |
1891 | return *this; | |
1892 | } | |
1893 | wxString& append(const wchar_t *sz, size_t n) | |
1894 | { | |
1895 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1896 | m_impl.append(str.data, str.len); | |
1897 | return *this; | |
1898 | } | |
1899 | ||
1900 | wxString& append(const wxCStrData& str) | |
1901 | { return append(str.AsString()); } | |
1902 | wxString& append(const wxCharBuffer& str) | |
1903 | { return append(str.data()); } | |
1904 | wxString& append(const wxWCharBuffer& str) | |
1905 | { return append(str.data()); } | |
1906 | ||
1907 | // append n copies of ch | |
1908 | wxString& append(size_t n, wxUniChar ch) | |
1909 | { | |
1910 | #if wxUSE_UNICODE_UTF8 | |
1911 | if ( !ch.IsAscii() ) | |
1912 | m_impl.append(wxStringOperations::EncodeNChars(n, ch)); | |
1913 | else | |
1914 | #endif | |
1915 | m_impl.append(n, (wxStringCharType)ch); | |
1916 | return *this; | |
1917 | } | |
1918 | // append from first to last | |
1919 | wxString& append(const_iterator first, const_iterator last) | |
1920 | { m_impl.append(first.impl(), last.impl()); return *this; } | |
1921 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
1922 | wxString& append(const char *first, const char *last) | |
1923 | { return append(first, last - first); } | |
1924 | wxString& append(const wchar_t *first, const wchar_t *last) | |
1925 | { return append(first, last - first); } | |
1926 | wxString& append(const wxCStrData& first, const wxCStrData& last) | |
1927 | { return append(CreateConstIterator(first), CreateConstIterator(last)); } | |
1928 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
1929 | ||
1930 | // same as `this_string = str' | |
1931 | wxString& assign(const wxString& str) | |
1932 | { m_impl = str.m_impl; return *this; } | |
1933 | wxString& assign(const wxString& str, size_t len) | |
1934 | { | |
1935 | m_impl.assign(str.m_impl, 0, str.LenToImpl(len)); | |
1936 | return *this; | |
1937 | } | |
1938 | // same as ` = str[pos..pos + n] | |
1939 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
1940 | { | |
1941 | size_t from, len; | |
1942 | str.PosLenToImpl(pos, n, &from, &len); | |
1943 | m_impl.assign(str.m_impl, from, len); | |
1944 | return *this; | |
1945 | } | |
1946 | // same as `= first n (or all if n == npos) characters of sz' | |
1947 | wxString& assign(const char *sz) | |
1948 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1949 | wxString& assign(const wchar_t *sz) | |
1950 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1951 | wxString& assign(const char *sz, size_t n) | |
1952 | { | |
1953 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1954 | m_impl.assign(str.data, str.len); | |
1955 | return *this; | |
1956 | } | |
1957 | wxString& assign(const wchar_t *sz, size_t n) | |
1958 | { | |
1959 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1960 | m_impl.assign(str.data, str.len); | |
1961 | return *this; | |
1962 | } | |
1963 | ||
1964 | wxString& assign(const wxCStrData& str) | |
1965 | { return assign(str.AsString()); } | |
1966 | wxString& assign(const wxCharBuffer& str) | |
1967 | { return assign(str.data()); } | |
1968 | wxString& assign(const wxWCharBuffer& str) | |
1969 | { return assign(str.data()); } | |
1970 | wxString& assign(const wxCStrData& str, size_t len) | |
1971 | { return assign(str.AsString(), len); } | |
1972 | wxString& assign(const wxCharBuffer& str, size_t len) | |
1973 | { return assign(str.data(), len); } | |
1974 | wxString& assign(const wxWCharBuffer& str, size_t len) | |
1975 | { return assign(str.data(), len); } | |
1976 | ||
1977 | // same as `= n copies of ch' | |
1978 | wxString& assign(size_t n, wxUniChar ch) | |
1979 | { | |
1980 | #if wxUSE_UNICODE_UTF8 | |
1981 | if ( !ch.IsAscii() ) | |
1982 | m_impl.assign(wxStringOperations::EncodeNChars(n, ch)); | |
1983 | else | |
1984 | #endif | |
1985 | m_impl.assign(n, (wxStringCharType)ch); | |
1986 | return *this; | |
1987 | } | |
1988 | ||
1989 | wxString& assign(size_t n, wxUniCharRef ch) | |
1990 | { return assign(n, wxUniChar(ch)); } | |
1991 | wxString& assign(size_t n, char ch) | |
1992 | { return assign(n, wxUniChar(ch)); } | |
1993 | wxString& assign(size_t n, unsigned char ch) | |
1994 | { return assign(n, wxUniChar(ch)); } | |
1995 | wxString& assign(size_t n, wchar_t ch) | |
1996 | { return assign(n, wxUniChar(ch)); } | |
1997 | ||
1998 | // assign from first to last | |
1999 | wxString& assign(const_iterator first, const_iterator last) | |
2000 | { m_impl.assign(first.impl(), last.impl()); return *this; } | |
2001 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
2002 | wxString& assign(const char *first, const char *last) | |
2003 | { return assign(first, last - first); } | |
2004 | wxString& assign(const wchar_t *first, const wchar_t *last) | |
2005 | { return assign(first, last - first); } | |
2006 | wxString& assign(const wxCStrData& first, const wxCStrData& last) | |
2007 | { return assign(CreateConstIterator(first), CreateConstIterator(last)); } | |
2008 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
2009 | ||
2010 | // string comparison | |
2011 | int compare(const wxString& str) const; | |
2012 | int compare(const char* sz) const; | |
2013 | int compare(const wchar_t* sz) const; | |
2014 | int compare(const wxCStrData& str) const | |
2015 | { return compare(str.AsString()); } | |
2016 | int compare(const wxCharBuffer& str) const | |
2017 | { return compare(str.data()); } | |
2018 | int compare(const wxWCharBuffer& str) const | |
2019 | { return compare(str.data()); } | |
2020 | // comparison with a substring | |
2021 | int compare(size_t nStart, size_t nLen, const wxString& str) const; | |
2022 | // comparison of 2 substrings | |
2023 | int compare(size_t nStart, size_t nLen, | |
2024 | const wxString& str, size_t nStart2, size_t nLen2) const; | |
2025 | // substring comparison with first nCount characters of sz | |
2026 | int compare(size_t nStart, size_t nLen, | |
2027 | const char* sz, size_t nCount = npos) const; | |
2028 | int compare(size_t nStart, size_t nLen, | |
2029 | const wchar_t* sz, size_t nCount = npos) const; | |
2030 | ||
2031 | // insert another string | |
2032 | wxString& insert(size_t nPos, const wxString& str) | |
2033 | { insert(begin() + nPos, str.begin(), str.end()); return *this; } | |
2034 | // insert n chars of str starting at nStart (in str) | |
2035 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
2036 | { | |
2037 | size_t from, len; | |
2038 | str.PosLenToImpl(nStart, n, &from, &len); | |
2039 | m_impl.insert(PosToImpl(nPos), str.m_impl, from, len); | |
2040 | return *this; | |
2041 | } | |
2042 | // insert first n (or all if n == npos) characters of sz | |
2043 | wxString& insert(size_t nPos, const char *sz) | |
2044 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
2045 | wxString& insert(size_t nPos, const wchar_t *sz) | |
2046 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
2047 | wxString& insert(size_t nPos, const char *sz, size_t n) | |
2048 | { | |
2049 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2050 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
2051 | return *this; | |
2052 | } | |
2053 | wxString& insert(size_t nPos, const wchar_t *sz, size_t n) | |
2054 | { | |
2055 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2056 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
2057 | return *this; | |
2058 | } | |
2059 | // insert n copies of ch | |
2060 | wxString& insert(size_t nPos, size_t n, wxUniChar ch) | |
2061 | { | |
2062 | #if wxUSE_UNICODE_UTF8 | |
2063 | if ( !ch.IsAscii() ) | |
2064 | m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch)); | |
2065 | else | |
2066 | #endif | |
2067 | m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch); | |
2068 | return *this; | |
2069 | } | |
2070 | iterator insert(iterator it, wxUniChar ch) | |
2071 | { | |
2072 | #if wxUSE_UNICODE_UTF8 | |
2073 | if ( !ch.IsAscii() ) | |
2074 | { | |
2075 | size_t pos = IterToImplPos(it); | |
2076 | m_impl.insert(pos, wxStringOperations::EncodeChar(ch)); | |
2077 | return iterator(this, m_impl.begin() + pos); | |
2078 | } | |
2079 | else | |
2080 | #endif | |
2081 | return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch)); | |
2082 | } | |
2083 | void insert(iterator it, const_iterator first, const_iterator last) | |
2084 | { m_impl.insert(it.impl(), first.impl(), last.impl()); } | |
2085 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
2086 | void insert(iterator it, const char *first, const char *last) | |
2087 | { insert(it - begin(), first, last - first); } | |
2088 | void insert(iterator it, const wchar_t *first, const wchar_t *last) | |
2089 | { insert(it - begin(), first, last - first); } | |
2090 | void insert(iterator it, const wxCStrData& first, const wxCStrData& last) | |
2091 | { insert(it, CreateConstIterator(first), CreateConstIterator(last)); } | |
2092 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
2093 | ||
2094 | void insert(iterator it, size_type n, wxUniChar ch) | |
2095 | { | |
2096 | #if wxUSE_UNICODE_UTF8 | |
2097 | if ( !ch.IsAscii() ) | |
2098 | m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch)); | |
2099 | else | |
2100 | #endif | |
2101 | m_impl.insert(it.impl(), n, (wxStringCharType)ch); | |
2102 | } | |
2103 | ||
2104 | // delete characters from nStart to nStart + nLen | |
2105 | wxString& erase(size_type pos = 0, size_type n = npos) | |
2106 | { | |
2107 | size_t from, len; | |
2108 | PosLenToImpl(pos, n, &from, &len); | |
2109 | m_impl.erase(from, len); | |
2110 | return *this; | |
2111 | } | |
2112 | // delete characters from first up to last | |
2113 | iterator erase(iterator first, iterator last) | |
2114 | { return iterator(this, m_impl.erase(first.impl(), last.impl())); } | |
2115 | iterator erase(iterator first) | |
2116 | { return iterator(this, m_impl.erase(first.impl())); } | |
2117 | ||
2118 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
2119 | void clear() { erase(); } | |
2120 | #else | |
2121 | void clear() { m_impl.clear(); } | |
2122 | #endif | |
2123 | ||
2124 | // replaces the substring of length nLen starting at nStart | |
2125 | wxString& replace(size_t nStart, size_t nLen, const char* sz) | |
2126 | { | |
2127 | size_t from, len; | |
2128 | PosLenToImpl(nStart, nLen, &from, &len); | |
2129 | m_impl.replace(from, len, ImplStr(sz)); | |
2130 | return *this; | |
2131 | } | |
2132 | wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz) | |
2133 | { | |
2134 | size_t from, len; | |
2135 | PosLenToImpl(nStart, nLen, &from, &len); | |
2136 | m_impl.replace(from, len, ImplStr(sz)); | |
2137 | return *this; | |
2138 | } | |
2139 | // replaces the substring of length nLen starting at nStart | |
2140 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
2141 | { | |
2142 | size_t from, len; | |
2143 | PosLenToImpl(nStart, nLen, &from, &len); | |
2144 | m_impl.replace(from, len, str.m_impl); | |
2145 | return *this; | |
2146 | } | |
2147 | // replaces the substring with nCount copies of ch | |
2148 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) | |
2149 | { | |
2150 | size_t from, len; | |
2151 | PosLenToImpl(nStart, nLen, &from, &len); | |
2152 | #if wxUSE_UNICODE_UTF8 | |
2153 | if ( !ch.IsAscii() ) | |
2154 | m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch)); | |
2155 | else | |
2156 | #endif | |
2157 | m_impl.replace(from, len, nCount, (wxStringCharType)ch); | |
2158 | return *this; | |
2159 | } | |
2160 | // replaces a substring with another substring | |
2161 | wxString& replace(size_t nStart, size_t nLen, | |
2162 | const wxString& str, size_t nStart2, size_t nLen2) | |
2163 | { | |
2164 | size_t from, len; | |
2165 | PosLenToImpl(nStart, nLen, &from, &len); | |
2166 | ||
2167 | size_t from2, len2; | |
2168 | str.PosLenToImpl(nStart2, nLen2, &from2, &len2); | |
2169 | ||
2170 | m_impl.replace(from, len, str.m_impl, from2, len2); | |
2171 | return *this; | |
2172 | } | |
2173 | // replaces the substring with first nCount chars of sz | |
2174 | wxString& replace(size_t nStart, size_t nLen, | |
2175 | const char* sz, size_t nCount) | |
2176 | { | |
2177 | size_t from, len; | |
2178 | PosLenToImpl(nStart, nLen, &from, &len); | |
2179 | ||
2180 | SubstrBufFromMB str(ImplStr(sz, nCount)); | |
2181 | ||
2182 | m_impl.replace(from, len, str.data, str.len); | |
2183 | return *this; | |
2184 | } | |
2185 | wxString& replace(size_t nStart, size_t nLen, | |
2186 | const wchar_t* sz, size_t nCount) | |
2187 | { | |
2188 | size_t from, len; | |
2189 | PosLenToImpl(nStart, nLen, &from, &len); | |
2190 | ||
2191 | SubstrBufFromWC str(ImplStr(sz, nCount)); | |
2192 | ||
2193 | m_impl.replace(from, len, str.data, str.len); | |
2194 | return *this; | |
2195 | } | |
2196 | wxString& replace(size_t nStart, size_t nLen, | |
2197 | const wxString& s, size_t nCount) | |
2198 | { | |
2199 | size_t from, len; | |
2200 | PosLenToImpl(nStart, nLen, &from, &len); | |
2201 | m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount)); | |
2202 | return *this; | |
2203 | } | |
2204 | ||
2205 | wxString& replace(iterator first, iterator last, const char* s) | |
2206 | { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; } | |
2207 | wxString& replace(iterator first, iterator last, const wchar_t* s) | |
2208 | { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; } | |
2209 | wxString& replace(iterator first, iterator last, const char* s, size_type n) | |
2210 | { | |
2211 | SubstrBufFromMB str(ImplStr(s, n)); | |
2212 | m_impl.replace(first.impl(), last.impl(), str.data, str.len); | |
2213 | return *this; | |
2214 | } | |
2215 | wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n) | |
2216 | { | |
2217 | SubstrBufFromWC str(ImplStr(s, n)); | |
2218 | m_impl.replace(first.impl(), last.impl(), str.data, str.len); | |
2219 | return *this; | |
2220 | } | |
2221 | wxString& replace(iterator first, iterator last, const wxString& s) | |
2222 | { m_impl.replace(first.impl(), last.impl(), s.m_impl); return *this; } | |
2223 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch) | |
2224 | { | |
2225 | #if wxUSE_UNICODE_UTF8 | |
2226 | if ( !ch.IsAscii() ) | |
2227 | m_impl.replace(first.impl(), last.impl(), | |
2228 | wxStringOperations::EncodeNChars(n, ch)); | |
2229 | else | |
2230 | #endif | |
2231 | m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch); | |
2232 | return *this; | |
2233 | } | |
2234 | wxString& replace(iterator first, iterator last, | |
2235 | const_iterator first1, const_iterator last1) | |
2236 | { | |
2237 | m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl()); | |
2238 | return *this; | |
2239 | } | |
2240 | wxString& replace(iterator first, iterator last, | |
2241 | const char *first1, const char *last1) | |
2242 | { replace(first, last, first1, last1 - first1); return *this; } | |
2243 | wxString& replace(iterator first, iterator last, | |
2244 | const wchar_t *first1, const wchar_t *last1) | |
2245 | { replace(first, last, first1, last1 - first1); return *this; } | |
2246 | ||
2247 | // swap two strings | |
2248 | void swap(wxString& str) | |
2249 | { m_impl.swap(str.m_impl); } | |
2250 | ||
2251 | // find a substring | |
2252 | size_t find(const wxString& str, size_t nStart = 0) const | |
2253 | { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); } | |
2254 | ||
2255 | // find first n characters of sz | |
2256 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const | |
2257 | { | |
2258 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2259 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
2260 | } | |
2261 | size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const | |
2262 | { | |
2263 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2264 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
2265 | } | |
2266 | size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const | |
2267 | { return find(s.data(), nStart, n); } | |
2268 | size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const | |
2269 | { return find(s.data(), nStart, n); } | |
2270 | size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const | |
2271 | { return find(s.AsWChar(), nStart, n); } | |
2272 | ||
2273 | // find the first occurence of character ch after nStart | |
2274 | size_t find(wxUniChar ch, size_t nStart = 0) const | |
2275 | { | |
2276 | return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch), | |
2277 | PosToImpl(nStart))); | |
2278 | } | |
2279 | size_t find(wxUniCharRef ch, size_t nStart = 0) const | |
2280 | { return find(wxUniChar(ch), nStart); } | |
2281 | size_t find(char ch, size_t nStart = 0) const | |
2282 | { return find(wxUniChar(ch), nStart); } | |
2283 | size_t find(unsigned char ch, size_t nStart = 0) const | |
2284 | { return find(wxUniChar(ch), nStart); } | |
2285 | size_t find(wchar_t ch, size_t nStart = 0) const | |
2286 | { return find(wxUniChar(ch), nStart); } | |
2287 | ||
2288 | // rfind() family is exactly like find() but works right to left | |
2289 | ||
2290 | // as find, but from the end | |
2291 | size_t rfind(const wxString& str, size_t nStart = npos) const | |
2292 | { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); } | |
2293 | ||
2294 | // as find, but from the end | |
2295 | size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const | |
2296 | { | |
2297 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2298 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
2299 | } | |
2300 | size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const | |
2301 | { | |
2302 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2303 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
2304 | } | |
2305 | size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const | |
2306 | { return rfind(s.data(), nStart, n); } | |
2307 | size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const | |
2308 | { return rfind(s.data(), nStart, n); } | |
2309 | size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const | |
2310 | { return rfind(s.AsWChar(), nStart, n); } | |
2311 | // as find, but from the end | |
2312 | size_t rfind(wxUniChar ch, size_t nStart = npos) const | |
2313 | { | |
2314 | return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch), | |
2315 | PosToImpl(nStart))); | |
2316 | } | |
2317 | size_t rfind(wxUniCharRef ch, size_t nStart = npos) const | |
2318 | { return rfind(wxUniChar(ch), nStart); } | |
2319 | size_t rfind(char ch, size_t nStart = npos) const | |
2320 | { return rfind(wxUniChar(ch), nStart); } | |
2321 | size_t rfind(unsigned char ch, size_t nStart = npos) const | |
2322 | { return rfind(wxUniChar(ch), nStart); } | |
2323 | size_t rfind(wchar_t ch, size_t nStart = npos) const | |
2324 | { return rfind(wxUniChar(ch), nStart); } | |
2325 | ||
2326 | // find first/last occurence of any character (not) in the set: | |
2327 | #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2328 | // FIXME-UTF8: this is not entirely correct, because it doesn't work if | |
2329 | // sizeof(wchar_t)==2 and surrogates are present in the string; | |
2330 | // should we care? Probably not. | |
2331 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
2332 | { return m_impl.find_first_of(str.m_impl, nStart); } | |
2333 | size_t find_first_of(const char* sz, size_t nStart = 0) const | |
2334 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
2335 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const | |
2336 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
2337 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const | |
2338 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } | |
2339 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const | |
2340 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } | |
2341 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
2342 | { return m_impl.find_first_of((wxChar)c, nStart); } | |
2343 | ||
2344 | size_t find_last_of(const wxString& str, size_t nStart = npos) const | |
2345 | { return m_impl.find_last_of(str.m_impl, nStart); } | |
2346 | size_t find_last_of(const char* sz, size_t nStart = npos) const | |
2347 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
2348 | size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const | |
2349 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
2350 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const | |
2351 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
2352 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const | |
2353 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
2354 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
2355 | { return m_impl.find_last_of((wxChar)c, nStart); } | |
2356 | ||
2357 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
2358 | { return m_impl.find_first_not_of(str.m_impl, nStart); } | |
2359 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const | |
2360 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } | |
2361 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const | |
2362 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } | |
2363 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const | |
2364 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } | |
2365 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const | |
2366 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } | |
2367 | size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const | |
2368 | { return m_impl.find_first_not_of((wxChar)c, nStart); } | |
2369 | ||
2370 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const | |
2371 | { return m_impl.find_last_not_of(str.m_impl, nStart); } | |
2372 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const | |
2373 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } | |
2374 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const | |
2375 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } | |
2376 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const | |
2377 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } | |
2378 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const | |
2379 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } | |
2380 | size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const | |
2381 | { return m_impl.find_last_not_of((wxChar)c, nStart); } | |
2382 | #else | |
2383 | // we can't use std::string implementation in UTF-8 build, because the | |
2384 | // character sets would be interpreted wrongly: | |
2385 | ||
2386 | // as strpbrk() but starts at nStart, returns npos if not found | |
2387 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
2388 | #if wxUSE_UNICODE // FIXME-UTF8: temporary | |
2389 | { return find_first_of(str.wc_str(), nStart); } | |
2390 | #else | |
2391 | { return find_first_of(str.mb_str(), nStart); } | |
2392 | #endif | |
2393 | // same as above | |
2394 | size_t find_first_of(const char* sz, size_t nStart = 0) const; | |
2395 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const; | |
2396 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const; | |
2397 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2398 | // same as find(char, size_t) | |
2399 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
2400 | { return find(c, nStart); } | |
2401 | // find the last (starting from nStart) char from str in this string | |
2402 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
2403 | #if wxUSE_UNICODE // FIXME-UTF8: temporary | |
2404 | { return find_last_of(str.wc_str(), nStart); } | |
2405 | #else | |
2406 | { return find_last_of(str.mb_str(), nStart); } | |
2407 | #endif | |
2408 | // same as above | |
2409 | size_t find_last_of (const char* sz, size_t nStart = npos) const; | |
2410 | size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const; | |
2411 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const; | |
2412 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2413 | // same as above | |
2414 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
2415 | { return rfind(c, nStart); } | |
2416 | ||
2417 | // find first/last occurence of any character not in the set | |
2418 | ||
2419 | // as strspn() (starting from nStart), returns npos on failure | |
2420 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
2421 | #if wxUSE_UNICODE // FIXME-UTF8: temporary | |
2422 | { return find_first_not_of(str.wc_str(), nStart); } | |
2423 | #else | |
2424 | { return find_first_not_of(str.mb_str(), nStart); } | |
2425 | #endif | |
2426 | // same as above | |
2427 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const; | |
2428 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const; | |
2429 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const; | |
2430 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2431 | // same as above | |
2432 | size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; | |
2433 | // as strcspn() | |
2434 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const | |
2435 | #if wxUSE_UNICODE // FIXME-UTF8: temporary | |
2436 | { return find_last_not_of(str.wc_str(), nStart); } | |
2437 | #else | |
2438 | { return find_last_not_of(str.mb_str(), nStart); } | |
2439 | #endif | |
2440 | // same as above | |
2441 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const; | |
2442 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const; | |
2443 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const; | |
2444 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
2445 | // same as above | |
2446 | size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; | |
2447 | #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not | |
2448 | ||
2449 | // provide char/wchar_t/wxUniCharRef overloads for char-finding functions | |
2450 | // above to resolve ambiguities: | |
2451 | size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const | |
2452 | { return find_first_of(wxUniChar(ch), nStart); } | |
2453 | size_t find_first_of(char ch, size_t nStart = 0) const | |
2454 | { return find_first_of(wxUniChar(ch), nStart); } | |
2455 | size_t find_first_of(unsigned char ch, size_t nStart = 0) const | |
2456 | { return find_first_of(wxUniChar(ch), nStart); } | |
2457 | size_t find_first_of(wchar_t ch, size_t nStart = 0) const | |
2458 | { return find_first_of(wxUniChar(ch), nStart); } | |
2459 | size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const | |
2460 | { return find_last_of(wxUniChar(ch), nStart); } | |
2461 | size_t find_last_of(char ch, size_t nStart = npos) const | |
2462 | { return find_last_of(wxUniChar(ch), nStart); } | |
2463 | size_t find_last_of(unsigned char ch, size_t nStart = npos) const | |
2464 | { return find_last_of(wxUniChar(ch), nStart); } | |
2465 | size_t find_last_of(wchar_t ch, size_t nStart = npos) const | |
2466 | { return find_last_of(wxUniChar(ch), nStart); } | |
2467 | size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const | |
2468 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
2469 | size_t find_first_not_of(char ch, size_t nStart = 0) const | |
2470 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
2471 | size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const | |
2472 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
2473 | size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const | |
2474 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
2475 | size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const | |
2476 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
2477 | size_t find_last_not_of(char ch, size_t nStart = npos) const | |
2478 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
2479 | size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const | |
2480 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
2481 | size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const | |
2482 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
2483 | ||
2484 | // and additional overloads for the versions taking strings: | |
2485 | size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const | |
2486 | { return find_first_of(sz.AsString(), nStart); } | |
2487 | size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
2488 | { return find_first_of(sz.data(), nStart); } | |
2489 | size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
2490 | { return find_first_of(sz.data(), nStart); } | |
2491 | size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
2492 | { return find_first_of(sz.AsWChar(), nStart, n); } | |
2493 | size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
2494 | { return find_first_of(sz.data(), nStart, n); } | |
2495 | size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
2496 | { return find_first_of(sz.data(), nStart, n); } | |
2497 | ||
2498 | size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const | |
2499 | { return find_last_of(sz.AsString(), nStart); } | |
2500 | size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
2501 | { return find_last_of(sz.data(), nStart); } | |
2502 | size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
2503 | { return find_last_of(sz.data(), nStart); } | |
2504 | size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
2505 | { return find_last_of(sz.AsWChar(), nStart, n); } | |
2506 | size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
2507 | { return find_last_of(sz.data(), nStart, n); } | |
2508 | size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
2509 | { return find_last_of(sz.data(), nStart, n); } | |
2510 | ||
2511 | size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const | |
2512 | { return find_first_not_of(sz.AsString(), nStart); } | |
2513 | size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
2514 | { return find_first_not_of(sz.data(), nStart); } | |
2515 | size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
2516 | { return find_first_not_of(sz.data(), nStart); } | |
2517 | size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
2518 | { return find_first_not_of(sz.AsWChar(), nStart, n); } | |
2519 | size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
2520 | { return find_first_not_of(sz.data(), nStart, n); } | |
2521 | size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
2522 | { return find_first_not_of(sz.data(), nStart, n); } | |
2523 | ||
2524 | size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const | |
2525 | { return find_last_not_of(sz.AsString(), nStart); } | |
2526 | size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
2527 | { return find_last_not_of(sz.data(), nStart); } | |
2528 | size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
2529 | { return find_last_not_of(sz.data(), nStart); } | |
2530 | size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
2531 | { return find_last_not_of(sz.AsWChar(), nStart, n); } | |
2532 | size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
2533 | { return find_last_not_of(sz.data(), nStart, n); } | |
2534 | size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
2535 | { return find_last_not_of(sz.data(), nStart, n); } | |
2536 | ||
2537 | // string += string | |
2538 | wxString& operator+=(const wxString& s) | |
2539 | { m_impl += s.m_impl; return *this; } | |
2540 | // string += C string | |
2541 | wxString& operator+=(const char *psz) | |
2542 | { m_impl += ImplStr(psz); return *this; } | |
2543 | wxString& operator+=(const wchar_t *pwz) | |
2544 | { m_impl += ImplStr(pwz); return *this; } | |
2545 | wxString& operator+=(const wxCStrData& s) | |
2546 | { m_impl += s.AsString().m_impl; return *this; } | |
2547 | wxString& operator+=(const wxCharBuffer& s) | |
2548 | { return operator+=(s.data()); } | |
2549 | wxString& operator+=(const wxWCharBuffer& s) | |
2550 | { return operator+=(s.data()); } | |
2551 | // string += char | |
2552 | wxString& operator+=(wxUniChar ch) | |
2553 | { m_impl += wxStringOperations::EncodeChar(ch); return *this; } | |
2554 | wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } | |
2555 | wxString& operator+=(int ch) { return *this += wxUniChar(ch); } | |
2556 | wxString& operator+=(char ch) { return *this += wxUniChar(ch); } | |
2557 | wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); } | |
2558 | wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } | |
2559 | ||
2560 | private: | |
2561 | #if !wxUSE_STL_BASED_WXSTRING | |
2562 | // helpers for wxStringBuffer and wxStringBufferLength | |
2563 | wxStringCharType *DoGetWriteBuf(size_t nLen) | |
2564 | { return m_impl.DoGetWriteBuf(nLen); } | |
2565 | void DoUngetWriteBuf() | |
2566 | { m_impl.DoUngetWriteBuf(); } | |
2567 | void DoUngetWriteBuf(size_t nLen) | |
2568 | { m_impl.DoUngetWriteBuf(nLen); } | |
2569 | #endif // !wxUSE_STL_BASED_WXSTRING | |
2570 | ||
2571 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
2572 | #if !wxUSE_UTF8_LOCALE_ONLY | |
2573 | int DoPrintfWchar(const wxChar *format, ...); | |
2574 | static wxString DoFormatWchar(const wxChar *format, ...); | |
2575 | #endif | |
2576 | #if wxUSE_UNICODE_UTF8 | |
2577 | int DoPrintfUtf8(const char *format, ...); | |
2578 | static wxString DoFormatUtf8(const char *format, ...); | |
2579 | #endif | |
2580 | #endif | |
2581 | ||
2582 | #if !wxUSE_STL_BASED_WXSTRING | |
2583 | // check string's data validity | |
2584 | bool IsValid() const { return m_impl.GetStringData()->IsValid(); } | |
2585 | #endif | |
2586 | ||
2587 | private: | |
2588 | wxStringImpl m_impl; | |
2589 | ||
2590 | #ifdef __VISUALC__ | |
2591 | // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by | |
2592 | // clients of class 'wxString'" - this is private, we don't care | |
2593 | #pragma warning (disable:4251) | |
2594 | #endif | |
2595 | ||
2596 | // buffers for compatibility conversion from (char*)c_str() and | |
2597 | // (wchar_t*)c_str(): | |
2598 | // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers | |
2599 | template<typename T> | |
2600 | struct ConvertedBuffer | |
2601 | { | |
2602 | ConvertedBuffer() : m_buf(NULL) {} | |
2603 | ~ConvertedBuffer() | |
2604 | { free(m_buf); } | |
2605 | ||
2606 | operator T*() const { return m_buf; } | |
2607 | ||
2608 | ConvertedBuffer& operator=(T *str) | |
2609 | { | |
2610 | free(m_buf); | |
2611 | m_buf = str; | |
2612 | return *this; | |
2613 | } | |
2614 | ||
2615 | T *m_buf; | |
2616 | }; | |
2617 | #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY | |
2618 | ConvertedBuffer<char> m_convertedToChar; | |
2619 | #endif | |
2620 | #if !wxUSE_UNICODE_WCHAR | |
2621 | ConvertedBuffer<wchar_t> m_convertedToWChar; | |
2622 | #endif | |
2623 | ||
2624 | #ifdef __VISUALC__ | |
2625 | #pragma warning (default:4251) | |
2626 | #endif | |
2627 | ||
2628 | #if wxUSE_UNICODE_UTF8 | |
2629 | // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently | |
2630 | // assigning to character pointer to by wxString::interator may | |
2631 | // change the underlying wxStringImpl iterator, so we have to | |
2632 | // keep track of all iterators and update them as necessary: | |
2633 | struct wxStringIteratorNodeHead | |
2634 | { | |
2635 | wxStringIteratorNodeHead() : ptr(NULL) {} | |
2636 | wxStringIteratorNode *ptr; | |
2637 | ||
2638 | // copying is disallowed as it would result in more than one pointer into | |
2639 | // the same linked list | |
2640 | DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead) | |
2641 | }; | |
2642 | ||
2643 | wxStringIteratorNodeHead m_iterators; | |
2644 | ||
2645 | friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode; | |
2646 | friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef; | |
2647 | #endif // wxUSE_UNICODE_UTF8 | |
2648 | ||
2649 | friend class WXDLLIMPEXP_FWD_BASE wxCStrData; | |
2650 | friend class wxStringInternalBuffer; | |
2651 | friend class wxStringInternalBufferLength; | |
2652 | }; | |
2653 | ||
2654 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
2655 | #pragma warning (default:4275) | |
2656 | #endif | |
2657 | ||
2658 | // string iterator operators that satisfy STL Random Access Iterator | |
2659 | // requirements: | |
2660 | inline wxString::iterator operator+(int n, wxString::iterator i) | |
2661 | { return i + n; } | |
2662 | inline wxString::iterator operator+(size_t n, wxString::iterator i) | |
2663 | { return i + n; } | |
2664 | inline wxString::const_iterator operator+(int n, wxString::const_iterator i) | |
2665 | { return i + n; } | |
2666 | inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i) | |
2667 | { return i + n; } | |
2668 | inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i) | |
2669 | { return i + n; } | |
2670 | inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i) | |
2671 | { return i + n; } | |
2672 | inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i) | |
2673 | { return i + n; } | |
2674 | inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i) | |
2675 | { return i + n; } | |
2676 | ||
2677 | // notice that even though for many compilers the friend declarations above are | |
2678 | // enough, from the point of view of C++ standard we must have the declarations | |
2679 | // here as friend ones are not injected in the enclosing namespace and without | |
2680 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
2681 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); | |
2682 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz); | |
2683 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz); | |
2684 | wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string); | |
2685 | wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string); | |
2686 | ||
2687 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); | |
2688 | wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
2689 | ||
2690 | inline wxString operator+(const wxString& string, wxUniCharRef ch) | |
2691 | { return string + (wxUniChar)ch; } | |
2692 | inline wxString operator+(const wxString& string, char ch) | |
2693 | { return string + wxUniChar(ch); } | |
2694 | inline wxString operator+(const wxString& string, wchar_t ch) | |
2695 | { return string + wxUniChar(ch); } | |
2696 | inline wxString operator+(wxUniCharRef ch, const wxString& string) | |
2697 | { return (wxUniChar)ch + string; } | |
2698 | inline wxString operator+(char ch, const wxString& string) | |
2699 | { return wxUniChar(ch) + string; } | |
2700 | inline wxString operator+(wchar_t ch, const wxString& string) | |
2701 | { return wxUniChar(ch) + string; } | |
2702 | ||
2703 | ||
2704 | #define wxGetEmptyString() wxString() | |
2705 | ||
2706 | // ---------------------------------------------------------------------------- | |
2707 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
2708 | // ---------------------------------------------------------------------------- | |
2709 | ||
2710 | #if !wxUSE_STL_BASED_WXSTRING | |
2711 | // string buffer for direct access to string data in their native | |
2712 | // representation: | |
2713 | class wxStringInternalBuffer | |
2714 | { | |
2715 | public: | |
2716 | typedef wxStringCharType CharType; | |
2717 | ||
2718 | wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024) | |
2719 | : m_str(str), m_buf(NULL) | |
2720 | { m_buf = m_str.DoGetWriteBuf(lenWanted); } | |
2721 | ||
2722 | ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); } | |
2723 | ||
2724 | operator wxStringCharType*() const { return m_buf; } | |
2725 | ||
2726 | private: | |
2727 | wxString& m_str; | |
2728 | wxStringCharType *m_buf; | |
2729 | ||
2730 | DECLARE_NO_COPY_CLASS(wxStringInternalBuffer) | |
2731 | }; | |
2732 | ||
2733 | class wxStringInternalBufferLength | |
2734 | { | |
2735 | public: | |
2736 | typedef wxStringCharType CharType; | |
2737 | ||
2738 | wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024) | |
2739 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
2740 | { | |
2741 | m_buf = m_str.DoGetWriteBuf(lenWanted); | |
2742 | wxASSERT(m_buf != NULL); | |
2743 | } | |
2744 | ||
2745 | ~wxStringInternalBufferLength() | |
2746 | { | |
2747 | wxASSERT(m_lenSet); | |
2748 | m_str.DoUngetWriteBuf(m_len); | |
2749 | } | |
2750 | ||
2751 | operator wxStringCharType*() const { return m_buf; } | |
2752 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2753 | ||
2754 | private: | |
2755 | wxString& m_str; | |
2756 | wxStringCharType *m_buf; | |
2757 | size_t m_len; | |
2758 | bool m_lenSet; | |
2759 | ||
2760 | DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength) | |
2761 | }; | |
2762 | ||
2763 | #endif // !wxUSE_STL_BASED_WXSTRING | |
2764 | ||
2765 | template<typename T> | |
2766 | class WXDLLIMPEXP_BASE wxStringTypeBufferBase | |
2767 | { | |
2768 | public: | |
2769 | typedef T CharType; | |
2770 | ||
2771 | wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024) | |
2772 | : m_str(str), m_buf(lenWanted) | |
2773 | { } | |
2774 | ||
2775 | ||
2776 | operator CharType*() { return m_buf.data(); } | |
2777 | ||
2778 | protected: | |
2779 | wxString& m_str; | |
2780 | wxCharTypeBuffer<CharType> m_buf; | |
2781 | }; | |
2782 | ||
2783 | template<typename T> | |
2784 | class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase | |
2785 | { | |
2786 | public: | |
2787 | typedef T CharType; | |
2788 | ||
2789 | wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024) | |
2790 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
2791 | { } | |
2792 | ||
2793 | operator CharType*() { return m_buf.data(); } | |
2794 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2795 | ||
2796 | protected: | |
2797 | wxString& m_str; | |
2798 | wxCharTypeBuffer<CharType> m_buf; | |
2799 | size_t m_len; | |
2800 | bool m_lenSet; | |
2801 | }; | |
2802 | ||
2803 | template<typename T> | |
2804 | class wxStringTypeBuffer : public wxStringTypeBufferBase<T> | |
2805 | { | |
2806 | public: | |
2807 | wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024) | |
2808 | : wxStringTypeBufferBase<T>(str, lenWanted) {} | |
2809 | ~wxStringTypeBuffer() | |
2810 | { | |
2811 | this->m_str.assign(this->m_buf.data()); | |
2812 | } | |
2813 | ||
2814 | DECLARE_NO_COPY_CLASS(wxStringTypeBuffer) | |
2815 | }; | |
2816 | ||
2817 | template<typename T> | |
2818 | class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T> | |
2819 | { | |
2820 | public: | |
2821 | wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024) | |
2822 | : wxStringTypeBufferLengthBase<T>(str, lenWanted) {} | |
2823 | ||
2824 | ~wxStringTypeBufferLength() | |
2825 | { | |
2826 | wxASSERT(this->m_lenSet); | |
2827 | this->m_str.assign(this->m_buf.data(), this->m_len); | |
2828 | } | |
2829 | ||
2830 | DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength) | |
2831 | }; | |
2832 | ||
2833 | #if wxUSE_STL_BASED_WXSTRING | |
2834 | ||
2835 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> ) | |
2836 | ||
2837 | class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType> | |
2838 | { | |
2839 | public: | |
2840 | wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024) | |
2841 | : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {} | |
2842 | ~wxStringInternalBuffer() | |
2843 | { m_str.m_impl.assign(m_buf.data()); } | |
2844 | ||
2845 | DECLARE_NO_COPY_CLASS(wxStringInternalBuffer) | |
2846 | }; | |
2847 | ||
2848 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( | |
2849 | wxStringTypeBufferLengthBase<wxStringCharType> ) | |
2850 | ||
2851 | class wxStringInternalBufferLength | |
2852 | : public wxStringTypeBufferLengthBase<wxStringCharType> | |
2853 | { | |
2854 | public: | |
2855 | wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024) | |
2856 | : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {} | |
2857 | ||
2858 | ~wxStringInternalBufferLength() | |
2859 | { | |
2860 | wxASSERT(m_lenSet); | |
2861 | m_str.m_impl.assign(m_buf.data(), m_len); | |
2862 | } | |
2863 | ||
2864 | DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength) | |
2865 | }; | |
2866 | #endif // wxUSE_STL_BASED_WXSTRING | |
2867 | ||
2868 | ||
2869 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 | |
2870 | typedef wxStringTypeBuffer<wxChar> wxStringBuffer; | |
2871 | typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength; | |
2872 | #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2873 | typedef wxStringInternalBuffer wxStringBuffer; | |
2874 | typedef wxStringInternalBufferLength wxStringBufferLength; | |
2875 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
2876 | ||
2877 | #if wxUSE_UNICODE_UTF8 | |
2878 | typedef wxStringInternalBuffer wxUTF8StringBuffer; | |
2879 | typedef wxStringInternalBufferLength wxUTF8StringBufferLength; | |
2880 | #elif wxUSE_UNICODE_WCHAR | |
2881 | ||
2882 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> ) | |
2883 | ||
2884 | class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char> | |
2885 | { | |
2886 | public: | |
2887 | wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024) | |
2888 | : wxStringTypeBufferBase<char>(str, lenWanted) {} | |
2889 | ~wxUTF8StringBuffer(); | |
2890 | ||
2891 | DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer) | |
2892 | }; | |
2893 | ||
2894 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> ) | |
2895 | ||
2896 | class WXDLLIMPEXP_BASE wxUTF8StringBufferLength | |
2897 | : public wxStringTypeBufferLengthBase<char> | |
2898 | { | |
2899 | public: | |
2900 | wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2901 | : wxStringTypeBufferLengthBase<char>(str, lenWanted) {} | |
2902 | ~wxUTF8StringBufferLength(); | |
2903 | ||
2904 | DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength) | |
2905 | }; | |
2906 | #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR | |
2907 | ||
2908 | ||
2909 | // --------------------------------------------------------------------------- | |
2910 | // wxString comparison functions: operator versions are always case sensitive | |
2911 | // --------------------------------------------------------------------------- | |
2912 | ||
2913 | #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p) | |
2914 | ||
2915 | wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) | |
2916 | ||
2917 | #undef wxCMP_WXCHAR_STRING | |
2918 | ||
2919 | // note that there is an optimization in operator==() and !=(): we (quickly) | |
2920 | // checks the strings length first, before comparing their data | |
2921 | inline bool operator==(const wxString& s1, const wxString& s2) | |
2922 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
2923 | inline bool operator!=(const wxString& s1, const wxString& s2) | |
2924 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
2925 | inline bool operator< (const wxString& s1, const wxString& s2) | |
2926 | { return s1.Cmp(s2) < 0; } | |
2927 | inline bool operator> (const wxString& s1, const wxString& s2) | |
2928 | { return s1.Cmp(s2) > 0; } | |
2929 | inline bool operator<=(const wxString& s1, const wxString& s2) | |
2930 | { return s1.Cmp(s2) <= 0; } | |
2931 | inline bool operator>=(const wxString& s1, const wxString& s2) | |
2932 | { return s1.Cmp(s2) >= 0; } | |
2933 | ||
2934 | inline bool operator==(const wxString& s1, const wxCStrData& s2) | |
2935 | { return s1 == s2.AsString(); } | |
2936 | inline bool operator==(const wxCStrData& s1, const wxString& s2) | |
2937 | { return s1.AsString() == s2; } | |
2938 | inline bool operator!=(const wxString& s1, const wxCStrData& s2) | |
2939 | { return s1 != s2.AsString(); } | |
2940 | inline bool operator!=(const wxCStrData& s1, const wxString& s2) | |
2941 | { return s1.AsString() != s2; } | |
2942 | ||
2943 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
2944 | { return (s1.Cmp((const wchar_t *)s2) == 0); } | |
2945 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) | |
2946 | { return (s2.Cmp((const wchar_t *)s1) == 0); } | |
2947 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) | |
2948 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
2949 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
2950 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
2951 | ||
2952 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) | |
2953 | { return (s1.Cmp((const char *)s2) == 0); } | |
2954 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) | |
2955 | { return (s2.Cmp((const char *)s1) == 0); } | |
2956 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) | |
2957 | { return (s1.Cmp((const char *)s2) != 0); } | |
2958 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
2959 | { return (s2.Cmp((const char *)s1) != 0); } | |
2960 | ||
2961 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) | |
2962 | { return string + (const wchar_t *)buf; } | |
2963 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) | |
2964 | { return (const wchar_t *)buf + string; } | |
2965 | ||
2966 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) | |
2967 | { return string + (const char *)buf; } | |
2968 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) | |
2969 | { return (const char *)buf + string; } | |
2970 | ||
2971 | // comparison with char | |
2972 | inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } | |
2973 | inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } | |
2974 | inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } | |
2975 | inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } | |
2976 | inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } | |
2977 | inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } | |
2978 | inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } | |
2979 | inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } | |
2980 | inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } | |
2981 | inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } | |
2982 | inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } | |
2983 | inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } | |
2984 | inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } | |
2985 | inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } | |
2986 | inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } | |
2987 | inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } | |
2988 | inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } | |
2989 | inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } | |
2990 | ||
2991 | // comparison with C string in Unicode build | |
2992 | #if wxUSE_UNICODE | |
2993 | ||
2994 | #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s | |
2995 | ||
2996 | wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) | |
2997 | ||
2998 | #undef wxCMP_CHAR_STRING | |
2999 | ||
3000 | #endif // wxUSE_UNICODE | |
3001 | ||
3002 | // we also need to provide the operators for comparison with wxCStrData to | |
3003 | // resolve ambiguity between operator(const wxChar *,const wxString &) and | |
3004 | // operator(const wxChar *, const wxChar *) for "p == s.c_str()" | |
3005 | // | |
3006 | // notice that these are (shallow) pointer comparisons, not (deep) string ones | |
3007 | #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() | |
3008 | #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() | |
3009 | ||
3010 | wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) | |
3011 | wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) | |
3012 | ||
3013 | #undef wxCMP_CHAR_CSTRDATA | |
3014 | #undef wxCMP_WCHAR_CSTRDATA | |
3015 | ||
3016 | // --------------------------------------------------------------------------- | |
3017 | // Implementation only from here until the end of file | |
3018 | // --------------------------------------------------------------------------- | |
3019 | ||
3020 | #if wxUSE_STD_IOSTREAM | |
3021 | ||
3022 | #include "wx/iosfwrap.h" | |
3023 | ||
3024 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); | |
3025 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); | |
3026 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&); | |
3027 | #ifndef __BORLANDC__ | |
3028 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&); | |
3029 | #endif | |
3030 | ||
3031 | #if wxUSE_UNICODE && defined(HAVE_WOSTREAM) | |
3032 | ||
3033 | WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&); | |
3034 | WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&); | |
3035 | WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxWCharBuffer&); | |
3036 | ||
3037 | #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM) | |
3038 | ||
3039 | #endif // wxUSE_STD_IOSTREAM | |
3040 | ||
3041 | // --------------------------------------------------------------------------- | |
3042 | // wxCStrData implementation | |
3043 | // --------------------------------------------------------------------------- | |
3044 | ||
3045 | inline wxCStrData::wxCStrData(char *buf) | |
3046 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
3047 | inline wxCStrData::wxCStrData(wchar_t *buf) | |
3048 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
3049 | ||
3050 | inline wxCStrData::wxCStrData(const wxCStrData& data) | |
3051 | : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str), | |
3052 | m_offset(data.m_offset), | |
3053 | m_owned(data.m_owned) | |
3054 | { | |
3055 | } | |
3056 | ||
3057 | inline wxCStrData::~wxCStrData() | |
3058 | { | |
3059 | if ( m_owned ) | |
3060 | delete wx_const_cast(wxString*, m_str); // cast to silence warnings | |
3061 | } | |
3062 | ||
3063 | // simple cases for AsChar() and AsWChar(), the complicated ones are | |
3064 | // in string.cpp | |
3065 | #if wxUSE_UNICODE_WCHAR | |
3066 | inline const wchar_t* wxCStrData::AsWChar() const | |
3067 | { | |
3068 | return m_str->wx_str() + m_offset; | |
3069 | } | |
3070 | #endif // wxUSE_UNICODE_WCHAR | |
3071 | ||
3072 | #if !wxUSE_UNICODE | |
3073 | inline const char* wxCStrData::AsChar() const | |
3074 | { | |
3075 | return m_str->wx_str() + m_offset; | |
3076 | } | |
3077 | #endif // !wxUSE_UNICODE | |
3078 | ||
3079 | #if wxUSE_UTF8_LOCALE_ONLY | |
3080 | inline const char* wxCStrData::AsChar() const | |
3081 | { | |
3082 | return wxStringOperations::AddToIter(m_str->wx_str(), m_offset); | |
3083 | } | |
3084 | #endif // wxUSE_UTF8_LOCALE_ONLY | |
3085 | ||
3086 | inline const wxCharBuffer wxCStrData::AsCharBuf() const | |
3087 | { | |
3088 | #if !wxUSE_UNICODE | |
3089 | return wxCharBuffer::CreateNonOwned(AsChar()); | |
3090 | #else | |
3091 | return AsString().mb_str(); | |
3092 | #endif | |
3093 | } | |
3094 | ||
3095 | inline const wxWCharBuffer wxCStrData::AsWCharBuf() const | |
3096 | { | |
3097 | #if wxUSE_UNICODE_WCHAR | |
3098 | return wxWCharBuffer::CreateNonOwned(AsWChar()); | |
3099 | #else | |
3100 | return AsString().wc_str(); | |
3101 | #endif | |
3102 | } | |
3103 | ||
3104 | inline wxString wxCStrData::AsString() const | |
3105 | { | |
3106 | if ( m_offset == 0 ) | |
3107 | return *m_str; | |
3108 | else | |
3109 | return m_str->Mid(m_offset); | |
3110 | } | |
3111 | ||
3112 | inline const wxStringCharType *wxCStrData::AsInternal() const | |
3113 | { | |
3114 | #if wxUSE_UNICODE_UTF8 | |
3115 | return wxStringOperations::AddToIter(m_str->wx_str(), m_offset); | |
3116 | #else | |
3117 | return m_str->wx_str() + m_offset; | |
3118 | #endif | |
3119 | } | |
3120 | ||
3121 | inline wxUniChar wxCStrData::operator*() const | |
3122 | { | |
3123 | if ( m_str->empty() ) | |
3124 | return wxUniChar(_T('\0')); | |
3125 | else | |
3126 | return (*m_str)[m_offset]; | |
3127 | } | |
3128 | ||
3129 | inline wxUniChar wxCStrData::operator[](size_t n) const | |
3130 | { | |
3131 | // NB: we intentionally use operator[] and not at() here because the former | |
3132 | // works for the terminating NUL while the latter does not | |
3133 | return (*m_str)[m_offset + n]; | |
3134 | } | |
3135 | ||
3136 | // ---------------------------------------------------------------------------- | |
3137 | // more wxCStrData operators | |
3138 | // ---------------------------------------------------------------------------- | |
3139 | ||
3140 | // we need to define those to allow "size_t pos = p - s.c_str()" where p is | |
3141 | // some pointer into the string | |
3142 | inline size_t operator-(const char *p, const wxCStrData& cs) | |
3143 | { | |
3144 | return p - cs.AsChar(); | |
3145 | } | |
3146 | ||
3147 | inline size_t operator-(const wchar_t *p, const wxCStrData& cs) | |
3148 | { | |
3149 | return p - cs.AsWChar(); | |
3150 | } | |
3151 | ||
3152 | // ---------------------------------------------------------------------------- | |
3153 | // implementation of wx[W]CharBuffer inline methods using wxCStrData | |
3154 | // ---------------------------------------------------------------------------- | |
3155 | ||
3156 | // FIXME-UTF8: move this to buffer.h | |
3157 | inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr) | |
3158 | : wxCharTypeBufferBase(cstr.AsCharBuf()) | |
3159 | { | |
3160 | } | |
3161 | ||
3162 | inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr) | |
3163 | : wxCharTypeBufferBase(cstr.AsWCharBuf()) | |
3164 | { | |
3165 | } | |
3166 | ||
3167 | #if wxUSE_UNICODE_UTF8 | |
3168 | // ---------------------------------------------------------------------------- | |
3169 | // implementation of wxStringIteratorNode inline methods | |
3170 | // ---------------------------------------------------------------------------- | |
3171 | ||
3172 | void wxStringIteratorNode::DoSet(const wxString *str, | |
3173 | wxStringImpl::const_iterator *citer, | |
3174 | wxStringImpl::iterator *iter) | |
3175 | { | |
3176 | m_prev = NULL; | |
3177 | m_iter = iter; | |
3178 | m_citer = citer; | |
3179 | m_str = str; | |
3180 | if ( str ) | |
3181 | { | |
3182 | m_next = str->m_iterators.ptr; | |
3183 | wx_const_cast(wxString*, m_str)->m_iterators.ptr = this; | |
3184 | if ( m_next ) | |
3185 | m_next->m_prev = this; | |
3186 | } | |
3187 | else | |
3188 | { | |
3189 | m_next = NULL; | |
3190 | } | |
3191 | } | |
3192 | ||
3193 | void wxStringIteratorNode::clear() | |
3194 | { | |
3195 | if ( m_next ) | |
3196 | m_next->m_prev = m_prev; | |
3197 | if ( m_prev ) | |
3198 | m_prev->m_next = m_next; | |
3199 | else if ( m_str ) // first in the list | |
3200 | wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next; | |
3201 | ||
3202 | m_next = m_prev = NULL; | |
3203 | m_citer = NULL; | |
3204 | m_iter = NULL; | |
3205 | m_str = NULL; | |
3206 | } | |
3207 | #endif // wxUSE_UNICODE_UTF8 | |
3208 | ||
3209 | #if WXWIN_COMPATIBILITY_2_8 | |
3210 | // lot of code out there doesn't explicitly include wx/crt.h, but uses | |
3211 | // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h, | |
3212 | // so let's include this header now that wxString is defined and it's safe | |
3213 | // to do it: | |
3214 | #include "wx/crt.h" | |
3215 | #endif | |
3216 | ||
3217 | #endif // _WX_WXSTRING_H_ |