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