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