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