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