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