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