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