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