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