]>
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 | ||
9b4da627 | 27 | #ifndef __WXPALMOS5__ |
9dea36ef | 28 | #if defined(__WXMAC__) || defined(__VISAGECPP__) |
3f4a0c5b | 29 | #include <ctype.h> |
17dff81c | 30 | #endif |
3f4a0c5b | 31 | |
9dea36ef DW |
32 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
33 | // problem in VACPP V4 with including stdlib.h multiple times | |
34 | // strconv includes it anyway | |
35 | # include <stdio.h> | |
36 | # include <string.h> | |
37 | # include <stdarg.h> | |
38 | # include <limits.h> | |
39 | #else | |
40 | # include <string.h> | |
41 | # include <stdio.h> | |
42 | # include <stdarg.h> | |
43 | # include <limits.h> | |
44 | # include <stdlib.h> | |
45 | #endif | |
c801d85f | 46 | |
1c2d1459 | 47 | #ifdef HAVE_STRCASECMP_IN_STRINGS_H |
1bfcb0b6 | 48 | #include <strings.h> // for strcasecmp() |
1c2d1459 | 49 | #endif // HAVE_STRCASECMP_IN_STRINGS_H |
9b4da627 | 50 | #endif // ! __WXPALMOS5__ |
ffecfa5a | 51 | |
52de37c7 | 52 | #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc. |
c9f78968 | 53 | #include "wx/strvararg.h" |
e90c1d2a VZ |
54 | #include "wx/buffer.h" // for wxCharBuffer |
55 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
a7ea63e2 | 56 | #include "wx/stringimpl.h" |
467175ab | 57 | #include "wx/stringops.h" |
a7ea63e2 | 58 | #include "wx/unichar.h" |
3f4a0c5b | 59 | |
68482dc5 VZ |
60 | // by default we cache the mapping of the positions in UTF-8 string to the byte |
61 | // offset as this results in noticeable performance improvements for loops over | |
62 | // strings using indices; comment out this line to disable this | |
63 | // | |
64 | // notice that this optimization is well worth using even in debug builds as it | |
65 | // changes asymptotic complexity of algorithms using indices to iterate over | |
66 | // wxString back to expected linear from quadratic | |
67 | // | |
68 | // also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to | |
69 | // use in DLL build under pre-Vista Windows so we disable this code for now, if | |
70 | // anybody really needs to use UTF-8 build under Windows with this optimization | |
71 | // it would have to be re-tested and probably corrected | |
b69470e4 SC |
72 | // CS: under OSX release builds the string destructor/cache cleanup sometimes |
73 | // crashes, disable until we find the true reason or a better workaround | |
74 | #if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__) && !defined(__WXOSX__) | |
68482dc5 VZ |
75 | #define wxUSE_STRING_POS_CACHE 1 |
76 | #else | |
77 | #define wxUSE_STRING_POS_CACHE 0 | |
78 | #endif | |
79 | ||
80 | #if wxUSE_STRING_POS_CACHE | |
81 | #include "wx/tls.h" | |
82 | ||
83 | // change this 0 to 1 to enable additional (very expensive) asserts | |
84 | // verifying that string caching logic works as expected | |
85 | #if 0 | |
86 | #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond) | |
87 | #else | |
88 | #define wxSTRING_CACHE_ASSERT(cond) | |
89 | #endif | |
90 | #endif // wxUSE_STRING_POS_CACHE | |
91 | ||
b5dbe15d | 92 | class WXDLLIMPEXP_FWD_BASE wxString; |
fb3c9042 | 93 | |
67cff9dc VZ |
94 | // unless this symbol is predefined to disable the compatibility functions, do |
95 | // use them | |
96 | #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
97 | #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1 | |
98 | #endif | |
99 | ||
062dc5fc VZ |
100 | namespace wxPrivate |
101 | { | |
102 | template <typename T> struct wxStringAsBufHelper; | |
103 | } | |
104 | ||
c801d85f KB |
105 | // --------------------------------------------------------------------------- |
106 | // macros | |
107 | // --------------------------------------------------------------------------- | |
108 | ||
5737d05f VZ |
109 | // casts [unfortunately!] needed to call some broken functions which require |
110 | // "char *" instead of "const char *" | |
2bb67b80 | 111 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
e90c1d2a VZ |
112 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) |
113 | #define wxMBSTRINGCAST (char *)(const char *) | |
114 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 115 | |
e90c1d2a VZ |
116 | // ---------------------------------------------------------------------------- |
117 | // constants | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
13874b5c VZ |
120 | #if WXWIN_COMPATIBILITY_2_6 |
121 | ||
122 | // deprecated in favour of wxString::npos, don't use in new code | |
123 | // | |
e90c1d2a | 124 | // maximum possible length for a string means "take all string" everywhere |
8f93a29f | 125 | #define wxSTRING_MAXLEN wxString::npos |
66b6b045 | 126 | |
13874b5c VZ |
127 | #endif // WXWIN_COMPATIBILITY_2_6 |
128 | ||
c801d85f | 129 | // --------------------------------------------------------------------------- |
e90c1d2a | 130 | // global functions complementing standard C string library replacements for |
3c67202d VZ |
131 | // strlen() and portable strcasecmp() |
132 | //--------------------------------------------------------------------------- | |
e90c1d2a | 133 | |
c7554da8 | 134 | #if WXWIN_COMPATIBILITY_2_8 |
e3f6cbd9 | 135 | // Use wxXXX() functions from wxcrt.h instead! These functions are for |
e90c1d2a | 136 | // backwards compatibility only. |
88150e60 | 137 | |
3c67202d | 138 | // checks whether the passed in pointer is NULL and if the string is empty |
c7554da8 | 139 | wxDEPRECATED( inline bool IsEmpty(const char *p) ); |
6d56eb5c | 140 | inline bool IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 141 | |
3c67202d | 142 | // safe version of strlen() (returns 0 if passed NULL pointer) |
c7554da8 | 143 | wxDEPRECATED( inline size_t Strlen(const char *psz) ); |
6d56eb5c | 144 | inline size_t Strlen(const char *psz) |
c801d85f KB |
145 | { return psz ? strlen(psz) : 0; } |
146 | ||
3c67202d | 147 | // portable strcasecmp/_stricmp |
c7554da8 | 148 | wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) ); |
6d56eb5c | 149 | inline int Stricmp(const char *psz1, const char *psz2) |
dd1eaa89 | 150 | { |
7f0586ef JS |
151 | #if defined(__VISUALC__) && defined(__WXWINCE__) |
152 | register char c1, c2; | |
153 | do { | |
154 | c1 = tolower(*psz1++); | |
155 | c2 = tolower(*psz2++); | |
156 | } while ( c1 && (c1 == c2) ); | |
157 | ||
158 | return c1 - c2; | |
159 | #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) | |
dd1eaa89 | 160 | return _stricmp(psz1, psz2); |
91b8de8d | 161 | #elif defined(__SC__) |
2432b92d | 162 | return _stricmp(psz1, psz2); |
dd1eaa89 VZ |
163 | #elif defined(__BORLANDC__) |
164 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
165 | #elif defined(__WATCOMC__) |
166 | return stricmp(psz1, psz2); | |
14704334 VS |
167 | #elif defined(__DJGPP__) |
168 | return stricmp(psz1, psz2); | |
91b8de8d RR |
169 | #elif defined(__EMX__) |
170 | return stricmp(psz1, psz2); | |
e2c87f4c | 171 | #elif defined(__WXPM__) |
1777b9bb | 172 | return stricmp(psz1, psz2); |
a0be6908 WS |
173 | #elif defined(__WXPALMOS__) || \ |
174 | defined(HAVE_STRCASECMP_IN_STRING_H) || \ | |
1c2d1459 VZ |
175 | defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ |
176 | defined(__GNUWIN32__) | |
dd1eaa89 | 177 | return strcasecmp(psz1, psz2); |
8be97d65 | 178 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
179 | register char c1, c2; |
180 | do { | |
181 | c1 = tolower(*psz1++); | |
182 | c2 = tolower(*psz2++); | |
183 | } while ( c1 && (c1 == c2) ); | |
184 | ||
185 | return c1 - c2; | |
dd1eaa89 VZ |
186 | #else |
187 | // almost all compilers/libraries provide this function (unfortunately under | |
188 | // different names), that's why we don't implement our own which will surely | |
189 | // be more efficient than this code (uncomment to use): | |
190 | /* | |
191 | register char c1, c2; | |
192 | do { | |
193 | c1 = tolower(*psz1++); | |
194 | c2 = tolower(*psz2++); | |
195 | } while ( c1 && (c1 == c2) ); | |
196 | ||
197 | return c1 - c2; | |
198 | */ | |
199 | ||
200 | #error "Please define string case-insensitive compare for your OS/compiler" | |
201 | #endif // OS/compiler | |
202 | } | |
c801d85f | 203 | |
c7554da8 VS |
204 | #endif // WXWIN_COMPATIBILITY_2_8 |
205 | ||
c9f78968 VS |
206 | // ---------------------------------------------------------------------------- |
207 | // wxCStrData | |
208 | // ---------------------------------------------------------------------------- | |
209 | ||
210 | // Lightweight object returned by wxString::c_str() and implicitly convertible | |
211 | // to either const char* or const wchar_t*. | |
9aee0061 | 212 | class WXDLLIMPEXP_BASE wxCStrData |
c9f78968 VS |
213 | { |
214 | private: | |
215 | // Ctors; for internal use by wxString and wxCStrData only | |
216 | wxCStrData(const wxString *str, size_t offset = 0, bool owned = false) | |
217 | : m_str(str), m_offset(offset), m_owned(owned) {} | |
218 | ||
219 | public: | |
220 | // Ctor constructs the object from char literal; they are needed to make | |
221 | // operator?: compile and they intentionally take char*, not const char* | |
92a17abc VS |
222 | inline wxCStrData(char *buf); |
223 | inline wxCStrData(wchar_t *buf); | |
224 | inline wxCStrData(const wxCStrData& data); | |
c9f78968 | 225 | |
9aee0061 | 226 | inline ~wxCStrData(); |
c9f78968 | 227 | |
9aee0061 VZ |
228 | // methods defined inline below must be declared inline or mingw32 3.4.5 |
229 | // warns about "<symbol> defined locally after being referenced with | |
230 | // dllimport linkage" | |
231 | #if wxUSE_UNICODE_WCHAR | |
232 | inline | |
233 | #endif | |
c9f78968 VS |
234 | const wchar_t* AsWChar() const; |
235 | operator const wchar_t*() const { return AsWChar(); } | |
11aac4ba | 236 | |
111d9948 | 237 | #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY |
9aee0061 VZ |
238 | inline |
239 | #endif | |
c9f78968 | 240 | const char* AsChar() const; |
dbea442a VZ |
241 | const unsigned char* AsUnsignedChar() const |
242 | { return (const unsigned char *) AsChar(); } | |
c9f78968 | 243 | operator const char*() const { return AsChar(); } |
dbea442a | 244 | operator const unsigned char*() const { return AsUnsignedChar(); } |
11aac4ba VS |
245 | |
246 | operator const void*() const { return AsChar(); } | |
c9f78968 | 247 | |
681e4412 VS |
248 | inline const wxCharBuffer AsCharBuf() const; |
249 | inline const wxWCharBuffer AsWCharBuf() const; | |
250 | ||
9aee0061 | 251 | inline wxString AsString() const; |
c9f78968 | 252 | |
2523e9b7 VS |
253 | // returns the value as C string in internal representation (equivalent |
254 | // to AsString().wx_str(), but more efficient) | |
255 | const wxStringCharType *AsInternal() const; | |
256 | ||
c9f78968 | 257 | // allow expressions like "c_str()[0]": |
9aee0061 | 258 | inline wxUniChar operator[](size_t n) const; |
c9f78968 | 259 | wxUniChar operator[](int n) const { return operator[](size_t(n)); } |
c1df0a3b | 260 | wxUniChar operator[](long n) const { return operator[](size_t(n)); } |
c9f78968 VS |
261 | #ifndef wxSIZE_T_IS_UINT |
262 | wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); } | |
263 | #endif // size_t != unsigned int | |
264 | ||
52988b5f | 265 | // These operators are needed to emulate the pointer semantics of c_str(): |
c9f78968 | 266 | // expressions like "wxChar *p = str.c_str() + 1;" should continue to work |
52988b5f VS |
267 | // (we need both versions to resolve ambiguities). Note that this means |
268 | // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it | |
269 | // is *not* number of Unicode characters in wxString. | |
c9f78968 VS |
270 | wxCStrData operator+(int n) const |
271 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
acf0c9ac VZ |
272 | wxCStrData operator+(long n) const |
273 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
c9f78968 VS |
274 | wxCStrData operator+(size_t n) const |
275 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
276 | ||
b5343e06 VZ |
277 | // and these for "str.c_str() + (p2 - p1)" (it also works for any integer |
278 | // expression but it must be ptrdiff_t and not e.g. int to work in this | |
279 | // example): | |
280 | wxCStrData operator-(ptrdiff_t n) const | |
f4c90fdf | 281 | { |
b5343e06 | 282 | wxASSERT_MSG( n <= (ptrdiff_t)m_offset, |
f4c90fdf VS |
283 | _T("attempt to construct address before the beginning of the string") ); |
284 | return wxCStrData(m_str, m_offset - n, m_owned); | |
285 | } | |
286 | ||
b77afb41 | 287 | // this operator is needed to make expressions like "*c_str()" or |
c9f78968 | 288 | // "*(c_str() + 2)" work |
9aee0061 | 289 | inline wxUniChar operator*() const; |
c9f78968 VS |
290 | |
291 | private: | |
52988b5f | 292 | // the wxString this object was returned for |
c9f78968 | 293 | const wxString *m_str; |
52988b5f VS |
294 | // Offset into c_str() return value. Note that this is *not* offset in |
295 | // m_str in Unicode characters. Instead, it is index into the | |
296 | // char*/wchar_t* buffer returned by c_str(). It's interpretation depends | |
297 | // on how is the wxCStrData instance used: if it is eventually cast to | |
298 | // const char*, m_offset will be in bytes form string's start; if it is | |
299 | // cast to const wchar_t*, it will be in wchar_t values. | |
c9f78968 | 300 | size_t m_offset; |
52988b5f | 301 | // should m_str be deleted, i.e. is it owned by us? |
c9f78968 VS |
302 | bool m_owned; |
303 | ||
b5dbe15d | 304 | friend class WXDLLIMPEXP_FWD_BASE wxString; |
c9f78968 VS |
305 | }; |
306 | ||
307 | // ---------------------------------------------------------------------------- | |
308 | // wxStringPrintfMixin | |
309 | // --------------------------------------------------------------------------- | |
310 | ||
311 | // NB: VC6 has a bug that causes linker errors if you have template methods | |
312 | // in a class using __declspec(dllimport). The solution is to split such | |
313 | // class into two classes, one that contains the template methods and does | |
314 | // *not* use WXDLLIMPEXP_BASE and another class that contains the rest | |
315 | // (with DLL linkage). | |
316 | // | |
317 | // We only do this for VC6 here, because the code is less efficient | |
318 | // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler | |
319 | // cannot compile this code. | |
320 | ||
321 | #if defined(__VISUALC__) && __VISUALC__ < 1300 | |
322 | #define wxNEEDS_WXSTRING_PRINTF_MIXIN | |
323 | #endif | |
324 | ||
325 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
326 | // this class contains implementation of wxString's vararg methods, it's | |
327 | // exported from wxBase DLL | |
328 | class WXDLLIMPEXP_BASE wxStringPrintfMixinBase | |
329 | { | |
330 | protected: | |
331 | wxStringPrintfMixinBase() {} | |
332 | ||
d1f6e2cf VS |
333 | #if !wxUSE_UTF8_LOCALE_ONLY |
334 | int DoPrintfWchar(const wxChar *format, ...); | |
335 | static wxString DoFormatWchar(const wxChar *format, ...); | |
336 | #endif | |
337 | #if wxUSE_UNICODE_UTF8 | |
338 | int DoPrintfUtf8(const char *format, ...); | |
339 | static wxString DoFormatUtf8(const char *format, ...); | |
340 | #endif | |
c9f78968 VS |
341 | }; |
342 | ||
343 | // this class contains template wrappers for wxString's vararg methods, it's | |
344 | // intentionally *not* exported from the DLL in order to fix the VC6 bug | |
345 | // described above | |
346 | class wxStringPrintfMixin : public wxStringPrintfMixinBase | |
347 | { | |
348 | private: | |
349 | // to further complicate things, we can't return wxString from | |
350 | // wxStringPrintfMixin::Format() because wxString is not yet declared at | |
351 | // this point; the solution is to use this fake type trait template - this | |
352 | // way the compiler won't know the return type until Format() is used | |
353 | // (this doesn't compile with Watcom, but VC6 compiles it just fine): | |
354 | template<typename T> struct StringReturnType | |
355 | { | |
356 | typedef wxString type; | |
357 | }; | |
358 | ||
359 | public: | |
360 | // these are duplicated wxString methods, they're also declared below | |
361 | // if !wxNEEDS_WXSTRING_PRINTF_MIXIN: | |
362 | ||
862bb5c7 | 363 | // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1; |
d1f6e2cf | 364 | WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type, |
1528e0b8 | 365 | Format, 1, (const wxFormatString&), |
d1f6e2cf | 366 | DoFormatWchar, DoFormatUtf8) |
2523e9b7 VS |
367 | // We have to implement the version without template arguments manually |
368 | // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC | |
369 | // normally does it itself. It has to be a template so that we can use | |
81051829 VS |
370 | // the hack, even though there's no real template parameter. We can't move |
371 | // it to wxStrig, because it would shadow these versions of Format() then. | |
2523e9b7 VS |
372 | template<typename T> |
373 | inline static typename StringReturnType<T>::type | |
81051829 | 374 | Format(const T& fmt) |
2523e9b7 | 375 | { |
81051829 VS |
376 | // NB: this doesn't compile if T is not (some form of) a string; |
377 | // this makes Format's prototype equivalent to | |
378 | // Format(const wxFormatString& fmt) | |
379 | return DoFormatWchar(wxFormatString(fmt)); | |
2523e9b7 VS |
380 | } |
381 | ||
382 | // int Printf(const wxString& format, ...); | |
1528e0b8 | 383 | WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&), |
d1f6e2cf | 384 | DoPrintfWchar, DoPrintfUtf8) |
862bb5c7 | 385 | // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2; |
1528e0b8 | 386 | WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&), |
d1f6e2cf | 387 | DoPrintfWchar, DoPrintfUtf8) |
c9f78968 VS |
388 | |
389 | protected: | |
390 | wxStringPrintfMixin() : wxStringPrintfMixinBase() {} | |
391 | }; | |
392 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
393 | ||
394 | ||
b6339dde VZ |
395 | // ---------------------------------------------------------------------------- |
396 | // wxString: string class trying to be compatible with std::string, MFC | |
397 | // CString and wxWindows 1.x wxString all at once | |
e87b7833 MB |
398 | // --------------------------------------------------------------------------- |
399 | ||
c9f78968 VS |
400 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
401 | // "non dll-interface class 'wxStringPrintfMixin' used as base interface | |
402 | // for dll-interface class 'wxString'" -- this is OK in our case | |
403 | #pragma warning (disable:4275) | |
e87b7833 MB |
404 | #endif |
405 | ||
b0c4d5d7 VS |
406 | #if wxUSE_UNICODE_UTF8 |
407 | // see the comment near wxString::iterator for why we need this | |
326a863a | 408 | class WXDLLIMPEXP_BASE wxStringIteratorNode |
b0c4d5d7 | 409 | { |
326a863a | 410 | public: |
39c20230 VS |
411 | wxStringIteratorNode() |
412 | : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {} | |
413 | wxStringIteratorNode(const wxString *str, | |
414 | wxStringImpl::const_iterator *citer) | |
415 | { DoSet(str, citer, NULL); } | |
416 | wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter) | |
417 | { DoSet(str, NULL, iter); } | |
418 | ~wxStringIteratorNode() | |
419 | { clear(); } | |
420 | ||
421 | inline void set(const wxString *str, wxStringImpl::const_iterator *citer) | |
422 | { clear(); DoSet(str, citer, NULL); } | |
423 | inline void set(const wxString *str, wxStringImpl::iterator *iter) | |
424 | { clear(); DoSet(str, NULL, iter); } | |
b0c4d5d7 VS |
425 | |
426 | const wxString *m_str; | |
427 | wxStringImpl::const_iterator *m_citer; | |
428 | wxStringImpl::iterator *m_iter; | |
429 | wxStringIteratorNode *m_prev, *m_next; | |
300b44a9 | 430 | |
39c20230 VS |
431 | private: |
432 | inline void clear(); | |
433 | inline void DoSet(const wxString *str, | |
434 | wxStringImpl::const_iterator *citer, | |
435 | wxStringImpl::iterator *iter); | |
436 | ||
300b44a9 VS |
437 | // the node belongs to a particular iterator instance, it's not copied |
438 | // when a copy of the iterator is made | |
c0c133e1 | 439 | wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode); |
b0c4d5d7 VS |
440 | }; |
441 | #endif // wxUSE_UNICODE_UTF8 | |
442 | ||
8f93a29f | 443 | class WXDLLIMPEXP_BASE wxString |
c9f78968 | 444 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
8f93a29f | 445 | : public wxStringPrintfMixin |
c9f78968 VS |
446 | #endif |
447 | { | |
e87b7833 MB |
448 | // NB: special care was taken in arranging the member functions in such order |
449 | // that all inline functions can be effectively inlined, verify that all | |
20aa779e | 450 | // performance critical functions are still inlined if you change order! |
8f93a29f | 451 | public: |
8f93a29f VS |
452 | // an 'invalid' value for string index, moved to this place due to a CW bug |
453 | static const size_t npos; | |
8f93a29f | 454 | |
e87b7833 MB |
455 | private: |
456 | // if we hadn't made these operators private, it would be possible to | |
457 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
458 | // converted to char in C and we do have operator=(char) | |
459 | // | |
460 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
461 | // to assign another numeric type to wxString will now result in | |
462 | // ambiguity between operator=(char) and operator=(int) | |
463 | wxString& operator=(int); | |
464 | ||
8f93a29f VS |
465 | // these methods are not implemented - there is _no_ conversion from int to |
466 | // string, you're doing something wrong if the compiler wants to call it! | |
467 | // | |
468 | // try `s << i' or `s.Printf("%d", i)' instead | |
469 | wxString(int); | |
470 | ||
471 | ||
472 | // buffer for holding temporary substring when using any of the methods | |
473 | // that take (char*,size_t) or (wchar_t*,size_t) arguments: | |
8f93a29f VS |
474 | template<typename T> |
475 | struct SubstrBufFromType | |
476 | { | |
477 | T data; | |
478 | size_t len; | |
479 | ||
8f93a29f | 480 | SubstrBufFromType(const T& data_, size_t len_) |
9ef1ad0d VZ |
481 | : data(data_), len(len_) |
482 | { | |
483 | wxASSERT_MSG( len != npos, "must have real length" ); | |
484 | } | |
8f93a29f VS |
485 | }; |
486 | ||
487 | #if wxUSE_UNICODE_UTF8 | |
81727065 VS |
488 | // even char* -> char* needs conversion, from locale charset to UTF-8 |
489 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC; | |
490 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB; | |
8f93a29f VS |
491 | #elif wxUSE_UNICODE_WCHAR |
492 | typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC; | |
493 | typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB; | |
8f93a29f VS |
494 | #else |
495 | typedef SubstrBufFromType<const char*> SubstrBufFromMB; | |
496 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC; | |
8f93a29f VS |
497 | #endif |
498 | ||
499 | ||
500 | // Functions implementing primitive operations on string data; wxString | |
501 | // methods and iterators are implemented in terms of it. The differences | |
502 | // between UTF-8 and wchar_t* representations of the string are mostly | |
503 | // contained here. | |
504 | ||
81727065 VS |
505 | #if wxUSE_UNICODE_UTF8 |
506 | static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength, | |
507 | const wxMBConv& conv); | |
508 | static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength, | |
509 | const wxMBConv& conv); | |
510 | #elif wxUSE_UNICODE_WCHAR | |
8f93a29f | 511 | static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength, |
a962cdf4 | 512 | const wxMBConv& conv); |
8f93a29f VS |
513 | #else |
514 | static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength, | |
a962cdf4 | 515 | const wxMBConv& conv); |
8f93a29f VS |
516 | #endif |
517 | ||
518 | #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE | |
a962cdf4 | 519 | // returns C string encoded as the implementation expects: |
8f93a29f | 520 | #if wxUSE_UNICODE |
a962cdf4 | 521 | static const wchar_t* ImplStr(const wchar_t* str) |
e8f59039 | 522 | { return str ? str : wxT(""); } |
a962cdf4 | 523 | static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) |
e8f59039 VS |
524 | { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); } |
525 | static wxWCharBuffer ImplStr(const char* str, | |
526 | const wxMBConv& conv = wxConvLibc) | |
527 | { return ConvertStr(str, npos, conv).data; } | |
528 | static SubstrBufFromMB ImplStr(const char* str, size_t n, | |
529 | const wxMBConv& conv = wxConvLibc) | |
530 | { return ConvertStr(str, n, conv); } | |
8f93a29f | 531 | #else |
e8f59039 VS |
532 | static const char* ImplStr(const char* str, |
533 | const wxMBConv& WXUNUSED(conv) = wxConvLibc) | |
534 | { return str ? str : ""; } | |
535 | static const SubstrBufFromMB ImplStr(const char* str, size_t n, | |
536 | const wxMBConv& WXUNUSED(conv) = wxConvLibc) | |
537 | { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); } | |
8f93a29f VS |
538 | static wxCharBuffer ImplStr(const wchar_t* str) |
539 | { return ConvertStr(str, npos, wxConvLibc).data; } | |
540 | static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
541 | { return ConvertStr(str, n, wxConvLibc); } | |
542 | #endif | |
543 | ||
8f93a29f VS |
544 | // translates position index in wxString to/from index in underlying |
545 | // wxStringImpl: | |
546 | static size_t PosToImpl(size_t pos) { return pos; } | |
547 | static void PosLenToImpl(size_t pos, size_t len, | |
548 | size_t *implPos, size_t *implLen) | |
549 | { *implPos = pos; *implLen = len; } | |
11aac4ba | 550 | static size_t LenToImpl(size_t len) { return len; } |
8f93a29f VS |
551 | static size_t PosFromImpl(size_t pos) { return pos; } |
552 | ||
68482dc5 VZ |
553 | // we don't want to define these as empty inline functions as it could |
554 | // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown | |
555 | // in debug build where the inline functions are not effectively inlined | |
556 | #define wxSTRING_INVALIDATE_CACHE() | |
557 | #define wxSTRING_INVALIDATE_CACHED_LENGTH() | |
558 | #define wxSTRING_UPDATE_CACHED_LENGTH(n) | |
559 | #define wxSTRING_SET_CACHED_LENGTH(n) | |
560 | ||
8f93a29f VS |
561 | #else // wxUSE_UNICODE_UTF8 |
562 | ||
467175ab VS |
563 | static wxCharBuffer ImplStr(const char* str, |
564 | const wxMBConv& conv = wxConvLibc) | |
565 | { return ConvertStr(str, npos, conv).data; } | |
566 | static SubstrBufFromMB ImplStr(const char* str, size_t n, | |
567 | const wxMBConv& conv = wxConvLibc) | |
568 | { return ConvertStr(str, n, conv); } | |
81727065 | 569 | |
467175ab | 570 | static wxCharBuffer ImplStr(const wchar_t* str) |
5487ff0f | 571 | { return ConvertStr(str, npos, wxMBConvUTF8()).data; } |
467175ab | 572 | static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) |
5487ff0f | 573 | { return ConvertStr(str, n, wxMBConvUTF8()); } |
81727065 | 574 | |
68482dc5 VZ |
575 | #if wxUSE_STRING_POS_CACHE |
576 | // this is an extremely simple cache used by PosToImpl(): each cache element | |
577 | // contains the string it applies to and the index corresponding to the last | |
578 | // used position in this wxString in its m_impl string | |
579 | // | |
580 | // NB: notice that this struct (and nested Element one) must be a POD or we | |
581 | // wouldn't be able to use a thread-local variable of this type, in | |
582 | // particular it should have no ctor -- we rely on statics being | |
583 | // initialized to 0 instead | |
584 | struct Cache | |
585 | { | |
586 | enum { SIZE = 8 }; | |
587 | ||
588 | struct Element | |
589 | { | |
590 | const wxString *str; // the string to which this element applies | |
591 | size_t pos, // the cached index in this string | |
592 | impl, // the corresponding position in its m_impl | |
593 | len; // cached length or npos if unknown | |
594 | ||
595 | // reset cached index to 0 | |
596 | void ResetPos() { pos = impl = 0; } | |
597 | ||
598 | // reset position and length | |
599 | void Reset() { ResetPos(); len = npos; } | |
600 | }; | |
601 | ||
602 | // cache the indices mapping for the last few string used | |
603 | Element cached[SIZE]; | |
604 | ||
605 | // the last used index | |
606 | unsigned lastUsed; | |
607 | }; | |
608 | ||
e810df36 VZ |
609 | #ifndef wxHAS_COMPILER_TLS |
610 | // we must use an accessor function and not a static variable when the TLS | |
611 | // variables support is implemented in the library (and not by the compiler) | |
612 | // because the global s_cache variable could be not yet initialized when a | |
613 | // ctor of another global object is executed and if that ctor uses any | |
614 | // wxString methods, bad things happen | |
615 | // | |
616 | // however notice that this approach does not work when compiler TLS is used, | |
617 | // at least not with g++ 4.1.2 under amd64 as it apparently compiles code | |
618 | // using this accessor incorrectly when optimizations are enabled (-O2 is | |
619 | // enough) -- luckily we don't need it then neither as static __thread | |
620 | // variables are initialized by 0 anyhow then and so we can use the variable | |
621 | // directly | |
e317bd3f | 622 | WXEXPORT static Cache& GetCache() |
8b73c531 VZ |
623 | { |
624 | static wxTLS_TYPE(Cache) s_cache; | |
625 | ||
626 | return wxTLS_VALUE(s_cache); | |
627 | } | |
cbec0f40 | 628 | |
ad8ae788 VZ |
629 | // this helper struct is used to ensure that GetCache() is called during |
630 | // static initialization time, i.e. before any threads creation, as otherwise | |
631 | // the static s_cache construction inside GetCache() wouldn't be MT-safe | |
632 | friend struct wxStrCacheInitializer; | |
e810df36 VZ |
633 | #else // wxHAS_COMPILER_TLS |
634 | static wxTLS_TYPE(Cache) ms_cache; | |
635 | static Cache& GetCache() { return wxTLS_VALUE(ms_cache); } | |
636 | #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS | |
637 | ||
638 | static Cache::Element *GetCacheBegin() { return GetCache().cached; } | |
639 | static Cache::Element *GetCacheEnd() { return GetCacheBegin() + Cache::SIZE; } | |
640 | static unsigned& LastUsedCacheElement() { return GetCache().lastUsed; } | |
ad8ae788 VZ |
641 | |
642 | // this is used in debug builds only to provide a convenient function, | |
643 | // callable from a debugger, to show the cache contents | |
68482dc5 VZ |
644 | friend struct wxStrCacheDumper; |
645 | ||
646 | // uncomment this to have access to some profiling statistics on program | |
647 | // termination | |
648 | //#define wxPROFILE_STRING_CACHE | |
649 | ||
650 | #ifdef wxPROFILE_STRING_CACHE | |
651 | static struct PosToImplCacheStats | |
652 | { | |
653 | unsigned postot, // total non-trivial calls to PosToImpl | |
654 | poshits, // cache hits from PosToImpl() | |
655 | mishits, // cached position beyond the needed one | |
656 | sumpos, // sum of all positions, used to compute the | |
657 | // average position after dividing by postot | |
658 | sumofs, // sum of all offsets after using the cache, used to | |
659 | // compute the average after dividing by hits | |
660 | lentot, // number of total calls to length() | |
661 | lenhits; // number of cache hits in length() | |
662 | } ms_cacheStats; | |
663 | ||
8c3b65d9 | 664 | friend struct wxStrCacheStatsDumper; |
68482dc5 VZ |
665 | |
666 | #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++ | |
667 | #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val) | |
668 | #else // !wxPROFILE_STRING_CACHE | |
669 | #define wxCACHE_PROFILE_FIELD_INC(field) | |
670 | #define wxCACHE_PROFILE_FIELD_ADD(field, val) | |
671 | #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE | |
672 | ||
673 | // note: it could seem that the functions below shouldn't be inline because | |
674 | // they are big, contain loops and so the compiler shouldn't be able to | |
675 | // inline them anyhow, however moving them into string.cpp does decrease the | |
676 | // code performance by ~5%, at least when using g++ 4.1 so do keep them here | |
677 | // unless tests show that it's not advantageous any more | |
678 | ||
679 | // return the pointer to the cache element for this string or NULL if not | |
680 | // cached | |
681 | Cache::Element *FindCacheElement() const | |
682 | { | |
683 | // profiling seems to show a small but consistent gain if we use this | |
684 | // simple loop instead of starting from the last used element (there are | |
685 | // a lot of misses in this function...) | |
e317bd3f | 686 | Cache::Element * const cacheBegin = GetCacheBegin(); |
6e42b980 | 687 | #ifndef wxHAS_COMPILER_TLS |
cbec0f40 | 688 | // during destruction tls calls may return NULL, in this case return NULL |
e317bd3f SC |
689 | // immediately without accessing anything else |
690 | if ( cacheBegin == NULL ) | |
c1158f7b | 691 | return NULL; |
e317bd3f SC |
692 | #endif |
693 | Cache::Element * const cacheEnd = GetCacheEnd(); | |
694 | for ( Cache::Element *c = cacheBegin; c != cacheEnd; c++ ) | |
68482dc5 VZ |
695 | { |
696 | if ( c->str == this ) | |
697 | return c; | |
698 | } | |
699 | ||
700 | return NULL; | |
701 | } | |
702 | ||
703 | // unlike FindCacheElement(), this one always returns a valid pointer to the | |
704 | // cache element for this string, it may have valid last cached position and | |
705 | // its corresponding index in the byte string or not | |
706 | Cache::Element *GetCacheElement() const | |
707 | { | |
8b73c531 VZ |
708 | Cache::Element * const cacheBegin = GetCacheBegin(); |
709 | Cache::Element * const cacheEnd = GetCacheEnd(); | |
710 | Cache::Element * const cacheStart = cacheBegin + LastUsedCacheElement(); | |
68482dc5 VZ |
711 | |
712 | // check the last used first, this does no (measurable) harm for a miss | |
713 | // but does help for simple loops addressing the same string all the time | |
714 | if ( cacheStart->str == this ) | |
715 | return cacheStart; | |
716 | ||
717 | // notice that we're going to check cacheStart again inside this call but | |
718 | // profiling shows that it's still faster to use a simple loop like | |
719 | // inside FindCacheElement() than manually looping with wrapping starting | |
720 | // from the cache entry after the start one | |
721 | Cache::Element *c = FindCacheElement(); | |
722 | if ( !c ) | |
723 | { | |
724 | // claim the next cache entry for this string | |
725 | c = cacheStart; | |
726 | if ( ++c == cacheEnd ) | |
727 | c = cacheBegin; | |
728 | ||
729 | c->str = this; | |
730 | c->Reset(); | |
731 | ||
732 | // and remember the last used element | |
8b73c531 | 733 | LastUsedCacheElement() = c - cacheBegin; |
68482dc5 VZ |
734 | } |
735 | ||
736 | return c; | |
737 | } | |
738 | ||
739 | size_t DoPosToImpl(size_t pos) const | |
740 | { | |
741 | wxCACHE_PROFILE_FIELD_INC(postot); | |
742 | ||
743 | // NB: although the case of pos == 1 (and offset from cached position | |
744 | // equal to 1) are common, nothing is gained by writing special code | |
745 | // for handling them, the compiler (at least g++ 4.1 used) seems to | |
746 | // optimize the code well enough on its own | |
747 | ||
748 | wxCACHE_PROFILE_FIELD_ADD(sumpos, pos); | |
749 | ||
750 | Cache::Element * const cache = GetCacheElement(); | |
751 | ||
752 | // cached position can't be 0 so if it is, it means that this entry was | |
753 | // used for length caching only so far, i.e. it doesn't count as a hit | |
754 | // from our point of view | |
755 | if ( cache->pos ) | |
1224b8a3 | 756 | { |
68482dc5 | 757 | wxCACHE_PROFILE_FIELD_INC(poshits); |
1224b8a3 | 758 | } |
68482dc5 VZ |
759 | |
760 | if ( pos == cache->pos ) | |
761 | return cache->impl; | |
762 | ||
763 | // this seems to happen only rarely so just reset the cache in this case | |
764 | // instead of complicating code even further by seeking backwards in this | |
765 | // case | |
766 | if ( cache->pos > pos ) | |
767 | { | |
768 | wxCACHE_PROFILE_FIELD_INC(mishits); | |
769 | ||
770 | cache->ResetPos(); | |
771 | } | |
772 | ||
773 | wxCACHE_PROFILE_FIELD_ADD(sumofs, pos - cache->pos); | |
774 | ||
775 | ||
776 | wxStringImpl::const_iterator i(m_impl.begin() + cache->impl); | |
777 | for ( size_t n = cache->pos; n < pos; n++ ) | |
778 | wxStringOperations::IncIter(i); | |
779 | ||
780 | cache->pos = pos; | |
781 | cache->impl = i - m_impl.begin(); | |
782 | ||
783 | wxSTRING_CACHE_ASSERT( | |
784 | (int)cache->impl == (begin() + pos).impl() - m_impl.begin() ); | |
785 | ||
786 | return cache->impl; | |
787 | } | |
788 | ||
789 | void InvalidateCache() | |
790 | { | |
791 | Cache::Element * const cache = FindCacheElement(); | |
792 | if ( cache ) | |
793 | cache->Reset(); | |
794 | } | |
795 | ||
796 | void InvalidateCachedLength() | |
797 | { | |
798 | Cache::Element * const cache = FindCacheElement(); | |
799 | if ( cache ) | |
800 | cache->len = npos; | |
801 | } | |
802 | ||
803 | void SetCachedLength(size_t len) | |
804 | { | |
805 | // we optimistically cache the length here even if the string wasn't | |
806 | // present in the cache before, this seems to do no harm and the | |
807 | // potential for avoiding length recomputation for long strings looks | |
808 | // interesting | |
809 | GetCacheElement()->len = len; | |
810 | } | |
811 | ||
812 | void UpdateCachedLength(ptrdiff_t delta) | |
813 | { | |
814 | Cache::Element * const cache = FindCacheElement(); | |
815 | if ( cache && cache->len != npos ) | |
816 | { | |
817 | wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache->len + delta >= 0 ); | |
818 | ||
819 | cache->len += delta; | |
820 | } | |
821 | } | |
822 | ||
823 | #define wxSTRING_INVALIDATE_CACHE() InvalidateCache() | |
824 | #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength() | |
825 | #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n) | |
826 | #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n) | |
827 | #else // !wxUSE_STRING_POS_CACHE | |
828 | size_t DoPosToImpl(size_t pos) const | |
829 | { | |
830 | return (begin() + pos).impl() - m_impl.begin(); | |
831 | } | |
832 | ||
833 | #define wxSTRING_INVALIDATE_CACHE() | |
834 | #define wxSTRING_INVALIDATE_CACHED_LENGTH() | |
835 | #define wxSTRING_UPDATE_CACHED_LENGTH(n) | |
836 | #define wxSTRING_SET_CACHED_LENGTH(n) | |
837 | #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE | |
838 | ||
8f93a29f VS |
839 | size_t PosToImpl(size_t pos) const |
840 | { | |
68482dc5 | 841 | return pos == 0 || pos == npos ? pos : DoPosToImpl(pos); |
64a044d5 VZ |
842 | } |
843 | ||
81727065 VS |
844 | void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const; |
845 | ||
846 | size_t LenToImpl(size_t len) const | |
847 | { | |
848 | size_t pos, len2; | |
849 | PosLenToImpl(0, len, &pos, &len2); | |
850 | return len2; | |
851 | } | |
852 | ||
8f93a29f VS |
853 | size_t PosFromImpl(size_t pos) const |
854 | { | |
855 | if ( pos == 0 || pos == npos ) | |
856 | return pos; | |
857 | else | |
b0c4d5d7 | 858 | return const_iterator(this, m_impl.begin() + pos) - begin(); |
8f93a29f | 859 | } |
8f93a29f VS |
860 | #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8 |
861 | ||
8f93a29f VS |
862 | public: |
863 | // standard types | |
864 | typedef wxUniChar value_type; | |
865 | typedef wxUniChar char_type; | |
866 | typedef wxUniCharRef reference; | |
867 | typedef wxChar* pointer; | |
868 | typedef const wxChar* const_pointer; | |
869 | ||
870 | typedef size_t size_type; | |
871 | typedef wxUniChar const_reference; | |
872 | ||
302acc45 | 873 | #if wxUSE_STD_STRING |
81727065 VS |
874 | #if wxUSE_UNICODE_UTF8 |
875 | // random access is not O(1), as required by Random Access Iterator | |
876 | #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag | |
877 | #else | |
878 | #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag | |
879 | #endif | |
0514adaf | 880 | #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category; |
a962cdf4 | 881 | #else |
302acc45 VZ |
882 | // not defining iterator_category at all in this case is better than defining |
883 | // it as some dummy type -- at least it results in more intelligible error | |
884 | // messages | |
0514adaf | 885 | #define WX_DEFINE_ITERATOR_CATEGORY(cat) |
a962cdf4 VS |
886 | #endif |
887 | ||
b0c4d5d7 | 888 | #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \ |
8f93a29f VS |
889 | private: \ |
890 | typedef wxStringImpl::iterator_name underlying_iterator; \ | |
891 | public: \ | |
0514adaf | 892 | WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \ |
8f93a29f | 893 | typedef wxUniChar value_type; \ |
a962cdf4 | 894 | typedef int difference_type; \ |
8f93a29f VS |
895 | typedef reference_type reference; \ |
896 | typedef pointer_type pointer; \ | |
897 | \ | |
a962cdf4 | 898 | reference operator[](size_t n) const { return *(*this + n); } \ |
8f93a29f VS |
899 | \ |
900 | iterator_name& operator++() \ | |
467175ab | 901 | { wxStringOperations::IncIter(m_cur); return *this; } \ |
8f93a29f | 902 | iterator_name& operator--() \ |
467175ab | 903 | { wxStringOperations::DecIter(m_cur); return *this; } \ |
8f93a29f VS |
904 | iterator_name operator++(int) \ |
905 | { \ | |
906 | iterator_name tmp = *this; \ | |
467175ab | 907 | wxStringOperations::IncIter(m_cur); \ |
8f93a29f VS |
908 | return tmp; \ |
909 | } \ | |
910 | iterator_name operator--(int) \ | |
911 | { \ | |
912 | iterator_name tmp = *this; \ | |
467175ab | 913 | wxStringOperations::DecIter(m_cur); \ |
8f93a29f VS |
914 | return tmp; \ |
915 | } \ | |
916 | \ | |
b5343e06 | 917 | iterator_name& operator+=(ptrdiff_t n) \ |
467175ab VS |
918 | { \ |
919 | m_cur = wxStringOperations::AddToIter(m_cur, n); \ | |
920 | return *this; \ | |
921 | } \ | |
b5343e06 | 922 | iterator_name& operator-=(ptrdiff_t n) \ |
467175ab VS |
923 | { \ |
924 | m_cur = wxStringOperations::AddToIter(m_cur, -n); \ | |
925 | return *this; \ | |
467175ab | 926 | } \ |
8f93a29f | 927 | \ |
89360a8c | 928 | difference_type operator-(const iterator_name& i) const \ |
467175ab | 929 | { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \ |
8f93a29f | 930 | \ |
f2a1b1bd | 931 | bool operator==(const iterator_name& i) const \ |
8f93a29f VS |
932 | { return m_cur == i.m_cur; } \ |
933 | bool operator!=(const iterator_name& i) const \ | |
934 | { return m_cur != i.m_cur; } \ | |
935 | \ | |
936 | bool operator<(const iterator_name& i) const \ | |
937 | { return m_cur < i.m_cur; } \ | |
938 | bool operator>(const iterator_name& i) const \ | |
939 | { return m_cur > i.m_cur; } \ | |
940 | bool operator<=(const iterator_name& i) const \ | |
941 | { return m_cur <= i.m_cur; } \ | |
942 | bool operator>=(const iterator_name& i) const \ | |
943 | { return m_cur >= i.m_cur; } \ | |
944 | \ | |
945 | private: \ | |
946 | /* for internal wxString use only: */ \ | |
cf9a878b | 947 | underlying_iterator impl() const { return m_cur; } \ |
8f93a29f | 948 | \ |
b5dbe15d VS |
949 | friend class wxString; \ |
950 | friend class wxCStrData; \ | |
8f93a29f VS |
951 | \ |
952 | private: \ | |
89360a8c | 953 | underlying_iterator m_cur |
8f93a29f | 954 | |
b5dbe15d | 955 | class WXDLLIMPEXP_FWD_BASE const_iterator; |
8f93a29f | 956 | |
81727065 | 957 | #if wxUSE_UNICODE_UTF8 |
b0c4d5d7 VS |
958 | // NB: In UTF-8 build, (non-const) iterator needs to keep reference |
959 | // to the underlying wxStringImpl, because UTF-8 is variable-length | |
960 | // encoding and changing the value pointer to by an iterator (using | |
961 | // its operator*) requires calling wxStringImpl::replace() if the old | |
962 | // and new values differ in their encoding's length. | |
963 | // | |
964 | // Furthermore, the replace() call may invalid all iterators for the | |
965 | // string, so we have to keep track of outstanding iterators and update | |
966 | // them if replace() happens. | |
967 | // | |
968 | // This is implemented by maintaining linked list of iterators for every | |
969 | // string and traversing it in wxUniCharRef::operator=(). Head of the | |
970 | // list is stored in wxString. (FIXME-UTF8) | |
971 | ||
541aa821 | 972 | class WXDLLIMPEXP_BASE iterator |
81727065 | 973 | { |
b0c4d5d7 | 974 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); |
c7dc0057 | 975 | |
c67b782d | 976 | public: |
39c20230 | 977 | iterator() {} |
b0c4d5d7 VS |
978 | iterator(const iterator& i) |
979 | : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} | |
300b44a9 | 980 | iterator& operator=(const iterator& i) |
162e998c PC |
981 | { |
982 | if (&i != this) | |
983 | { | |
984 | m_cur = i.m_cur; | |
985 | m_node.set(i.str(), &m_cur); | |
986 | } | |
987 | return *this; | |
988 | } | |
b0c4d5d7 VS |
989 | |
990 | reference operator*() | |
6bd4f281 | 991 | { return wxUniCharRef::CreateForString(*str(), m_cur); } |
81727065 | 992 | |
b5343e06 | 993 | iterator operator+(ptrdiff_t n) const |
b0c4d5d7 | 994 | { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } |
b5343e06 | 995 | iterator operator-(ptrdiff_t n) const |
b0c4d5d7 | 996 | { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } |
81727065 VS |
997 | |
998 | private: | |
999 | iterator(wxString *str, underlying_iterator ptr) | |
b0c4d5d7 | 1000 | : m_cur(ptr), m_node(str, &m_cur) {} |
c7dc0057 | 1001 | |
5c33522f | 1002 | wxString* str() const { return const_cast<wxString*>(m_node.m_str); } |
b0c4d5d7 VS |
1003 | |
1004 | wxStringIteratorNode m_node; | |
81727065 VS |
1005 | |
1006 | friend class const_iterator; | |
1007 | }; | |
cf9a878b | 1008 | |
541aa821 | 1009 | class WXDLLIMPEXP_BASE const_iterator |
b0c4d5d7 VS |
1010 | { |
1011 | // NB: reference_type is intentionally value, not reference, the character | |
1012 | // may be encoded differently in wxString data: | |
1013 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); | |
1014 | ||
1015 | public: | |
39c20230 | 1016 | const_iterator() {} |
b0c4d5d7 VS |
1017 | const_iterator(const const_iterator& i) |
1018 | : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} | |
1019 | const_iterator(const iterator& i) | |
1020 | : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} | |
1021 | ||
300b44a9 | 1022 | const_iterator& operator=(const const_iterator& i) |
162e998c PC |
1023 | { |
1024 | if (&i != this) | |
1025 | { | |
1026 | m_cur = i.m_cur; | |
1027 | m_node.set(i.str(), &m_cur); | |
1028 | } | |
1029 | return *this; | |
1030 | } | |
300b44a9 | 1031 | const_iterator& operator=(const iterator& i) |
39c20230 | 1032 | { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } |
300b44a9 | 1033 | |
b0c4d5d7 VS |
1034 | reference operator*() const |
1035 | { return wxStringOperations::DecodeChar(m_cur); } | |
1036 | ||
b5343e06 | 1037 | const_iterator operator+(ptrdiff_t n) const |
b0c4d5d7 | 1038 | { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } |
b5343e06 | 1039 | const_iterator operator-(ptrdiff_t n) const |
b0c4d5d7 | 1040 | { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } |
b0c4d5d7 VS |
1041 | |
1042 | private: | |
1043 | // for internal wxString use only: | |
1044 | const_iterator(const wxString *str, underlying_iterator ptr) | |
1045 | : m_cur(ptr), m_node(str, &m_cur) {} | |
b0c4d5d7 VS |
1046 | |
1047 | const wxString* str() const { return m_node.m_str; } | |
1048 | ||
1049 | wxStringIteratorNode m_node; | |
1050 | }; | |
1051 | ||
cf9a878b VS |
1052 | size_t IterToImplPos(wxString::iterator i) const |
1053 | { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); } | |
1054 | ||
68482dc5 VZ |
1055 | iterator GetIterForNthChar(size_t n) |
1056 | { return iterator(this, m_impl.begin() + PosToImpl(n)); } | |
1057 | const_iterator GetIterForNthChar(size_t n) const | |
1058 | { return const_iterator(this, m_impl.begin() + PosToImpl(n)); } | |
81727065 | 1059 | #else // !wxUSE_UNICODE_UTF8 |
cf9a878b | 1060 | |
541aa821 | 1061 | class WXDLLIMPEXP_BASE iterator |
8f93a29f | 1062 | { |
b0c4d5d7 | 1063 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); |
8f93a29f | 1064 | |
81727065 | 1065 | public: |
39c20230 | 1066 | iterator() {} |
81727065 VS |
1067 | iterator(const iterator& i) : m_cur(i.m_cur) {} |
1068 | ||
b0c4d5d7 VS |
1069 | reference operator*() |
1070 | { return wxUniCharRef::CreateForString(m_cur); } | |
1071 | ||
b5343e06 | 1072 | iterator operator+(ptrdiff_t n) const |
467175ab | 1073 | { return iterator(wxStringOperations::AddToIter(m_cur, n)); } |
b5343e06 | 1074 | iterator operator-(ptrdiff_t n) const |
467175ab | 1075 | { return iterator(wxStringOperations::AddToIter(m_cur, -n)); } |
81727065 VS |
1076 | |
1077 | private: | |
1078 | // for internal wxString use only: | |
1079 | iterator(underlying_iterator ptr) : m_cur(ptr) {} | |
1080 | iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {} | |
1081 | ||
8f93a29f VS |
1082 | friend class const_iterator; |
1083 | }; | |
1084 | ||
541aa821 | 1085 | class WXDLLIMPEXP_BASE const_iterator |
8f93a29f VS |
1086 | { |
1087 | // NB: reference_type is intentionally value, not reference, the character | |
1088 | // may be encoded differently in wxString data: | |
b0c4d5d7 | 1089 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); |
8f93a29f VS |
1090 | |
1091 | public: | |
39c20230 | 1092 | const_iterator() {} |
81727065 | 1093 | const_iterator(const const_iterator& i) : m_cur(i.m_cur) {} |
8f93a29f | 1094 | const_iterator(const iterator& i) : m_cur(i.m_cur) {} |
81727065 | 1095 | |
b0c4d5d7 VS |
1096 | reference operator*() const |
1097 | { return wxStringOperations::DecodeChar(m_cur); } | |
1098 | ||
b5343e06 | 1099 | const_iterator operator+(ptrdiff_t n) const |
467175ab | 1100 | { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); } |
b5343e06 | 1101 | const_iterator operator-(ptrdiff_t n) const |
467175ab | 1102 | { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); } |
81727065 VS |
1103 | |
1104 | private: | |
1105 | // for internal wxString use only: | |
1106 | const_iterator(underlying_iterator ptr) : m_cur(ptr) {} | |
b0c4d5d7 VS |
1107 | const_iterator(const wxString *WXUNUSED(str), underlying_iterator ptr) |
1108 | : m_cur(ptr) {} | |
8f93a29f | 1109 | }; |
68482dc5 VZ |
1110 | |
1111 | iterator GetIterForNthChar(size_t n) { return begin() + n; } | |
1112 | const_iterator GetIterForNthChar(size_t n) const { return begin() + n; } | |
b0c4d5d7 | 1113 | #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8 |
8f93a29f | 1114 | |
a962cdf4 | 1115 | #undef WX_STR_ITERATOR_TAG |
8f93a29f VS |
1116 | #undef WX_STR_ITERATOR_IMPL |
1117 | ||
1118 | friend class iterator; | |
1119 | friend class const_iterator; | |
1120 | ||
1121 | template <typename T> | |
1122 | class reverse_iterator_impl | |
1123 | { | |
1124 | public: | |
1125 | typedef T iterator_type; | |
a962cdf4 | 1126 | |
0514adaf | 1127 | WX_DEFINE_ITERATOR_CATEGORY(typename T::iterator_category) |
8f93a29f | 1128 | typedef typename T::value_type value_type; |
a962cdf4 | 1129 | typedef typename T::difference_type difference_type; |
8f93a29f VS |
1130 | typedef typename T::reference reference; |
1131 | typedef typename T::pointer *pointer; | |
1132 | ||
39c20230 | 1133 | reverse_iterator_impl() {} |
8f93a29f VS |
1134 | reverse_iterator_impl(iterator_type i) : m_cur(i) {} |
1135 | reverse_iterator_impl(const reverse_iterator_impl& ri) | |
1136 | : m_cur(ri.m_cur) {} | |
1137 | ||
1138 | iterator_type base() const { return m_cur; } | |
1139 | ||
1140 | reference operator*() const { return *(m_cur-1); } | |
a962cdf4 | 1141 | reference operator[](size_t n) const { return *(*this + n); } |
8f93a29f VS |
1142 | |
1143 | reverse_iterator_impl& operator++() | |
1144 | { --m_cur; return *this; } | |
1145 | reverse_iterator_impl operator++(int) | |
1146 | { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } | |
1147 | reverse_iterator_impl& operator--() | |
1148 | { ++m_cur; return *this; } | |
1149 | reverse_iterator_impl operator--(int) | |
1150 | { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } | |
1151 | ||
50eeb96f | 1152 | // NB: explicit <T> in the functions below is to keep BCC 5.5 happy |
b5343e06 | 1153 | reverse_iterator_impl operator+(ptrdiff_t n) const |
50eeb96f | 1154 | { return reverse_iterator_impl<T>(m_cur - n); } |
b5343e06 | 1155 | reverse_iterator_impl operator-(ptrdiff_t n) const |
50eeb96f | 1156 | { return reverse_iterator_impl<T>(m_cur + n); } |
b5343e06 | 1157 | reverse_iterator_impl operator+=(ptrdiff_t n) |
8f93a29f | 1158 | { m_cur -= n; return *this; } |
b5343e06 | 1159 | reverse_iterator_impl operator-=(ptrdiff_t n) |
8f93a29f VS |
1160 | { m_cur += n; return *this; } |
1161 | ||
1162 | unsigned operator-(const reverse_iterator_impl& i) const | |
1163 | { return i.m_cur - m_cur; } | |
1164 | ||
1165 | bool operator==(const reverse_iterator_impl& ri) const | |
1166 | { return m_cur == ri.m_cur; } | |
1167 | bool operator!=(const reverse_iterator_impl& ri) const | |
1168 | { return !(*this == ri); } | |
1169 | ||
1170 | bool operator<(const reverse_iterator_impl& i) const | |
1171 | { return m_cur > i.m_cur; } | |
1172 | bool operator>(const reverse_iterator_impl& i) const | |
1173 | { return m_cur < i.m_cur; } | |
1174 | bool operator<=(const reverse_iterator_impl& i) const | |
1175 | { return m_cur >= i.m_cur; } | |
1176 | bool operator>=(const reverse_iterator_impl& i) const | |
1177 | { return m_cur <= i.m_cur; } | |
1178 | ||
1179 | private: | |
1180 | iterator_type m_cur; | |
1181 | }; | |
e87b7833 | 1182 | |
8f93a29f VS |
1183 | typedef reverse_iterator_impl<iterator> reverse_iterator; |
1184 | typedef reverse_iterator_impl<const_iterator> const_reverse_iterator; | |
e90c1d2a | 1185 | |
67cff9dc VZ |
1186 | private: |
1187 | // used to transform an expression built using c_str() (and hence of type | |
1188 | // wxCStrData) to an iterator into the string | |
1189 | static const_iterator CreateConstIterator(const wxCStrData& data) | |
1190 | { | |
b0c4d5d7 VS |
1191 | return const_iterator(data.m_str, |
1192 | (data.m_str->begin() + data.m_offset).impl()); | |
67cff9dc VZ |
1193 | } |
1194 | ||
59953bf4 VS |
1195 | // in UTF-8 STL build, creation from std::string requires conversion under |
1196 | // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor; | |
1197 | // instead we define dummy type that lets us have wxString ctor for creation | |
1198 | // from wxStringImpl that couldn't be used by user code (in all other builds, | |
1199 | // "standard" ctors can be used): | |
1200 | #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING | |
1201 | struct CtorFromStringImplTag {}; | |
1202 | ||
1203 | wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src) | |
1204 | : m_impl(src) {} | |
1205 | ||
1206 | static wxString FromImpl(const wxStringImpl& src) | |
1207 | { return wxString((CtorFromStringImplTag*)NULL, src); } | |
1208 | #else | |
82a99c69 | 1209 | #if !wxUSE_STL_BASED_WXSTRING |
59953bf4 | 1210 | wxString(const wxStringImpl& src) : m_impl(src) { } |
82a99c69 VS |
1211 | // else: already defined as wxString(wxStdString) below |
1212 | #endif | |
59953bf4 VS |
1213 | static wxString FromImpl(const wxStringImpl& src) { return wxString(src); } |
1214 | #endif | |
1215 | ||
67cff9dc VZ |
1216 | public: |
1217 | // constructors and destructor | |
1218 | // ctor for an empty string | |
1219 | wxString() {} | |
1220 | ||
1221 | // copy ctor | |
67cff9dc VZ |
1222 | wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { } |
1223 | ||
1224 | // string containing nRepeat copies of ch | |
623633ee | 1225 | wxString(wxUniChar ch, size_t nRepeat = 1 ) |
67cff9dc VZ |
1226 | { assign(nRepeat, ch); } |
1227 | wxString(size_t nRepeat, wxUniChar ch) | |
1228 | { assign(nRepeat, ch); } | |
1229 | wxString(wxUniCharRef ch, size_t nRepeat = 1) | |
1230 | { assign(nRepeat, ch); } | |
1231 | wxString(size_t nRepeat, wxUniCharRef ch) | |
1232 | { assign(nRepeat, ch); } | |
1233 | wxString(char ch, size_t nRepeat = 1) | |
1234 | { assign(nRepeat, ch); } | |
1235 | wxString(size_t nRepeat, char ch) | |
1236 | { assign(nRepeat, ch); } | |
1237 | wxString(wchar_t ch, size_t nRepeat = 1) | |
1238 | { assign(nRepeat, ch); } | |
1239 | wxString(size_t nRepeat, wchar_t ch) | |
1240 | { assign(nRepeat, ch); } | |
1241 | ||
1242 | // ctors from char* strings: | |
1243 | wxString(const char *psz) | |
1244 | : m_impl(ImplStr(psz)) {} | |
1245 | wxString(const char *psz, const wxMBConv& conv) | |
1246 | : m_impl(ImplStr(psz, conv)) {} | |
1247 | wxString(const char *psz, size_t nLength) | |
1248 | { assign(psz, nLength); } | |
1249 | wxString(const char *psz, const wxMBConv& conv, size_t nLength) | |
1250 | { | |
1251 | SubstrBufFromMB str(ImplStr(psz, nLength, conv)); | |
1252 | m_impl.assign(str.data, str.len); | |
1253 | } | |
1254 | ||
1255 | // and unsigned char*: | |
1256 | wxString(const unsigned char *psz) | |
1257 | : m_impl(ImplStr((const char*)psz)) {} | |
1258 | wxString(const unsigned char *psz, const wxMBConv& conv) | |
1259 | : m_impl(ImplStr((const char*)psz, conv)) {} | |
1260 | wxString(const unsigned char *psz, size_t nLength) | |
1261 | { assign((const char*)psz, nLength); } | |
1262 | wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength) | |
1263 | { | |
1264 | SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv)); | |
1265 | m_impl.assign(str.data, str.len); | |
1266 | } | |
1267 | ||
1268 | // ctors from wchar_t* strings: | |
1269 | wxString(const wchar_t *pwz) | |
1270 | : m_impl(ImplStr(pwz)) {} | |
1271 | wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv)) | |
1272 | : m_impl(ImplStr(pwz)) {} | |
1273 | wxString(const wchar_t *pwz, size_t nLength) | |
1274 | { assign(pwz, nLength); } | |
1275 | wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength) | |
1276 | { assign(pwz, nLength); } | |
1277 | ||
1278 | wxString(const wxCharBuffer& buf) | |
1279 | { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length | |
1280 | wxString(const wxWCharBuffer& buf) | |
1281 | { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length | |
1282 | ||
06e9cf13 VS |
1283 | // NB: this version uses m_impl.c_str() to force making a copy of the |
1284 | // string, so that "wxString(str.c_str())" idiom for passing strings | |
1285 | // between threads works | |
67cff9dc | 1286 | wxString(const wxCStrData& cstr) |
06e9cf13 | 1287 | : m_impl(cstr.AsString().m_impl.c_str()) { } |
67cff9dc VZ |
1288 | |
1289 | // as we provide both ctors with this signature for both char and unsigned | |
1290 | // char string, we need to provide one for wxCStrData to resolve ambiguity | |
1291 | wxString(const wxCStrData& cstr, size_t nLength) | |
1292 | : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {} | |
1293 | ||
1294 | // and because wxString is convertible to wxCStrData and const wxChar * | |
1295 | // we also need to provide this one | |
1296 | wxString(const wxString& str, size_t nLength) | |
1545d942 | 1297 | { assign(str, nLength); } |
67cff9dc | 1298 | |
68482dc5 VZ |
1299 | |
1300 | #if wxUSE_STRING_POS_CACHE | |
1301 | ~wxString() | |
1302 | { | |
1303 | // we need to invalidate our cache entry as another string could be | |
1304 | // recreated at the same address (unlikely, but still possible, with the | |
1305 | // heap-allocated strings but perfectly common with stack-allocated ones) | |
1306 | InvalidateCache(); | |
1307 | } | |
1308 | #endif // wxUSE_STRING_POS_CACHE | |
1309 | ||
67cff9dc | 1310 | // even if we're not built with wxUSE_STL == 1 it is very convenient to allow |
59953bf4 VS |
1311 | // implicit conversions from std::string to wxString and vice verse as this |
1312 | // allows to use the same strings in non-GUI and GUI code, however we don't | |
1313 | // want to unconditionally add this ctor as it would make wx lib dependent on | |
67cff9dc VZ |
1314 | // libstdc++ on some Linux versions which is bad, so instead we ask the |
1315 | // client code to define this wxUSE_STD_STRING symbol if they need it | |
59953bf4 | 1316 | #if wxUSE_STD_STRING |
59953bf4 VS |
1317 | #if wxUSE_UNICODE_WCHAR |
1318 | wxString(const wxStdWideString& str) : m_impl(str) {} | |
1319 | #else // UTF-8 or ANSI | |
1320 | wxString(const wxStdWideString& str) | |
1321 | { assign(str.c_str(), str.length()); } | |
1322 | #endif | |
1323 | ||
1324 | #if !wxUSE_UNICODE // ANSI build | |
1325 | // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too | |
1326 | wxString(const std::string& str) : m_impl(str) {} | |
1327 | #else // Unicode | |
1328 | wxString(const std::string& str) | |
1329 | { assign(str.c_str(), str.length()); } | |
1330 | #endif | |
7978bc72 | 1331 | #endif // wxUSE_STD_STRING |
59953bf4 | 1332 | |
7978bc72 VS |
1333 | // Unlike ctor from std::string, we provide conversion to std::string only |
1334 | // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default), | |
1335 | // because it conflicts with operator const char/wchar_t*: | |
1336 | #if wxUSE_STL | |
59953bf4 VS |
1337 | #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING |
1338 | // wxStringImpl is std::string in the encoding we want | |
1339 | operator const wxStdWideString&() const { return m_impl; } | |
1340 | #else | |
1341 | // wxStringImpl is either not std::string or needs conversion | |
1342 | operator wxStdWideString() const | |
1343 | // FIXME-UTF8: broken for embedded NULs | |
1344 | { return wxStdWideString(wc_str()); } | |
1345 | #endif | |
1346 | ||
111d9948 | 1347 | #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING |
59953bf4 VS |
1348 | // wxStringImpl is std::string in the encoding we want |
1349 | operator const std::string&() const { return m_impl; } | |
1350 | #else | |
1351 | // wxStringImpl is either not std::string or needs conversion | |
1352 | operator std::string() const | |
1353 | // FIXME-UTF8: broken for embedded NULs | |
1354 | { return std::string(mb_str()); } | |
1355 | #endif | |
111d9948 | 1356 | #endif // wxUSE_STL |
67cff9dc | 1357 | |
06e9cf13 VS |
1358 | wxString Clone() const |
1359 | { | |
1360 | // make a deep copy of the string, i.e. the returned string will have | |
1361 | // ref count = 1 with refcounted implementation | |
1362 | return wxString::FromImpl(wxStringImpl(m_impl.c_str(), m_impl.length())); | |
1363 | } | |
1364 | ||
8f93a29f | 1365 | // first valid index position |
b0c4d5d7 | 1366 | const_iterator begin() const { return const_iterator(this, m_impl.begin()); } |
81727065 | 1367 | iterator begin() { return iterator(this, m_impl.begin()); } |
8f93a29f | 1368 | // position one after the last valid one |
b0c4d5d7 | 1369 | const_iterator end() const { return const_iterator(this, m_impl.end()); } |
81727065 | 1370 | iterator end() { return iterator(this, m_impl.end()); } |
8f93a29f VS |
1371 | |
1372 | // first element of the reversed string | |
1373 | const_reverse_iterator rbegin() const | |
1374 | { return const_reverse_iterator(end()); } | |
1375 | reverse_iterator rbegin() | |
1376 | { return reverse_iterator(end()); } | |
1377 | // one beyond the end of the reversed string | |
1378 | const_reverse_iterator rend() const | |
1379 | { return const_reverse_iterator(begin()); } | |
1380 | reverse_iterator rend() | |
1381 | { return reverse_iterator(begin()); } | |
1382 | ||
1383 | // std::string methods: | |
1384 | #if wxUSE_UNICODE_UTF8 | |
68482dc5 VZ |
1385 | size_t length() const |
1386 | { | |
1387 | #if wxUSE_STRING_POS_CACHE | |
1388 | wxCACHE_PROFILE_FIELD_INC(lentot); | |
1389 | ||
1390 | Cache::Element * const cache = GetCacheElement(); | |
1391 | ||
1392 | if ( cache->len == npos ) | |
1393 | { | |
1394 | // it's probably not worth trying to be clever and using cache->pos | |
1395 | // here as it's probably 0 anyhow -- you usually call length() before | |
1396 | // starting to index the string | |
1397 | cache->len = end() - begin(); | |
1398 | } | |
1399 | else | |
1400 | { | |
1401 | wxCACHE_PROFILE_FIELD_INC(lenhits); | |
1402 | ||
1403 | wxSTRING_CACHE_ASSERT( (int)cache->len == end() - begin() ); | |
1404 | } | |
1405 | ||
1406 | return cache->len; | |
1407 | #else // !wxUSE_STRING_POS_CACHE | |
1408 | return end() - begin(); | |
1409 | #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE | |
1410 | } | |
8f93a29f VS |
1411 | #else |
1412 | size_t length() const { return m_impl.length(); } | |
1413 | #endif | |
ce76f779 | 1414 | |
8f93a29f VS |
1415 | size_type size() const { return length(); } |
1416 | size_type max_size() const { return npos; } | |
e90c1d2a | 1417 | |
8f93a29f | 1418 | bool empty() const { return m_impl.empty(); } |
b77afb41 | 1419 | |
68482dc5 VZ |
1420 | // NB: these methods don't have a well-defined meaning in UTF-8 case |
1421 | size_type capacity() const { return m_impl.capacity(); } | |
1422 | void reserve(size_t sz) { m_impl.reserve(sz); } | |
b77afb41 | 1423 | |
8f93a29f VS |
1424 | void resize(size_t nSize, wxUniChar ch = wxT('\0')) |
1425 | { | |
fe1b98f5 VZ |
1426 | const size_t len = length(); |
1427 | if ( nSize == len) | |
1428 | return; | |
1429 | ||
8f93a29f | 1430 | #if wxUSE_UNICODE_UTF8 |
fe1b98f5 | 1431 | if ( nSize < len ) |
8f93a29f | 1432 | { |
68482dc5 VZ |
1433 | wxSTRING_INVALIDATE_CACHE(); |
1434 | ||
fe1b98f5 VZ |
1435 | // we can't use wxStringImpl::resize() for truncating the string as it |
1436 | // counts in bytes, not characters | |
1437 | erase(nSize); | |
1438 | return; | |
8f93a29f | 1439 | } |
fe1b98f5 VZ |
1440 | |
1441 | // we also can't use (presumably more efficient) resize() if we have to | |
1442 | // append characters taking more than one byte | |
1443 | if ( !ch.IsAscii() ) | |
68482dc5 | 1444 | { |
fe1b98f5 | 1445 | append(nSize - len, ch); |
68482dc5 VZ |
1446 | } |
1447 | else // can use (presumably faster) resize() version | |
fe1b98f5 | 1448 | #endif // wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
1449 | { |
1450 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
1451 | ||
8f93a29f | 1452 | m_impl.resize(nSize, (wxStringCharType)ch); |
68482dc5 | 1453 | } |
8f93a29f | 1454 | } |
e90c1d2a | 1455 | |
8f93a29f VS |
1456 | wxString substr(size_t nStart = 0, size_t nLen = npos) const |
1457 | { | |
1458 | size_t pos, len; | |
1459 | PosLenToImpl(nStart, nLen, &pos, &len); | |
59953bf4 | 1460 | return FromImpl(m_impl.substr(pos, len)); |
8f93a29f | 1461 | } |
e90c1d2a | 1462 | |
3c67202d VZ |
1463 | // generic attributes & operations |
1464 | // as standard strlen() | |
e87b7833 | 1465 | size_t Len() const { return length(); } |
3c67202d | 1466 | // string contains any characters? |
e87b7833 | 1467 | bool IsEmpty() const { return empty(); } |
d775fa82 | 1468 | // empty string is "false", so !str will return true |
20aa779e | 1469 | bool operator!() const { return empty(); } |
735d1db6 VZ |
1470 | // truncate the string to given length |
1471 | wxString& Truncate(size_t uiLen); | |
3c67202d | 1472 | // empty string contents |
24f86c43 | 1473 | void Empty() { clear(); } |
3c67202d | 1474 | // empty the string and free memory |
68482dc5 | 1475 | void Clear() { clear(); } |
dd1eaa89 | 1476 | |
3c67202d VZ |
1477 | // contents test |
1478 | // Is an ascii value | |
c801d85f | 1479 | bool IsAscii() const; |
3c67202d | 1480 | // Is a number |
c801d85f | 1481 | bool IsNumber() const; |
3c67202d | 1482 | // Is a word |
c801d85f | 1483 | bool IsWord() const; |
c801d85f | 1484 | |
3c67202d VZ |
1485 | // data access (all indexes are 0 based) |
1486 | // read access | |
8f93a29f | 1487 | wxUniChar at(size_t n) const |
68482dc5 | 1488 | { return wxStringOperations::DecodeChar(m_impl.begin() + PosToImpl(n)); } |
c9f78968 | 1489 | wxUniChar GetChar(size_t n) const |
9d9ad673 | 1490 | { return at(n); } |
3c67202d | 1491 | // read/write access |
8f93a29f | 1492 | wxUniCharRef at(size_t n) |
68482dc5 | 1493 | { return *GetIterForNthChar(n); } |
c9f78968 | 1494 | wxUniCharRef GetWritableChar(size_t n) |
9d9ad673 | 1495 | { return at(n); } |
3c67202d | 1496 | // write access |
68482dc5 | 1497 | void SetChar(size_t n, wxUniChar ch) |
dd79ca2f | 1498 | { at(n) = ch; } |
c801d85f | 1499 | |
3c67202d | 1500 | // get last character |
8f93a29f | 1501 | wxUniChar Last() const |
564ab31a VS |
1502 | { |
1503 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); | |
1504 | return *rbegin(); | |
1505 | } | |
09443a26 | 1506 | |
3c67202d | 1507 | // get writable last character |
c9f78968 | 1508 | wxUniCharRef Last() |
564ab31a VS |
1509 | { |
1510 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); | |
1511 | return *rbegin(); | |
1512 | } | |
c801d85f | 1513 | |
d836ee96 | 1514 | /* |
9d9ad673 | 1515 | Note that we we must define all of the overloads below to avoid |
c9f78968 | 1516 | ambiguity when using str[0]. |
d836ee96 | 1517 | */ |
c9f78968 | 1518 | wxUniChar operator[](int n) const |
8f93a29f | 1519 | { return at(n); } |
c1df0a3b | 1520 | wxUniChar operator[](long n) const |
8f93a29f | 1521 | { return at(n); } |
c9f78968 | 1522 | wxUniChar operator[](size_t n) const |
8f93a29f | 1523 | { return at(n); } |
01398433 | 1524 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 1525 | wxUniChar operator[](unsigned int n) const |
8f93a29f | 1526 | { return at(n); } |
01398433 | 1527 | #endif // size_t != unsigned int |
01398433 | 1528 | |
9d9ad673 | 1529 | // operator versions of GetWriteableChar() |
c9f78968 | 1530 | wxUniCharRef operator[](int n) |
8f93a29f | 1531 | { return at(n); } |
c1df0a3b | 1532 | wxUniCharRef operator[](long n) |
8f93a29f | 1533 | { return at(n); } |
c9f78968 | 1534 | wxUniCharRef operator[](size_t n) |
8f93a29f | 1535 | { return at(n); } |
d836ee96 | 1536 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 1537 | wxUniCharRef operator[](unsigned int n) |
8f93a29f | 1538 | { return at(n); } |
d836ee96 | 1539 | #endif // size_t != unsigned int |
c801d85f | 1540 | |
18666b42 VZ |
1541 | |
1542 | /* | |
1543 | Overview of wxString conversions, implicit and explicit: | |
1544 | ||
1545 | - wxString has a std::[w]string-like c_str() method, however it does | |
1546 | not return a C-style string directly but instead returns wxCStrData | |
1547 | helper object which is convertible to either "char *" narrow string | |
1548 | or "wchar_t *" wide string. Usually the correct conversion will be | |
1549 | applied by the compiler automatically but if this doesn't happen you | |
1550 | need to explicitly choose one using wxCStrData::AsChar() or AsWChar() | |
1551 | methods or another wxString conversion function. | |
1552 | ||
1553 | - One of the places where the conversion does *NOT* happen correctly is | |
1554 | when c_str() is passed to a vararg function such as printf() so you | |
1555 | must *NOT* use c_str() with them. Either use wxPrintf() (all wx | |
1556 | functions do handle c_str() correctly, even if they appear to be | |
1557 | vararg (but they're not, really)) or add an explicit AsChar() or, if | |
1558 | compatibility with previous wxWidgets versions is important, add a | |
1559 | cast to "const char *". | |
1560 | ||
1561 | - In non-STL mode only, wxString is also implicitly convertible to | |
1562 | wxCStrData. The same warning as above applies. | |
1563 | ||
1564 | - c_str() is polymorphic as it can be converted to either narrow or | |
1565 | wide string. If you explicitly need one or the other, choose to use | |
1566 | mb_str() (for narrow) or wc_str() (for wide) instead. Notice that | |
1567 | these functions can return either the pointer to string directly (if | |
1568 | this is what the string uses internally) or a temporary buffer | |
1569 | containing the string and convertible to it. Again, conversion will | |
1570 | usually be done automatically by the compiler but beware of the | |
1571 | vararg functions: you need an explicit cast when using them. | |
1572 | ||
1573 | - There are also non-const versions of mb_str() and wc_str() called | |
1574 | char_str() and wchar_str(). They are only meant to be used with | |
1575 | non-const-correct functions and they always return buffers. | |
1576 | ||
1577 | - Finally wx_str() returns whatever string representation is used by | |
1578 | wxString internally. It may be either a narrow or wide string | |
1579 | depending on wxWidgets build mode but it will always be a raw pointer | |
1580 | (and not a buffer). | |
1581 | */ | |
1582 | ||
1583 | // explicit conversion to wxCStrData | |
c9f78968 | 1584 | wxCStrData c_str() const { return wxCStrData(this); } |
8f93a29f | 1585 | wxCStrData data() const { return c_str(); } |
c9f78968 | 1586 | |
18666b42 | 1587 | // implicit conversion to wxCStrData |
c9f78968 | 1588 | operator wxCStrData() const { return c_str(); } |
7978bc72 | 1589 | |
4f167b46 VZ |
1590 | // the first two operators conflict with operators for conversion to |
1591 | // std::string and they must be disabled in STL build; the next one only | |
1592 | // makes sense if conversions to char* are also defined and not defining it | |
1593 | // in STL build also helps us to get more clear error messages for the code | |
1594 | // which relies on implicit conversion to char* in STL build | |
7978bc72 | 1595 | #if !wxUSE_STL |
11aac4ba VS |
1596 | operator const char*() const { return c_str(); } |
1597 | operator const wchar_t*() const { return c_str(); } | |
e015c2a3 | 1598 | |
353a4edc VZ |
1599 | // implicit conversion to untyped pointer for compatibility with previous |
1600 | // wxWidgets versions: this is the same as conversion to const char * so it | |
1601 | // may fail! | |
1602 | operator const void*() const { return c_str(); } | |
4f167b46 | 1603 | #endif // wxUSE_STL |
353a4edc | 1604 | |
e015c2a3 | 1605 | // identical to c_str(), for MFC compatibility |
c9f78968 VS |
1606 | const wxCStrData GetData() const { return c_str(); } |
1607 | ||
1608 | // explicit conversion to C string in internal representation (char*, | |
1609 | // wchar_t*, UTF-8-encoded char*, depending on the build): | |
81727065 | 1610 | const wxStringCharType *wx_str() const { return m_impl.c_str(); } |
e90c1d2a | 1611 | |
ef0f1387 | 1612 | // conversion to *non-const* multibyte or widestring buffer; modifying |
c67b782d VS |
1613 | // returned buffer won't affect the string, these methods are only useful |
1614 | // for passing values to const-incorrect functions | |
8060b0be | 1615 | wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const |
c67b782d VS |
1616 | { return mb_str(conv); } |
1617 | wxWritableWCharBuffer wchar_str() const { return wc_str(); } | |
ef0f1387 | 1618 | |
062dc5fc VZ |
1619 | // conversion to the buffer of the given type T (= char or wchar_t) and |
1620 | // also optionally return the buffer length | |
1621 | // | |
1622 | // this is mostly/only useful for the template functions | |
1623 | // | |
1624 | // FIXME-VC6: the second argument only exists for VC6 which doesn't support | |
1625 | // explicit template function selection, do not use it unless | |
1626 | // you must support VC6! | |
1627 | template <typename T> | |
1628 | wxCharTypeBuffer<T> tchar_str(size_t *len = NULL, | |
1629 | T * WXUNUSED(dummy) = NULL) const | |
1630 | { | |
1631 | #if wxUSE_UNICODE | |
1632 | // we need a helper dispatcher depending on type | |
1633 | return wxPrivate::wxStringAsBufHelper<T>::Get(*this, len); | |
1634 | #else // ANSI | |
1635 | // T can only be char in ANSI build | |
1636 | if ( len ) | |
1637 | *len = length(); | |
1638 | ||
1639 | return wxCharTypeBuffer<T>::CreateNonOwned(wx_str()); | |
1640 | #endif // Unicode build kind | |
1641 | } | |
1642 | ||
e015c2a3 VZ |
1643 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
1644 | // converting numbers or strings which are certain not to contain special | |
1645 | // chars (typically system functions, X atoms, environment variables etc.) | |
1646 | // | |
1647 | // the behaviour of these functions with the strings containing anything | |
1648 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 1649 | #if wxUSE_UNICODE |
628f9e95 VZ |
1650 | static wxString FromAscii(const char *ascii, size_t len); |
1651 | static wxString FromAscii(const char *ascii); | |
1652 | static wxString FromAscii(char ascii); | |
b1ac3b56 | 1653 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
1654 | #else // ANSI |
1655 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
e6310bbc VS |
1656 | static wxString FromAscii(const char *ascii, size_t len) |
1657 | { return wxString( ascii, len ); } | |
628f9e95 | 1658 | static wxString FromAscii(char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
1659 | const char *ToAscii() const { return c_str(); } |
1660 | #endif // Unicode/!Unicode | |
b1ac3b56 | 1661 | |
628f9e95 VZ |
1662 | // also provide unsigned char overloads as signed/unsigned doesn't matter |
1663 | // for 7 bit ASCII characters | |
1664 | static wxString FromAscii(const unsigned char *ascii) | |
1665 | { return FromAscii((const char *)ascii); } | |
1666 | static wxString FromAscii(const unsigned char *ascii, size_t len) | |
1667 | { return FromAscii((const char *)ascii, len); } | |
1668 | ||
5f167b77 VS |
1669 | // conversion to/from UTF-8: |
1670 | #if wxUSE_UNICODE_UTF8 | |
cc209a51 | 1671 | static wxString FromUTF8Unchecked(const char *utf8) |
5f167b77 | 1672 | { |
f966a73d VS |
1673 | if ( !utf8 ) |
1674 | return wxEmptyString; | |
1675 | ||
5f167b77 VS |
1676 | wxASSERT( wxStringOperations::IsValidUtf8String(utf8) ); |
1677 | return FromImpl(wxStringImpl(utf8)); | |
1678 | } | |
cc209a51 | 1679 | static wxString FromUTF8Unchecked(const char *utf8, size_t len) |
5f167b77 | 1680 | { |
f966a73d VS |
1681 | if ( !utf8 ) |
1682 | return wxEmptyString; | |
1683 | if ( len == npos ) | |
cc209a51 | 1684 | return FromUTF8Unchecked(utf8); |
f966a73d | 1685 | |
5f167b77 VS |
1686 | wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) ); |
1687 | return FromImpl(wxStringImpl(utf8, len)); | |
1688 | } | |
cc209a51 VZ |
1689 | |
1690 | static wxString FromUTF8(const char *utf8) | |
1691 | { | |
1692 | if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8) ) | |
1693 | return ""; | |
1694 | ||
1695 | return FromImpl(wxStringImpl(utf8)); | |
1696 | } | |
1697 | static wxString FromUTF8(const char *utf8, size_t len) | |
1698 | { | |
1699 | if ( len == npos ) | |
1700 | return FromUTF8(utf8); | |
1701 | ||
1702 | if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8, len) ) | |
1703 | return ""; | |
1704 | ||
1705 | return FromImpl(wxStringImpl(utf8, len)); | |
1706 | } | |
1707 | ||
5f167b77 VS |
1708 | const char* utf8_str() const { return wx_str(); } |
1709 | const char* ToUTF8() const { return wx_str(); } | |
062dc5fc VZ |
1710 | |
1711 | // this function exists in UTF-8 build only and returns the length of the | |
1712 | // internal UTF-8 representation | |
1713 | size_t utf8_length() const { return m_impl.length(); } | |
5f167b77 | 1714 | #elif wxUSE_UNICODE_WCHAR |
cc209a51 | 1715 | static wxString FromUTF8(const char *utf8, size_t len = npos) |
5487ff0f | 1716 | { return wxString(utf8, wxMBConvUTF8(), len); } |
cc209a51 VZ |
1717 | static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos) |
1718 | { | |
1719 | const wxString s(utf8, wxMBConvUTF8(), len); | |
1720 | wxASSERT_MSG( !utf8 || !*utf8 || !s.empty(), | |
1721 | "string must be valid UTF-8" ); | |
1722 | return s; | |
1723 | } | |
5487ff0f | 1724 | const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); } |
5f167b77 VS |
1725 | const wxCharBuffer ToUTF8() const { return utf8_str(); } |
1726 | #else // ANSI | |
1727 | static wxString FromUTF8(const char *utf8) | |
5487ff0f | 1728 | { return wxString(wxMBConvUTF8().cMB2WC(utf8)); } |
5f167b77 | 1729 | static wxString FromUTF8(const char *utf8, size_t len) |
06c73b98 | 1730 | { |
96e93f5b VZ |
1731 | size_t wlen; |
1732 | wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen)); | |
1733 | return wxString(buf.data(), wlen); | |
1734 | } | |
1735 | static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos) | |
1736 | { | |
1737 | size_t wlen; | |
1738 | wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, | |
1739 | len == npos ? wxNO_LEN : len, | |
1740 | &wlen)); | |
1741 | wxASSERT_MSG( !utf8 || !*utf8 || wlen, | |
1742 | "string must be valid UTF-8" ); | |
1743 | ||
1744 | return wxString(buf.data(), wlen); | |
06c73b98 | 1745 | } |
5487ff0f VS |
1746 | const wxCharBuffer utf8_str() const |
1747 | { return wxMBConvUTF8().cWC2MB(wc_str()); } | |
5f167b77 VS |
1748 | const wxCharBuffer ToUTF8() const { return utf8_str(); } |
1749 | #endif | |
1750 | ||
cb2f2135 VS |
1751 | // functions for storing binary data in wxString: |
1752 | #if wxUSE_UNICODE | |
1753 | static wxString From8BitData(const char *data, size_t len) | |
1754 | { return wxString(data, wxConvISO8859_1, len); } | |
1755 | // version for NUL-terminated data: | |
1756 | static wxString From8BitData(const char *data) | |
1757 | { return wxString(data, wxConvISO8859_1); } | |
1758 | const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); } | |
1759 | #else // ANSI | |
1760 | static wxString From8BitData(const char *data, size_t len) | |
1761 | { return wxString(data, len); } | |
1762 | // version for NUL-terminated data: | |
1763 | static wxString From8BitData(const char *data) | |
1764 | { return wxString(data); } | |
1765 | const char *To8BitData() const { return c_str(); } | |
1766 | #endif // Unicode/ANSI | |
1767 | ||
2b5f62a0 | 1768 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 1769 | // buffer with temporary data |
f6bcfd97 BP |
1770 | // |
1771 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
1772 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
1773 | // fn_str() to return a string which should be used with the OS APIs | |
1774 | // accepting the file names. The return value is always the same, but the | |
1775 | // type differs because a function may either return pointer to the buffer | |
1776 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 1777 | #if wxUSE_UNICODE |
111d9948 VS |
1778 | |
1779 | #if wxUSE_UTF8_LOCALE_ONLY | |
1780 | const char* mb_str() const { return wx_str(); } | |
1781 | const wxCharBuffer mb_str(const wxMBConv& conv) const; | |
1782 | #else | |
830f8f11 | 1783 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; |
111d9948 | 1784 | #endif |
f6bcfd97 | 1785 | |
e90c1d2a VZ |
1786 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
1787 | ||
81727065 | 1788 | #if wxUSE_UNICODE_WCHAR |
6ad68ad8 | 1789 | const wchar_t* wc_str() const { return wx_str(); } |
81727065 VS |
1790 | #elif wxUSE_UNICODE_UTF8 |
1791 | const wxWCharBuffer wc_str() const; | |
1792 | #endif | |
f6bcfd97 | 1793 | // for compatibility with !wxUSE_UNICODE version |
81727065 VS |
1794 | const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const |
1795 | { return wc_str(); } | |
e90c1d2a | 1796 | |
2bb67b80 | 1797 | #if wxMBFILES |
5f709e67 | 1798 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 1799 | #else // !wxMBFILES |
81727065 | 1800 | const wxWX2WCbuf fn_str() const { return wc_str(); } |
e90c1d2a | 1801 | #endif // wxMBFILES/!wxMBFILES |
81727065 | 1802 | |
e90c1d2a | 1803 | #else // ANSI |
81727065 | 1804 | const wxChar* mb_str() const { return wx_str(); } |
f6bcfd97 BP |
1805 | |
1806 | // for compatibility with wxUSE_UNICODE version | |
6ad68ad8 | 1807 | const char* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); } |
f6bcfd97 | 1808 | |
e90c1d2a | 1809 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 1810 | |
6f841509 | 1811 | #if wxUSE_WCHAR_T |
ef0f1387 | 1812 | const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const; |
e90c1d2a | 1813 | #endif // wxUSE_WCHAR_T |
c47e732c | 1814 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); } |
e90c1d2a | 1815 | #endif // Unicode/ANSI |
c801d85f | 1816 | |
6ad68ad8 RR |
1817 | #if wxUSE_UNICODE_UTF8 |
1818 | const wxWCharBuffer t_str() const { return wc_str(); } | |
1819 | #elif wxUSE_UNICODE_WCHAR | |
1820 | const wchar_t* t_str() const { return wx_str(); } | |
1821 | #else | |
1822 | const char* t_str() const { return wx_str(); } | |
cbec0f40 | 1823 | #endif |
6ad68ad8 RR |
1824 | |
1825 | ||
3c67202d VZ |
1826 | // overloaded assignment |
1827 | // from another wxString | |
04abe4bc | 1828 | wxString& operator=(const wxString& stringSrc) |
68482dc5 VZ |
1829 | { |
1830 | if ( this != &stringSrc ) | |
1831 | { | |
1832 | wxSTRING_INVALIDATE_CACHE(); | |
1833 | ||
1834 | m_impl = stringSrc.m_impl; | |
1835 | } | |
1836 | ||
1837 | return *this; | |
1838 | } | |
1839 | ||
8f93a29f VS |
1840 | wxString& operator=(const wxCStrData& cstr) |
1841 | { return *this = cstr.AsString(); } | |
3c67202d | 1842 | // from a character |
c9f78968 | 1843 | wxString& operator=(wxUniChar ch) |
84d2877f | 1844 | { |
68482dc5 VZ |
1845 | wxSTRING_INVALIDATE_CACHE(); |
1846 | ||
84d2877f VS |
1847 | #if wxUSE_UNICODE_UTF8 |
1848 | if ( !ch.IsAscii() ) | |
1849 | m_impl = wxStringOperations::EncodeChar(ch); | |
1850 | else | |
68482dc5 | 1851 | #endif // wxUSE_UNICODE_UTF8 |
84d2877f VS |
1852 | m_impl = (wxStringCharType)ch; |
1853 | return *this; | |
1854 | } | |
68482dc5 | 1855 | |
c9f78968 | 1856 | wxString& operator=(wxUniCharRef ch) |
8f93a29f | 1857 | { return operator=((wxUniChar)ch); } |
c9f78968 | 1858 | wxString& operator=(char ch) |
8f93a29f | 1859 | { return operator=(wxUniChar(ch)); } |
50e02008 VS |
1860 | wxString& operator=(unsigned char ch) |
1861 | { return operator=(wxUniChar(ch)); } | |
c9f78968 | 1862 | wxString& operator=(wchar_t ch) |
8f93a29f | 1863 | { return operator=(wxUniChar(ch)); } |
c57e3bd5 RN |
1864 | // from a C string - STL probably will crash on NULL, |
1865 | // so we need to compensate in that case | |
992527a5 | 1866 | #if wxUSE_STL_BASED_WXSTRING |
04abe4bc | 1867 | wxString& operator=(const char *psz) |
68482dc5 VZ |
1868 | { |
1869 | wxSTRING_INVALIDATE_CACHE(); | |
1870 | ||
1871 | if ( psz ) | |
1872 | m_impl = ImplStr(psz); | |
1873 | else | |
1874 | clear(); | |
1875 | ||
1876 | return *this; | |
1877 | } | |
1878 | ||
04abe4bc | 1879 | wxString& operator=(const wchar_t *pwz) |
68482dc5 VZ |
1880 | { |
1881 | wxSTRING_INVALIDATE_CACHE(); | |
1882 | ||
1883 | if ( pwz ) | |
1884 | m_impl = ImplStr(pwz); | |
1885 | else | |
1886 | clear(); | |
1887 | ||
1888 | return *this; | |
1889 | } | |
1890 | #else // !wxUSE_STL_BASED_WXSTRING | |
04abe4bc | 1891 | wxString& operator=(const char *psz) |
68482dc5 VZ |
1892 | { |
1893 | wxSTRING_INVALIDATE_CACHE(); | |
1894 | ||
1895 | m_impl = ImplStr(psz); | |
1896 | ||
1897 | return *this; | |
1898 | } | |
1899 | ||
04abe4bc | 1900 | wxString& operator=(const wchar_t *pwz) |
68482dc5 VZ |
1901 | { |
1902 | wxSTRING_INVALIDATE_CACHE(); | |
1903 | ||
1904 | m_impl = ImplStr(pwz); | |
1905 | ||
1906 | return *this; | |
1907 | } | |
1908 | #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING | |
1909 | ||
04abe4bc VS |
1910 | wxString& operator=(const unsigned char *psz) |
1911 | { return operator=((const char*)psz); } | |
c57e3bd5 | 1912 | |
111bb7f2 | 1913 | // from wxWCharBuffer |
8f93a29f | 1914 | wxString& operator=(const wxWCharBuffer& s) |
04abe4bc | 1915 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs |
111bb7f2 | 1916 | // from wxCharBuffer |
04abe4bc VS |
1917 | wxString& operator=(const wxCharBuffer& s) |
1918 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs | |
3c67202d VZ |
1919 | |
1920 | // string concatenation | |
1921 | // in place concatenation | |
1922 | /* | |
1923 | Concatenate and return the result. Note that the left to right | |
1924 | associativity of << allows to write things like "str << str1 << str2 | |
1925 | << ..." (unlike with +=) | |
1926 | */ | |
1927 | // string += string | |
dd1eaa89 VZ |
1928 | wxString& operator<<(const wxString& s) |
1929 | { | |
8f93a29f VS |
1930 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
1931 | wxASSERT_MSG( s.IsValid(), | |
09443a26 | 1932 | _T("did you forget to call UngetWriteBuf()?") ); |
e87b7833 | 1933 | #endif |
dd1eaa89 | 1934 | |
e87b7833 | 1935 | append(s); |
dd1eaa89 VZ |
1936 | return *this; |
1937 | } | |
3c67202d | 1938 | // string += C string |
8f93a29f | 1939 | wxString& operator<<(const char *psz) |
c9f78968 | 1940 | { append(psz); return *this; } |
8f93a29f VS |
1941 | wxString& operator<<(const wchar_t *pwz) |
1942 | { append(pwz); return *this; } | |
c9f78968 | 1943 | wxString& operator<<(const wxCStrData& psz) |
8f93a29f | 1944 | { append(psz.AsString()); return *this; } |
3c67202d | 1945 | // string += char |
c9f78968 VS |
1946 | wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } |
1947 | wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } | |
1948 | wxString& operator<<(char ch) { append(1, ch); return *this; } | |
50e02008 | 1949 | wxString& operator<<(unsigned char ch) { append(1, ch); return *this; } |
c9f78968 | 1950 | wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } |
2bb67b80 OK |
1951 | |
1952 | // string += buffer (i.e. from wxGetString) | |
09443a26 | 1953 | wxString& operator<<(const wxWCharBuffer& s) |
d7330233 | 1954 | { return operator<<((const wchar_t *)s); } |
09443a26 | 1955 | wxString& operator<<(const wxCharBuffer& s) |
d7330233 | 1956 | { return operator<<((const char *)s); } |
d7330233 | 1957 | |
3c67202d | 1958 | // string += C string |
09443a26 VZ |
1959 | wxString& Append(const wxString& s) |
1960 | { | |
5a8231ef WS |
1961 | // test for empty() to share the string if possible |
1962 | if ( empty() ) | |
09443a26 VZ |
1963 | *this = s; |
1964 | else | |
e87b7833 | 1965 | append(s); |
09443a26 VZ |
1966 | return *this; |
1967 | } | |
8f93a29f | 1968 | wxString& Append(const char* psz) |
e87b7833 | 1969 | { append(psz); return *this; } |
8f93a29f VS |
1970 | wxString& Append(const wchar_t* pwz) |
1971 | { append(pwz); return *this; } | |
d3cefe87 VS |
1972 | wxString& Append(const wxCStrData& psz) |
1973 | { append(psz); return *this; } | |
1974 | wxString& Append(const wxCharBuffer& psz) | |
1975 | { append(psz); return *this; } | |
1976 | wxString& Append(const wxWCharBuffer& psz) | |
1977 | { append(psz); return *this; } | |
f416695a VS |
1978 | wxString& Append(const char* psz, size_t nLen) |
1979 | { append(psz, nLen); return *this; } | |
1980 | wxString& Append(const wchar_t* pwz, size_t nLen) | |
1981 | { append(pwz, nLen); return *this; } | |
1982 | wxString& Append(const wxCStrData& psz, size_t nLen) | |
1983 | { append(psz, nLen); return *this; } | |
1984 | wxString& Append(const wxCharBuffer& psz, size_t nLen) | |
1985 | { append(psz, nLen); return *this; } | |
1986 | wxString& Append(const wxWCharBuffer& psz, size_t nLen) | |
1987 | { append(psz, nLen); return *this; } | |
3c67202d | 1988 | // append count copies of given character |
c9f78968 VS |
1989 | wxString& Append(wxUniChar ch, size_t count = 1u) |
1990 | { append(count, ch); return *this; } | |
1991 | wxString& Append(wxUniCharRef ch, size_t count = 1u) | |
1992 | { append(count, ch); return *this; } | |
1993 | wxString& Append(char ch, size_t count = 1u) | |
1994 | { append(count, ch); return *this; } | |
50e02008 VS |
1995 | wxString& Append(unsigned char ch, size_t count = 1u) |
1996 | { append(count, ch); return *this; } | |
c9f78968 | 1997 | wxString& Append(wchar_t ch, size_t count = 1u) |
e87b7833 | 1998 | { append(count, ch); return *this; } |
3c67202d VZ |
1999 | |
2000 | // prepend a string, return the string itself | |
2001 | wxString& Prepend(const wxString& str) | |
2002 | { *this = str + *this; return *this; } | |
2003 | ||
2004 | // non-destructive concatenation | |
1fc8cfbd VZ |
2005 | // two strings |
2006 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
2007 | const wxString& string2); | |
2008 | // string with a single char | |
c9f78968 | 2009 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
1fc8cfbd | 2010 | // char with a string |
c9f78968 | 2011 | friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); |
1fc8cfbd VZ |
2012 | // string with C string |
2013 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
8f93a29f VS |
2014 | const char *psz); |
2015 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
2016 | const wchar_t *pwz); | |
1fc8cfbd | 2017 | // C string with string |
8f93a29f VS |
2018 | friend wxString WXDLLIMPEXP_BASE operator+(const char *psz, |
2019 | const wxString& string); | |
2020 | friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, | |
1fc8cfbd | 2021 | const wxString& string); |
3c67202d VZ |
2022 | |
2023 | // stream-like functions | |
2024 | // insert an int into string | |
3ce65f6c VZ |
2025 | wxString& operator<<(int i) |
2026 | { return (*this) << Format(_T("%d"), i); } | |
2027 | // insert an unsigned int into string | |
2028 | wxString& operator<<(unsigned int ui) | |
2029 | { return (*this) << Format(_T("%u"), ui); } | |
2030 | // insert a long into string | |
2031 | wxString& operator<<(long l) | |
2032 | { return (*this) << Format(_T("%ld"), l); } | |
2033 | // insert an unsigned long into string | |
2034 | wxString& operator<<(unsigned long ul) | |
2035 | { return (*this) << Format(_T("%lu"), ul); } | |
3fc1adbd MW |
2036 | #if defined wxLongLong_t && !defined wxLongLongIsLong |
2037 | // insert a long long if they exist and aren't longs | |
2038 | wxString& operator<<(wxLongLong_t ll) | |
b7859132 MW |
2039 | { |
2040 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
2041 | return (*this) << Format(fmt, ll); | |
2042 | } | |
3fc1adbd MW |
2043 | // insert an unsigned long long |
2044 | wxString& operator<<(wxULongLong_t ull) | |
b7859132 MW |
2045 | { |
2046 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
2047 | return (*this) << Format(fmt , ull); | |
2048 | } | |
68482dc5 | 2049 | #endif // wxLongLong_t && !wxLongLongIsLong |
3c67202d | 2050 | // insert a float into string |
3ce65f6c VZ |
2051 | wxString& operator<<(float f) |
2052 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 2053 | // insert a double into string |
3ce65f6c VZ |
2054 | wxString& operator<<(double d) |
2055 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 2056 | |
3c67202d | 2057 | // string comparison |
30b21f9a | 2058 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
8f93a29f VS |
2059 | int Cmp(const char *psz) const |
2060 | { return compare(psz); } | |
2061 | int Cmp(const wchar_t *pwz) const | |
2062 | { return compare(pwz); } | |
2063 | int Cmp(const wxString& s) const | |
2064 | { return compare(s); } | |
d3cefe87 VS |
2065 | int Cmp(const wxCStrData& s) const |
2066 | { return compare(s); } | |
2067 | int Cmp(const wxCharBuffer& s) const | |
2068 | { return compare(s); } | |
2069 | int Cmp(const wxWCharBuffer& s) const | |
2070 | { return compare(s); } | |
3c67202d | 2071 | // same as Cmp() but not case-sensitive |
dcb68102 | 2072 | int CmpNoCase(const wxString& s) const; |
2712e317 | 2073 | |
3c67202d VZ |
2074 | // test for the string equality, either considering case or not |
2075 | // (if compareWithCase then the case matters) | |
11aac4ba | 2076 | bool IsSameAs(const wxString& str, bool compareWithCase = true) const |
2712e317 VS |
2077 | { |
2078 | #if !wxUSE_UNICODE_UTF8 | |
2079 | // in UTF-8 build, length() is O(n) and doing this would be _slower_ | |
2080 | if ( length() != str.length() ) | |
2081 | return false; | |
2082 | #endif | |
2083 | return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; | |
2084 | } | |
11aac4ba VS |
2085 | bool IsSameAs(const char *str, bool compareWithCase = true) const |
2086 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
2087 | bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const | |
2088 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
2712e317 | 2089 | |
d3cefe87 VS |
2090 | bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const |
2091 | { return IsSameAs(str.AsString(), compareWithCase); } | |
2092 | bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const | |
2093 | { return IsSameAs(str.data(), compareWithCase); } | |
2094 | bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const | |
2095 | { return IsSameAs(str.data(), compareWithCase); } | |
20aa779e | 2096 | // comparison with a single character: returns true if equal |
52de37c7 | 2097 | bool IsSameAs(wxUniChar c, bool compareWithCase = true) const; |
11aac4ba VS |
2098 | // FIXME-UTF8: remove these overloads |
2099 | bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const | |
2100 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
2101 | bool IsSameAs(char c, bool compareWithCase = true) const | |
2102 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
2103 | bool IsSameAs(unsigned char c, bool compareWithCase = true) const | |
2104 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
2105 | bool IsSameAs(wchar_t c, bool compareWithCase = true) const | |
2106 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
2107 | bool IsSameAs(int c, bool compareWithCase = true) const | |
2108 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
3c67202d VZ |
2109 | |
2110 | // simple sub-string extraction | |
2111 | // return substring starting at nFirst of length nCount (or till the end | |
2112 | // if nCount = default value) | |
e87b7833 | 2113 | wxString Mid(size_t nFirst, size_t nCount = npos) const; |
3c67202d | 2114 | |
f6bcfd97 | 2115 | // operator version of Mid() |
3c67202d VZ |
2116 | wxString operator()(size_t start, size_t len) const |
2117 | { return Mid(start, len); } | |
2118 | ||
3affcd07 VZ |
2119 | // check if the string starts with the given prefix and return the rest |
2120 | // of the string in the provided pointer if it is not NULL; otherwise | |
2121 | // return false | |
c5e7a7d7 | 2122 | bool StartsWith(const wxString& prefix, wxString *rest = NULL) const; |
3affcd07 VZ |
2123 | // check if the string ends with the given suffix and return the |
2124 | // beginning of the string before the suffix in the provided pointer if | |
2125 | // it is not NULL; otherwise return false | |
c5e7a7d7 | 2126 | bool EndsWith(const wxString& suffix, wxString *rest = NULL) const; |
f6bcfd97 | 2127 | |
3c67202d | 2128 | // get first nCount characters |
c801d85f | 2129 | wxString Left(size_t nCount) const; |
3c67202d | 2130 | // get last nCount characters |
c801d85f | 2131 | wxString Right(size_t nCount) const; |
7929902d | 2132 | // get all characters before the first occurrence of ch |
3c67202d | 2133 | // (returns the whole string if ch not found) |
c9f78968 | 2134 | wxString BeforeFirst(wxUniChar ch) const; |
7929902d | 2135 | // get all characters before the last occurrence of ch |
3c67202d | 2136 | // (returns empty string if ch not found) |
c9f78968 | 2137 | wxString BeforeLast(wxUniChar ch) const; |
7929902d | 2138 | // get all characters after the first occurrence of ch |
3c67202d | 2139 | // (returns empty string if ch not found) |
c9f78968 | 2140 | wxString AfterFirst(wxUniChar ch) const; |
7929902d | 2141 | // get all characters after the last occurrence of ch |
3c67202d | 2142 | // (returns the whole string if ch not found) |
c9f78968 | 2143 | wxString AfterLast(wxUniChar ch) const; |
3c67202d VZ |
2144 | |
2145 | // for compatibility only, use more explicitly named functions above | |
c9f78968 VS |
2146 | wxString Before(wxUniChar ch) const { return BeforeLast(ch); } |
2147 | wxString After(wxUniChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
2148 | |
2149 | // case conversion | |
c84c52de | 2150 | // convert to upper case in place, return the string itself |
c801d85f | 2151 | wxString& MakeUpper(); |
c84c52de | 2152 | // convert to upper case, return the copy of the string |
0c7db140 | 2153 | wxString Upper() const { return wxString(*this).MakeUpper(); } |
c84c52de | 2154 | // convert to lower case in place, return the string itself |
c801d85f | 2155 | wxString& MakeLower(); |
c84c52de | 2156 | // convert to lower case, return the copy of the string |
0c7db140 VZ |
2157 | wxString Lower() const { return wxString(*this).MakeLower(); } |
2158 | // convert the first character to the upper case and the rest to the | |
2159 | // lower one, return the modified string itself | |
2160 | wxString& MakeCapitalized(); | |
2161 | // convert the first character to the upper case and the rest to the | |
2162 | // lower one, return the copy of the string | |
2163 | wxString Capitalize() const { return wxString(*this).MakeCapitalized(); } | |
c801d85f | 2164 | |
3c67202d VZ |
2165 | // trimming/padding whitespace (either side) and truncating |
2166 | // remove spaces from left or from right (default) side | |
d775fa82 | 2167 | wxString& Trim(bool bFromRight = true); |
3c67202d | 2168 | // add nCount copies chPad in the beginning or at the end (default) |
c9f78968 | 2169 | wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); |
dd1eaa89 | 2170 | |
3c67202d VZ |
2171 | // searching and replacing |
2172 | // searching (return starting index, or -1 if not found) | |
c9f78968 | 2173 | int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr |
8a540c88 VS |
2174 | int Find(wxUniCharRef ch, bool bFromEnd = false) const |
2175 | { return Find(wxUniChar(ch), bFromEnd); } | |
2176 | int Find(char ch, bool bFromEnd = false) const | |
2177 | { return Find(wxUniChar(ch), bFromEnd); } | |
2178 | int Find(unsigned char ch, bool bFromEnd = false) const | |
2179 | { return Find(wxUniChar(ch), bFromEnd); } | |
2180 | int Find(wchar_t ch, bool bFromEnd = false) const | |
2181 | { return Find(wxUniChar(ch), bFromEnd); } | |
3c67202d | 2182 | // searching (return starting index, or -1 if not found) |
8a540c88 VS |
2183 | int Find(const wxString& sub) const // like strstr |
2184 | { | |
2185 | size_type idx = find(sub); | |
2186 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
2187 | } | |
2188 | int Find(const char *sub) const // like strstr | |
2189 | { | |
2190 | size_type idx = find(sub); | |
2191 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
2192 | } | |
2193 | int Find(const wchar_t *sub) const // like strstr | |
2194 | { | |
2195 | size_type idx = find(sub); | |
2196 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
2197 | } | |
2198 | ||
d3cefe87 VS |
2199 | int Find(const wxCStrData& sub) const |
2200 | { return Find(sub.AsString()); } | |
2201 | int Find(const wxCharBuffer& sub) const | |
2202 | { return Find(sub.data()); } | |
2203 | int Find(const wxWCharBuffer& sub) const | |
2204 | { return Find(sub.data()); } | |
2205 | ||
7929902d | 2206 | // replace first (or all of bReplaceAll) occurrences of substring with |
3c67202d | 2207 | // another string, returns the number of replacements made |
8a540c88 VS |
2208 | size_t Replace(const wxString& strOld, |
2209 | const wxString& strNew, | |
d775fa82 | 2210 | bool bReplaceAll = true); |
3c67202d VZ |
2211 | |
2212 | // check if the string contents matches a mask containing '*' and '?' | |
8a540c88 | 2213 | bool Matches(const wxString& mask) const; |
c801d85f | 2214 | |
529e491c FM |
2215 | // conversion to numbers: all functions return true only if the whole |
2216 | // string is a number and put the value of this number into the pointer | |
2217 | // provided, the base is the numeric base in which the conversion should be | |
2218 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
2219 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
2220 | // convert to a signed integer | |
2221 | bool ToLong(long *val, int base = 10) const; | |
2222 | // convert to an unsigned integer | |
2223 | bool ToULong(unsigned long *val, int base = 10) const; | |
2224 | // convert to wxLongLong | |
d6718dd1 | 2225 | #if defined(wxLongLong_t) |
529e491c FM |
2226 | bool ToLongLong(wxLongLong_t *val, int base = 10) const; |
2227 | // convert to wxULongLong | |
2228 | bool ToULongLong(wxULongLong_t *val, int base = 10) const; | |
d6718dd1 | 2229 | #endif // wxLongLong_t |
529e491c FM |
2230 | // convert to a double |
2231 | bool ToDouble(double *val) const; | |
2232 | ||
2233 | #if wxUSE_XLOCALE | |
2234 | // conversions to numbers using C locale | |
2235 | // convert to a signed integer | |
2236 | bool ToCLong(long *val, int base = 10) const; | |
2237 | // convert to an unsigned integer | |
2238 | bool ToCULong(unsigned long *val, int base = 10) const; | |
2239 | // convert to a double | |
2240 | bool ToCDouble(double *val) const; | |
2241 | #endif | |
2242 | ||
c9f78968 | 2243 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
90e572f1 | 2244 | // formatted input/output |
3c67202d | 2245 | // as sprintf(), returns the number of characters written or < 0 on error |
7357f981 | 2246 | // (take 'this' into account in attribute parameter count) |
2523e9b7 | 2247 | // int Printf(const wxString& format, ...); |
1528e0b8 | 2248 | WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&), |
d1f6e2cf | 2249 | DoPrintfWchar, DoPrintfUtf8) |
2523e9b7 | 2250 | #ifdef __WATCOMC__ |
59a14f69 VS |
2251 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 |
2252 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&), | |
2253 | (wxFormatString(f1))); | |
2254 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&), | |
2255 | (wxFormatString(f1))); | |
2256 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*), | |
2257 | (wxFormatString(f1))); | |
2258 | WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*), | |
2259 | (wxFormatString(f1))); | |
2523e9b7 | 2260 | #endif |
c9f78968 | 2261 | #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN |
3c67202d | 2262 | // as vprintf(), returns the number of characters written or < 0 on error |
c9f78968 | 2263 | int PrintfV(const wxString& format, va_list argptr); |
dd1eaa89 | 2264 | |
c9f78968 | 2265 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
341e7d28 | 2266 | // returns the string containing the result of Printf() to it |
862bb5c7 | 2267 | // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1; |
1528e0b8 | 2268 | WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&), |
d1f6e2cf | 2269 | DoFormatWchar, DoFormatUtf8) |
2523e9b7 VS |
2270 | #ifdef __WATCOMC__ |
2271 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
59a14f69 VS |
2272 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&), |
2273 | (wxFormatString(f1))); | |
2274 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&), | |
2275 | (wxFormatString(f1))); | |
2276 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*), | |
2277 | (wxFormatString(f1))); | |
2278 | WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*), | |
2279 | (wxFormatString(f1))); | |
2523e9b7 | 2280 | #endif |
c9f78968 | 2281 | #endif |
341e7d28 | 2282 | // the same as above, but takes a va_list |
c9f78968 | 2283 | static wxString FormatV(const wxString& format, va_list argptr); |
341e7d28 | 2284 | |
3c67202d VZ |
2285 | // raw access to string memory |
2286 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 2287 | // only works if the data of this string is not shared |
0367b928 | 2288 | bool Alloc(size_t nLen) { reserve(nLen); return capacity() >= nLen; } |
3c67202d | 2289 | // minimize the string's memory |
dd1eaa89 | 2290 | // only works if the data of this string is not shared |
b1801e0e | 2291 | bool Shrink(); |
8f93a29f | 2292 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
d8a4b666 VS |
2293 | // These are deprecated, use wxStringBuffer or wxStringBufferLength instead |
2294 | // | |
3c67202d VZ |
2295 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
2296 | // a.s.a.p. to put string back in a reasonable state! | |
c87a0bc8 | 2297 | wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) ); |
3c67202d | 2298 | // call this immediately after GetWriteBuf() has been used |
d8a4b666 VS |
2299 | wxDEPRECATED( void UngetWriteBuf() ); |
2300 | wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); | |
8f93a29f | 2301 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8 |
c801d85f | 2302 | |
77ffb593 | 2303 | // wxWidgets version 1 compatibility functions |
3c67202d VZ |
2304 | |
2305 | // use Mid() | |
2306 | wxString SubString(size_t from, size_t to) const | |
2307 | { return Mid(from, (to - from + 1)); } | |
2308 | // values for second parameter of CompareTo function | |
c801d85f | 2309 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 2310 | // values for first parameter of Strip function |
c801d85f | 2311 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 2312 | |
c9f78968 | 2313 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
7357f981 GD |
2314 | // use Printf() |
2315 | // (take 'this' into account in attribute parameter count) | |
862bb5c7 | 2316 | // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2; |
1528e0b8 | 2317 | WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&), |
d1f6e2cf | 2318 | DoPrintfWchar, DoPrintfUtf8) |
2523e9b7 VS |
2319 | #ifdef __WATCOMC__ |
2320 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
59a14f69 VS |
2321 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&), |
2322 | (wxFormatString(f1))); | |
2323 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&), | |
2324 | (wxFormatString(f1))); | |
2325 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*), | |
2326 | (wxFormatString(f1))); | |
2327 | WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*), | |
2328 | (wxFormatString(f1))); | |
2523e9b7 | 2329 | #endif |
c9f78968 | 2330 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN |
c801d85f | 2331 | |
3c67202d | 2332 | // use Cmp() |
68482dc5 | 2333 | int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 2334 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 2335 | |
8f93a29f | 2336 | // use length() |
e87b7833 | 2337 | size_t Length() const { return length(); } |
3c67202d | 2338 | // Count the number of characters |
c9f78968 | 2339 | int Freq(wxUniChar ch) const; |
3c67202d | 2340 | // use MakeLower |
c801d85f | 2341 | void LowerCase() { MakeLower(); } |
3c67202d | 2342 | // use MakeUpper |
c801d85f | 2343 | void UpperCase() { MakeUpper(); } |
3c67202d | 2344 | // use Trim except that it doesn't change this string |
c801d85f KB |
2345 | wxString Strip(stripType w = trailing) const; |
2346 | ||
3c67202d | 2347 | // use Find (more general variants not yet supported) |
2bb67b80 | 2348 | size_t Index(const wxChar* psz) const { return Find(psz); } |
c9f78968 | 2349 | size_t Index(wxUniChar ch) const { return Find(ch); } |
3c67202d | 2350 | // use Truncate |
c801d85f | 2351 | wxString& Remove(size_t pos) { return Truncate(pos); } |
e87b7833 | 2352 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } |
c801d85f | 2353 | |
e87b7833 MB |
2354 | wxString& Remove(size_t nStart, size_t nLen) |
2355 | { return (wxString&)erase( nStart, nLen ); } | |
dd1eaa89 | 2356 | |
3c67202d | 2357 | // use Find() |
8f93a29f | 2358 | int First( wxUniChar ch ) const { return Find(ch); } |
8a540c88 | 2359 | int First( wxUniCharRef ch ) const { return Find(ch); } |
c9f78968 | 2360 | int First( char ch ) const { return Find(ch); } |
50e02008 | 2361 | int First( unsigned char ch ) const { return Find(ch); } |
c9f78968 | 2362 | int First( wchar_t ch ) const { return Find(ch); } |
8a540c88 | 2363 | int First( const wxString& str ) const { return Find(str); } |
8f93a29f | 2364 | int Last( wxUniChar ch ) const { return Find(ch, true); } |
d775fa82 | 2365 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } |
c801d85f | 2366 | |
5a8231ef WS |
2367 | // use empty() |
2368 | bool IsNull() const { return empty(); } | |
c801d85f | 2369 | |
3c67202d | 2370 | // std::string compatibility functions |
dd1eaa89 | 2371 | |
3c67202d VZ |
2372 | // take nLen chars starting at nPos |
2373 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
1545d942 | 2374 | { assign(str, nPos, nLen); } |
f2a1b1bd | 2375 | // take all characters from first to last |
e87b7833 | 2376 | wxString(const_iterator first, const_iterator last) |
cf9a878b | 2377 | : m_impl(first.impl(), last.impl()) { } |
67cff9dc VZ |
2378 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER |
2379 | // the 2 overloads below are for compatibility with the existing code using | |
2380 | // pointers instead of iterators | |
f2a1b1bd VZ |
2381 | wxString(const char *first, const char *last) |
2382 | { | |
2383 | SubstrBufFromMB str(ImplStr(first, last - first)); | |
2384 | m_impl.assign(str.data, str.len); | |
2385 | } | |
2386 | wxString(const wchar_t *first, const wchar_t *last) | |
2387 | { | |
2388 | SubstrBufFromWC str(ImplStr(first, last - first)); | |
2389 | m_impl.assign(str.data, str.len); | |
2390 | } | |
67cff9dc VZ |
2391 | // and this one is needed to compile code adding offsets to c_str() result |
2392 | wxString(const wxCStrData& first, const wxCStrData& last) | |
cf9a878b VS |
2393 | : m_impl(CreateConstIterator(first).impl(), |
2394 | CreateConstIterator(last).impl()) | |
67cff9dc VZ |
2395 | { |
2396 | wxASSERT_MSG( first.m_str == last.m_str, | |
2397 | _T("pointers must be into the same string") ); | |
2398 | } | |
2399 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
5460b9e3 | 2400 | |
3c67202d | 2401 | // lib.string.modifiers |
3c67202d VZ |
2402 | // append elements str[pos], ..., str[pos+n] |
2403 | wxString& append(const wxString& str, size_t pos, size_t n) | |
8f93a29f | 2404 | { |
68482dc5 VZ |
2405 | wxSTRING_UPDATE_CACHED_LENGTH(n); |
2406 | ||
2407 | size_t from, len; | |
2408 | str.PosLenToImpl(pos, n, &from, &len); | |
2409 | m_impl.append(str.m_impl, from, len); | |
2410 | return *this; | |
8f93a29f | 2411 | } |
e87b7833 MB |
2412 | // append a string |
2413 | wxString& append(const wxString& str) | |
68482dc5 VZ |
2414 | { |
2415 | wxSTRING_UPDATE_CACHED_LENGTH(str.length()); | |
2416 | ||
2417 | m_impl.append(str.m_impl); | |
2418 | return *this; | |
2419 | } | |
2420 | ||
3c67202d | 2421 | // append first n (or all if n == npos) characters of sz |
8f93a29f | 2422 | wxString& append(const char *sz) |
68482dc5 VZ |
2423 | { |
2424 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
2425 | ||
2426 | m_impl.append(ImplStr(sz)); | |
2427 | return *this; | |
2428 | } | |
2429 | ||
8f93a29f | 2430 | wxString& append(const wchar_t *sz) |
68482dc5 VZ |
2431 | { |
2432 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
2433 | ||
2434 | m_impl.append(ImplStr(sz)); | |
2435 | return *this; | |
2436 | } | |
2437 | ||
8f93a29f VS |
2438 | wxString& append(const char *sz, size_t n) |
2439 | { | |
68482dc5 VZ |
2440 | wxSTRING_INVALIDATE_CACHED_LENGTH(); |
2441 | ||
2442 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2443 | m_impl.append(str.data, str.len); | |
2444 | return *this; | |
8f93a29f VS |
2445 | } |
2446 | wxString& append(const wchar_t *sz, size_t n) | |
2447 | { | |
68482dc5 VZ |
2448 | wxSTRING_UPDATE_CACHED_LENGTH(n); |
2449 | ||
2450 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2451 | m_impl.append(str.data, str.len); | |
2452 | return *this; | |
8f93a29f | 2453 | } |
d3cefe87 VS |
2454 | |
2455 | wxString& append(const wxCStrData& str) | |
2456 | { return append(str.AsString()); } | |
2457 | wxString& append(const wxCharBuffer& str) | |
2458 | { return append(str.data()); } | |
2459 | wxString& append(const wxWCharBuffer& str) | |
2460 | { return append(str.data()); } | |
f416695a VS |
2461 | wxString& append(const wxCStrData& str, size_t n) |
2462 | { return append(str.AsString(), 0, n); } | |
2463 | wxString& append(const wxCharBuffer& str, size_t n) | |
2464 | { return append(str.data(), n); } | |
2465 | wxString& append(const wxWCharBuffer& str, size_t n) | |
2466 | { return append(str.data(), n); } | |
d3cefe87 | 2467 | |
3c67202d | 2468 | // append n copies of ch |
c9f78968 | 2469 | wxString& append(size_t n, wxUniChar ch) |
8f93a29f VS |
2470 | { |
2471 | #if wxUSE_UNICODE_UTF8 | |
68482dc5 VZ |
2472 | if ( !ch.IsAscii() ) |
2473 | { | |
2474 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
2475 | ||
2476 | m_impl.append(wxStringOperations::EncodeNChars(n, ch)); | |
2477 | } | |
2478 | else // ASCII | |
8f93a29f | 2479 | #endif |
68482dc5 VZ |
2480 | { |
2481 | wxSTRING_UPDATE_CACHED_LENGTH(n); | |
2482 | ||
2483 | m_impl.append(n, (wxStringCharType)ch); | |
2484 | } | |
2485 | ||
2486 | return *this; | |
8f93a29f | 2487 | } |
68482dc5 | 2488 | |
f416695a VS |
2489 | wxString& append(size_t n, wxUniCharRef ch) |
2490 | { return append(n, wxUniChar(ch)); } | |
2491 | wxString& append(size_t n, char ch) | |
2492 | { return append(n, wxUniChar(ch)); } | |
2493 | wxString& append(size_t n, unsigned char ch) | |
2494 | { return append(n, wxUniChar(ch)); } | |
2495 | wxString& append(size_t n, wchar_t ch) | |
2496 | { return append(n, wxUniChar(ch)); } | |
2497 | ||
e87b7833 MB |
2498 | // append from first to last |
2499 | wxString& append(const_iterator first, const_iterator last) | |
68482dc5 VZ |
2500 | { |
2501 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
2502 | ||
2503 | m_impl.append(first.impl(), last.impl()); | |
2504 | return *this; | |
2505 | } | |
67cff9dc | 2506 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER |
f2a1b1bd VZ |
2507 | wxString& append(const char *first, const char *last) |
2508 | { return append(first, last - first); } | |
2509 | wxString& append(const wchar_t *first, const wchar_t *last) | |
2510 | { return append(first, last - first); } | |
67cff9dc VZ |
2511 | wxString& append(const wxCStrData& first, const wxCStrData& last) |
2512 | { return append(CreateConstIterator(first), CreateConstIterator(last)); } | |
2513 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
3c67202d VZ |
2514 | |
2515 | // same as `this_string = str' | |
735d1db6 | 2516 | wxString& assign(const wxString& str) |
68482dc5 VZ |
2517 | { |
2518 | wxSTRING_SET_CACHED_LENGTH(str.length()); | |
2519 | ||
2520 | m_impl = str.m_impl; | |
2521 | ||
2522 | return *this; | |
2523 | } | |
2524 | ||
11aac4ba VS |
2525 | wxString& assign(const wxString& str, size_t len) |
2526 | { | |
68482dc5 VZ |
2527 | wxSTRING_SET_CACHED_LENGTH(len); |
2528 | ||
2529 | m_impl.assign(str.m_impl, 0, str.LenToImpl(len)); | |
2530 | ||
2531 | return *this; | |
11aac4ba | 2532 | } |
68482dc5 | 2533 | |
3c67202d VZ |
2534 | // same as ` = str[pos..pos + n] |
2535 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
8f93a29f | 2536 | { |
68482dc5 VZ |
2537 | size_t from, len; |
2538 | str.PosLenToImpl(pos, n, &from, &len); | |
2539 | m_impl.assign(str.m_impl, from, len); | |
2540 | ||
2541 | // it's important to call this after PosLenToImpl() above in case str is | |
2542 | // the same string as this one | |
2543 | wxSTRING_SET_CACHED_LENGTH(n); | |
2544 | ||
2545 | return *this; | |
8f93a29f | 2546 | } |
68482dc5 | 2547 | |
3c67202d | 2548 | // same as `= first n (or all if n == npos) characters of sz' |
8f93a29f | 2549 | wxString& assign(const char *sz) |
68482dc5 VZ |
2550 | { |
2551 | wxSTRING_INVALIDATE_CACHE(); | |
2552 | ||
2553 | m_impl.assign(ImplStr(sz)); | |
2554 | ||
2555 | return *this; | |
2556 | } | |
2557 | ||
8f93a29f | 2558 | wxString& assign(const wchar_t *sz) |
68482dc5 VZ |
2559 | { |
2560 | wxSTRING_INVALIDATE_CACHE(); | |
2561 | ||
2562 | m_impl.assign(ImplStr(sz)); | |
2563 | ||
2564 | return *this; | |
2565 | } | |
2566 | ||
8f93a29f VS |
2567 | wxString& assign(const char *sz, size_t n) |
2568 | { | |
68482dc5 VZ |
2569 | wxSTRING_SET_CACHED_LENGTH(n); |
2570 | ||
2571 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2572 | m_impl.assign(str.data, str.len); | |
2573 | ||
2574 | return *this; | |
8f93a29f | 2575 | } |
68482dc5 | 2576 | |
8f93a29f VS |
2577 | wxString& assign(const wchar_t *sz, size_t n) |
2578 | { | |
68482dc5 VZ |
2579 | wxSTRING_SET_CACHED_LENGTH(n); |
2580 | ||
2581 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2582 | m_impl.assign(str.data, str.len); | |
2583 | ||
2584 | return *this; | |
8f93a29f | 2585 | } |
d3cefe87 VS |
2586 | |
2587 | wxString& assign(const wxCStrData& str) | |
2588 | { return assign(str.AsString()); } | |
2589 | wxString& assign(const wxCharBuffer& str) | |
2590 | { return assign(str.data()); } | |
2591 | wxString& assign(const wxWCharBuffer& str) | |
2592 | { return assign(str.data()); } | |
2593 | wxString& assign(const wxCStrData& str, size_t len) | |
2594 | { return assign(str.AsString(), len); } | |
2595 | wxString& assign(const wxCharBuffer& str, size_t len) | |
2596 | { return assign(str.data(), len); } | |
2597 | wxString& assign(const wxWCharBuffer& str, size_t len) | |
2598 | { return assign(str.data(), len); } | |
2599 | ||
3c67202d | 2600 | // same as `= n copies of ch' |
c9f78968 | 2601 | wxString& assign(size_t n, wxUniChar ch) |
8f93a29f | 2602 | { |
68482dc5 VZ |
2603 | wxSTRING_SET_CACHED_LENGTH(n); |
2604 | ||
8f93a29f | 2605 | #if wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
2606 | if ( !ch.IsAscii() ) |
2607 | m_impl.assign(wxStringOperations::EncodeNChars(n, ch)); | |
2608 | else | |
8f93a29f | 2609 | #endif |
68482dc5 VZ |
2610 | m_impl.assign(n, (wxStringCharType)ch); |
2611 | ||
2612 | return *this; | |
8f93a29f | 2613 | } |
11aac4ba VS |
2614 | |
2615 | wxString& assign(size_t n, wxUniCharRef ch) | |
2616 | { return assign(n, wxUniChar(ch)); } | |
2617 | wxString& assign(size_t n, char ch) | |
2618 | { return assign(n, wxUniChar(ch)); } | |
2619 | wxString& assign(size_t n, unsigned char ch) | |
2620 | { return assign(n, wxUniChar(ch)); } | |
2621 | wxString& assign(size_t n, wchar_t ch) | |
2622 | { return assign(n, wxUniChar(ch)); } | |
2623 | ||
e87b7833 MB |
2624 | // assign from first to last |
2625 | wxString& assign(const_iterator first, const_iterator last) | |
68482dc5 VZ |
2626 | { |
2627 | wxSTRING_INVALIDATE_CACHE(); | |
2628 | ||
2629 | m_impl.assign(first.impl(), last.impl()); | |
2630 | ||
2631 | return *this; | |
2632 | } | |
67cff9dc | 2633 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER |
f2a1b1bd VZ |
2634 | wxString& assign(const char *first, const char *last) |
2635 | { return assign(first, last - first); } | |
2636 | wxString& assign(const wchar_t *first, const wchar_t *last) | |
2637 | { return assign(first, last - first); } | |
67cff9dc VZ |
2638 | wxString& assign(const wxCStrData& first, const wxCStrData& last) |
2639 | { return assign(CreateConstIterator(first), CreateConstIterator(last)); } | |
2640 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
e87b7833 MB |
2641 | |
2642 | // string comparison | |
8f93a29f | 2643 | int compare(const wxString& str) const; |
d3cefe87 VS |
2644 | int compare(const char* sz) const; |
2645 | int compare(const wchar_t* sz) const; | |
2646 | int compare(const wxCStrData& str) const | |
2647 | { return compare(str.AsString()); } | |
2648 | int compare(const wxCharBuffer& str) const | |
2649 | { return compare(str.data()); } | |
2650 | int compare(const wxWCharBuffer& str) const | |
2651 | { return compare(str.data()); } | |
e87b7833 | 2652 | // comparison with a substring |
8f93a29f | 2653 | int compare(size_t nStart, size_t nLen, const wxString& str) const; |
e87b7833 MB |
2654 | // comparison of 2 substrings |
2655 | int compare(size_t nStart, size_t nLen, | |
8f93a29f | 2656 | const wxString& str, size_t nStart2, size_t nLen2) const; |
e87b7833 MB |
2657 | // substring comparison with first nCount characters of sz |
2658 | int compare(size_t nStart, size_t nLen, | |
8f93a29f VS |
2659 | const char* sz, size_t nCount = npos) const; |
2660 | int compare(size_t nStart, size_t nLen, | |
2661 | const wchar_t* sz, size_t nCount = npos) const; | |
3c67202d VZ |
2662 | |
2663 | // insert another string | |
e87b7833 | 2664 | wxString& insert(size_t nPos, const wxString& str) |
68482dc5 | 2665 | { insert(GetIterForNthChar(nPos), str.begin(), str.end()); return *this; } |
3c67202d VZ |
2666 | // insert n chars of str starting at nStart (in str) |
2667 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
8f93a29f | 2668 | { |
68482dc5 VZ |
2669 | wxSTRING_UPDATE_CACHED_LENGTH(n); |
2670 | ||
2671 | size_t from, len; | |
2672 | str.PosLenToImpl(nStart, n, &from, &len); | |
2673 | m_impl.insert(PosToImpl(nPos), str.m_impl, from, len); | |
2674 | ||
2675 | return *this; | |
8f93a29f | 2676 | } |
68482dc5 | 2677 | |
3c67202d | 2678 | // insert first n (or all if n == npos) characters of sz |
8f93a29f | 2679 | wxString& insert(size_t nPos, const char *sz) |
68482dc5 VZ |
2680 | { |
2681 | wxSTRING_INVALIDATE_CACHE(); | |
2682 | ||
2683 | m_impl.insert(PosToImpl(nPos), ImplStr(sz)); | |
2684 | ||
2685 | return *this; | |
2686 | } | |
2687 | ||
8f93a29f | 2688 | wxString& insert(size_t nPos, const wchar_t *sz) |
68482dc5 VZ |
2689 | { |
2690 | wxSTRING_INVALIDATE_CACHE(); | |
2691 | ||
2692 | m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; | |
2693 | } | |
2694 | ||
8f93a29f VS |
2695 | wxString& insert(size_t nPos, const char *sz, size_t n) |
2696 | { | |
68482dc5 VZ |
2697 | wxSTRING_UPDATE_CACHED_LENGTH(n); |
2698 | ||
2699 | SubstrBufFromMB str(ImplStr(sz, n)); | |
2700 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
2701 | ||
2702 | return *this; | |
8f93a29f | 2703 | } |
68482dc5 | 2704 | |
8f93a29f VS |
2705 | wxString& insert(size_t nPos, const wchar_t *sz, size_t n) |
2706 | { | |
68482dc5 VZ |
2707 | wxSTRING_UPDATE_CACHED_LENGTH(n); |
2708 | ||
2709 | SubstrBufFromWC str(ImplStr(sz, n)); | |
2710 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
2711 | ||
2712 | return *this; | |
8f93a29f | 2713 | } |
68482dc5 | 2714 | |
3c67202d | 2715 | // insert n copies of ch |
c9f78968 | 2716 | wxString& insert(size_t nPos, size_t n, wxUniChar ch) |
8f93a29f | 2717 | { |
68482dc5 VZ |
2718 | wxSTRING_UPDATE_CACHED_LENGTH(n); |
2719 | ||
8f93a29f | 2720 | #if wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
2721 | if ( !ch.IsAscii() ) |
2722 | m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch)); | |
2723 | else | |
8f93a29f | 2724 | #endif |
68482dc5 VZ |
2725 | m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch); |
2726 | return *this; | |
8f93a29f | 2727 | } |
68482dc5 | 2728 | |
c9f78968 | 2729 | iterator insert(iterator it, wxUniChar ch) |
81727065 | 2730 | { |
68482dc5 VZ |
2731 | wxSTRING_UPDATE_CACHED_LENGTH(1); |
2732 | ||
81727065 | 2733 | #if wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
2734 | if ( !ch.IsAscii() ) |
2735 | { | |
2736 | size_t pos = IterToImplPos(it); | |
2737 | m_impl.insert(pos, wxStringOperations::EncodeChar(ch)); | |
2738 | return iterator(this, m_impl.begin() + pos); | |
2739 | } | |
2740 | else | |
81727065 | 2741 | #endif |
68482dc5 | 2742 | return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch)); |
81727065 | 2743 | } |
68482dc5 | 2744 | |
e87b7833 | 2745 | void insert(iterator it, const_iterator first, const_iterator last) |
68482dc5 VZ |
2746 | { |
2747 | wxSTRING_INVALIDATE_CACHE(); | |
2748 | ||
2749 | m_impl.insert(it.impl(), first.impl(), last.impl()); | |
2750 | } | |
2751 | ||
67cff9dc | 2752 | #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER |
f2a1b1bd VZ |
2753 | void insert(iterator it, const char *first, const char *last) |
2754 | { insert(it - begin(), first, last - first); } | |
67cff9dc | 2755 | void insert(iterator it, const wchar_t *first, const wchar_t *last) |
f2a1b1bd | 2756 | { insert(it - begin(), first, last - first); } |
67cff9dc VZ |
2757 | void insert(iterator it, const wxCStrData& first, const wxCStrData& last) |
2758 | { insert(it, CreateConstIterator(first), CreateConstIterator(last)); } | |
2759 | #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER | |
2760 | ||
c9f78968 | 2761 | void insert(iterator it, size_type n, wxUniChar ch) |
8f93a29f | 2762 | { |
68482dc5 VZ |
2763 | wxSTRING_UPDATE_CACHED_LENGTH(n); |
2764 | ||
8f93a29f | 2765 | #if wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
2766 | if ( !ch.IsAscii() ) |
2767 | m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch)); | |
2768 | else | |
8f93a29f | 2769 | #endif |
68482dc5 | 2770 | m_impl.insert(it.impl(), n, (wxStringCharType)ch); |
8f93a29f | 2771 | } |
3c67202d VZ |
2772 | |
2773 | // delete characters from nStart to nStart + nLen | |
e87b7833 | 2774 | wxString& erase(size_type pos = 0, size_type n = npos) |
8f93a29f | 2775 | { |
68482dc5 VZ |
2776 | wxSTRING_INVALIDATE_CACHE(); |
2777 | ||
2778 | size_t from, len; | |
2779 | PosLenToImpl(pos, n, &from, &len); | |
2780 | m_impl.erase(from, len); | |
2781 | ||
2782 | return *this; | |
8f93a29f | 2783 | } |
68482dc5 | 2784 | |
f2a1b1bd | 2785 | // delete characters from first up to last |
e87b7833 | 2786 | iterator erase(iterator first, iterator last) |
68482dc5 VZ |
2787 | { |
2788 | wxSTRING_INVALIDATE_CACHE(); | |
2789 | ||
2790 | return iterator(this, m_impl.erase(first.impl(), last.impl())); | |
2791 | } | |
2792 | ||
e87b7833 | 2793 | iterator erase(iterator first) |
68482dc5 VZ |
2794 | { |
2795 | wxSTRING_UPDATE_CACHED_LENGTH(-1); | |
2796 | ||
2797 | return iterator(this, m_impl.erase(first.impl())); | |
2798 | } | |
e87b7833 MB |
2799 | |
2800 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
2801 | void clear() { erase(); } | |
8f93a29f | 2802 | #else |
68482dc5 VZ |
2803 | void clear() |
2804 | { | |
2805 | wxSTRING_SET_CACHED_LENGTH(0); | |
2806 | ||
2807 | m_impl.clear(); | |
2808 | } | |
e87b7833 | 2809 | #endif |
3c67202d VZ |
2810 | |
2811 | // replaces the substring of length nLen starting at nStart | |
8f93a29f VS |
2812 | wxString& replace(size_t nStart, size_t nLen, const char* sz) |
2813 | { | |
68482dc5 VZ |
2814 | wxSTRING_INVALIDATE_CACHE(); |
2815 | ||
2816 | size_t from, len; | |
2817 | PosLenToImpl(nStart, nLen, &from, &len); | |
2818 | m_impl.replace(from, len, ImplStr(sz)); | |
2819 | ||
2820 | return *this; | |
8f93a29f | 2821 | } |
68482dc5 | 2822 | |
8f93a29f VS |
2823 | wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz) |
2824 | { | |
68482dc5 VZ |
2825 | wxSTRING_INVALIDATE_CACHE(); |
2826 | ||
2827 | size_t from, len; | |
2828 | PosLenToImpl(nStart, nLen, &from, &len); | |
2829 | m_impl.replace(from, len, ImplStr(sz)); | |
2830 | ||
2831 | return *this; | |
8f93a29f | 2832 | } |
68482dc5 | 2833 | |
e87b7833 MB |
2834 | // replaces the substring of length nLen starting at nStart |
2835 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
8f93a29f | 2836 | { |
68482dc5 VZ |
2837 | wxSTRING_INVALIDATE_CACHE(); |
2838 | ||
2839 | size_t from, len; | |
2840 | PosLenToImpl(nStart, nLen, &from, &len); | |
2841 | m_impl.replace(from, len, str.m_impl); | |
2842 | ||
2843 | return *this; | |
8f93a29f | 2844 | } |
68482dc5 | 2845 | |
3c67202d | 2846 | // replaces the substring with nCount copies of ch |
c9f78968 | 2847 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) |
8f93a29f | 2848 | { |
68482dc5 VZ |
2849 | wxSTRING_INVALIDATE_CACHE(); |
2850 | ||
2851 | size_t from, len; | |
2852 | PosLenToImpl(nStart, nLen, &from, &len); | |
8f93a29f | 2853 | #if wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
2854 | if ( !ch.IsAscii() ) |
2855 | m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch)); | |
2856 | else | |
8f93a29f | 2857 | #endif |
68482dc5 VZ |
2858 | m_impl.replace(from, len, nCount, (wxStringCharType)ch); |
2859 | ||
2860 | return *this; | |
8f93a29f | 2861 | } |
68482dc5 | 2862 | |
3c67202d VZ |
2863 | // replaces a substring with another substring |
2864 | wxString& replace(size_t nStart, size_t nLen, | |
e87b7833 | 2865 | const wxString& str, size_t nStart2, size_t nLen2) |
8f93a29f | 2866 | { |
68482dc5 | 2867 | wxSTRING_INVALIDATE_CACHE(); |
64a044d5 | 2868 | |
68482dc5 VZ |
2869 | size_t from, len; |
2870 | PosLenToImpl(nStart, nLen, &from, &len); | |
64a044d5 | 2871 | |
68482dc5 VZ |
2872 | size_t from2, len2; |
2873 | str.PosLenToImpl(nStart2, nLen2, &from2, &len2); | |
2874 | ||
2875 | m_impl.replace(from, len, str.m_impl, from2, len2); | |
2876 | ||
2877 | return *this; | |
8f93a29f | 2878 | } |
68482dc5 | 2879 | |
e87b7833 | 2880 | // replaces the substring with first nCount chars of sz |
fb20fa43 | 2881 | wxString& replace(size_t nStart, size_t nLen, |
8f93a29f VS |
2882 | const char* sz, size_t nCount) |
2883 | { | |
68482dc5 | 2884 | wxSTRING_INVALIDATE_CACHE(); |
64a044d5 | 2885 | |
68482dc5 VZ |
2886 | size_t from, len; |
2887 | PosLenToImpl(nStart, nLen, &from, &len); | |
64a044d5 | 2888 | |
68482dc5 VZ |
2889 | SubstrBufFromMB str(ImplStr(sz, nCount)); |
2890 | ||
2891 | m_impl.replace(from, len, str.data, str.len); | |
2892 | ||
2893 | return *this; | |
8f93a29f | 2894 | } |
68482dc5 | 2895 | |
8f93a29f VS |
2896 | wxString& replace(size_t nStart, size_t nLen, |
2897 | const wchar_t* sz, size_t nCount) | |
2898 | { | |
68482dc5 | 2899 | wxSTRING_INVALIDATE_CACHE(); |
64a044d5 | 2900 | |
68482dc5 VZ |
2901 | size_t from, len; |
2902 | PosLenToImpl(nStart, nLen, &from, &len); | |
64a044d5 | 2903 | |
68482dc5 VZ |
2904 | SubstrBufFromWC str(ImplStr(sz, nCount)); |
2905 | ||
2906 | m_impl.replace(from, len, str.data, str.len); | |
2907 | ||
2908 | return *this; | |
8f93a29f | 2909 | } |
68482dc5 | 2910 | |
11aac4ba VS |
2911 | wxString& replace(size_t nStart, size_t nLen, |
2912 | const wxString& s, size_t nCount) | |
2913 | { | |
68482dc5 VZ |
2914 | wxSTRING_INVALIDATE_CACHE(); |
2915 | ||
2916 | size_t from, len; | |
2917 | PosLenToImpl(nStart, nLen, &from, &len); | |
2918 | m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount)); | |
2919 | ||
2920 | return *this; | |
11aac4ba VS |
2921 | } |
2922 | ||
8f93a29f | 2923 | wxString& replace(iterator first, iterator last, const char* s) |
68482dc5 VZ |
2924 | { |
2925 | wxSTRING_INVALIDATE_CACHE(); | |
2926 | ||
2927 | m_impl.replace(first.impl(), last.impl(), ImplStr(s)); | |
2928 | ||
2929 | return *this; | |
2930 | } | |
2931 | ||
8f93a29f | 2932 | wxString& replace(iterator first, iterator last, const wchar_t* s) |
68482dc5 VZ |
2933 | { |
2934 | wxSTRING_INVALIDATE_CACHE(); | |
2935 | ||
2936 | m_impl.replace(first.impl(), last.impl(), ImplStr(s)); | |
2937 | ||
2938 | return *this; | |
2939 | } | |
2940 | ||
8f93a29f VS |
2941 | wxString& replace(iterator first, iterator last, const char* s, size_type n) |
2942 | { | |
68482dc5 VZ |
2943 | wxSTRING_INVALIDATE_CACHE(); |
2944 | ||
2945 | SubstrBufFromMB str(ImplStr(s, n)); | |
2946 | m_impl.replace(first.impl(), last.impl(), str.data, str.len); | |
2947 | ||
2948 | return *this; | |
8f93a29f | 2949 | } |
68482dc5 | 2950 | |
8f93a29f VS |
2951 | wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n) |
2952 | { | |
68482dc5 VZ |
2953 | wxSTRING_INVALIDATE_CACHE(); |
2954 | ||
2955 | SubstrBufFromWC str(ImplStr(s, n)); | |
2956 | m_impl.replace(first.impl(), last.impl(), str.data, str.len); | |
2957 | ||
2958 | return *this; | |
8f93a29f | 2959 | } |
68482dc5 | 2960 | |
e87b7833 | 2961 | wxString& replace(iterator first, iterator last, const wxString& s) |
68482dc5 VZ |
2962 | { |
2963 | wxSTRING_INVALIDATE_CACHE(); | |
2964 | ||
2965 | m_impl.replace(first.impl(), last.impl(), s.m_impl); | |
2966 | ||
2967 | return *this; | |
2968 | } | |
2969 | ||
8f93a29f VS |
2970 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch) |
2971 | { | |
68482dc5 VZ |
2972 | wxSTRING_INVALIDATE_CACHE(); |
2973 | ||
8f93a29f | 2974 | #if wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
2975 | if ( !ch.IsAscii() ) |
2976 | m_impl.replace(first.impl(), last.impl(), | |
2977 | wxStringOperations::EncodeNChars(n, ch)); | |
2978 | else | |
8f93a29f | 2979 | #endif |
68482dc5 VZ |
2980 | m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch); |
2981 | ||
2982 | return *this; | |
8f93a29f | 2983 | } |
68482dc5 | 2984 | |
e87b7833 MB |
2985 | wxString& replace(iterator first, iterator last, |
2986 | const_iterator first1, const_iterator last1) | |
cf9a878b | 2987 | { |
68482dc5 VZ |
2988 | wxSTRING_INVALIDATE_CACHE(); |
2989 | ||
2990 | m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl()); | |
2991 | ||
2992 | return *this; | |
cf9a878b | 2993 | } |
68482dc5 | 2994 | |
f2a1b1bd VZ |
2995 | wxString& replace(iterator first, iterator last, |
2996 | const char *first1, const char *last1) | |
2997 | { replace(first, last, first1, last1 - first1); return *this; } | |
2998 | wxString& replace(iterator first, iterator last, | |
2999 | const wchar_t *first1, const wchar_t *last1) | |
3000 | { replace(first, last, first1, last1 - first1); return *this; } | |
8f93a29f VS |
3001 | |
3002 | // swap two strings | |
3003 | void swap(wxString& str) | |
68482dc5 | 3004 | { |
af912213 VZ |
3005 | #if wxUSE_STRING_POS_CACHE |
3006 | // we modify not only this string but also the other one directly so we | |
3007 | // need to invalidate cache for both of them (we could also try to | |
3008 | // exchange their cache entries but it seems unlikely to be worth it) | |
3009 | InvalidateCache(); | |
3010 | str.InvalidateCache(); | |
3011 | #endif // wxUSE_STRING_POS_CACHE | |
68482dc5 VZ |
3012 | |
3013 | m_impl.swap(str.m_impl); | |
3014 | } | |
8f93a29f VS |
3015 | |
3016 | // find a substring | |
3017 | size_t find(const wxString& str, size_t nStart = 0) const | |
3018 | { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); } | |
3019 | ||
3020 | // find first n characters of sz | |
3021 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const | |
3022 | { | |
3023 | SubstrBufFromMB str(ImplStr(sz, n)); | |
3024 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
3025 | } | |
3026 | size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const | |
3027 | { | |
3028 | SubstrBufFromWC str(ImplStr(sz, n)); | |
3029 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
3030 | } | |
d3cefe87 VS |
3031 | size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const |
3032 | { return find(s.data(), nStart, n); } | |
3033 | size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const | |
3034 | { return find(s.data(), nStart, n); } | |
3035 | size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const | |
3036 | { return find(s.AsWChar(), nStart, n); } | |
8f93a29f | 3037 | |
7929902d | 3038 | // find the first occurrence of character ch after nStart |
8f93a29f | 3039 | size_t find(wxUniChar ch, size_t nStart = 0) const |
467175ab | 3040 | { |
84d2877f VS |
3041 | #if wxUSE_UNICODE_UTF8 |
3042 | if ( !ch.IsAscii() ) | |
3043 | return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch), | |
3044 | PosToImpl(nStart))); | |
3045 | else | |
3046 | #endif | |
3047 | return PosFromImpl(m_impl.find((wxStringCharType)ch, | |
3048 | PosToImpl(nStart))); | |
3049 | ||
467175ab | 3050 | } |
8f93a29f VS |
3051 | size_t find(wxUniCharRef ch, size_t nStart = 0) const |
3052 | { return find(wxUniChar(ch), nStart); } | |
3053 | size_t find(char ch, size_t nStart = 0) const | |
3054 | { return find(wxUniChar(ch), nStart); } | |
50e02008 VS |
3055 | size_t find(unsigned char ch, size_t nStart = 0) const |
3056 | { return find(wxUniChar(ch), nStart); } | |
8f93a29f VS |
3057 | size_t find(wchar_t ch, size_t nStart = 0) const |
3058 | { return find(wxUniChar(ch), nStart); } | |
3059 | ||
3060 | // rfind() family is exactly like find() but works right to left | |
3061 | ||
3062 | // as find, but from the end | |
3063 | size_t rfind(const wxString& str, size_t nStart = npos) const | |
3064 | { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); } | |
3065 | ||
3066 | // as find, but from the end | |
3067 | size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const | |
3068 | { | |
3069 | SubstrBufFromMB str(ImplStr(sz, n)); | |
3070 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
3071 | } | |
3072 | size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const | |
3073 | { | |
3074 | SubstrBufFromWC str(ImplStr(sz, n)); | |
3075 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
3076 | } | |
d3cefe87 VS |
3077 | size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const |
3078 | { return rfind(s.data(), nStart, n); } | |
3079 | size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const | |
3080 | { return rfind(s.data(), nStart, n); } | |
3081 | size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const | |
3082 | { return rfind(s.AsWChar(), nStart, n); } | |
8f93a29f VS |
3083 | // as find, but from the end |
3084 | size_t rfind(wxUniChar ch, size_t nStart = npos) const | |
467175ab | 3085 | { |
84d2877f VS |
3086 | #if wxUSE_UNICODE_UTF8 |
3087 | if ( !ch.IsAscii() ) | |
3088 | return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch), | |
3089 | PosToImpl(nStart))); | |
3090 | else | |
3091 | #endif | |
3092 | return PosFromImpl(m_impl.rfind((wxStringCharType)ch, | |
3093 | PosToImpl(nStart))); | |
467175ab | 3094 | } |
8f93a29f VS |
3095 | size_t rfind(wxUniCharRef ch, size_t nStart = npos) const |
3096 | { return rfind(wxUniChar(ch), nStart); } | |
3097 | size_t rfind(char ch, size_t nStart = npos) const | |
3098 | { return rfind(wxUniChar(ch), nStart); } | |
50e02008 VS |
3099 | size_t rfind(unsigned char ch, size_t nStart = npos) const |
3100 | { return rfind(wxUniChar(ch), nStart); } | |
8f93a29f VS |
3101 | size_t rfind(wchar_t ch, size_t nStart = npos) const |
3102 | { return rfind(wxUniChar(ch), nStart); } | |
3103 | ||
7929902d | 3104 | // find first/last occurrence of any character (not) in the set: |
8f93a29f VS |
3105 | #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
3106 | // FIXME-UTF8: this is not entirely correct, because it doesn't work if | |
3107 | // sizeof(wchar_t)==2 and surrogates are present in the string; | |
3108 | // should we care? Probably not. | |
3109 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
a962cdf4 | 3110 | { return m_impl.find_first_of(str.m_impl, nStart); } |
8f93a29f VS |
3111 | size_t find_first_of(const char* sz, size_t nStart = 0) const |
3112 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
3113 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const | |
3114 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
a962cdf4 | 3115 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 3116 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 3117 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f VS |
3118 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
3119 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
3120 | { return m_impl.find_first_of((wxChar)c, nStart); } | |
3121 | ||
a962cdf4 VS |
3122 | size_t find_last_of(const wxString& str, size_t nStart = npos) const |
3123 | { return m_impl.find_last_of(str.m_impl, nStart); } | |
8f93a29f VS |
3124 | size_t find_last_of(const char* sz, size_t nStart = npos) const |
3125 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
3126 | size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const | |
3127 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
3128 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const | |
3129 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
3130 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const | |
3131 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
3132 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
3133 | { return m_impl.find_last_of((wxChar)c, nStart); } | |
3134 | ||
a962cdf4 | 3135 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const |
8f93a29f | 3136 | { return m_impl.find_first_not_of(str.m_impl, nStart); } |
a962cdf4 | 3137 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const |
8f93a29f | 3138 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 3139 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const |
8f93a29f | 3140 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 3141 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 3142 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 3143 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 3144 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 3145 | size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const |
8f93a29f VS |
3146 | { return m_impl.find_first_not_of((wxChar)c, nStart); } |
3147 | ||
a962cdf4 | 3148 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const |
8f93a29f | 3149 | { return m_impl.find_last_not_of(str.m_impl, nStart); } |
a962cdf4 | 3150 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const |
8f93a29f | 3151 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 3152 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const |
8f93a29f | 3153 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 3154 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 3155 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 3156 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 3157 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 3158 | size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const |
8f93a29f VS |
3159 | { return m_impl.find_last_not_of((wxChar)c, nStart); } |
3160 | #else | |
3161 | // we can't use std::string implementation in UTF-8 build, because the | |
3162 | // character sets would be interpreted wrongly: | |
3163 | ||
3164 | // as strpbrk() but starts at nStart, returns npos if not found | |
3165 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
81727065 | 3166 | #if wxUSE_UNICODE // FIXME-UTF8: temporary |
d3cefe87 | 3167 | { return find_first_of(str.wc_str(), nStart); } |
81727065 | 3168 | #else |
d3cefe87 | 3169 | { return find_first_of(str.mb_str(), nStart); } |
81727065 | 3170 | #endif |
8f93a29f VS |
3171 | // same as above |
3172 | size_t find_first_of(const char* sz, size_t nStart = 0) const; | |
3173 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const; | |
3174 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const; | |
3175 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
3176 | // same as find(char, size_t) | |
3177 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
3178 | { return find(c, nStart); } | |
3179 | // find the last (starting from nStart) char from str in this string | |
3180 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
81727065 | 3181 | #if wxUSE_UNICODE // FIXME-UTF8: temporary |
d3cefe87 | 3182 | { return find_last_of(str.wc_str(), nStart); } |
81727065 | 3183 | #else |
d3cefe87 | 3184 | { return find_last_of(str.mb_str(), nStart); } |
81727065 | 3185 | #endif |
8f93a29f VS |
3186 | // same as above |
3187 | size_t find_last_of (const char* sz, size_t nStart = npos) const; | |
3188 | size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const; | |
3189 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const; | |
3190 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
3191 | // same as above | |
3192 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
3193 | { return rfind(c, nStart); } | |
3194 | ||
7929902d | 3195 | // find first/last occurrence of any character not in the set |
8f93a29f VS |
3196 | |
3197 | // as strspn() (starting from nStart), returns npos on failure | |
3198 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
81727065 | 3199 | #if wxUSE_UNICODE // FIXME-UTF8: temporary |
d3cefe87 | 3200 | { return find_first_not_of(str.wc_str(), nStart); } |
81727065 | 3201 | #else |
d3cefe87 | 3202 | { return find_first_not_of(str.mb_str(), nStart); } |
81727065 | 3203 | #endif |
8f93a29f VS |
3204 | // same as above |
3205 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const; | |
3206 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const; | |
3207 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const; | |
3208 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
3209 | // same as above | |
3210 | size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; | |
3211 | // as strcspn() | |
3212 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const | |
81727065 | 3213 | #if wxUSE_UNICODE // FIXME-UTF8: temporary |
d3cefe87 | 3214 | { return find_last_not_of(str.wc_str(), nStart); } |
81727065 | 3215 | #else |
d3cefe87 | 3216 | { return find_last_not_of(str.mb_str(), nStart); } |
81727065 | 3217 | #endif |
8f93a29f VS |
3218 | // same as above |
3219 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const; | |
3220 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const; | |
3221 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const; | |
3222 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
3223 | // same as above | |
3224 | size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; | |
3225 | #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not | |
3226 | ||
3227 | // provide char/wchar_t/wxUniCharRef overloads for char-finding functions | |
3228 | // above to resolve ambiguities: | |
3229 | size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const | |
3230 | { return find_first_of(wxUniChar(ch), nStart); } | |
3231 | size_t find_first_of(char ch, size_t nStart = 0) const | |
3232 | { return find_first_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
3233 | size_t find_first_of(unsigned char ch, size_t nStart = 0) const |
3234 | { return find_first_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
3235 | size_t find_first_of(wchar_t ch, size_t nStart = 0) const |
3236 | { return find_first_of(wxUniChar(ch), nStart); } | |
3237 | size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const | |
3238 | { return find_last_of(wxUniChar(ch), nStart); } | |
3239 | size_t find_last_of(char ch, size_t nStart = npos) const | |
3240 | { return find_last_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
3241 | size_t find_last_of(unsigned char ch, size_t nStart = npos) const |
3242 | { return find_last_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
3243 | size_t find_last_of(wchar_t ch, size_t nStart = npos) const |
3244 | { return find_last_of(wxUniChar(ch), nStart); } | |
3245 | size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const | |
3246 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
3247 | size_t find_first_not_of(char ch, size_t nStart = 0) const | |
3248 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
3249 | size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const |
3250 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
3251 | size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const |
3252 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
3253 | size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const | |
3254 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
3255 | size_t find_last_not_of(char ch, size_t nStart = npos) const | |
3256 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
3257 | size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const |
3258 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
3259 | size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const |
3260 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
3c67202d | 3261 | |
d3cefe87 VS |
3262 | // and additional overloads for the versions taking strings: |
3263 | size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const | |
3264 | { return find_first_of(sz.AsString(), nStart); } | |
3265 | size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
3266 | { return find_first_of(sz.data(), nStart); } | |
3267 | size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
3268 | { return find_first_of(sz.data(), nStart); } | |
3269 | size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
3270 | { return find_first_of(sz.AsWChar(), nStart, n); } | |
3271 | size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
3272 | { return find_first_of(sz.data(), nStart, n); } | |
3273 | size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
3274 | { return find_first_of(sz.data(), nStart, n); } | |
3275 | ||
3276 | size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const | |
3277 | { return find_last_of(sz.AsString(), nStart); } | |
3278 | size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
3279 | { return find_last_of(sz.data(), nStart); } | |
3280 | size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
3281 | { return find_last_of(sz.data(), nStart); } | |
3282 | size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
3283 | { return find_last_of(sz.AsWChar(), nStart, n); } | |
3284 | size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
3285 | { return find_last_of(sz.data(), nStart, n); } | |
3286 | size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
3287 | { return find_last_of(sz.data(), nStart, n); } | |
3288 | ||
3289 | size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const | |
3290 | { return find_first_not_of(sz.AsString(), nStart); } | |
3291 | size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
3292 | { return find_first_not_of(sz.data(), nStart); } | |
3293 | size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
3294 | { return find_first_not_of(sz.data(), nStart); } | |
3295 | size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
3296 | { return find_first_not_of(sz.AsWChar(), nStart, n); } | |
3297 | size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
3298 | { return find_first_not_of(sz.data(), nStart, n); } | |
3299 | size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
3300 | { return find_first_not_of(sz.data(), nStart, n); } | |
3301 | ||
3302 | size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const | |
3303 | { return find_last_not_of(sz.AsString(), nStart); } | |
3304 | size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const | |
3305 | { return find_last_not_of(sz.data(), nStart); } | |
3306 | size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const | |
3307 | { return find_last_not_of(sz.data(), nStart); } | |
3308 | size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const | |
3309 | { return find_last_not_of(sz.AsWChar(), nStart, n); } | |
3310 | size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const | |
3311 | { return find_last_not_of(sz.data(), nStart, n); } | |
3312 | size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const | |
3313 | { return find_last_not_of(sz.data(), nStart, n); } | |
3314 | ||
e87b7833 MB |
3315 | // string += string |
3316 | wxString& operator+=(const wxString& s) | |
68482dc5 VZ |
3317 | { |
3318 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
3319 | ||
3320 | m_impl += s.m_impl; | |
3321 | return *this; | |
3322 | } | |
e87b7833 | 3323 | // string += C string |
8f93a29f | 3324 | wxString& operator+=(const char *psz) |
68482dc5 VZ |
3325 | { |
3326 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
3327 | ||
3328 | m_impl += ImplStr(psz); | |
3329 | return *this; | |
3330 | } | |
8f93a29f | 3331 | wxString& operator+=(const wchar_t *pwz) |
68482dc5 VZ |
3332 | { |
3333 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
3334 | ||
3335 | m_impl += ImplStr(pwz); | |
3336 | return *this; | |
3337 | } | |
c9f78968 | 3338 | wxString& operator+=(const wxCStrData& s) |
68482dc5 VZ |
3339 | { |
3340 | wxSTRING_INVALIDATE_CACHED_LENGTH(); | |
3341 | ||
3342 | m_impl += s.AsString().m_impl; | |
3343 | return *this; | |
3344 | } | |
d3cefe87 VS |
3345 | wxString& operator+=(const wxCharBuffer& s) |
3346 | { return operator+=(s.data()); } | |
3347 | wxString& operator+=(const wxWCharBuffer& s) | |
3348 | { return operator+=(s.data()); } | |
e87b7833 | 3349 | // string += char |
c9f78968 | 3350 | wxString& operator+=(wxUniChar ch) |
84d2877f | 3351 | { |
68482dc5 VZ |
3352 | wxSTRING_UPDATE_CACHED_LENGTH(1); |
3353 | ||
84d2877f | 3354 | #if wxUSE_UNICODE_UTF8 |
68482dc5 VZ |
3355 | if ( !ch.IsAscii() ) |
3356 | m_impl += wxStringOperations::EncodeChar(ch); | |
3357 | else | |
84d2877f | 3358 | #endif |
68482dc5 VZ |
3359 | m_impl += (wxStringCharType)ch; |
3360 | return *this; | |
84d2877f | 3361 | } |
c9f78968 | 3362 | wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } |
b7d40341 | 3363 | wxString& operator+=(int ch) { return *this += wxUniChar(ch); } |
c9f78968 | 3364 | wxString& operator+=(char ch) { return *this += wxUniChar(ch); } |
b77afb41 | 3365 | wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); } |
c9f78968 | 3366 | wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } |
d8a4b666 VS |
3367 | |
3368 | private: | |
2523e9b7 | 3369 | #if !wxUSE_STL_BASED_WXSTRING |
d8a4b666 | 3370 | // helpers for wxStringBuffer and wxStringBufferLength |
8f93a29f | 3371 | wxStringCharType *DoGetWriteBuf(size_t nLen) |
68482dc5 VZ |
3372 | { |
3373 | return m_impl.DoGetWriteBuf(nLen); | |
3374 | } | |
3375 | ||
8f93a29f | 3376 | void DoUngetWriteBuf() |
68482dc5 VZ |
3377 | { |
3378 | wxSTRING_INVALIDATE_CACHE(); | |
3379 | ||
3380 | m_impl.DoUngetWriteBuf(); | |
3381 | } | |
3382 | ||
8f93a29f | 3383 | void DoUngetWriteBuf(size_t nLen) |
68482dc5 VZ |
3384 | { |
3385 | wxSTRING_SET_CACHED_LENGTH(nLen); | |
3386 | ||
3387 | m_impl.DoUngetWriteBuf(nLen); | |
3388 | } | |
2523e9b7 | 3389 | #endif // !wxUSE_STL_BASED_WXSTRING |
c9f78968 VS |
3390 | |
3391 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
d1f6e2cf VS |
3392 | #if !wxUSE_UTF8_LOCALE_ONLY |
3393 | int DoPrintfWchar(const wxChar *format, ...); | |
3394 | static wxString DoFormatWchar(const wxChar *format, ...); | |
3395 | #endif | |
3396 | #if wxUSE_UNICODE_UTF8 | |
3397 | int DoPrintfUtf8(const char *format, ...); | |
3398 | static wxString DoFormatUtf8(const char *format, ...); | |
3399 | #endif | |
c9f78968 | 3400 | #endif |
8f93a29f VS |
3401 | |
3402 | #if !wxUSE_STL_BASED_WXSTRING | |
3403 | // check string's data validity | |
3404 | bool IsValid() const { return m_impl.GetStringData()->IsValid(); } | |
3405 | #endif | |
3406 | ||
3407 | private: | |
3408 | wxStringImpl m_impl; | |
132276cf VS |
3409 | |
3410 | // buffers for compatibility conversion from (char*)c_str() and | |
3411 | // (wchar_t*)c_str(): | |
3412 | // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers | |
3413 | template<typename T> | |
3414 | struct ConvertedBuffer | |
3415 | { | |
3416 | ConvertedBuffer() : m_buf(NULL) {} | |
3417 | ~ConvertedBuffer() | |
3418 | { free(m_buf); } | |
3419 | ||
05f32fc3 | 3420 | operator T*() const { return m_buf; } |
132276cf VS |
3421 | |
3422 | ConvertedBuffer& operator=(T *str) | |
3423 | { | |
3424 | free(m_buf); | |
3425 | m_buf = str; | |
3426 | return *this; | |
3427 | } | |
3428 | ||
3429 | T *m_buf; | |
3430 | }; | |
111d9948 | 3431 | #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY |
132276cf VS |
3432 | ConvertedBuffer<char> m_convertedToChar; |
3433 | #endif | |
3434 | #if !wxUSE_UNICODE_WCHAR | |
3435 | ConvertedBuffer<wchar_t> m_convertedToWChar; | |
3436 | #endif | |
2523e9b7 | 3437 | |
b0c4d5d7 VS |
3438 | #if wxUSE_UNICODE_UTF8 |
3439 | // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently | |
3440 | // assigning to character pointer to by wxString::interator may | |
3441 | // change the underlying wxStringImpl iterator, so we have to | |
3442 | // keep track of all iterators and update them as necessary: | |
3443 | struct wxStringIteratorNodeHead | |
3444 | { | |
3445 | wxStringIteratorNodeHead() : ptr(NULL) {} | |
3446 | wxStringIteratorNode *ptr; | |
300b44a9 VS |
3447 | |
3448 | // copying is disallowed as it would result in more than one pointer into | |
3449 | // the same linked list | |
c0c133e1 | 3450 | wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead); |
b0c4d5d7 VS |
3451 | }; |
3452 | ||
3453 | wxStringIteratorNodeHead m_iterators; | |
3454 | ||
b5dbe15d VS |
3455 | friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode; |
3456 | friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef; | |
b0c4d5d7 VS |
3457 | #endif // wxUSE_UNICODE_UTF8 |
3458 | ||
b5dbe15d | 3459 | friend class WXDLLIMPEXP_FWD_BASE wxCStrData; |
6798451b VS |
3460 | friend class wxStringInternalBuffer; |
3461 | friend class wxStringInternalBufferLength; | |
c801d85f KB |
3462 | }; |
3463 | ||
c9f78968 VS |
3464 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
3465 | #pragma warning (default:4275) | |
3466 | #endif | |
3467 | ||
a962cdf4 VS |
3468 | // string iterator operators that satisfy STL Random Access Iterator |
3469 | // requirements: | |
b5343e06 | 3470 | inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i) |
a962cdf4 | 3471 | { return i + n; } |
b5343e06 | 3472 | inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i) |
a962cdf4 | 3473 | { return i + n; } |
b5343e06 | 3474 | inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i) |
a962cdf4 | 3475 | { return i + n; } |
b5343e06 | 3476 | inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i) |
a962cdf4 VS |
3477 | { return i + n; } |
3478 | ||
1fc8cfbd VZ |
3479 | // notice that even though for many compilers the friend declarations above are |
3480 | // enough, from the point of view of C++ standard we must have the declarations | |
3481 | // here as friend ones are not injected in the enclosing namespace and without | |
3482 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
c9f78968 | 3483 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
8f93a29f VS |
3484 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz); |
3485 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz); | |
3486 | wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string); | |
3487 | wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string); | |
1fc8cfbd | 3488 | |
c9f78968 VS |
3489 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
3490 | wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
3491 | ||
3492 | inline wxString operator+(const wxString& string, wxUniCharRef ch) | |
3493 | { return string + (wxUniChar)ch; } | |
3494 | inline wxString operator+(const wxString& string, char ch) | |
3495 | { return string + wxUniChar(ch); } | |
3496 | inline wxString operator+(const wxString& string, wchar_t ch) | |
3497 | { return string + wxUniChar(ch); } | |
3498 | inline wxString operator+(wxUniCharRef ch, const wxString& string) | |
3499 | { return (wxUniChar)ch + string; } | |
3500 | inline wxString operator+(char ch, const wxString& string) | |
3501 | { return wxUniChar(ch) + string; } | |
3502 | inline wxString operator+(wchar_t ch, const wxString& string) | |
3503 | { return wxUniChar(ch) + string; } | |
3504 | ||
b7146cbe | 3505 | |
a0f63de9 | 3506 | #define wxGetEmptyString() wxString() |
07170120 | 3507 | |
062dc5fc VZ |
3508 | // ---------------------------------------------------------------------------- |
3509 | // helper functions which couldn't be defined inline | |
3510 | // ---------------------------------------------------------------------------- | |
3511 | ||
3512 | namespace wxPrivate | |
3513 | { | |
3514 | ||
3515 | #if wxUSE_UNICODE_WCHAR | |
3516 | ||
3517 | template <> | |
3518 | struct wxStringAsBufHelper<char> | |
3519 | { | |
3520 | static wxCharBuffer Get(const wxString& s, size_t *len) | |
3521 | { | |
3522 | wxCharBuffer buf(s.mb_str()); | |
3523 | if ( len ) | |
3524 | *len = buf ? strlen(buf) : 0; | |
3525 | return buf; | |
3526 | } | |
3527 | }; | |
3528 | ||
3529 | template <> | |
3530 | struct wxStringAsBufHelper<wchar_t> | |
3531 | { | |
3532 | static wxWCharBuffer Get(const wxString& s, size_t *len) | |
3533 | { | |
3534 | if ( len ) | |
3535 | *len = s.length(); | |
3536 | return wxWCharBuffer::CreateNonOwned(s.wx_str()); | |
3537 | } | |
3538 | }; | |
3539 | ||
3540 | #elif wxUSE_UNICODE_UTF8 | |
3541 | ||
3542 | template <> | |
3543 | struct wxStringAsBufHelper<char> | |
3544 | { | |
3545 | static wxCharBuffer Get(const wxString& s, size_t *len) | |
3546 | { | |
3547 | if ( len ) | |
3548 | *len = s.utf8_length(); | |
3549 | return wxCharBuffer::CreateNonOwned(s.wx_str()); | |
3550 | } | |
3551 | }; | |
3552 | ||
3553 | template <> | |
3554 | struct wxStringAsBufHelper<wchar_t> | |
3555 | { | |
3556 | static wxWCharBuffer Get(const wxString& s, size_t *len) | |
3557 | { | |
3558 | wxWCharBuffer wbuf(s.wc_str()); | |
3559 | if ( len ) | |
3560 | *len = wxWcslen(wbuf); | |
3561 | return wbuf; | |
3562 | } | |
3563 | }; | |
3564 | ||
3565 | #endif // Unicode build kind | |
3566 | ||
3567 | } // namespace wxPrivate | |
3568 | ||
1d218550 VZ |
3569 | // ---------------------------------------------------------------------------- |
3570 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
3571 | // ---------------------------------------------------------------------------- | |
3572 | ||
2523e9b7 VS |
3573 | #if !wxUSE_STL_BASED_WXSTRING |
3574 | // string buffer for direct access to string data in their native | |
3575 | // representation: | |
6798451b | 3576 | class wxStringInternalBuffer |
2523e9b7 VS |
3577 | { |
3578 | public: | |
3579 | typedef wxStringCharType CharType; | |
3580 | ||
6798451b | 3581 | wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024) |
2523e9b7 VS |
3582 | : m_str(str), m_buf(NULL) |
3583 | { m_buf = m_str.DoGetWriteBuf(lenWanted); } | |
3584 | ||
6798451b | 3585 | ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); } |
2523e9b7 VS |
3586 | |
3587 | operator wxStringCharType*() const { return m_buf; } | |
941b78cc | 3588 | |
2523e9b7 VS |
3589 | private: |
3590 | wxString& m_str; | |
3591 | wxStringCharType *m_buf; | |
3592 | ||
c0c133e1 | 3593 | wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer); |
2523e9b7 VS |
3594 | }; |
3595 | ||
6798451b | 3596 | class wxStringInternalBufferLength |
941b78cc MB |
3597 | { |
3598 | public: | |
2523e9b7 VS |
3599 | typedef wxStringCharType CharType; |
3600 | ||
6798451b | 3601 | wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024) |
2523e9b7 VS |
3602 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) |
3603 | { | |
3604 | m_buf = m_str.DoGetWriteBuf(lenWanted); | |
3605 | wxASSERT(m_buf != NULL); | |
3606 | } | |
3607 | ||
6798451b | 3608 | ~wxStringInternalBufferLength() |
2523e9b7 VS |
3609 | { |
3610 | wxASSERT(m_lenSet); | |
3611 | m_str.DoUngetWriteBuf(m_len); | |
3612 | } | |
3613 | ||
3614 | operator wxStringCharType*() const { return m_buf; } | |
3615 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
3616 | ||
3617 | private: | |
3618 | wxString& m_str; | |
3619 | wxStringCharType *m_buf; | |
3620 | size_t m_len; | |
3621 | bool m_lenSet; | |
3622 | ||
c0c133e1 | 3623 | wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength); |
2523e9b7 VS |
3624 | }; |
3625 | ||
3626 | #endif // !wxUSE_STL_BASED_WXSTRING | |
3627 | ||
3628 | template<typename T> | |
6b583d40 | 3629 | class wxStringTypeBufferBase |
2523e9b7 VS |
3630 | { |
3631 | public: | |
3632 | typedef T CharType; | |
3633 | ||
3634 | wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024) | |
e87b7833 | 3635 | : m_str(str), m_buf(lenWanted) |
062dc5fc VZ |
3636 | { |
3637 | // for compatibility with old wxStringBuffer which provided direct | |
3638 | // access to wxString internal buffer, initialize ourselves with the | |
3639 | // string initial contents | |
3640 | ||
3641 | // FIXME-VC6: remove the ugly (CharType *)NULL and use normal | |
3642 | // tchar_str<CharType> | |
3643 | size_t len; | |
3644 | const wxCharTypeBuffer<CharType> buf(str.tchar_str(&len, (CharType *)NULL)); | |
3645 | if ( buf ) | |
3646 | { | |
3647 | if ( len > lenWanted ) | |
3648 | { | |
3649 | // in this case there is not enough space for terminating NUL, | |
3650 | // ensure that we still put it there | |
3651 | m_buf.data()[lenWanted] = 0; | |
3652 | len = lenWanted - 1; | |
3653 | } | |
3654 | ||
39329d2e | 3655 | memcpy(m_buf.data(), buf, (len + 1)*sizeof(CharType)); |
062dc5fc VZ |
3656 | } |
3657 | //else: conversion failed, this can happen when trying to get Unicode | |
3658 | // string contents into a char string | |
3659 | } | |
941b78cc | 3660 | |
2523e9b7 | 3661 | operator CharType*() { return m_buf.data(); } |
941b78cc | 3662 | |
2523e9b7 | 3663 | protected: |
941b78cc | 3664 | wxString& m_str; |
2523e9b7 | 3665 | wxCharTypeBuffer<CharType> m_buf; |
941b78cc MB |
3666 | }; |
3667 | ||
2523e9b7 | 3668 | template<typename T> |
6b583d40 | 3669 | class wxStringTypeBufferLengthBase : public wxStringTypeBufferBase<T> |
941b78cc MB |
3670 | { |
3671 | public: | |
2523e9b7 | 3672 | wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024) |
062dc5fc VZ |
3673 | : wxStringTypeBufferBase<T>(str, lenWanted), |
3674 | m_len(0), | |
3675 | m_lenSet(false) | |
941b78cc MB |
3676 | { } |
3677 | ||
062dc5fc VZ |
3678 | ~wxStringTypeBufferLengthBase() |
3679 | { | |
3680 | wxASSERT_MSG( this->m_lenSet, "forgot to call SetLength()" ); | |
3681 | } | |
3682 | ||
941b78cc MB |
3683 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } |
3684 | ||
2523e9b7 | 3685 | protected: |
062dc5fc VZ |
3686 | size_t m_len; |
3687 | bool m_lenSet; | |
941b78cc MB |
3688 | }; |
3689 | ||
2523e9b7 VS |
3690 | template<typename T> |
3691 | class wxStringTypeBuffer : public wxStringTypeBufferBase<T> | |
3692 | { | |
3693 | public: | |
3694 | wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024) | |
062dc5fc VZ |
3695 | : wxStringTypeBufferBase<T>(str, lenWanted) |
3696 | { } | |
3697 | ||
2523e9b7 VS |
3698 | ~wxStringTypeBuffer() |
3699 | { | |
3700 | this->m_str.assign(this->m_buf.data()); | |
3701 | } | |
941b78cc | 3702 | |
c0c133e1 | 3703 | wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer); |
2523e9b7 VS |
3704 | }; |
3705 | ||
3706 | template<typename T> | |
3707 | class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T> | |
1d218550 VZ |
3708 | { |
3709 | public: | |
2523e9b7 | 3710 | wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024) |
062dc5fc VZ |
3711 | : wxStringTypeBufferLengthBase<T>(str, lenWanted) |
3712 | { } | |
e015c2a3 | 3713 | |
2523e9b7 VS |
3714 | ~wxStringTypeBufferLength() |
3715 | { | |
2523e9b7 VS |
3716 | this->m_str.assign(this->m_buf.data(), this->m_len); |
3717 | } | |
e015c2a3 | 3718 | |
c0c133e1 | 3719 | wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength); |
2523e9b7 | 3720 | }; |
e015c2a3 | 3721 | |
2523e9b7 | 3722 | #if wxUSE_STL_BASED_WXSTRING |
7c77f334 VZ |
3723 | |
3724 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> ) | |
3725 | ||
6798451b | 3726 | class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType> |
2523e9b7 VS |
3727 | { |
3728 | public: | |
6798451b | 3729 | wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024) |
2523e9b7 | 3730 | : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {} |
6798451b | 3731 | ~wxStringInternalBuffer() |
2523e9b7 | 3732 | { m_str.m_impl.assign(m_buf.data()); } |
e015c2a3 | 3733 | |
c0c133e1 | 3734 | wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer); |
1d218550 VZ |
3735 | }; |
3736 | ||
7c77f334 VZ |
3737 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( |
3738 | wxStringTypeBufferLengthBase<wxStringCharType> ) | |
3739 | ||
3740 | class wxStringInternalBufferLength | |
3741 | : public wxStringTypeBufferLengthBase<wxStringCharType> | |
941b78cc MB |
3742 | { |
3743 | public: | |
6798451b | 3744 | wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024) |
2523e9b7 | 3745 | : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {} |
941b78cc | 3746 | |
6798451b | 3747 | ~wxStringInternalBufferLength() |
941b78cc | 3748 | { |
2523e9b7 | 3749 | m_str.m_impl.assign(m_buf.data(), m_len); |
941b78cc MB |
3750 | } |
3751 | ||
c0c133e1 | 3752 | wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength); |
941b78cc | 3753 | }; |
062dc5fc | 3754 | |
2523e9b7 VS |
3755 | #endif // wxUSE_STL_BASED_WXSTRING |
3756 | ||
941b78cc | 3757 | |
2523e9b7 VS |
3758 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 |
3759 | typedef wxStringTypeBuffer<wxChar> wxStringBuffer; | |
3760 | typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength; | |
3761 | #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
6798451b VS |
3762 | typedef wxStringInternalBuffer wxStringBuffer; |
3763 | typedef wxStringInternalBufferLength wxStringBufferLength; | |
8f93a29f | 3764 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
941b78cc | 3765 | |
7d46f92b | 3766 | #if wxUSE_UNICODE_UTF8 |
628f87da VS |
3767 | typedef wxStringInternalBuffer wxUTF8StringBuffer; |
3768 | typedef wxStringInternalBufferLength wxUTF8StringBufferLength; | |
7d46f92b | 3769 | #elif wxUSE_UNICODE_WCHAR |
7c77f334 VZ |
3770 | |
3771 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> ) | |
3772 | ||
6b583d40 VZ |
3773 | // Note about inlined dtors in the classes below: this is done not for |
3774 | // performance reasons but just to avoid linking errors in the MSVC DLL build | |
3775 | // under Windows: if a class has non-inline methods it must be declared as | |
3776 | // being DLL-exported but, due to an extremely interesting feature of MSVC 7 | |
3777 | // and later, any template class which is used as a base of a DLL-exported | |
3778 | // class is implicitly made DLL-exported too, as explained at the bottom of | |
3779 | // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes, | |
3780 | // _inheriting_ from a class can change whether it is being exported from DLL) | |
3781 | // | |
3782 | // But this results in link errors because the base template class is not DLL- | |
3783 | // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it | |
3784 | // does have only inline functions. So the simplest fix is to just make all the | |
3785 | // functions of these classes inline too. | |
3786 | ||
3787 | class wxUTF8StringBuffer : public wxStringTypeBufferBase<char> | |
628f87da VS |
3788 | { |
3789 | public: | |
3790 | wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024) | |
3791 | : wxStringTypeBufferBase<char>(str, lenWanted) {} | |
6b583d40 VZ |
3792 | ~wxUTF8StringBuffer() |
3793 | { | |
3794 | wxMBConvStrictUTF8 conv; | |
3795 | size_t wlen = conv.ToWChar(NULL, 0, m_buf); | |
3796 | wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" ); | |
3797 | ||
3798 | wxStringInternalBuffer wbuf(m_str, wlen); | |
3799 | conv.ToWChar(wbuf, wlen, m_buf); | |
3800 | } | |
628f87da | 3801 | |
c0c133e1 | 3802 | wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer); |
628f87da VS |
3803 | }; |
3804 | ||
7c77f334 VZ |
3805 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> ) |
3806 | ||
6b583d40 | 3807 | class wxUTF8StringBufferLength : public wxStringTypeBufferLengthBase<char> |
628f87da VS |
3808 | { |
3809 | public: | |
3810 | wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024) | |
3811 | : wxStringTypeBufferLengthBase<char>(str, lenWanted) {} | |
6b583d40 VZ |
3812 | ~wxUTF8StringBufferLength() |
3813 | { | |
3814 | wxCHECK_RET(m_lenSet, "length not set"); | |
3815 | ||
3816 | wxMBConvStrictUTF8 conv; | |
3817 | size_t wlen = conv.ToWChar(NULL, 0, m_buf, m_len); | |
3818 | wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" ); | |
3819 | ||
3820 | wxStringInternalBufferLength wbuf(m_str, wlen); | |
3821 | conv.ToWChar(wbuf, wlen, m_buf, m_len); | |
3822 | wbuf.SetLength(wlen); | |
3823 | } | |
628f87da | 3824 | |
c0c133e1 | 3825 | wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength); |
628f87da | 3826 | }; |
7d46f92b | 3827 | #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR |
628f87da VS |
3828 | |
3829 | ||
c801d85f | 3830 | // --------------------------------------------------------------------------- |
3c67202d | 3831 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 3832 | // --------------------------------------------------------------------------- |
f33fee2a | 3833 | |
831faf97 | 3834 | #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p) |
fb52c2b6 VZ |
3835 | |
3836 | wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) | |
3837 | ||
3838 | #undef wxCMP_WXCHAR_STRING | |
3839 | ||
0cbe5ee3 | 3840 | inline bool operator==(const wxString& s1, const wxString& s2) |
2712e317 | 3841 | { return s1.IsSameAs(s2); } |
0cbe5ee3 | 3842 | inline bool operator!=(const wxString& s1, const wxString& s2) |
2712e317 | 3843 | { return !s1.IsSameAs(s2); } |
0cbe5ee3 VZ |
3844 | inline bool operator< (const wxString& s1, const wxString& s2) |
3845 | { return s1.Cmp(s2) < 0; } | |
0cbe5ee3 VZ |
3846 | inline bool operator> (const wxString& s1, const wxString& s2) |
3847 | { return s1.Cmp(s2) > 0; } | |
0cbe5ee3 VZ |
3848 | inline bool operator<=(const wxString& s1, const wxString& s2) |
3849 | { return s1.Cmp(s2) <= 0; } | |
0cbe5ee3 VZ |
3850 | inline bool operator>=(const wxString& s1, const wxString& s2) |
3851 | { return s1.Cmp(s2) >= 0; } | |
3c67202d | 3852 | |
5d0fac27 VS |
3853 | inline bool operator==(const wxString& s1, const wxCStrData& s2) |
3854 | { return s1 == s2.AsString(); } | |
3855 | inline bool operator==(const wxCStrData& s1, const wxString& s2) | |
3856 | { return s1.AsString() == s2; } | |
3857 | inline bool operator!=(const wxString& s1, const wxCStrData& s2) | |
3858 | { return s1 != s2.AsString(); } | |
3859 | inline bool operator!=(const wxCStrData& s1, const wxString& s2) | |
3860 | { return s1.AsString() != s2; } | |
3861 | ||
5ca07d0f | 3862 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) |
f33fee2a | 3863 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 3864 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 3865 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
3866 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
3867 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
3868 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
3869 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
be39d6f5 | 3870 | |
5ca07d0f | 3871 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 3872 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 3873 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 3874 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
3875 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
3876 | { return (s1.Cmp((const char *)s2) != 0); } | |
3877 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
3878 | { return (s2.Cmp((const char *)s1) != 0); } | |
5ca07d0f | 3879 | |
6d56eb5c | 3880 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 3881 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 3882 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 3883 | { return (const wchar_t *)buf + string; } |
be39d6f5 | 3884 | |
6d56eb5c | 3885 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 3886 | { return string + (const char *)buf; } |
6d56eb5c | 3887 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 3888 | { return (const char *)buf + string; } |
f04f3991 | 3889 | |
a962cdf4 | 3890 | // comparison with char |
c9f78968 VS |
3891 | inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } |
3892 | inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } | |
3893 | inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } | |
3894 | inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } | |
3895 | inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } | |
3896 | inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } | |
3897 | inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } | |
3898 | inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } | |
3899 | inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } | |
3900 | inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } | |
3901 | inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } | |
3902 | inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } | |
3903 | inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } | |
3904 | inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } | |
3905 | inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } | |
3906 | inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } | |
3907 | inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } | |
3908 | inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } | |
e51b17a1 | 3909 | |
d7330233 VS |
3910 | // comparison with C string in Unicode build |
3911 | #if wxUSE_UNICODE | |
fb52c2b6 VZ |
3912 | |
3913 | #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s | |
3914 | ||
3915 | wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) | |
3916 | ||
3917 | #undef wxCMP_CHAR_STRING | |
3918 | ||
d7330233 VS |
3919 | #endif // wxUSE_UNICODE |
3920 | ||
fb52c2b6 VZ |
3921 | // we also need to provide the operators for comparison with wxCStrData to |
3922 | // resolve ambiguity between operator(const wxChar *,const wxString &) and | |
3923 | // operator(const wxChar *, const wxChar *) for "p == s.c_str()" | |
3924 | // | |
3925 | // notice that these are (shallow) pointer comparisons, not (deep) string ones | |
3926 | #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() | |
3927 | #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() | |
3928 | ||
11aac4ba VS |
3929 | wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) |
3930 | wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) | |
fb52c2b6 VZ |
3931 | |
3932 | #undef wxCMP_CHAR_CSTRDATA | |
3933 | #undef wxCMP_WCHAR_CSTRDATA | |
3934 | ||
c801d85f | 3935 | // --------------------------------------------------------------------------- |
3c67202d | 3936 | // Implementation only from here until the end of file |
c801d85f KB |
3937 | // --------------------------------------------------------------------------- |
3938 | ||
e87b7833 | 3939 | #if wxUSE_STD_IOSTREAM |
c801d85f | 3940 | |
65f19af1 | 3941 | #include "wx/iosfwrap.h" |
c801d85f | 3942 | |
bddd7a8d | 3943 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); |
c9f78968 | 3944 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); |
04abe4bc VS |
3945 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&); |
3946 | #ifndef __BORLANDC__ | |
3947 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&); | |
3948 | #endif | |
c801d85f | 3949 | |
6a6ea041 | 3950 | #if wxUSE_UNICODE && defined(HAVE_WOSTREAM) |
6b61b594 VZ |
3951 | |
3952 | WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&); | |
3953 | WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&); | |
3954 | WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxWCharBuffer&); | |
3955 | ||
6a6ea041 | 3956 | #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM) |
6b61b594 | 3957 | |
7a906e1a | 3958 | #endif // wxUSE_STD_IOSTREAM |
c801d85f | 3959 | |
c9f78968 VS |
3960 | // --------------------------------------------------------------------------- |
3961 | // wxCStrData implementation | |
3962 | // --------------------------------------------------------------------------- | |
3963 | ||
3964 | inline wxCStrData::wxCStrData(char *buf) | |
3965 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
3966 | inline wxCStrData::wxCStrData(wchar_t *buf) | |
3967 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
3968 | ||
92a17abc VS |
3969 | inline wxCStrData::wxCStrData(const wxCStrData& data) |
3970 | : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str), | |
3971 | m_offset(data.m_offset), | |
3972 | m_owned(data.m_owned) | |
3973 | { | |
3974 | } | |
3975 | ||
c9f78968 VS |
3976 | inline wxCStrData::~wxCStrData() |
3977 | { | |
3978 | if ( m_owned ) | |
5c33522f | 3979 | delete const_cast<wxString*>(m_str); // cast to silence warnings |
c9f78968 VS |
3980 | } |
3981 | ||
11aac4ba VS |
3982 | // simple cases for AsChar() and AsWChar(), the complicated ones are |
3983 | // in string.cpp | |
3984 | #if wxUSE_UNICODE_WCHAR | |
c9f78968 | 3985 | inline const wchar_t* wxCStrData::AsWChar() const |
11aac4ba VS |
3986 | { |
3987 | return m_str->wx_str() + m_offset; | |
3988 | } | |
3989 | #endif // wxUSE_UNICODE_WCHAR | |
3990 | ||
bfb6847d | 3991 | #if !wxUSE_UNICODE |
c9f78968 | 3992 | inline const char* wxCStrData::AsChar() const |
c9f78968 | 3993 | { |
bfb6847d | 3994 | return m_str->wx_str() + m_offset; |
c9f78968 | 3995 | } |
11aac4ba | 3996 | #endif // !wxUSE_UNICODE |
c9f78968 | 3997 | |
bfb6847d VS |
3998 | #if wxUSE_UTF8_LOCALE_ONLY |
3999 | inline const char* wxCStrData::AsChar() const | |
4000 | { | |
4001 | return wxStringOperations::AddToIter(m_str->wx_str(), m_offset); | |
4002 | } | |
4003 | #endif // wxUSE_UTF8_LOCALE_ONLY | |
4004 | ||
681e4412 VS |
4005 | inline const wxCharBuffer wxCStrData::AsCharBuf() const |
4006 | { | |
4007 | #if !wxUSE_UNICODE | |
4008 | return wxCharBuffer::CreateNonOwned(AsChar()); | |
4009 | #else | |
4010 | return AsString().mb_str(); | |
4011 | #endif | |
4012 | } | |
4013 | ||
4014 | inline const wxWCharBuffer wxCStrData::AsWCharBuf() const | |
4015 | { | |
4016 | #if wxUSE_UNICODE_WCHAR | |
4017 | return wxWCharBuffer::CreateNonOwned(AsWChar()); | |
4018 | #else | |
4019 | return AsString().wc_str(); | |
4020 | #endif | |
4021 | } | |
4022 | ||
c9f78968 VS |
4023 | inline wxString wxCStrData::AsString() const |
4024 | { | |
4025 | if ( m_offset == 0 ) | |
4026 | return *m_str; | |
4027 | else | |
4028 | return m_str->Mid(m_offset); | |
4029 | } | |
4030 | ||
2523e9b7 VS |
4031 | inline const wxStringCharType *wxCStrData::AsInternal() const |
4032 | { | |
bfb6847d | 4033 | #if wxUSE_UNICODE_UTF8 |
2523e9b7 | 4034 | return wxStringOperations::AddToIter(m_str->wx_str(), m_offset); |
bfb6847d VS |
4035 | #else |
4036 | return m_str->wx_str() + m_offset; | |
4037 | #endif | |
2523e9b7 VS |
4038 | } |
4039 | ||
c9f78968 VS |
4040 | inline wxUniChar wxCStrData::operator*() const |
4041 | { | |
4042 | if ( m_str->empty() ) | |
4043 | return wxUniChar(_T('\0')); | |
4044 | else | |
4045 | return (*m_str)[m_offset]; | |
4046 | } | |
4047 | ||
4048 | inline wxUniChar wxCStrData::operator[](size_t n) const | |
4049 | { | |
378964d4 VS |
4050 | // NB: we intentionally use operator[] and not at() here because the former |
4051 | // works for the terminating NUL while the latter does not | |
4052 | return (*m_str)[m_offset + n]; | |
c9f78968 VS |
4053 | } |
4054 | ||
cc5bd48e VZ |
4055 | // ---------------------------------------------------------------------------- |
4056 | // more wxCStrData operators | |
4057 | // ---------------------------------------------------------------------------- | |
4058 | ||
4059 | // we need to define those to allow "size_t pos = p - s.c_str()" where p is | |
4060 | // some pointer into the string | |
4061 | inline size_t operator-(const char *p, const wxCStrData& cs) | |
4062 | { | |
4063 | return p - cs.AsChar(); | |
4064 | } | |
4065 | ||
4066 | inline size_t operator-(const wchar_t *p, const wxCStrData& cs) | |
4067 | { | |
4068 | return p - cs.AsWChar(); | |
4069 | } | |
4070 | ||
414b7b40 VZ |
4071 | // ---------------------------------------------------------------------------- |
4072 | // implementation of wx[W]CharBuffer inline methods using wxCStrData | |
4073 | // ---------------------------------------------------------------------------- | |
4074 | ||
11aac4ba VS |
4075 | // FIXME-UTF8: move this to buffer.h |
4076 | inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr) | |
681e4412 | 4077 | : wxCharTypeBufferBase(cstr.AsCharBuf()) |
11aac4ba VS |
4078 | { |
4079 | } | |
4080 | ||
4081 | inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr) | |
681e4412 | 4082 | : wxCharTypeBufferBase(cstr.AsWCharBuf()) |
414b7b40 VZ |
4083 | { |
4084 | } | |
4085 | ||
b0c4d5d7 VS |
4086 | #if wxUSE_UNICODE_UTF8 |
4087 | // ---------------------------------------------------------------------------- | |
4088 | // implementation of wxStringIteratorNode inline methods | |
4089 | // ---------------------------------------------------------------------------- | |
4090 | ||
39c20230 VS |
4091 | void wxStringIteratorNode::DoSet(const wxString *str, |
4092 | wxStringImpl::const_iterator *citer, | |
4093 | wxStringImpl::iterator *iter) | |
b0c4d5d7 | 4094 | { |
42fc0309 | 4095 | m_prev = NULL; |
39c20230 VS |
4096 | m_iter = iter; |
4097 | m_citer = citer; | |
4098 | m_str = str; | |
4099 | if ( str ) | |
4100 | { | |
4101 | m_next = str->m_iterators.ptr; | |
5c33522f | 4102 | const_cast<wxString*>(m_str)->m_iterators.ptr = this; |
39c20230 VS |
4103 | if ( m_next ) |
4104 | m_next->m_prev = this; | |
4105 | } | |
42fc0309 VS |
4106 | else |
4107 | { | |
4108 | m_next = NULL; | |
4109 | } | |
b0c4d5d7 VS |
4110 | } |
4111 | ||
39c20230 | 4112 | void wxStringIteratorNode::clear() |
b0c4d5d7 VS |
4113 | { |
4114 | if ( m_next ) | |
4115 | m_next->m_prev = m_prev; | |
4116 | if ( m_prev ) | |
4117 | m_prev->m_next = m_next; | |
39c20230 | 4118 | else if ( m_str ) // first in the list |
5c33522f | 4119 | const_cast<wxString*>(m_str)->m_iterators.ptr = m_next; |
39c20230 VS |
4120 | |
4121 | m_next = m_prev = NULL; | |
4122 | m_citer = NULL; | |
4123 | m_iter = NULL; | |
4124 | m_str = NULL; | |
b0c4d5d7 VS |
4125 | } |
4126 | #endif // wxUSE_UNICODE_UTF8 | |
4127 | ||
e3e8e3c0 | 4128 | #if WXWIN_COMPATIBILITY_2_8 |
3a3dde0d | 4129 | // lot of code out there doesn't explicitly include wx/crt.h, but uses |
e3e8e3c0 VS |
4130 | // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h, |
4131 | // so let's include this header now that wxString is defined and it's safe | |
4132 | // to do it: | |
3a3dde0d | 4133 | #include "wx/crt.h" |
e3e8e3c0 VS |
4134 | #endif |
4135 | ||
cbec0f40 FM |
4136 | // ---------------------------------------------------------------------------- |
4137 | // Checks on wxString characters | |
4138 | // ---------------------------------------------------------------------------- | |
4139 | ||
4140 | template<bool (T)(const wxUniChar& c)> | |
4141 | inline bool wxStringCheck(const wxString& val) | |
4142 | { | |
4143 | for ( wxString::const_iterator i = val.begin(); | |
4144 | i != val.end(); | |
4145 | ++i ) | |
4146 | if (T(*i) == 0) | |
4147 | return false; | |
4148 | return true; | |
4149 | } | |
4150 | ||
11aac4ba | 4151 | #endif // _WX_WXSTRING_H_ |