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