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