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