]>
Commit | Line | Data |
---|---|---|
3c67202d | 1 | /////////////////////////////////////////////////////////////////////////////// |
1c2d1459 | 2 | // Name: wx/string.h |
a7ea63e2 | 3 | // Purpose: wxString class |
c801d85f KB |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
3c67202d | 10 | /////////////////////////////////////////////////////////////////////////////// |
c801d85f | 11 | |
e90c1d2a VZ |
12 | /* |
13 | Efficient string class [more or less] compatible with MFC CString, | |
77ffb593 | 14 | wxWidgets version 1 wxString and std::string and some handy functions |
e90c1d2a VZ |
15 | missing from string.h. |
16 | */ | |
17 | ||
a7ea63e2 VS |
18 | #ifndef _WX_WXSTRING_H__ |
19 | #define _WX_WXSTRING_H__ | |
c801d85f | 20 | |
e90c1d2a VZ |
21 | // ---------------------------------------------------------------------------- |
22 | // headers | |
23 | // ---------------------------------------------------------------------------- | |
24 | ||
5737d05f VZ |
25 | #include "wx/defs.h" // everybody should include this |
26 | ||
9dea36ef | 27 | #if defined(__WXMAC__) || defined(__VISAGECPP__) |
3f4a0c5b | 28 | #include <ctype.h> |
17dff81c | 29 | #endif |
3f4a0c5b | 30 | |
9dea36ef DW |
31 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
32 | // problem in VACPP V4 with including stdlib.h multiple times | |
33 | // strconv includes it anyway | |
34 | # include <stdio.h> | |
35 | # include <string.h> | |
36 | # include <stdarg.h> | |
37 | # include <limits.h> | |
38 | #else | |
39 | # include <string.h> | |
40 | # include <stdio.h> | |
41 | # include <stdarg.h> | |
42 | # include <limits.h> | |
43 | # include <stdlib.h> | |
44 | #endif | |
c801d85f | 45 | |
1c2d1459 | 46 | #ifdef HAVE_STRCASECMP_IN_STRINGS_H |
1bfcb0b6 | 47 | #include <strings.h> // for strcasecmp() |
1c2d1459 | 48 | #endif // HAVE_STRCASECMP_IN_STRINGS_H |
1bfcb0b6 | 49 | |
4055ed82 | 50 | #ifdef __WXPALMOS__ |
ffecfa5a JS |
51 | #include <StringMgr.h> |
52 | #endif | |
53 | ||
e3f6cbd9 | 54 | #include "wx/wxchar.h" // for wxChar, wxStrlen() etc. |
c9f78968 | 55 | #include "wx/strvararg.h" |
e90c1d2a VZ |
56 | #include "wx/buffer.h" // for wxCharBuffer |
57 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
a7ea63e2 VS |
58 | #include "wx/stringimpl.h" |
59 | #include "wx/unichar.h" | |
3f4a0c5b | 60 | |
fb3c9042 VZ |
61 | class WXDLLIMPEXP_BASE wxString; |
62 | ||
c801d85f KB |
63 | // --------------------------------------------------------------------------- |
64 | // macros | |
65 | // --------------------------------------------------------------------------- | |
66 | ||
5737d05f VZ |
67 | // casts [unfortunately!] needed to call some broken functions which require |
68 | // "char *" instead of "const char *" | |
2bb67b80 | 69 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
e90c1d2a VZ |
70 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) |
71 | #define wxMBSTRINGCAST (char *)(const char *) | |
72 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 73 | |
e90c1d2a VZ |
74 | // ---------------------------------------------------------------------------- |
75 | // constants | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
13874b5c VZ |
78 | #if WXWIN_COMPATIBILITY_2_6 |
79 | ||
80 | // deprecated in favour of wxString::npos, don't use in new code | |
81 | // | |
e90c1d2a | 82 | // maximum possible length for a string means "take all string" everywhere |
8f93a29f | 83 | #define wxSTRING_MAXLEN wxString::npos |
66b6b045 | 84 | |
13874b5c VZ |
85 | #endif // WXWIN_COMPATIBILITY_2_6 |
86 | ||
c801d85f | 87 | // --------------------------------------------------------------------------- |
e90c1d2a | 88 | // global functions complementing standard C string library replacements for |
3c67202d VZ |
89 | // strlen() and portable strcasecmp() |
90 | //--------------------------------------------------------------------------- | |
e90c1d2a | 91 | |
c7554da8 | 92 | #if WXWIN_COMPATIBILITY_2_8 |
e3f6cbd9 | 93 | // Use wxXXX() functions from wxcrt.h instead! These functions are for |
e90c1d2a | 94 | // backwards compatibility only. |
88150e60 | 95 | |
3c67202d | 96 | // checks whether the passed in pointer is NULL and if the string is empty |
c7554da8 | 97 | wxDEPRECATED( inline bool IsEmpty(const char *p) ); |
6d56eb5c | 98 | inline bool IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 99 | |
3c67202d | 100 | // safe version of strlen() (returns 0 if passed NULL pointer) |
c7554da8 | 101 | wxDEPRECATED( inline size_t Strlen(const char *psz) ); |
6d56eb5c | 102 | inline size_t Strlen(const char *psz) |
c801d85f KB |
103 | { return psz ? strlen(psz) : 0; } |
104 | ||
3c67202d | 105 | // portable strcasecmp/_stricmp |
c7554da8 | 106 | wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) ); |
6d56eb5c | 107 | inline int Stricmp(const char *psz1, const char *psz2) |
dd1eaa89 | 108 | { |
7f0586ef JS |
109 | #if defined(__VISUALC__) && defined(__WXWINCE__) |
110 | register char c1, c2; | |
111 | do { | |
112 | c1 = tolower(*psz1++); | |
113 | c2 = tolower(*psz2++); | |
114 | } while ( c1 && (c1 == c2) ); | |
115 | ||
116 | return c1 - c2; | |
117 | #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) | |
dd1eaa89 | 118 | return _stricmp(psz1, psz2); |
91b8de8d | 119 | #elif defined(__SC__) |
2432b92d | 120 | return _stricmp(psz1, psz2); |
91b8de8d | 121 | #elif defined(__SALFORDC__) |
a3ef5bf5 | 122 | return stricmp(psz1, psz2); |
dd1eaa89 VZ |
123 | #elif defined(__BORLANDC__) |
124 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
125 | #elif defined(__WATCOMC__) |
126 | return stricmp(psz1, psz2); | |
14704334 VS |
127 | #elif defined(__DJGPP__) |
128 | return stricmp(psz1, psz2); | |
91b8de8d RR |
129 | #elif defined(__EMX__) |
130 | return stricmp(psz1, psz2); | |
e2c87f4c | 131 | #elif defined(__WXPM__) |
1777b9bb | 132 | return stricmp(psz1, psz2); |
a0be6908 WS |
133 | #elif defined(__WXPALMOS__) || \ |
134 | defined(HAVE_STRCASECMP_IN_STRING_H) || \ | |
1c2d1459 VZ |
135 | defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ |
136 | defined(__GNUWIN32__) | |
dd1eaa89 | 137 | return strcasecmp(psz1, psz2); |
8be97d65 | 138 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
139 | register char c1, c2; |
140 | do { | |
141 | c1 = tolower(*psz1++); | |
142 | c2 = tolower(*psz2++); | |
143 | } while ( c1 && (c1 == c2) ); | |
144 | ||
145 | return c1 - c2; | |
dd1eaa89 VZ |
146 | #else |
147 | // almost all compilers/libraries provide this function (unfortunately under | |
148 | // different names), that's why we don't implement our own which will surely | |
149 | // be more efficient than this code (uncomment to use): | |
150 | /* | |
151 | register char c1, c2; | |
152 | do { | |
153 | c1 = tolower(*psz1++); | |
154 | c2 = tolower(*psz2++); | |
155 | } while ( c1 && (c1 == c2) ); | |
156 | ||
157 | return c1 - c2; | |
158 | */ | |
159 | ||
160 | #error "Please define string case-insensitive compare for your OS/compiler" | |
161 | #endif // OS/compiler | |
162 | } | |
c801d85f | 163 | |
c7554da8 VS |
164 | #endif // WXWIN_COMPATIBILITY_2_8 |
165 | ||
c9f78968 VS |
166 | // ---------------------------------------------------------------------------- |
167 | // wxCStrData | |
168 | // ---------------------------------------------------------------------------- | |
169 | ||
170 | // Lightweight object returned by wxString::c_str() and implicitly convertible | |
171 | // to either const char* or const wchar_t*. | |
9aee0061 | 172 | class WXDLLIMPEXP_BASE wxCStrData |
c9f78968 VS |
173 | { |
174 | private: | |
175 | // Ctors; for internal use by wxString and wxCStrData only | |
176 | wxCStrData(const wxString *str, size_t offset = 0, bool owned = false) | |
177 | : m_str(str), m_offset(offset), m_owned(owned) {} | |
178 | ||
179 | public: | |
180 | // Ctor constructs the object from char literal; they are needed to make | |
181 | // operator?: compile and they intentionally take char*, not const char* | |
182 | wxCStrData(char *buf); | |
183 | wxCStrData(wchar_t *buf); | |
184 | ||
9aee0061 | 185 | inline ~wxCStrData(); |
c9f78968 | 186 | |
9aee0061 VZ |
187 | // methods defined inline below must be declared inline or mingw32 3.4.5 |
188 | // warns about "<symbol> defined locally after being referenced with | |
189 | // dllimport linkage" | |
190 | #if wxUSE_UNICODE_WCHAR | |
191 | inline | |
192 | #endif | |
c9f78968 VS |
193 | const wchar_t* AsWChar() const; |
194 | operator const wchar_t*() const { return AsWChar(); } | |
11aac4ba | 195 | |
9aee0061 | 196 | inline operator bool() const; |
11aac4ba | 197 | |
9aee0061 VZ |
198 | #if !wxUSE_UNICODE |
199 | inline | |
200 | #endif | |
c9f78968 | 201 | const char* AsChar() const; |
dbea442a VZ |
202 | const unsigned char* AsUnsignedChar() const |
203 | { return (const unsigned char *) AsChar(); } | |
c9f78968 | 204 | operator const char*() const { return AsChar(); } |
dbea442a | 205 | operator const unsigned char*() const { return AsUnsignedChar(); } |
11aac4ba VS |
206 | |
207 | operator const void*() const { return AsChar(); } | |
c9f78968 | 208 | |
9aee0061 | 209 | inline wxString AsString() const; |
c9f78968 VS |
210 | |
211 | // allow expressions like "c_str()[0]": | |
9aee0061 | 212 | inline wxUniChar operator[](size_t n) const; |
c9f78968 | 213 | wxUniChar operator[](int n) const { return operator[](size_t(n)); } |
c1df0a3b | 214 | wxUniChar operator[](long n) const { return operator[](size_t(n)); } |
c9f78968 VS |
215 | #ifndef wxSIZE_T_IS_UINT |
216 | wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); } | |
217 | #endif // size_t != unsigned int | |
218 | ||
acf0c9ac | 219 | // these operators are needed to emulate the pointer semantics of c_str(): |
c9f78968 VS |
220 | // expressions like "wxChar *p = str.c_str() + 1;" should continue to work |
221 | // (we need both versions to resolve ambiguities): | |
222 | wxCStrData operator+(int n) const | |
223 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
acf0c9ac VZ |
224 | wxCStrData operator+(long n) const |
225 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
c9f78968 VS |
226 | wxCStrData operator+(size_t n) const |
227 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
228 | ||
f4c90fdf VS |
229 | // and these for "str.c_str() + n - 2": |
230 | wxCStrData operator-(int n) const | |
231 | { | |
232 | wxASSERT_MSG( n <= (int)m_offset, | |
233 | _T("attempt to construct address before the beginning of the string") ); | |
234 | return wxCStrData(m_str, m_offset - n, m_owned); | |
235 | } | |
236 | wxCStrData operator-(long n) const | |
237 | { | |
238 | wxASSERT_MSG( n <= (int)m_offset, | |
239 | _T("attempt to construct address before the beginning of the string") ); | |
240 | return wxCStrData(m_str, m_offset - n, m_owned); | |
241 | } | |
242 | wxCStrData operator-(size_t n) const | |
243 | { | |
759d51f2 | 244 | wxASSERT_MSG( n <= m_offset, |
f4c90fdf VS |
245 | _T("attempt to construct address before the beginning of the string") ); |
246 | return wxCStrData(m_str, m_offset - n, m_owned); | |
247 | } | |
248 | ||
b77afb41 | 249 | // this operator is needed to make expressions like "*c_str()" or |
c9f78968 | 250 | // "*(c_str() + 2)" work |
9aee0061 | 251 | inline wxUniChar operator*() const; |
c9f78968 VS |
252 | |
253 | private: | |
254 | const wxString *m_str; | |
255 | size_t m_offset; | |
256 | bool m_owned; | |
257 | ||
258 | friend class WXDLLIMPEXP_BASE wxString; | |
259 | }; | |
260 | ||
261 | // ---------------------------------------------------------------------------- | |
262 | // wxStringPrintfMixin | |
263 | // --------------------------------------------------------------------------- | |
264 | ||
265 | // NB: VC6 has a bug that causes linker errors if you have template methods | |
266 | // in a class using __declspec(dllimport). The solution is to split such | |
267 | // class into two classes, one that contains the template methods and does | |
268 | // *not* use WXDLLIMPEXP_BASE and another class that contains the rest | |
269 | // (with DLL linkage). | |
270 | // | |
271 | // We only do this for VC6 here, because the code is less efficient | |
272 | // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler | |
273 | // cannot compile this code. | |
274 | ||
275 | #if defined(__VISUALC__) && __VISUALC__ < 1300 | |
276 | #define wxNEEDS_WXSTRING_PRINTF_MIXIN | |
277 | #endif | |
278 | ||
279 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
280 | // this class contains implementation of wxString's vararg methods, it's | |
281 | // exported from wxBase DLL | |
282 | class WXDLLIMPEXP_BASE wxStringPrintfMixinBase | |
283 | { | |
284 | protected: | |
285 | wxStringPrintfMixinBase() {} | |
286 | ||
287 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
288 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
289 | }; | |
290 | ||
291 | // this class contains template wrappers for wxString's vararg methods, it's | |
292 | // intentionally *not* exported from the DLL in order to fix the VC6 bug | |
293 | // described above | |
294 | class wxStringPrintfMixin : public wxStringPrintfMixinBase | |
295 | { | |
296 | private: | |
297 | // to further complicate things, we can't return wxString from | |
298 | // wxStringPrintfMixin::Format() because wxString is not yet declared at | |
299 | // this point; the solution is to use this fake type trait template - this | |
300 | // way the compiler won't know the return type until Format() is used | |
301 | // (this doesn't compile with Watcom, but VC6 compiles it just fine): | |
302 | template<typename T> struct StringReturnType | |
303 | { | |
304 | typedef wxString type; | |
305 | }; | |
306 | ||
307 | public: | |
308 | // these are duplicated wxString methods, they're also declared below | |
309 | // if !wxNEEDS_WXSTRING_PRINTF_MIXIN: | |
310 | ||
311 | // int Printf(const wxChar *pszFormat, ...); | |
312 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
313 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; | |
314 | WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type, | |
315 | Format, DoFormat) | |
316 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
317 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
318 | ||
319 | protected: | |
320 | wxStringPrintfMixin() : wxStringPrintfMixinBase() {} | |
321 | }; | |
322 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
323 | ||
324 | ||
b6339dde VZ |
325 | // ---------------------------------------------------------------------------- |
326 | // wxString: string class trying to be compatible with std::string, MFC | |
327 | // CString and wxWindows 1.x wxString all at once | |
e87b7833 MB |
328 | // --------------------------------------------------------------------------- |
329 | ||
c9f78968 VS |
330 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
331 | // "non dll-interface class 'wxStringPrintfMixin' used as base interface | |
332 | // for dll-interface class 'wxString'" -- this is OK in our case | |
333 | #pragma warning (disable:4275) | |
e87b7833 MB |
334 | #endif |
335 | ||
8f93a29f | 336 | class WXDLLIMPEXP_BASE wxString |
c9f78968 | 337 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
8f93a29f | 338 | : public wxStringPrintfMixin |
c9f78968 VS |
339 | #endif |
340 | { | |
e87b7833 MB |
341 | // NB: special care was taken in arranging the member functions in such order |
342 | // that all inline functions can be effectively inlined, verify that all | |
20aa779e | 343 | // performance critical functions are still inlined if you change order! |
8f93a29f | 344 | public: |
8f93a29f VS |
345 | // an 'invalid' value for string index, moved to this place due to a CW bug |
346 | static const size_t npos; | |
8f93a29f | 347 | |
e87b7833 MB |
348 | private: |
349 | // if we hadn't made these operators private, it would be possible to | |
350 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
351 | // converted to char in C and we do have operator=(char) | |
352 | // | |
353 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
354 | // to assign another numeric type to wxString will now result in | |
355 | // ambiguity between operator=(char) and operator=(int) | |
356 | wxString& operator=(int); | |
357 | ||
8f93a29f VS |
358 | // these methods are not implemented - there is _no_ conversion from int to |
359 | // string, you're doing something wrong if the compiler wants to call it! | |
360 | // | |
361 | // try `s << i' or `s.Printf("%d", i)' instead | |
362 | wxString(int); | |
363 | ||
364 | ||
365 | // buffer for holding temporary substring when using any of the methods | |
366 | // that take (char*,size_t) or (wchar_t*,size_t) arguments: | |
367 | // FIXME-UTF8: This will need changes when UTF8 build is introduced | |
368 | template<typename T> | |
369 | struct SubstrBufFromType | |
370 | { | |
371 | T data; | |
372 | size_t len; | |
373 | ||
374 | SubstrBufFromType() {} | |
375 | SubstrBufFromType(const T& data_, size_t len_) | |
376 | : data(data_), len(len_) {} | |
377 | }; | |
378 | ||
379 | #if wxUSE_UNICODE_UTF8 | |
380 | // FIXME-UTF8: this will have to use slightly different type | |
381 | #elif wxUSE_UNICODE_WCHAR | |
382 | typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC; | |
383 | typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB; | |
8f93a29f VS |
384 | #else |
385 | typedef SubstrBufFromType<const char*> SubstrBufFromMB; | |
386 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC; | |
8f93a29f VS |
387 | #endif |
388 | ||
389 | ||
390 | // Functions implementing primitive operations on string data; wxString | |
391 | // methods and iterators are implemented in terms of it. The differences | |
392 | // between UTF-8 and wchar_t* representations of the string are mostly | |
393 | // contained here. | |
394 | ||
395 | #if wxUSE_UNICODE | |
396 | // FIXME-UTF8: This will need changes when UTF8 build is introduced | |
397 | static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength, | |
a962cdf4 | 398 | const wxMBConv& conv); |
8f93a29f VS |
399 | #else |
400 | static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength, | |
a962cdf4 | 401 | const wxMBConv& conv); |
8f93a29f VS |
402 | #endif |
403 | ||
404 | #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE | |
a962cdf4 | 405 | // returns C string encoded as the implementation expects: |
8f93a29f | 406 | #if wxUSE_UNICODE |
a962cdf4 | 407 | static const wchar_t* ImplStr(const wchar_t* str) |
e8f59039 | 408 | { return str ? str : wxT(""); } |
a962cdf4 | 409 | static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) |
e8f59039 VS |
410 | { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); } |
411 | static wxWCharBuffer ImplStr(const char* str, | |
412 | const wxMBConv& conv = wxConvLibc) | |
413 | { return ConvertStr(str, npos, conv).data; } | |
414 | static SubstrBufFromMB ImplStr(const char* str, size_t n, | |
415 | const wxMBConv& conv = wxConvLibc) | |
416 | { return ConvertStr(str, n, conv); } | |
8f93a29f | 417 | #else |
e8f59039 VS |
418 | static const char* ImplStr(const char* str, |
419 | const wxMBConv& WXUNUSED(conv) = wxConvLibc) | |
420 | { return str ? str : ""; } | |
421 | static const SubstrBufFromMB ImplStr(const char* str, size_t n, | |
422 | const wxMBConv& WXUNUSED(conv) = wxConvLibc) | |
423 | { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); } | |
8f93a29f VS |
424 | static wxCharBuffer ImplStr(const wchar_t* str) |
425 | { return ConvertStr(str, npos, wxConvLibc).data; } | |
426 | static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
427 | { return ConvertStr(str, n, wxConvLibc); } | |
428 | #endif | |
429 | ||
430 | // moves the iterator to the next Unicode character | |
431 | static void IncIter(wxStringImpl::iterator& i) { ++i; } | |
432 | static void IncIter(wxStringImpl::const_iterator& i) { ++i; } | |
433 | // moves the iterator to the previous Unicode character | |
434 | static void DecIter(wxStringImpl::iterator& i) { --i; } | |
435 | static void DecIter(wxStringImpl::const_iterator& i) { --i; } | |
436 | // moves the iterator by n Unicode characters | |
437 | static wxStringImpl::iterator AddToIter(wxStringImpl::iterator i, int n) | |
438 | { return i + n; } | |
439 | static wxStringImpl::const_iterator AddToIter(wxStringImpl::const_iterator i, int n) | |
440 | { return i + n; } | |
441 | // returns distance of the two iterators in Unicode characters | |
442 | static int DiffIters(wxStringImpl::iterator i1, wxStringImpl::iterator i2) | |
443 | { return i1 - i2; } | |
444 | static int DiffIters(wxStringImpl::const_iterator i1, wxStringImpl::const_iterator i2) | |
445 | { return i1 - i2; } | |
446 | ||
447 | // encodes the character to a form used to represent it in internal | |
448 | // representation (returns a string in UTF8 version) | |
449 | static wxChar EncodeChar(wxUniChar ch) { return (wxChar)ch; } | |
450 | ||
451 | // translates position index in wxString to/from index in underlying | |
452 | // wxStringImpl: | |
453 | static size_t PosToImpl(size_t pos) { return pos; } | |
454 | static void PosLenToImpl(size_t pos, size_t len, | |
455 | size_t *implPos, size_t *implLen) | |
456 | { *implPos = pos; *implLen = len; } | |
11aac4ba | 457 | static size_t LenToImpl(size_t len) { return len; } |
8f93a29f VS |
458 | static size_t PosFromImpl(size_t pos) { return pos; } |
459 | ||
460 | #else // wxUSE_UNICODE_UTF8 | |
461 | ||
462 | typedef char Utf8CharBuffer[5]; | |
463 | static Utf8CharBuffer EncodeChar(wxUniChar ch); | |
464 | // returns n copies of ch encoded in UTF-8 string | |
465 | static wxCharBuffer EncodeNChars(size_t n, wxUniChar ch); | |
466 | ||
467 | size_t PosToImpl(size_t pos) const | |
468 | { | |
469 | if ( pos == 0 || pos == npos ) | |
470 | return pos; | |
471 | else | |
472 | return wxStringImpl::const_iterator(begin() + pos) - m_impl.begin(); | |
473 | } | |
474 | ||
475 | size_t PosFromImpl(size_t pos) const | |
476 | { | |
477 | if ( pos == 0 || pos == npos ) | |
478 | return pos; | |
479 | else | |
480 | return const_iterator(m_impl.begin() + pos) - begin(); | |
481 | } | |
482 | ||
483 | // FIXME: return as-is without copying under UTF8 locale, return | |
484 | // converted string under other locales - needs wxCharBuffer | |
485 | // changes | |
486 | static wxCharBuffer ImplStr(const char* str); | |
487 | ||
488 | static wxCharBuffer ImplStr(const wchar_t* str) | |
489 | { return wxConvUTF8.cWC2MB(str); } | |
490 | #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8 | |
491 | ||
492 | ||
493 | public: | |
494 | // constructors and destructor | |
495 | // ctor for an empty string | |
496 | wxString() {} | |
04abe4bc | 497 | |
8f93a29f VS |
498 | // copy ctor |
499 | wxString(const wxStringImpl& stringSrc) : m_impl(stringSrc) { } | |
04abe4bc VS |
500 | wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { } |
501 | ||
8f93a29f VS |
502 | // string containing nRepeat copies of ch |
503 | wxString(wxUniChar ch, size_t nRepeat = 1) | |
04abe4bc | 504 | { assign(nRepeat, ch); } |
8f93a29f | 505 | wxString(size_t nRepeat, wxUniChar ch) |
04abe4bc | 506 | { assign(nRepeat, ch); } |
8f93a29f | 507 | wxString(wxUniCharRef ch, size_t nRepeat = 1) |
04abe4bc | 508 | { assign(nRepeat, ch); } |
8f93a29f | 509 | wxString(size_t nRepeat, wxUniCharRef ch) |
04abe4bc | 510 | { assign(nRepeat, ch); } |
8f93a29f | 511 | wxString(char ch, size_t nRepeat = 1) |
04abe4bc | 512 | { assign(nRepeat, ch); } |
8f93a29f | 513 | wxString(size_t nRepeat, char ch) |
04abe4bc | 514 | { assign(nRepeat, ch); } |
8f93a29f | 515 | wxString(wchar_t ch, size_t nRepeat = 1) |
04abe4bc | 516 | { assign(nRepeat, ch); } |
8f93a29f | 517 | wxString(size_t nRepeat, wchar_t ch) |
04abe4bc VS |
518 | { assign(nRepeat, ch); } |
519 | ||
520 | // ctors from char* strings: | |
521 | wxString(const char *psz) | |
522 | : m_impl(ImplStr(psz)) {} | |
523 | wxString(const char *psz, const wxMBConv& conv) | |
524 | : m_impl(ImplStr(psz, conv)) {} | |
525 | wxString(const char *psz, size_t nLength) | |
526 | { assign(psz, nLength); } | |
527 | wxString(const char *psz, const wxMBConv& conv, size_t nLength) | |
528 | { | |
529 | SubstrBufFromMB str(ImplStr(psz, nLength, conv)); | |
530 | m_impl.assign(str.data, str.len); | |
531 | } | |
8f93a29f | 532 | |
04abe4bc VS |
533 | // and unsigned char*: |
534 | wxString(const unsigned char *psz) | |
535 | : m_impl(ImplStr((const char*)psz)) {} | |
536 | wxString(const unsigned char *psz, const wxMBConv& conv) | |
537 | : m_impl(ImplStr((const char*)psz, conv)) {} | |
538 | wxString(const unsigned char *psz, size_t nLength) | |
539 | { assign((const char*)psz, nLength); } | |
540 | wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength) | |
541 | { | |
542 | SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv)); | |
543 | m_impl.assign(str.data, str.len); | |
544 | } | |
8f93a29f | 545 | |
04abe4bc VS |
546 | // ctors from wchar_t* strings: |
547 | wxString(const wchar_t *pwz) | |
548 | : m_impl(ImplStr(pwz)) {} | |
549 | wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv)) | |
550 | : m_impl(ImplStr(pwz)) {} | |
551 | wxString(const wchar_t *pwz, size_t nLength) | |
552 | { assign(pwz, nLength); } | |
553 | wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength) | |
554 | { assign(pwz, nLength); } | |
555 | ||
556 | wxString(const wxCharBuffer& buf) | |
557 | { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length | |
558 | wxString(const wxWCharBuffer& buf) | |
559 | { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length | |
8f93a29f | 560 | |
a962cdf4 VS |
561 | wxString(const wxCStrData& cstr) |
562 | : m_impl(cstr.AsString().m_impl) { } | |
563 | ||
8f93a29f VS |
564 | // as we provide both ctors with this signature for both char and unsigned |
565 | // char string, we need to provide one for wxCStrData to resolve ambiguity | |
566 | wxString(const wxCStrData& cstr, size_t nLength) | |
11aac4ba | 567 | : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {} |
8f93a29f VS |
568 | |
569 | // and because wxString is convertible to wxCStrData and const wxChar * | |
570 | // we also need to provide this one | |
571 | wxString(const wxString& str, size_t nLength) | |
11aac4ba VS |
572 | : m_impl(str.Mid(0, nLength).m_impl) {} |
573 | ||
8f93a29f VS |
574 | public: |
575 | // standard types | |
576 | typedef wxUniChar value_type; | |
577 | typedef wxUniChar char_type; | |
578 | typedef wxUniCharRef reference; | |
579 | typedef wxChar* pointer; | |
580 | typedef const wxChar* const_pointer; | |
581 | ||
582 | typedef size_t size_type; | |
583 | typedef wxUniChar const_reference; | |
584 | ||
a962cdf4 VS |
585 | #if wxUSE_STL |
586 | #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag | |
587 | #else | |
588 | #define WX_STR_ITERATOR_TAG void /* dummy type */ | |
589 | #endif | |
590 | ||
8f93a29f VS |
591 | #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \ |
592 | reference_type, reference_ctor) \ | |
593 | private: \ | |
594 | typedef wxStringImpl::iterator_name underlying_iterator; \ | |
595 | public: \ | |
a962cdf4 | 596 | typedef WX_STR_ITERATOR_TAG iterator_category; \ |
8f93a29f | 597 | typedef wxUniChar value_type; \ |
a962cdf4 | 598 | typedef int difference_type; \ |
8f93a29f VS |
599 | typedef reference_type reference; \ |
600 | typedef pointer_type pointer; \ | |
601 | \ | |
602 | iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \ | |
603 | \ | |
604 | reference operator*() const { return reference_ctor; } \ | |
a962cdf4 | 605 | reference operator[](size_t n) const { return *(*this + n); } \ |
8f93a29f VS |
606 | \ |
607 | iterator_name& operator++() \ | |
608 | { wxString::IncIter(m_cur); return *this; } \ | |
609 | iterator_name& operator--() \ | |
610 | { wxString::DecIter(m_cur); return *this; } \ | |
611 | iterator_name operator++(int) \ | |
612 | { \ | |
613 | iterator_name tmp = *this; \ | |
614 | wxString::IncIter(m_cur); \ | |
615 | return tmp; \ | |
616 | } \ | |
617 | iterator_name operator--(int) \ | |
618 | { \ | |
619 | iterator_name tmp = *this; \ | |
620 | wxString::DecIter(m_cur); \ | |
621 | return tmp; \ | |
622 | } \ | |
623 | \ | |
624 | iterator_name operator+(int n) const \ | |
625 | { return iterator_name(wxString::AddToIter(m_cur, n)); } \ | |
626 | iterator_name operator+(size_t n) const \ | |
627 | { return iterator_name(wxString::AddToIter(m_cur, (int)n)); } \ | |
628 | iterator_name operator-(int n) const \ | |
629 | { return iterator_name(wxString::AddToIter(m_cur, -n)); } \ | |
630 | iterator_name operator-(size_t n) const \ | |
631 | { return iterator_name(wxString::AddToIter(m_cur, -(int)n)); } \ | |
30d560f4 | 632 | iterator_name& operator+=(int n) \ |
8f93a29f | 633 | { m_cur = wxString::AddToIter(m_cur, n); return *this; } \ |
30d560f4 | 634 | iterator_name& operator+=(size_t n) \ |
8f93a29f | 635 | { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \ |
30d560f4 | 636 | iterator_name& operator-=(int n) \ |
8f93a29f | 637 | { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \ |
30d560f4 | 638 | iterator_name& operator-=(size_t n) \ |
8f93a29f VS |
639 | { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \ |
640 | \ | |
89360a8c | 641 | difference_type operator-(const iterator_name& i) const \ |
8f93a29f VS |
642 | { return wxString::DiffIters(m_cur, i.m_cur); } \ |
643 | \ | |
f2a1b1bd | 644 | bool operator==(const iterator_name& i) const \ |
8f93a29f VS |
645 | { return m_cur == i.m_cur; } \ |
646 | bool operator!=(const iterator_name& i) const \ | |
647 | { return m_cur != i.m_cur; } \ | |
648 | \ | |
649 | bool operator<(const iterator_name& i) const \ | |
650 | { return m_cur < i.m_cur; } \ | |
651 | bool operator>(const iterator_name& i) const \ | |
652 | { return m_cur > i.m_cur; } \ | |
653 | bool operator<=(const iterator_name& i) const \ | |
654 | { return m_cur <= i.m_cur; } \ | |
655 | bool operator>=(const iterator_name& i) const \ | |
656 | { return m_cur >= i.m_cur; } \ | |
657 | \ | |
658 | private: \ | |
659 | /* for internal wxString use only: */ \ | |
660 | iterator_name(underlying_iterator ptr) : m_cur(ptr) {} \ | |
661 | operator underlying_iterator() const { return m_cur; } \ | |
662 | \ | |
663 | friend class WXDLLIMPEXP_BASE wxString; \ | |
8f93a29f VS |
664 | friend class WXDLLIMPEXP_BASE wxCStrData; \ |
665 | \ | |
666 | private: \ | |
89360a8c | 667 | underlying_iterator m_cur |
8f93a29f VS |
668 | |
669 | class const_iterator; | |
670 | ||
671 | class iterator | |
672 | { | |
673 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, | |
89360a8c | 674 | wxUniCharRef::CreateForString(m_cur)); |
8f93a29f VS |
675 | |
676 | friend class const_iterator; | |
677 | }; | |
678 | ||
679 | class const_iterator | |
680 | { | |
681 | // NB: reference_type is intentionally value, not reference, the character | |
682 | // may be encoded differently in wxString data: | |
683 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar, | |
89360a8c | 684 | wxUniChar(*m_cur)); |
8f93a29f VS |
685 | |
686 | public: | |
687 | const_iterator(const iterator& i) : m_cur(i.m_cur) {} | |
688 | }; | |
689 | ||
a962cdf4 | 690 | #undef WX_STR_ITERATOR_TAG |
8f93a29f VS |
691 | #undef WX_STR_ITERATOR_IMPL |
692 | ||
693 | friend class iterator; | |
694 | friend class const_iterator; | |
695 | ||
696 | template <typename T> | |
697 | class reverse_iterator_impl | |
698 | { | |
699 | public: | |
700 | typedef T iterator_type; | |
a962cdf4 VS |
701 | |
702 | typedef typename T::iterator_category iterator_category; | |
8f93a29f | 703 | typedef typename T::value_type value_type; |
a962cdf4 | 704 | typedef typename T::difference_type difference_type; |
8f93a29f VS |
705 | typedef typename T::reference reference; |
706 | typedef typename T::pointer *pointer; | |
707 | ||
708 | reverse_iterator_impl(iterator_type i) : m_cur(i) {} | |
709 | reverse_iterator_impl(const reverse_iterator_impl& ri) | |
710 | : m_cur(ri.m_cur) {} | |
711 | ||
712 | iterator_type base() const { return m_cur; } | |
713 | ||
714 | reference operator*() const { return *(m_cur-1); } | |
a962cdf4 | 715 | reference operator[](size_t n) const { return *(*this + n); } |
8f93a29f VS |
716 | |
717 | reverse_iterator_impl& operator++() | |
718 | { --m_cur; return *this; } | |
719 | reverse_iterator_impl operator++(int) | |
720 | { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } | |
721 | reverse_iterator_impl& operator--() | |
722 | { ++m_cur; return *this; } | |
723 | reverse_iterator_impl operator--(int) | |
724 | { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } | |
725 | ||
50eeb96f | 726 | // NB: explicit <T> in the functions below is to keep BCC 5.5 happy |
8f93a29f | 727 | reverse_iterator_impl operator+(int n) const |
50eeb96f | 728 | { return reverse_iterator_impl<T>(m_cur - n); } |
8f93a29f | 729 | reverse_iterator_impl operator+(size_t n) const |
50eeb96f | 730 | { return reverse_iterator_impl<T>(m_cur - n); } |
8f93a29f | 731 | reverse_iterator_impl operator-(int n) const |
50eeb96f | 732 | { return reverse_iterator_impl<T>(m_cur + n); } |
8f93a29f | 733 | reverse_iterator_impl operator-(size_t n) const |
50eeb96f | 734 | { return reverse_iterator_impl<T>(m_cur + n); } |
8f93a29f VS |
735 | reverse_iterator_impl operator+=(int n) |
736 | { m_cur -= n; return *this; } | |
737 | reverse_iterator_impl operator+=(size_t n) | |
738 | { m_cur -= n; return *this; } | |
739 | reverse_iterator_impl operator-=(int n) | |
740 | { m_cur += n; return *this; } | |
741 | reverse_iterator_impl operator-=(size_t n) | |
742 | { m_cur += n; return *this; } | |
743 | ||
744 | unsigned operator-(const reverse_iterator_impl& i) const | |
745 | { return i.m_cur - m_cur; } | |
746 | ||
747 | bool operator==(const reverse_iterator_impl& ri) const | |
748 | { return m_cur == ri.m_cur; } | |
749 | bool operator!=(const reverse_iterator_impl& ri) const | |
750 | { return !(*this == ri); } | |
751 | ||
752 | bool operator<(const reverse_iterator_impl& i) const | |
753 | { return m_cur > i.m_cur; } | |
754 | bool operator>(const reverse_iterator_impl& i) const | |
755 | { return m_cur < i.m_cur; } | |
756 | bool operator<=(const reverse_iterator_impl& i) const | |
757 | { return m_cur >= i.m_cur; } | |
758 | bool operator>=(const reverse_iterator_impl& i) const | |
759 | { return m_cur <= i.m_cur; } | |
760 | ||
761 | private: | |
762 | iterator_type m_cur; | |
763 | }; | |
e87b7833 | 764 | |
8f93a29f VS |
765 | typedef reverse_iterator_impl<iterator> reverse_iterator; |
766 | typedef reverse_iterator_impl<const_iterator> const_reverse_iterator; | |
e90c1d2a | 767 | |
8f93a29f VS |
768 | // first valid index position |
769 | const_iterator begin() const { return const_iterator(m_impl.begin()); } | |
770 | iterator begin() { return iterator(m_impl.begin()); } | |
771 | // position one after the last valid one | |
772 | const_iterator end() const { return const_iterator(m_impl.end()); } | |
773 | iterator end() { return iterator(m_impl.end()); } | |
774 | ||
775 | // first element of the reversed string | |
776 | const_reverse_iterator rbegin() const | |
777 | { return const_reverse_iterator(end()); } | |
778 | reverse_iterator rbegin() | |
779 | { return reverse_iterator(end()); } | |
780 | // one beyond the end of the reversed string | |
781 | const_reverse_iterator rend() const | |
782 | { return const_reverse_iterator(begin()); } | |
783 | reverse_iterator rend() | |
784 | { return reverse_iterator(begin()); } | |
785 | ||
786 | // std::string methods: | |
787 | #if wxUSE_UNICODE_UTF8 | |
788 | size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize! | |
789 | #else | |
790 | size_t length() const { return m_impl.length(); } | |
791 | #endif | |
ce76f779 | 792 | |
8f93a29f VS |
793 | size_type size() const { return length(); } |
794 | size_type max_size() const { return npos; } | |
e90c1d2a | 795 | |
8f93a29f | 796 | bool empty() const { return m_impl.empty(); } |
b77afb41 | 797 | |
8f93a29f VS |
798 | size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8 |
799 | void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8 | |
b77afb41 | 800 | |
8f93a29f VS |
801 | void resize(size_t nSize, wxUniChar ch = wxT('\0')) |
802 | { | |
803 | #if wxUSE_UNICODE_UTF8 | |
804 | if ( !ch.IsAscii() ) | |
805 | { | |
806 | size_t len = length(); | |
807 | if ( nSize == len) | |
808 | return; | |
809 | else if ( nSize < len ) | |
810 | erase(nSize); | |
811 | else | |
812 | append(nSize - len, ch); | |
813 | } | |
814 | else | |
815 | #endif | |
816 | m_impl.resize(nSize, (wxStringCharType)ch); | |
817 | } | |
e90c1d2a | 818 | |
8f93a29f VS |
819 | wxString substr(size_t nStart = 0, size_t nLen = npos) const |
820 | { | |
821 | size_t pos, len; | |
822 | PosLenToImpl(nStart, nLen, &pos, &len); | |
823 | return m_impl.substr(pos, len); | |
824 | } | |
e90c1d2a | 825 | |
3c67202d VZ |
826 | // generic attributes & operations |
827 | // as standard strlen() | |
e87b7833 | 828 | size_t Len() const { return length(); } |
3c67202d | 829 | // string contains any characters? |
e87b7833 | 830 | bool IsEmpty() const { return empty(); } |
d775fa82 | 831 | // empty string is "false", so !str will return true |
20aa779e | 832 | bool operator!() const { return empty(); } |
735d1db6 VZ |
833 | // truncate the string to given length |
834 | wxString& Truncate(size_t uiLen); | |
3c67202d | 835 | // empty string contents |
dd1eaa89 VZ |
836 | void Empty() |
837 | { | |
735d1db6 | 838 | Truncate(0); |
dd1eaa89 | 839 | |
5a8231ef | 840 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); |
7be07660 | 841 | } |
3c67202d | 842 | // empty the string and free memory |
7be07660 VZ |
843 | void Clear() |
844 | { | |
e87b7833 MB |
845 | wxString tmp(wxEmptyString); |
846 | swap(tmp); | |
dd1eaa89 VZ |
847 | } |
848 | ||
3c67202d VZ |
849 | // contents test |
850 | // Is an ascii value | |
c801d85f | 851 | bool IsAscii() const; |
3c67202d | 852 | // Is a number |
c801d85f | 853 | bool IsNumber() const; |
3c67202d | 854 | // Is a word |
c801d85f | 855 | bool IsWord() const; |
c801d85f | 856 | |
3c67202d VZ |
857 | // data access (all indexes are 0 based) |
858 | // read access | |
8f93a29f VS |
859 | wxUniChar at(size_t n) const |
860 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
c9f78968 | 861 | wxUniChar GetChar(size_t n) const |
9d9ad673 | 862 | { return at(n); } |
3c67202d | 863 | // read/write access |
8f93a29f VS |
864 | wxUniCharRef at(size_t n) |
865 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
c9f78968 | 866 | wxUniCharRef GetWritableChar(size_t n) |
9d9ad673 | 867 | { return at(n); } |
3c67202d | 868 | // write access |
c9f78968 | 869 | void SetChar(size_t n, wxUniChar ch) |
9d9ad673 | 870 | { at(n) = ch; } |
c801d85f | 871 | |
3c67202d | 872 | // get last character |
8f93a29f | 873 | wxUniChar Last() const |
09443a26 | 874 | { |
5a8231ef | 875 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
09443a26 | 876 | |
9d9ad673 | 877 | return at(length() - 1); |
09443a26 VZ |
878 | } |
879 | ||
3c67202d | 880 | // get writable last character |
c9f78968 | 881 | wxUniCharRef Last() |
09443a26 | 882 | { |
5a8231ef | 883 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
9d9ad673 | 884 | return at(length() - 1); |
09443a26 | 885 | } |
c801d85f | 886 | |
d836ee96 | 887 | /* |
9d9ad673 | 888 | Note that we we must define all of the overloads below to avoid |
c9f78968 | 889 | ambiguity when using str[0]. |
d836ee96 | 890 | */ |
c9f78968 | 891 | wxUniChar operator[](int n) const |
8f93a29f | 892 | { return at(n); } |
c1df0a3b | 893 | wxUniChar operator[](long n) const |
8f93a29f | 894 | { return at(n); } |
c9f78968 | 895 | wxUniChar operator[](size_t n) const |
8f93a29f | 896 | { return at(n); } |
01398433 | 897 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 898 | wxUniChar operator[](unsigned int n) const |
8f93a29f | 899 | { return at(n); } |
01398433 | 900 | #endif // size_t != unsigned int |
01398433 | 901 | |
9d9ad673 | 902 | // operator versions of GetWriteableChar() |
c9f78968 | 903 | wxUniCharRef operator[](int n) |
8f93a29f | 904 | { return at(n); } |
c1df0a3b | 905 | wxUniCharRef operator[](long n) |
8f93a29f | 906 | { return at(n); } |
c9f78968 | 907 | wxUniCharRef operator[](size_t n) |
8f93a29f | 908 | { return at(n); } |
d836ee96 | 909 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 910 | wxUniCharRef operator[](unsigned int n) |
8f93a29f | 911 | { return at(n); } |
d836ee96 | 912 | #endif // size_t != unsigned int |
c801d85f | 913 | |
c9f78968 VS |
914 | // explicit conversion to C string (use this with printf()!) |
915 | wxCStrData c_str() const { return wxCStrData(this); } | |
8f93a29f | 916 | wxCStrData data() const { return c_str(); } |
c9f78968 | 917 | |
3c67202d | 918 | // implicit conversion to C string |
c9f78968 | 919 | operator wxCStrData() const { return c_str(); } |
11aac4ba VS |
920 | operator const char*() const { return c_str(); } |
921 | operator const wchar_t*() const { return c_str(); } | |
e015c2a3 | 922 | |
e015c2a3 | 923 | // identical to c_str(), for MFC compatibility |
c9f78968 VS |
924 | const wxCStrData GetData() const { return c_str(); } |
925 | ||
926 | // explicit conversion to C string in internal representation (char*, | |
927 | // wchar_t*, UTF-8-encoded char*, depending on the build): | |
8f93a29f | 928 | const_pointer wx_str() const { return m_impl.c_str(); } |
e90c1d2a | 929 | |
ef0f1387 VS |
930 | // conversion to *non-const* multibyte or widestring buffer; modifying |
931 | // returned buffer won't affect the string, these methods are only useful | |
932 | // for passing values to const-incorrect functions | |
933 | wxWritableCharBuffer char_str() const { return mb_str(); } | |
934 | wxWritableWCharBuffer wchar_str() const { return wc_str(); } | |
935 | ||
e015c2a3 VZ |
936 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
937 | // converting numbers or strings which are certain not to contain special | |
938 | // chars (typically system functions, X atoms, environment variables etc.) | |
939 | // | |
940 | // the behaviour of these functions with the strings containing anything | |
941 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 942 | #if wxUSE_UNICODE |
2b5f62a0 VZ |
943 | static wxString FromAscii(const char *ascii); // string |
944 | static wxString FromAscii(const char ascii); // char | |
b1ac3b56 | 945 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
946 | #else // ANSI |
947 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
2b5f62a0 | 948 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
949 | const char *ToAscii() const { return c_str(); } |
950 | #endif // Unicode/!Unicode | |
b1ac3b56 | 951 | |
2b5f62a0 | 952 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 953 | // buffer with temporary data |
f6bcfd97 BP |
954 | // |
955 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
956 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
957 | // fn_str() to return a string which should be used with the OS APIs | |
958 | // accepting the file names. The return value is always the same, but the | |
959 | // type differs because a function may either return pointer to the buffer | |
960 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 961 | #if wxUSE_UNICODE |
830f8f11 | 962 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; |
f6bcfd97 | 963 | |
e90c1d2a VZ |
964 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
965 | ||
e87b7833 | 966 | const wxChar* wc_str() const { return c_str(); } |
f6bcfd97 BP |
967 | |
968 | // for compatibility with !wxUSE_UNICODE version | |
830f8f11 | 969 | const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
e90c1d2a | 970 | |
2bb67b80 | 971 | #if wxMBFILES |
5f709e67 | 972 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 973 | #else // !wxMBFILES |
e87b7833 | 974 | const wxChar* fn_str() const { return c_str(); } |
e90c1d2a VZ |
975 | #endif // wxMBFILES/!wxMBFILES |
976 | #else // ANSI | |
e87b7833 | 977 | const wxChar* mb_str() const { return c_str(); } |
f6bcfd97 BP |
978 | |
979 | // for compatibility with wxUSE_UNICODE version | |
830f8f11 | 980 | const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
f6bcfd97 | 981 | |
e90c1d2a | 982 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 983 | |
6f841509 | 984 | #if wxUSE_WCHAR_T |
ef0f1387 | 985 | const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const; |
e90c1d2a | 986 | #endif // wxUSE_WCHAR_T |
d5c8817c SC |
987 | #ifdef __WXOSX__ |
988 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
989 | #else | |
e87b7833 | 990 | const wxChar* fn_str() const { return c_str(); } |
d5c8817c | 991 | #endif |
e90c1d2a | 992 | #endif // Unicode/ANSI |
c801d85f | 993 | |
3c67202d VZ |
994 | // overloaded assignment |
995 | // from another wxString | |
04abe4bc VS |
996 | wxString& operator=(const wxString& stringSrc) |
997 | { m_impl = stringSrc.m_impl; return *this; } | |
8f93a29f VS |
998 | wxString& operator=(const wxCStrData& cstr) |
999 | { return *this = cstr.AsString(); } | |
3c67202d | 1000 | // from a character |
c9f78968 | 1001 | wxString& operator=(wxUniChar ch) |
8f93a29f | 1002 | { m_impl = EncodeChar(ch); return *this; } |
c9f78968 | 1003 | wxString& operator=(wxUniCharRef ch) |
8f93a29f | 1004 | { return operator=((wxUniChar)ch); } |
c9f78968 | 1005 | wxString& operator=(char ch) |
8f93a29f | 1006 | { return operator=(wxUniChar(ch)); } |
50e02008 VS |
1007 | wxString& operator=(unsigned char ch) |
1008 | { return operator=(wxUniChar(ch)); } | |
c9f78968 | 1009 | wxString& operator=(wchar_t ch) |
8f93a29f | 1010 | { return operator=(wxUniChar(ch)); } |
c57e3bd5 RN |
1011 | // from a C string - STL probably will crash on NULL, |
1012 | // so we need to compensate in that case | |
992527a5 | 1013 | #if wxUSE_STL_BASED_WXSTRING |
04abe4bc VS |
1014 | wxString& operator=(const char *psz) |
1015 | { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; } | |
1016 | wxString& operator=(const wchar_t *pwz) | |
1017 | { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; } | |
c57e3bd5 | 1018 | #else |
04abe4bc VS |
1019 | wxString& operator=(const char *psz) |
1020 | { m_impl = ImplStr(psz); return *this; } | |
1021 | wxString& operator=(const wchar_t *pwz) | |
1022 | { m_impl = ImplStr(pwz); return *this; } | |
c57e3bd5 | 1023 | #endif |
04abe4bc VS |
1024 | wxString& operator=(const unsigned char *psz) |
1025 | { return operator=((const char*)psz); } | |
c57e3bd5 | 1026 | |
111bb7f2 | 1027 | // from wxWCharBuffer |
8f93a29f | 1028 | wxString& operator=(const wxWCharBuffer& s) |
04abe4bc | 1029 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs |
111bb7f2 | 1030 | // from wxCharBuffer |
04abe4bc VS |
1031 | wxString& operator=(const wxCharBuffer& s) |
1032 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs | |
3c67202d VZ |
1033 | |
1034 | // string concatenation | |
1035 | // in place concatenation | |
1036 | /* | |
1037 | Concatenate and return the result. Note that the left to right | |
1038 | associativity of << allows to write things like "str << str1 << str2 | |
1039 | << ..." (unlike with +=) | |
1040 | */ | |
1041 | // string += string | |
dd1eaa89 VZ |
1042 | wxString& operator<<(const wxString& s) |
1043 | { | |
8f93a29f VS |
1044 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
1045 | wxASSERT_MSG( s.IsValid(), | |
09443a26 | 1046 | _T("did you forget to call UngetWriteBuf()?") ); |
e87b7833 | 1047 | #endif |
dd1eaa89 | 1048 | |
e87b7833 | 1049 | append(s); |
dd1eaa89 VZ |
1050 | return *this; |
1051 | } | |
3c67202d | 1052 | // string += C string |
8f93a29f | 1053 | wxString& operator<<(const char *psz) |
c9f78968 | 1054 | { append(psz); return *this; } |
8f93a29f VS |
1055 | wxString& operator<<(const wchar_t *pwz) |
1056 | { append(pwz); return *this; } | |
c9f78968 | 1057 | wxString& operator<<(const wxCStrData& psz) |
8f93a29f | 1058 | { append(psz.AsString()); return *this; } |
3c67202d | 1059 | // string += char |
c9f78968 VS |
1060 | wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } |
1061 | wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } | |
1062 | wxString& operator<<(char ch) { append(1, ch); return *this; } | |
50e02008 | 1063 | wxString& operator<<(unsigned char ch) { append(1, ch); return *this; } |
c9f78968 | 1064 | wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } |
2bb67b80 OK |
1065 | |
1066 | // string += buffer (i.e. from wxGetString) | |
09443a26 | 1067 | wxString& operator<<(const wxWCharBuffer& s) |
d7330233 VS |
1068 | { return operator<<((const wchar_t *)s); } |
1069 | wxString& operator+=(const wxWCharBuffer& s) | |
1070 | { return operator<<((const wchar_t *)s); } | |
8f93a29f | 1071 | |
09443a26 | 1072 | wxString& operator<<(const wxCharBuffer& s) |
d7330233 VS |
1073 | { return operator<<((const char *)s); } |
1074 | wxString& operator+=(const wxCharBuffer& s) | |
1075 | { return operator<<((const char *)s); } | |
d7330233 | 1076 | |
3c67202d | 1077 | // string += C string |
09443a26 VZ |
1078 | wxString& Append(const wxString& s) |
1079 | { | |
5a8231ef WS |
1080 | // test for empty() to share the string if possible |
1081 | if ( empty() ) | |
09443a26 VZ |
1082 | *this = s; |
1083 | else | |
e87b7833 | 1084 | append(s); |
09443a26 VZ |
1085 | return *this; |
1086 | } | |
c9f78968 VS |
1087 | wxString& Append(const wxCStrData& psz) |
1088 | { append(psz); return *this; } | |
8f93a29f | 1089 | wxString& Append(const char* psz) |
e87b7833 | 1090 | { append(psz); return *this; } |
8f93a29f VS |
1091 | wxString& Append(const wchar_t* pwz) |
1092 | { append(pwz); return *this; } | |
3c67202d | 1093 | // append count copies of given character |
c9f78968 VS |
1094 | wxString& Append(wxUniChar ch, size_t count = 1u) |
1095 | { append(count, ch); return *this; } | |
1096 | wxString& Append(wxUniCharRef ch, size_t count = 1u) | |
1097 | { append(count, ch); return *this; } | |
1098 | wxString& Append(char ch, size_t count = 1u) | |
1099 | { append(count, ch); return *this; } | |
50e02008 VS |
1100 | wxString& Append(unsigned char ch, size_t count = 1u) |
1101 | { append(count, ch); return *this; } | |
c9f78968 | 1102 | wxString& Append(wchar_t ch, size_t count = 1u) |
e87b7833 | 1103 | { append(count, ch); return *this; } |
8f93a29f | 1104 | wxString& Append(const char* psz, size_t nLen) |
e87b7833 | 1105 | { append(psz, nLen); return *this; } |
8f93a29f VS |
1106 | wxString& Append(const wchar_t* pwz, size_t nLen) |
1107 | { append(pwz, nLen); return *this; } | |
3c67202d VZ |
1108 | |
1109 | // prepend a string, return the string itself | |
1110 | wxString& Prepend(const wxString& str) | |
1111 | { *this = str + *this; return *this; } | |
1112 | ||
1113 | // non-destructive concatenation | |
1fc8cfbd VZ |
1114 | // two strings |
1115 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
1116 | const wxString& string2); | |
1117 | // string with a single char | |
c9f78968 | 1118 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
1fc8cfbd | 1119 | // char with a string |
c9f78968 | 1120 | friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); |
1fc8cfbd VZ |
1121 | // string with C string |
1122 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
8f93a29f VS |
1123 | const char *psz); |
1124 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1125 | const wchar_t *pwz); | |
1fc8cfbd | 1126 | // C string with string |
8f93a29f VS |
1127 | friend wxString WXDLLIMPEXP_BASE operator+(const char *psz, |
1128 | const wxString& string); | |
1129 | friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, | |
1fc8cfbd | 1130 | const wxString& string); |
3c67202d VZ |
1131 | |
1132 | // stream-like functions | |
1133 | // insert an int into string | |
3ce65f6c VZ |
1134 | wxString& operator<<(int i) |
1135 | { return (*this) << Format(_T("%d"), i); } | |
1136 | // insert an unsigned int into string | |
1137 | wxString& operator<<(unsigned int ui) | |
1138 | { return (*this) << Format(_T("%u"), ui); } | |
1139 | // insert a long into string | |
1140 | wxString& operator<<(long l) | |
1141 | { return (*this) << Format(_T("%ld"), l); } | |
1142 | // insert an unsigned long into string | |
1143 | wxString& operator<<(unsigned long ul) | |
1144 | { return (*this) << Format(_T("%lu"), ul); } | |
3fc1adbd MW |
1145 | #if defined wxLongLong_t && !defined wxLongLongIsLong |
1146 | // insert a long long if they exist and aren't longs | |
1147 | wxString& operator<<(wxLongLong_t ll) | |
b7859132 MW |
1148 | { |
1149 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
1150 | return (*this) << Format(fmt, ll); | |
1151 | } | |
3fc1adbd MW |
1152 | // insert an unsigned long long |
1153 | wxString& operator<<(wxULongLong_t ull) | |
b7859132 MW |
1154 | { |
1155 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
1156 | return (*this) << Format(fmt , ull); | |
1157 | } | |
3fc1adbd | 1158 | #endif |
3c67202d | 1159 | // insert a float into string |
3ce65f6c VZ |
1160 | wxString& operator<<(float f) |
1161 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 1162 | // insert a double into string |
3ce65f6c VZ |
1163 | wxString& operator<<(double d) |
1164 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 1165 | |
3c67202d | 1166 | // string comparison |
30b21f9a | 1167 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
8f93a29f VS |
1168 | int Cmp(const char *psz) const |
1169 | { return compare(psz); } | |
1170 | int Cmp(const wchar_t *pwz) const | |
1171 | { return compare(pwz); } | |
1172 | int Cmp(const wxString& s) const | |
1173 | { return compare(s); } | |
3c67202d | 1174 | // same as Cmp() but not case-sensitive |
dcb68102 | 1175 | int CmpNoCase(const wxString& s) const; |
8f93a29f VS |
1176 | int CmpNoCase(const char *psz) const |
1177 | { return CmpNoCase(wxString(psz)); } | |
1178 | int CmpNoCase(const wchar_t *pwz) const | |
1179 | { return CmpNoCase(wxString(pwz)); } | |
3c67202d VZ |
1180 | // test for the string equality, either considering case or not |
1181 | // (if compareWithCase then the case matters) | |
11aac4ba VS |
1182 | bool IsSameAs(const wxString& str, bool compareWithCase = true) const |
1183 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
1184 | bool IsSameAs(const char *str, bool compareWithCase = true) const | |
1185 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
1186 | bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const | |
1187 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
20aa779e | 1188 | // comparison with a single character: returns true if equal |
c9f78968 | 1189 | bool IsSameAs(wxUniChar c, bool compareWithCase = true) const |
f33fee2a | 1190 | { |
e87b7833 | 1191 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c |
f33fee2a VZ |
1192 | : wxToupper(GetChar(0u)) == wxToupper(c)); |
1193 | } | |
11aac4ba VS |
1194 | // FIXME-UTF8: remove these overloads |
1195 | bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const | |
1196 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1197 | bool IsSameAs(char c, bool compareWithCase = true) const | |
1198 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1199 | bool IsSameAs(unsigned char c, bool compareWithCase = true) const | |
1200 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1201 | bool IsSameAs(wchar_t c, bool compareWithCase = true) const | |
1202 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1203 | bool IsSameAs(int c, bool compareWithCase = true) const | |
1204 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
3c67202d VZ |
1205 | |
1206 | // simple sub-string extraction | |
1207 | // return substring starting at nFirst of length nCount (or till the end | |
1208 | // if nCount = default value) | |
e87b7833 | 1209 | wxString Mid(size_t nFirst, size_t nCount = npos) const; |
3c67202d | 1210 | |
f6bcfd97 | 1211 | // operator version of Mid() |
3c67202d VZ |
1212 | wxString operator()(size_t start, size_t len) const |
1213 | { return Mid(start, len); } | |
1214 | ||
3affcd07 VZ |
1215 | // check if the string starts with the given prefix and return the rest |
1216 | // of the string in the provided pointer if it is not NULL; otherwise | |
1217 | // return false | |
f6bcfd97 | 1218 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; |
3affcd07 VZ |
1219 | // check if the string ends with the given suffix and return the |
1220 | // beginning of the string before the suffix in the provided pointer if | |
1221 | // it is not NULL; otherwise return false | |
1222 | bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const; | |
f6bcfd97 | 1223 | |
3c67202d | 1224 | // get first nCount characters |
c801d85f | 1225 | wxString Left(size_t nCount) const; |
3c67202d | 1226 | // get last nCount characters |
c801d85f | 1227 | wxString Right(size_t nCount) const; |
c0881dc3 | 1228 | // get all characters before the first occurance of ch |
3c67202d | 1229 | // (returns the whole string if ch not found) |
c9f78968 | 1230 | wxString BeforeFirst(wxUniChar ch) const; |
3c67202d VZ |
1231 | // get all characters before the last occurence of ch |
1232 | // (returns empty string if ch not found) | |
c9f78968 | 1233 | wxString BeforeLast(wxUniChar ch) const; |
3c67202d VZ |
1234 | // get all characters after the first occurence of ch |
1235 | // (returns empty string if ch not found) | |
c9f78968 | 1236 | wxString AfterFirst(wxUniChar ch) const; |
3c67202d VZ |
1237 | // get all characters after the last occurence of ch |
1238 | // (returns the whole string if ch not found) | |
c9f78968 | 1239 | wxString AfterLast(wxUniChar ch) const; |
3c67202d VZ |
1240 | |
1241 | // for compatibility only, use more explicitly named functions above | |
c9f78968 VS |
1242 | wxString Before(wxUniChar ch) const { return BeforeLast(ch); } |
1243 | wxString After(wxUniChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
1244 | |
1245 | // case conversion | |
c84c52de | 1246 | // convert to upper case in place, return the string itself |
c801d85f | 1247 | wxString& MakeUpper(); |
c84c52de | 1248 | // convert to upper case, return the copy of the string |
03ab016d JS |
1249 | // Here's something to remember: BC++ doesn't like returns in inlines. |
1250 | wxString Upper() const ; | |
c84c52de | 1251 | // convert to lower case in place, return the string itself |
c801d85f | 1252 | wxString& MakeLower(); |
c84c52de | 1253 | // convert to lower case, return the copy of the string |
03ab016d | 1254 | wxString Lower() const ; |
c801d85f | 1255 | |
3c67202d VZ |
1256 | // trimming/padding whitespace (either side) and truncating |
1257 | // remove spaces from left or from right (default) side | |
d775fa82 | 1258 | wxString& Trim(bool bFromRight = true); |
3c67202d | 1259 | // add nCount copies chPad in the beginning or at the end (default) |
c9f78968 | 1260 | wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); |
dd1eaa89 | 1261 | |
3c67202d VZ |
1262 | // searching and replacing |
1263 | // searching (return starting index, or -1 if not found) | |
c9f78968 | 1264 | int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr |
8a540c88 VS |
1265 | int Find(wxUniCharRef ch, bool bFromEnd = false) const |
1266 | { return Find(wxUniChar(ch), bFromEnd); } | |
1267 | int Find(char ch, bool bFromEnd = false) const | |
1268 | { return Find(wxUniChar(ch), bFromEnd); } | |
1269 | int Find(unsigned char ch, bool bFromEnd = false) const | |
1270 | { return Find(wxUniChar(ch), bFromEnd); } | |
1271 | int Find(wchar_t ch, bool bFromEnd = false) const | |
1272 | { return Find(wxUniChar(ch), bFromEnd); } | |
3c67202d | 1273 | // searching (return starting index, or -1 if not found) |
8a540c88 VS |
1274 | // FIXME-UTF8: keep wxString overload only |
1275 | int Find(const wxString& sub) const // like strstr | |
1276 | { | |
1277 | size_type idx = find(sub); | |
1278 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1279 | } | |
1280 | int Find(const char *sub) const // like strstr | |
1281 | { | |
1282 | size_type idx = find(sub); | |
1283 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1284 | } | |
1285 | int Find(const wchar_t *sub) const // like strstr | |
1286 | { | |
1287 | size_type idx = find(sub); | |
1288 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1289 | } | |
1290 | ||
3c67202d VZ |
1291 | // replace first (or all of bReplaceAll) occurences of substring with |
1292 | // another string, returns the number of replacements made | |
8a540c88 VS |
1293 | size_t Replace(const wxString& strOld, |
1294 | const wxString& strNew, | |
d775fa82 | 1295 | bool bReplaceAll = true); |
3c67202d VZ |
1296 | |
1297 | // check if the string contents matches a mask containing '*' and '?' | |
8a540c88 | 1298 | bool Matches(const wxString& mask) const; |
c801d85f | 1299 | |
d775fa82 | 1300 | // conversion to numbers: all functions return true only if the whole |
538f35cc VZ |
1301 | // string is a number and put the value of this number into the pointer |
1302 | // provided, the base is the numeric base in which the conversion should be | |
1303 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1304 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
cd0b1709 | 1305 | // convert to a signed integer |
538f35cc | 1306 | bool ToLong(long *val, int base = 10) const; |
cd0b1709 | 1307 | // convert to an unsigned integer |
538f35cc | 1308 | bool ToULong(unsigned long *val, int base = 10) const; |
d6718dd1 VZ |
1309 | // convert to wxLongLong |
1310 | #if defined(wxLongLong_t) | |
1311 | bool ToLongLong(wxLongLong_t *val, int base = 10) const; | |
1312 | // convert to wxULongLong | |
1313 | bool ToULongLong(wxULongLong_t *val, int base = 10) const; | |
1314 | #endif // wxLongLong_t | |
cd0b1709 VZ |
1315 | // convert to a double |
1316 | bool ToDouble(double *val) const; | |
1317 | ||
d6718dd1 | 1318 | |
c9f78968 | 1319 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
90e572f1 | 1320 | // formatted input/output |
3c67202d | 1321 | // as sprintf(), returns the number of characters written or < 0 on error |
7357f981 | 1322 | // (take 'this' into account in attribute parameter count) |
c9f78968 VS |
1323 | // int Printf(const wxChar *pszFormat, ...); |
1324 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
1325 | #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN | |
3c67202d | 1326 | // as vprintf(), returns the number of characters written or < 0 on error |
c9f78968 | 1327 | int PrintfV(const wxString& format, va_list argptr); |
dd1eaa89 | 1328 | |
c9f78968 | 1329 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
341e7d28 | 1330 | // returns the string containing the result of Printf() to it |
c9f78968 VS |
1331 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; |
1332 | WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat) | |
1333 | #endif | |
341e7d28 | 1334 | // the same as above, but takes a va_list |
c9f78968 | 1335 | static wxString FormatV(const wxString& format, va_list argptr); |
341e7d28 | 1336 | |
3c67202d VZ |
1337 | // raw access to string memory |
1338 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 1339 | // only works if the data of this string is not shared |
e87b7833 | 1340 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } |
3c67202d | 1341 | // minimize the string's memory |
dd1eaa89 | 1342 | // only works if the data of this string is not shared |
b1801e0e | 1343 | bool Shrink(); |
8f93a29f | 1344 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
d8a4b666 VS |
1345 | // These are deprecated, use wxStringBuffer or wxStringBufferLength instead |
1346 | // | |
3c67202d VZ |
1347 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
1348 | // a.s.a.p. to put string back in a reasonable state! | |
d8a4b666 | 1349 | wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) ); |
3c67202d | 1350 | // call this immediately after GetWriteBuf() has been used |
d8a4b666 VS |
1351 | wxDEPRECATED( void UngetWriteBuf() ); |
1352 | wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); | |
8f93a29f | 1353 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8 |
c801d85f | 1354 | |
77ffb593 | 1355 | // wxWidgets version 1 compatibility functions |
3c67202d VZ |
1356 | |
1357 | // use Mid() | |
1358 | wxString SubString(size_t from, size_t to) const | |
1359 | { return Mid(from, (to - from + 1)); } | |
1360 | // values for second parameter of CompareTo function | |
c801d85f | 1361 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 1362 | // values for first parameter of Strip function |
c801d85f | 1363 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 1364 | |
c9f78968 | 1365 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
7357f981 GD |
1366 | // use Printf() |
1367 | // (take 'this' into account in attribute parameter count) | |
c9f78968 VS |
1368 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; |
1369 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
1370 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
c801d85f | 1371 | |
3c67202d | 1372 | // use Cmp() |
2bb67b80 | 1373 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 1374 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 1375 | |
8f93a29f | 1376 | // use length() |
e87b7833 | 1377 | size_t Length() const { return length(); } |
3c67202d | 1378 | // Count the number of characters |
c9f78968 | 1379 | int Freq(wxUniChar ch) const; |
3c67202d | 1380 | // use MakeLower |
c801d85f | 1381 | void LowerCase() { MakeLower(); } |
3c67202d | 1382 | // use MakeUpper |
c801d85f | 1383 | void UpperCase() { MakeUpper(); } |
3c67202d | 1384 | // use Trim except that it doesn't change this string |
c801d85f KB |
1385 | wxString Strip(stripType w = trailing) const; |
1386 | ||
3c67202d | 1387 | // use Find (more general variants not yet supported) |
2bb67b80 | 1388 | size_t Index(const wxChar* psz) const { return Find(psz); } |
c9f78968 | 1389 | size_t Index(wxUniChar ch) const { return Find(ch); } |
3c67202d | 1390 | // use Truncate |
c801d85f | 1391 | wxString& Remove(size_t pos) { return Truncate(pos); } |
e87b7833 | 1392 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } |
c801d85f | 1393 | |
e87b7833 MB |
1394 | wxString& Remove(size_t nStart, size_t nLen) |
1395 | { return (wxString&)erase( nStart, nLen ); } | |
dd1eaa89 | 1396 | |
3c67202d | 1397 | // use Find() |
8f93a29f | 1398 | int First( wxUniChar ch ) const { return Find(ch); } |
8a540c88 | 1399 | int First( wxUniCharRef ch ) const { return Find(ch); } |
c9f78968 | 1400 | int First( char ch ) const { return Find(ch); } |
50e02008 | 1401 | int First( unsigned char ch ) const { return Find(ch); } |
c9f78968 | 1402 | int First( wchar_t ch ) const { return Find(ch); } |
2bb67b80 | 1403 | int First( const wxChar* psz ) const { return Find(psz); } |
8a540c88 | 1404 | int First( const wxString& str ) const { return Find(str); } |
8f93a29f | 1405 | int Last( wxUniChar ch ) const { return Find(ch, true); } |
d775fa82 | 1406 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } |
c801d85f | 1407 | |
5a8231ef WS |
1408 | // use empty() |
1409 | bool IsNull() const { return empty(); } | |
c801d85f | 1410 | |
3c67202d | 1411 | // std::string compatibility functions |
dd1eaa89 | 1412 | |
3c67202d VZ |
1413 | // take nLen chars starting at nPos |
1414 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
8f93a29f | 1415 | : m_impl(str.m_impl, nPos, nLen) { } |
f2a1b1bd | 1416 | // take all characters from first to last |
e87b7833 | 1417 | wxString(const_iterator first, const_iterator last) |
8f93a29f | 1418 | : m_impl(first, last) { } |
f2a1b1bd VZ |
1419 | wxString(const char *first, const char *last) |
1420 | { | |
1421 | SubstrBufFromMB str(ImplStr(first, last - first)); | |
1422 | m_impl.assign(str.data, str.len); | |
1423 | } | |
1424 | wxString(const wchar_t *first, const wchar_t *last) | |
1425 | { | |
1426 | SubstrBufFromWC str(ImplStr(first, last - first)); | |
1427 | m_impl.assign(str.data, str.len); | |
1428 | } | |
5460b9e3 | 1429 | |
3c67202d | 1430 | // lib.string.modifiers |
3c67202d VZ |
1431 | // append elements str[pos], ..., str[pos+n] |
1432 | wxString& append(const wxString& str, size_t pos, size_t n) | |
8f93a29f VS |
1433 | { |
1434 | size_t from, len; | |
1435 | str.PosLenToImpl(pos, n, &from, &len); | |
1436 | m_impl.append(str.m_impl, from, len); | |
1437 | return *this; | |
1438 | } | |
e87b7833 MB |
1439 | // append a string |
1440 | wxString& append(const wxString& str) | |
8f93a29f | 1441 | { m_impl.append(str.m_impl); return *this; } |
c9f78968 | 1442 | wxString& append(const wxCStrData& str) |
8f93a29f | 1443 | { m_impl.append(str.AsString().m_impl); return *this; } |
3c67202d | 1444 | // append first n (or all if n == npos) characters of sz |
8f93a29f VS |
1445 | wxString& append(const char *sz) |
1446 | { m_impl.append(ImplStr(sz)); return *this; } | |
1447 | wxString& append(const wchar_t *sz) | |
1448 | { m_impl.append(ImplStr(sz)); return *this; } | |
1449 | wxString& append(const char *sz, size_t n) | |
1450 | { | |
1451 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1452 | m_impl.append(str.data, str.len); | |
1453 | return *this; | |
1454 | } | |
1455 | wxString& append(const wchar_t *sz, size_t n) | |
1456 | { | |
1457 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1458 | m_impl.append(str.data, str.len); | |
1459 | return *this; | |
1460 | } | |
3c67202d | 1461 | // append n copies of ch |
c9f78968 | 1462 | wxString& append(size_t n, wxUniChar ch) |
8f93a29f VS |
1463 | { |
1464 | #if wxUSE_UNICODE_UTF8 | |
1465 | if ( !ch.IsAscii() ) | |
1466 | m_impl.append(EncodeNChars(n, ch)); | |
1467 | else | |
1468 | #endif | |
1469 | m_impl.append(n, (wxStringCharType)ch); | |
1470 | return *this; | |
1471 | } | |
e87b7833 MB |
1472 | // append from first to last |
1473 | wxString& append(const_iterator first, const_iterator last) | |
8f93a29f | 1474 | { m_impl.append(first, last); return *this; } |
f2a1b1bd VZ |
1475 | wxString& append(const char *first, const char *last) |
1476 | { return append(first, last - first); } | |
1477 | wxString& append(const wchar_t *first, const wchar_t *last) | |
1478 | { return append(first, last - first); } | |
3c67202d VZ |
1479 | |
1480 | // same as `this_string = str' | |
735d1db6 | 1481 | wxString& assign(const wxString& str) |
8f93a29f | 1482 | { m_impl = str.m_impl; return *this; } |
11aac4ba VS |
1483 | wxString& assign(const wxString& str, size_t len) |
1484 | { | |
1485 | m_impl.assign(str.m_impl, 0, str.LenToImpl(len)); | |
1486 | return *this; | |
1487 | } | |
3c67202d VZ |
1488 | // same as ` = str[pos..pos + n] |
1489 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
8f93a29f VS |
1490 | { |
1491 | size_t from, len; | |
1492 | str.PosLenToImpl(pos, n, &from, &len); | |
1493 | m_impl.assign(str.m_impl, from, len); | |
1494 | return *this; | |
1495 | } | |
3c67202d | 1496 | // same as `= first n (or all if n == npos) characters of sz' |
8f93a29f VS |
1497 | wxString& assign(const char *sz) |
1498 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1499 | wxString& assign(const wchar_t *sz) | |
1500 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1501 | wxString& assign(const char *sz, size_t n) | |
1502 | { | |
1503 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1504 | m_impl.assign(str.data, str.len); | |
1505 | return *this; | |
1506 | } | |
1507 | wxString& assign(const wchar_t *sz, size_t n) | |
1508 | { | |
1509 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1510 | m_impl.assign(str.data, str.len); | |
1511 | return *this; | |
1512 | } | |
3c67202d | 1513 | // same as `= n copies of ch' |
c9f78968 | 1514 | wxString& assign(size_t n, wxUniChar ch) |
8f93a29f VS |
1515 | { |
1516 | #if wxUSE_UNICODE_UTF8 | |
1517 | if ( !ch.IsAscii() ) | |
1518 | m_impl.assign(EncodeNChars(n, ch)); | |
1519 | else | |
1520 | #endif | |
1521 | m_impl.assign(n, (wxStringCharType)ch); | |
1522 | return *this; | |
1523 | } | |
11aac4ba VS |
1524 | |
1525 | wxString& assign(size_t n, wxUniCharRef ch) | |
1526 | { return assign(n, wxUniChar(ch)); } | |
1527 | wxString& assign(size_t n, char ch) | |
1528 | { return assign(n, wxUniChar(ch)); } | |
1529 | wxString& assign(size_t n, unsigned char ch) | |
1530 | { return assign(n, wxUniChar(ch)); } | |
1531 | wxString& assign(size_t n, wchar_t ch) | |
1532 | { return assign(n, wxUniChar(ch)); } | |
1533 | ||
e87b7833 MB |
1534 | // assign from first to last |
1535 | wxString& assign(const_iterator first, const_iterator last) | |
8f93a29f | 1536 | { m_impl.assign(first, last); return *this; } |
f2a1b1bd VZ |
1537 | wxString& assign(const char *first, const char *last) |
1538 | { return assign(first, last - first); } | |
1539 | wxString& assign(const wchar_t *first, const wchar_t *last) | |
1540 | { return assign(first, last - first); } | |
e87b7833 MB |
1541 | |
1542 | // string comparison | |
8f93a29f | 1543 | int compare(const wxString& str) const; |
e87b7833 | 1544 | // comparison with a substring |
8f93a29f | 1545 | int compare(size_t nStart, size_t nLen, const wxString& str) const; |
e87b7833 MB |
1546 | // comparison of 2 substrings |
1547 | int compare(size_t nStart, size_t nLen, | |
8f93a29f | 1548 | const wxString& str, size_t nStart2, size_t nLen2) const; |
e87b7833 | 1549 | // just like strcmp() |
8f93a29f VS |
1550 | int compare(const char* sz) const; |
1551 | int compare(const wchar_t* sz) const; | |
e87b7833 MB |
1552 | // substring comparison with first nCount characters of sz |
1553 | int compare(size_t nStart, size_t nLen, | |
8f93a29f VS |
1554 | const char* sz, size_t nCount = npos) const; |
1555 | int compare(size_t nStart, size_t nLen, | |
1556 | const wchar_t* sz, size_t nCount = npos) const; | |
3c67202d VZ |
1557 | |
1558 | // insert another string | |
e87b7833 | 1559 | wxString& insert(size_t nPos, const wxString& str) |
8f93a29f | 1560 | { insert(begin() + nPos, str.begin(), str.end()); return *this; } |
3c67202d VZ |
1561 | // insert n chars of str starting at nStart (in str) |
1562 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
8f93a29f VS |
1563 | { |
1564 | size_t from, len; | |
1565 | str.PosLenToImpl(nStart, n, &from, &len); | |
1566 | m_impl.insert(PosToImpl(nPos), str.m_impl, from, len); | |
1567 | return *this; | |
1568 | } | |
3c67202d | 1569 | // insert first n (or all if n == npos) characters of sz |
8f93a29f VS |
1570 | wxString& insert(size_t nPos, const char *sz) |
1571 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1572 | wxString& insert(size_t nPos, const wchar_t *sz) | |
1573 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1574 | wxString& insert(size_t nPos, const char *sz, size_t n) | |
1575 | { | |
1576 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1577 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1578 | return *this; | |
1579 | } | |
1580 | wxString& insert(size_t nPos, const wchar_t *sz, size_t n) | |
1581 | { | |
1582 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1583 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1584 | return *this; | |
1585 | } | |
3c67202d | 1586 | // insert n copies of ch |
c9f78968 | 1587 | wxString& insert(size_t nPos, size_t n, wxUniChar ch) |
8f93a29f VS |
1588 | { |
1589 | #if wxUSE_UNICODE_UTF8 | |
1590 | if ( !ch.IsAscii() ) | |
1591 | m_impl.insert(begin() + nPos, EncodeNChars(n, ch)); | |
1592 | else | |
1593 | #endif | |
1594 | m_impl.insert(begin() + nPos, n, (wxStringCharType)ch); | |
1595 | return *this; | |
1596 | } | |
c9f78968 | 1597 | iterator insert(iterator it, wxUniChar ch) |
8f93a29f | 1598 | { return iterator(m_impl.insert(it, EncodeChar(ch))); } |
e87b7833 | 1599 | void insert(iterator it, const_iterator first, const_iterator last) |
8f93a29f | 1600 | { m_impl.insert(it, first, last); } |
f2a1b1bd VZ |
1601 | void insert(iterator it, const char *first, const char *last) |
1602 | { insert(it - begin(), first, last - first); } | |
1603 | void append(iterator it, const wchar_t *first, const wchar_t *last) | |
1604 | { insert(it - begin(), first, last - first); } | |
c9f78968 | 1605 | void insert(iterator it, size_type n, wxUniChar ch) |
8f93a29f VS |
1606 | { |
1607 | #if wxUSE_UNICODE_UTF8 | |
1608 | if ( !ch.IsAscii() ) | |
1609 | m_impl.insert(it, EncodeNChars(n, ch)); | |
1610 | else | |
1611 | #endif | |
1612 | m_impl.insert(it, n, (wxStringCharType)ch); | |
1613 | } | |
3c67202d VZ |
1614 | |
1615 | // delete characters from nStart to nStart + nLen | |
e87b7833 | 1616 | wxString& erase(size_type pos = 0, size_type n = npos) |
8f93a29f VS |
1617 | { |
1618 | size_t from, len; | |
1619 | PosLenToImpl(pos, n, &from, &len); | |
1620 | m_impl.erase(from, len); | |
1621 | return *this; | |
1622 | } | |
f2a1b1bd | 1623 | // delete characters from first up to last |
e87b7833 | 1624 | iterator erase(iterator first, iterator last) |
8f93a29f | 1625 | { return iterator(m_impl.erase(first, last)); } |
e87b7833 | 1626 | iterator erase(iterator first) |
8f93a29f | 1627 | { return iterator(m_impl.erase(first)); } |
e87b7833 MB |
1628 | |
1629 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
1630 | void clear() { erase(); } | |
8f93a29f VS |
1631 | #else |
1632 | void clear() { m_impl.clear(); } | |
e87b7833 | 1633 | #endif |
3c67202d VZ |
1634 | |
1635 | // replaces the substring of length nLen starting at nStart | |
8f93a29f VS |
1636 | wxString& replace(size_t nStart, size_t nLen, const char* sz) |
1637 | { | |
1638 | size_t from, len; | |
1639 | PosLenToImpl(nStart, nLen, &from, &len); | |
1640 | m_impl.replace(from, len, ImplStr(sz)); | |
1641 | return *this; | |
1642 | } | |
1643 | wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz) | |
1644 | { | |
1645 | size_t from, len; | |
1646 | PosLenToImpl(nStart, nLen, &from, &len); | |
1647 | m_impl.replace(from, len, ImplStr(sz)); | |
1648 | return *this; | |
1649 | } | |
e87b7833 MB |
1650 | // replaces the substring of length nLen starting at nStart |
1651 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
8f93a29f VS |
1652 | { |
1653 | size_t from, len; | |
1654 | PosLenToImpl(nStart, nLen, &from, &len); | |
1655 | m_impl.replace(from, len, str.m_impl); | |
1656 | return *this; | |
1657 | } | |
3c67202d | 1658 | // replaces the substring with nCount copies of ch |
c9f78968 | 1659 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) |
8f93a29f VS |
1660 | { |
1661 | size_t from, len; | |
1662 | PosLenToImpl(nStart, nLen, &from, &len); | |
1663 | #if wxUSE_UNICODE_UTF8 | |
1664 | if ( !ch.IsAscii() ) | |
1665 | m_impl.replace(from, len, EncodeNChars(nCount, ch)); | |
1666 | else | |
1667 | #endif | |
1668 | m_impl.replace(from, len, nCount, (wxStringCharType)ch); | |
1669 | return *this; | |
1670 | } | |
3c67202d VZ |
1671 | // replaces a substring with another substring |
1672 | wxString& replace(size_t nStart, size_t nLen, | |
e87b7833 | 1673 | const wxString& str, size_t nStart2, size_t nLen2) |
8f93a29f VS |
1674 | { |
1675 | size_t from, len; | |
1676 | PosLenToImpl(nStart, nLen, &from, &len); | |
1677 | ||
1678 | size_t from2, len2; | |
1679 | str.PosLenToImpl(nStart2, nLen2, &from2, &len2); | |
1680 | ||
1681 | m_impl.replace(from, len, str.m_impl, from2, len2); | |
1682 | return *this; | |
1683 | } | |
e87b7833 | 1684 | // replaces the substring with first nCount chars of sz |
fb20fa43 | 1685 | wxString& replace(size_t nStart, size_t nLen, |
8f93a29f VS |
1686 | const char* sz, size_t nCount) |
1687 | { | |
1688 | size_t from, len; | |
1689 | PosLenToImpl(nStart, nLen, &from, &len); | |
1690 | ||
1691 | SubstrBufFromMB str(ImplStr(sz, nCount)); | |
1692 | ||
1693 | m_impl.replace(from, len, str.data, str.len); | |
1694 | return *this; | |
1695 | } | |
1696 | wxString& replace(size_t nStart, size_t nLen, | |
1697 | const wchar_t* sz, size_t nCount) | |
1698 | { | |
1699 | size_t from, len; | |
1700 | PosLenToImpl(nStart, nLen, &from, &len); | |
1701 | ||
1702 | SubstrBufFromWC str(ImplStr(sz, nCount)); | |
1703 | ||
1704 | m_impl.replace(from, len, str.data, str.len); | |
1705 | return *this; | |
1706 | } | |
11aac4ba VS |
1707 | wxString& replace(size_t nStart, size_t nLen, |
1708 | const wxString& s, size_t nCount) | |
1709 | { | |
1710 | size_t from, len; | |
1711 | PosLenToImpl(nStart, nLen, &from, &len); | |
1712 | m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount)); | |
1713 | return *this; | |
1714 | } | |
1715 | ||
8f93a29f VS |
1716 | wxString& replace(iterator first, iterator last, const char* s) |
1717 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
1718 | wxString& replace(iterator first, iterator last, const wchar_t* s) | |
1719 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
1720 | wxString& replace(iterator first, iterator last, const char* s, size_type n) | |
1721 | { | |
1722 | SubstrBufFromMB str(ImplStr(s, n)); | |
1723 | m_impl.replace(first, last, str.data, str.len); | |
1724 | return *this; | |
1725 | } | |
1726 | wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n) | |
1727 | { | |
1728 | SubstrBufFromWC str(ImplStr(s, n)); | |
1729 | m_impl.replace(first, last, str.data, str.len); | |
1730 | return *this; | |
1731 | } | |
e87b7833 | 1732 | wxString& replace(iterator first, iterator last, const wxString& s) |
8f93a29f VS |
1733 | { m_impl.replace(first, last, s.m_impl); return *this; } |
1734 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch) | |
1735 | { | |
1736 | #if wxUSE_UNICODE_UTF8 | |
1737 | if ( !ch.IsAscii() ) | |
1738 | m_impl.replace(first, last, EncodeNChars(n, ch)); | |
1739 | else | |
1740 | #endif | |
1741 | m_impl.replace(first, last, n, (wxStringCharType)ch); | |
1742 | return *this; | |
1743 | } | |
e87b7833 MB |
1744 | wxString& replace(iterator first, iterator last, |
1745 | const_iterator first1, const_iterator last1) | |
8f93a29f | 1746 | { m_impl.replace(first, last, first1, last1); return *this; } |
f2a1b1bd VZ |
1747 | wxString& replace(iterator first, iterator last, |
1748 | const char *first1, const char *last1) | |
1749 | { replace(first, last, first1, last1 - first1); return *this; } | |
1750 | wxString& replace(iterator first, iterator last, | |
1751 | const wchar_t *first1, const wchar_t *last1) | |
1752 | { replace(first, last, first1, last1 - first1); return *this; } | |
8f93a29f VS |
1753 | |
1754 | // swap two strings | |
1755 | void swap(wxString& str) | |
1756 | { m_impl.swap(str.m_impl); } | |
1757 | ||
1758 | // find a substring | |
1759 | size_t find(const wxString& str, size_t nStart = 0) const | |
1760 | { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); } | |
1761 | ||
1762 | // find first n characters of sz | |
1763 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const | |
1764 | { | |
1765 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1766 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
1767 | } | |
1768 | size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const | |
1769 | { | |
1770 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1771 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
1772 | } | |
1773 | ||
1774 | // find the first occurence of character ch after nStart | |
1775 | size_t find(wxUniChar ch, size_t nStart = 0) const | |
1776 | { return PosFromImpl(m_impl.find(EncodeChar(ch), PosToImpl(nStart))); } | |
1777 | size_t find(wxUniCharRef ch, size_t nStart = 0) const | |
1778 | { return find(wxUniChar(ch), nStart); } | |
1779 | size_t find(char ch, size_t nStart = 0) const | |
1780 | { return find(wxUniChar(ch), nStart); } | |
50e02008 VS |
1781 | size_t find(unsigned char ch, size_t nStart = 0) const |
1782 | { return find(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1783 | size_t find(wchar_t ch, size_t nStart = 0) const |
1784 | { return find(wxUniChar(ch), nStart); } | |
1785 | ||
1786 | // rfind() family is exactly like find() but works right to left | |
1787 | ||
1788 | // as find, but from the end | |
1789 | size_t rfind(const wxString& str, size_t nStart = npos) const | |
1790 | { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); } | |
1791 | ||
1792 | // as find, but from the end | |
1793 | size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const | |
1794 | { | |
1795 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1796 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
1797 | } | |
1798 | size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const | |
1799 | { | |
1800 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1801 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
1802 | } | |
1803 | // as find, but from the end | |
1804 | size_t rfind(wxUniChar ch, size_t nStart = npos) const | |
1805 | { return PosFromImpl(m_impl.rfind(EncodeChar(ch), PosToImpl(nStart))); } | |
1806 | size_t rfind(wxUniCharRef ch, size_t nStart = npos) const | |
1807 | { return rfind(wxUniChar(ch), nStart); } | |
1808 | size_t rfind(char ch, size_t nStart = npos) const | |
1809 | { return rfind(wxUniChar(ch), nStart); } | |
50e02008 VS |
1810 | size_t rfind(unsigned char ch, size_t nStart = npos) const |
1811 | { return rfind(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1812 | size_t rfind(wchar_t ch, size_t nStart = npos) const |
1813 | { return rfind(wxUniChar(ch), nStart); } | |
1814 | ||
1815 | // find first/last occurence of any character (not) in the set: | |
1816 | #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
1817 | // FIXME-UTF8: this is not entirely correct, because it doesn't work if | |
1818 | // sizeof(wchar_t)==2 and surrogates are present in the string; | |
1819 | // should we care? Probably not. | |
1820 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
a962cdf4 | 1821 | { return m_impl.find_first_of(str.m_impl, nStart); } |
8f93a29f VS |
1822 | size_t find_first_of(const char* sz, size_t nStart = 0) const |
1823 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
1824 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const | |
1825 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
a962cdf4 | 1826 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 1827 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1828 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f VS |
1829 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
1830 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
1831 | { return m_impl.find_first_of((wxChar)c, nStart); } | |
1832 | ||
a962cdf4 VS |
1833 | size_t find_last_of(const wxString& str, size_t nStart = npos) const |
1834 | { return m_impl.find_last_of(str.m_impl, nStart); } | |
8f93a29f VS |
1835 | size_t find_last_of(const char* sz, size_t nStart = npos) const |
1836 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
1837 | size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const | |
1838 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
1839 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const | |
1840 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
1841 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const | |
1842 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
1843 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
1844 | { return m_impl.find_last_of((wxChar)c, nStart); } | |
1845 | ||
a962cdf4 | 1846 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const |
8f93a29f | 1847 | { return m_impl.find_first_not_of(str.m_impl, nStart); } |
a962cdf4 | 1848 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const |
8f93a29f | 1849 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1850 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const |
8f93a29f | 1851 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1852 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 1853 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1854 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 1855 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1856 | size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const |
8f93a29f VS |
1857 | { return m_impl.find_first_not_of((wxChar)c, nStart); } |
1858 | ||
a962cdf4 | 1859 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const |
8f93a29f | 1860 | { return m_impl.find_last_not_of(str.m_impl, nStart); } |
a962cdf4 | 1861 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const |
8f93a29f | 1862 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1863 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const |
8f93a29f | 1864 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1865 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 1866 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1867 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 1868 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1869 | size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const |
8f93a29f VS |
1870 | { return m_impl.find_last_not_of((wxChar)c, nStart); } |
1871 | #else | |
1872 | // we can't use std::string implementation in UTF-8 build, because the | |
1873 | // character sets would be interpreted wrongly: | |
1874 | ||
1875 | // as strpbrk() but starts at nStart, returns npos if not found | |
1876 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
1877 | { return find_first_of((const wxChar*)str.c_str(), nStart); } | |
1878 | // same as above | |
1879 | size_t find_first_of(const char* sz, size_t nStart = 0) const; | |
1880 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const; | |
1881 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const; | |
1882 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1883 | // same as find(char, size_t) | |
1884 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
1885 | { return find(c, nStart); } | |
1886 | // find the last (starting from nStart) char from str in this string | |
1887 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
1888 | { return find_last_of((const wxChar*)str.c_str(), nStart); } | |
1889 | // same as above | |
1890 | size_t find_last_of (const char* sz, size_t nStart = npos) const; | |
1891 | size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const; | |
1892 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const; | |
1893 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1894 | // same as above | |
1895 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
1896 | { return rfind(c, nStart); } | |
1897 | ||
1898 | // find first/last occurence of any character not in the set | |
1899 | ||
1900 | // as strspn() (starting from nStart), returns npos on failure | |
1901 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
1902 | { return find_first_not_of((const wxChar*)str.c_str(), nStart); } | |
1903 | // same as above | |
1904 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const; | |
1905 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const; | |
1906 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const; | |
1907 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1908 | // same as above | |
1909 | size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; | |
1910 | // as strcspn() | |
1911 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const | |
1912 | { return find_last_not_of((const wxChar*)str.c_str(), nStart); } | |
1913 | // same as above | |
1914 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const; | |
1915 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const; | |
1916 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const; | |
1917 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1918 | // same as above | |
1919 | size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; | |
1920 | #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not | |
1921 | ||
1922 | // provide char/wchar_t/wxUniCharRef overloads for char-finding functions | |
1923 | // above to resolve ambiguities: | |
1924 | size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const | |
1925 | { return find_first_of(wxUniChar(ch), nStart); } | |
1926 | size_t find_first_of(char ch, size_t nStart = 0) const | |
1927 | { return find_first_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1928 | size_t find_first_of(unsigned char ch, size_t nStart = 0) const |
1929 | { return find_first_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1930 | size_t find_first_of(wchar_t ch, size_t nStart = 0) const |
1931 | { return find_first_of(wxUniChar(ch), nStart); } | |
1932 | size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const | |
1933 | { return find_last_of(wxUniChar(ch), nStart); } | |
1934 | size_t find_last_of(char ch, size_t nStart = npos) const | |
1935 | { return find_last_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1936 | size_t find_last_of(unsigned char ch, size_t nStart = npos) const |
1937 | { return find_last_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1938 | size_t find_last_of(wchar_t ch, size_t nStart = npos) const |
1939 | { return find_last_of(wxUniChar(ch), nStart); } | |
1940 | size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const | |
1941 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
1942 | size_t find_first_not_of(char ch, size_t nStart = 0) const | |
1943 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1944 | size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const |
1945 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1946 | size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const |
1947 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
1948 | size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const | |
1949 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
1950 | size_t find_last_not_of(char ch, size_t nStart = npos) const | |
1951 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1952 | size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const |
1953 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1954 | size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const |
1955 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
3c67202d | 1956 | |
e87b7833 MB |
1957 | // string += string |
1958 | wxString& operator+=(const wxString& s) | |
8f93a29f | 1959 | { m_impl += s.m_impl; return *this; } |
e87b7833 | 1960 | // string += C string |
8f93a29f VS |
1961 | wxString& operator+=(const char *psz) |
1962 | { m_impl += ImplStr(psz); return *this; } | |
1963 | wxString& operator+=(const wchar_t *pwz) | |
1964 | { m_impl += ImplStr(pwz); return *this; } | |
c9f78968 | 1965 | wxString& operator+=(const wxCStrData& s) |
8f93a29f | 1966 | { m_impl += s.AsString().m_impl; return *this; } |
e87b7833 | 1967 | // string += char |
c9f78968 | 1968 | wxString& operator+=(wxUniChar ch) |
8f93a29f | 1969 | { m_impl += EncodeChar(ch); return *this; } |
c9f78968 | 1970 | wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } |
b7d40341 | 1971 | wxString& operator+=(int ch) { return *this += wxUniChar(ch); } |
c9f78968 | 1972 | wxString& operator+=(char ch) { return *this += wxUniChar(ch); } |
b77afb41 | 1973 | wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); } |
c9f78968 | 1974 | wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } |
d8a4b666 VS |
1975 | |
1976 | private: | |
8f93a29f | 1977 | #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
d8a4b666 | 1978 | // helpers for wxStringBuffer and wxStringBufferLength |
8f93a29f VS |
1979 | wxStringCharType *DoGetWriteBuf(size_t nLen) |
1980 | { return m_impl.DoGetWriteBuf(nLen); } | |
1981 | void DoUngetWriteBuf() | |
1982 | { m_impl.DoUngetWriteBuf(); } | |
1983 | void DoUngetWriteBuf(size_t nLen) | |
1984 | { m_impl.DoUngetWriteBuf(nLen); } | |
d8a4b666 VS |
1985 | |
1986 | friend class WXDLLIMPEXP_BASE wxStringBuffer; | |
1987 | friend class WXDLLIMPEXP_BASE wxStringBufferLength; | |
8f93a29f | 1988 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
c9f78968 VS |
1989 | |
1990 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1991 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
1992 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
1993 | #endif | |
8f93a29f VS |
1994 | |
1995 | #if !wxUSE_STL_BASED_WXSTRING | |
1996 | // check string's data validity | |
1997 | bool IsValid() const { return m_impl.GetStringData()->IsValid(); } | |
1998 | #endif | |
1999 | ||
2000 | private: | |
2001 | wxStringImpl m_impl; | |
132276cf VS |
2002 | |
2003 | // buffers for compatibility conversion from (char*)c_str() and | |
2004 | // (wchar_t*)c_str(): | |
2005 | // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers | |
2006 | template<typename T> | |
2007 | struct ConvertedBuffer | |
2008 | { | |
2009 | ConvertedBuffer() : m_buf(NULL) {} | |
2010 | ~ConvertedBuffer() | |
2011 | { free(m_buf); } | |
2012 | ||
2013 | operator const T*() const { return m_buf; } | |
2014 | ||
2015 | ConvertedBuffer& operator=(T *str) | |
2016 | { | |
2017 | free(m_buf); | |
2018 | m_buf = str; | |
2019 | return *this; | |
2020 | } | |
2021 | ||
2022 | T *m_buf; | |
2023 | }; | |
2024 | #if wxUSE_UNICODE | |
2025 | ConvertedBuffer<char> m_convertedToChar; | |
2026 | #endif | |
2027 | #if !wxUSE_UNICODE_WCHAR | |
2028 | ConvertedBuffer<wchar_t> m_convertedToWChar; | |
2029 | #endif | |
2030 | friend class WXDLLIMPEXP_BASE wxCStrData; | |
c801d85f KB |
2031 | }; |
2032 | ||
c9f78968 VS |
2033 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
2034 | #pragma warning (default:4275) | |
2035 | #endif | |
2036 | ||
a962cdf4 VS |
2037 | // string iterator operators that satisfy STL Random Access Iterator |
2038 | // requirements: | |
2039 | inline wxString::iterator operator+(int n, wxString::iterator i) | |
2040 | { return i + n; } | |
2041 | inline wxString::iterator operator+(size_t n, wxString::iterator i) | |
2042 | { return i + n; } | |
2043 | inline wxString::const_iterator operator+(int n, wxString::const_iterator i) | |
2044 | { return i + n; } | |
2045 | inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i) | |
2046 | { return i + n; } | |
2047 | inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i) | |
2048 | { return i + n; } | |
2049 | inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i) | |
2050 | { return i + n; } | |
2051 | inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i) | |
2052 | { return i + n; } | |
2053 | inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i) | |
2054 | { return i + n; } | |
2055 | ||
1fc8cfbd VZ |
2056 | // notice that even though for many compilers the friend declarations above are |
2057 | // enough, from the point of view of C++ standard we must have the declarations | |
2058 | // here as friend ones are not injected in the enclosing namespace and without | |
2059 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
c9f78968 | 2060 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
8f93a29f VS |
2061 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz); |
2062 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz); | |
2063 | wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string); | |
2064 | wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string); | |
1fc8cfbd | 2065 | |
c9f78968 VS |
2066 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
2067 | wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
2068 | ||
2069 | inline wxString operator+(const wxString& string, wxUniCharRef ch) | |
2070 | { return string + (wxUniChar)ch; } | |
2071 | inline wxString operator+(const wxString& string, char ch) | |
2072 | { return string + wxUniChar(ch); } | |
2073 | inline wxString operator+(const wxString& string, wchar_t ch) | |
2074 | { return string + wxUniChar(ch); } | |
2075 | inline wxString operator+(wxUniCharRef ch, const wxString& string) | |
2076 | { return (wxUniChar)ch + string; } | |
2077 | inline wxString operator+(char ch, const wxString& string) | |
2078 | { return wxUniChar(ch) + string; } | |
2079 | inline wxString operator+(wchar_t ch, const wxString& string) | |
2080 | { return wxUniChar(ch) + string; } | |
2081 | ||
b7146cbe | 2082 | |
992527a5 | 2083 | #if wxUSE_STL_BASED_WXSTRING |
07170120 | 2084 | // return an empty wxString (not very useful with wxUSE_STL == 1) |
12f99210 | 2085 | inline const wxString wxGetEmptyString() { return wxString(); } |
992527a5 | 2086 | #else // !wxUSE_STL_BASED_WXSTRING |
07170120 VZ |
2087 | // return an empty wxString (more efficient than wxString() here) |
2088 | inline const wxString& wxGetEmptyString() | |
2089 | { | |
2090 | return *(wxString *)&wxEmptyString; | |
2091 | } | |
992527a5 | 2092 | #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING |
07170120 | 2093 | |
1d218550 VZ |
2094 | // ---------------------------------------------------------------------------- |
2095 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
2096 | // ---------------------------------------------------------------------------- | |
2097 | ||
8f93a29f | 2098 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 |
941b78cc MB |
2099 | |
2100 | class WXDLLIMPEXP_BASE wxStringBuffer | |
2101 | { | |
2102 | public: | |
2103 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
e87b7833 | 2104 | : m_str(str), m_buf(lenWanted) |
941b78cc MB |
2105 | { } |
2106 | ||
fe5fc682 | 2107 | ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); } |
941b78cc MB |
2108 | |
2109 | operator wxChar*() { return m_buf.data(); } | |
2110 | ||
2111 | private: | |
2112 | wxString& m_str; | |
2113 | #if wxUSE_UNICODE | |
2114 | wxWCharBuffer m_buf; | |
2115 | #else | |
2116 | wxCharBuffer m_buf; | |
2117 | #endif | |
941b78cc MB |
2118 | |
2119 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
2120 | }; | |
2121 | ||
2122 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
2123 | { | |
2124 | public: | |
2125 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2126 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
2127 | { } | |
2128 | ||
2129 | ~wxStringBufferLength() | |
2130 | { | |
2131 | wxASSERT(m_lenSet); | |
2132 | m_str.assign(m_buf.data(), m_len); | |
2133 | } | |
2134 | ||
2135 | operator wxChar*() { return m_buf.data(); } | |
2136 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2137 | ||
2138 | private: | |
2139 | wxString& m_str; | |
2140 | #if wxUSE_UNICODE | |
2141 | wxWCharBuffer m_buf; | |
2142 | #else | |
2143 | wxCharBuffer m_buf; | |
2144 | #endif | |
2145 | size_t m_len; | |
2146 | bool m_lenSet; | |
2147 | ||
2148 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2149 | }; | |
2150 | ||
8f93a29f | 2151 | #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
941b78cc | 2152 | |
bddd7a8d | 2153 | class WXDLLIMPEXP_BASE wxStringBuffer |
1d218550 VZ |
2154 | { |
2155 | public: | |
2156 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
b1801e0e | 2157 | : m_str(str), m_buf(NULL) |
d8a4b666 | 2158 | { m_buf = m_str.DoGetWriteBuf(lenWanted); } |
e015c2a3 | 2159 | |
d8a4b666 | 2160 | ~wxStringBuffer() { m_str.DoUngetWriteBuf(); } |
e015c2a3 | 2161 | |
1d218550 | 2162 | operator wxChar*() const { return m_buf; } |
e015c2a3 | 2163 | |
1d218550 VZ |
2164 | private: |
2165 | wxString& m_str; | |
2166 | wxChar *m_buf; | |
e015c2a3 VZ |
2167 | |
2168 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1d218550 VZ |
2169 | }; |
2170 | ||
941b78cc MB |
2171 | class WXDLLIMPEXP_BASE wxStringBufferLength |
2172 | { | |
2173 | public: | |
2174 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2175 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
4055ed82 | 2176 | { |
d8a4b666 | 2177 | m_buf = m_str.DoGetWriteBuf(lenWanted); |
494b978f RN |
2178 | wxASSERT(m_buf != NULL); |
2179 | } | |
941b78cc MB |
2180 | |
2181 | ~wxStringBufferLength() | |
2182 | { | |
2183 | wxASSERT(m_lenSet); | |
d8a4b666 | 2184 | m_str.DoUngetWriteBuf(m_len); |
941b78cc MB |
2185 | } |
2186 | ||
2187 | operator wxChar*() const { return m_buf; } | |
2188 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2189 | ||
2190 | private: | |
2191 | wxString& m_str; | |
2192 | wxChar *m_buf; | |
2193 | size_t m_len; | |
2194 | bool m_lenSet; | |
2195 | ||
2196 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2197 | }; | |
2198 | ||
8f93a29f | 2199 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
941b78cc | 2200 | |
c801d85f | 2201 | // --------------------------------------------------------------------------- |
3c67202d | 2202 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 2203 | // --------------------------------------------------------------------------- |
f33fee2a | 2204 | |
fb52c2b6 VZ |
2205 | #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0 |
2206 | ||
2207 | wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) | |
2208 | ||
2209 | #undef wxCMP_WXCHAR_STRING | |
2210 | ||
2211 | // note that there is an optimization in operator==() and !=(): we (quickly) | |
2212 | // checks the strings length first, before comparing their data | |
0cbe5ee3 VZ |
2213 | inline bool operator==(const wxString& s1, const wxString& s2) |
2214 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
0cbe5ee3 VZ |
2215 | inline bool operator!=(const wxString& s1, const wxString& s2) |
2216 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
0cbe5ee3 VZ |
2217 | inline bool operator< (const wxString& s1, const wxString& s2) |
2218 | { return s1.Cmp(s2) < 0; } | |
0cbe5ee3 VZ |
2219 | inline bool operator> (const wxString& s1, const wxString& s2) |
2220 | { return s1.Cmp(s2) > 0; } | |
0cbe5ee3 VZ |
2221 | inline bool operator<=(const wxString& s1, const wxString& s2) |
2222 | { return s1.Cmp(s2) <= 0; } | |
0cbe5ee3 VZ |
2223 | inline bool operator>=(const wxString& s1, const wxString& s2) |
2224 | { return s1.Cmp(s2) >= 0; } | |
3c67202d | 2225 | |
5ca07d0f OK |
2226 | #if wxUSE_UNICODE |
2227 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 2228 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 2229 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 2230 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
2231 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
2232 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
2233 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
2234 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
0cbe5ee3 | 2235 | #else // !wxUSE_UNICODE |
5ca07d0f | 2236 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 2237 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 2238 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 2239 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
2240 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
2241 | { return (s1.Cmp((const char *)s2) != 0); } | |
2242 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
2243 | { return (s2.Cmp((const char *)s1) != 0); } | |
0cbe5ee3 | 2244 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
5ca07d0f | 2245 | |
028a2b5d | 2246 | #if wxUSE_UNICODE |
6d56eb5c | 2247 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 2248 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 2249 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 2250 | { return (const wchar_t *)buf + string; } |
0cbe5ee3 | 2251 | #else // !wxUSE_UNICODE |
6d56eb5c | 2252 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 2253 | { return string + (const char *)buf; } |
6d56eb5c | 2254 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 2255 | { return (const char *)buf + string; } |
0cbe5ee3 | 2256 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
f04f3991 | 2257 | |
a962cdf4 | 2258 | // comparison with char |
c9f78968 VS |
2259 | inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } |
2260 | inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } | |
2261 | inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } | |
2262 | inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } | |
2263 | inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } | |
2264 | inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } | |
2265 | inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } | |
2266 | inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } | |
2267 | inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } | |
2268 | inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } | |
2269 | inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } | |
2270 | inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } | |
2271 | inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } | |
2272 | inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } | |
2273 | inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } | |
2274 | inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } | |
2275 | inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } | |
2276 | inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } | |
e51b17a1 | 2277 | |
d7330233 VS |
2278 | // comparison with C string in Unicode build |
2279 | #if wxUSE_UNICODE | |
fb52c2b6 VZ |
2280 | |
2281 | #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s | |
2282 | ||
2283 | wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) | |
2284 | ||
2285 | #undef wxCMP_CHAR_STRING | |
2286 | ||
d7330233 VS |
2287 | #endif // wxUSE_UNICODE |
2288 | ||
fb52c2b6 VZ |
2289 | // we also need to provide the operators for comparison with wxCStrData to |
2290 | // resolve ambiguity between operator(const wxChar *,const wxString &) and | |
2291 | // operator(const wxChar *, const wxChar *) for "p == s.c_str()" | |
2292 | // | |
2293 | // notice that these are (shallow) pointer comparisons, not (deep) string ones | |
2294 | #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() | |
2295 | #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() | |
2296 | ||
11aac4ba VS |
2297 | wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) |
2298 | wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) | |
fb52c2b6 VZ |
2299 | |
2300 | #undef wxCMP_CHAR_CSTRDATA | |
2301 | #undef wxCMP_WCHAR_CSTRDATA | |
2302 | ||
c801d85f | 2303 | // --------------------------------------------------------------------------- |
3c67202d | 2304 | // Implementation only from here until the end of file |
c801d85f KB |
2305 | // --------------------------------------------------------------------------- |
2306 | ||
e87b7833 | 2307 | #if wxUSE_STD_IOSTREAM |
c801d85f | 2308 | |
65f19af1 | 2309 | #include "wx/iosfwrap.h" |
c801d85f | 2310 | |
bddd7a8d | 2311 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); |
c9f78968 | 2312 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); |
04abe4bc VS |
2313 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&); |
2314 | #ifndef __BORLANDC__ | |
2315 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&); | |
2316 | #endif | |
c801d85f | 2317 | |
3c67202d | 2318 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 2319 | |
c9f78968 VS |
2320 | // --------------------------------------------------------------------------- |
2321 | // wxCStrData implementation | |
2322 | // --------------------------------------------------------------------------- | |
2323 | ||
2324 | inline wxCStrData::wxCStrData(char *buf) | |
2325 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2326 | inline wxCStrData::wxCStrData(wchar_t *buf) | |
2327 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2328 | ||
2329 | inline wxCStrData::~wxCStrData() | |
2330 | { | |
2331 | if ( m_owned ) | |
2332 | delete m_str; | |
2333 | } | |
2334 | ||
11aac4ba VS |
2335 | inline wxCStrData::operator bool() const |
2336 | { | |
2337 | return !m_str->empty(); | |
1bb592b8 | 2338 | } |
11aac4ba VS |
2339 | |
2340 | // simple cases for AsChar() and AsWChar(), the complicated ones are | |
2341 | // in string.cpp | |
2342 | #if wxUSE_UNICODE_WCHAR | |
c9f78968 | 2343 | inline const wchar_t* wxCStrData::AsWChar() const |
11aac4ba VS |
2344 | { |
2345 | return m_str->wx_str() + m_offset; | |
2346 | } | |
2347 | #endif // wxUSE_UNICODE_WCHAR | |
2348 | ||
2349 | #if !wxUSE_UNICODE | |
c9f78968 | 2350 | inline const char* wxCStrData::AsChar() const |
c9f78968 | 2351 | { |
a962cdf4 | 2352 | return m_str->wx_str() + m_offset; |
c9f78968 | 2353 | } |
11aac4ba | 2354 | #endif // !wxUSE_UNICODE |
c9f78968 VS |
2355 | |
2356 | inline wxString wxCStrData::AsString() const | |
2357 | { | |
2358 | if ( m_offset == 0 ) | |
2359 | return *m_str; | |
2360 | else | |
2361 | return m_str->Mid(m_offset); | |
2362 | } | |
2363 | ||
c9f78968 VS |
2364 | inline wxUniChar wxCStrData::operator*() const |
2365 | { | |
2366 | if ( m_str->empty() ) | |
2367 | return wxUniChar(_T('\0')); | |
2368 | else | |
2369 | return (*m_str)[m_offset]; | |
2370 | } | |
2371 | ||
2372 | inline wxUniChar wxCStrData::operator[](size_t n) const | |
2373 | { | |
2374 | return m_str->at(m_offset + n); | |
2375 | } | |
2376 | ||
cc5bd48e VZ |
2377 | // ---------------------------------------------------------------------------- |
2378 | // more wxCStrData operators | |
2379 | // ---------------------------------------------------------------------------- | |
2380 | ||
2381 | // we need to define those to allow "size_t pos = p - s.c_str()" where p is | |
2382 | // some pointer into the string | |
2383 | inline size_t operator-(const char *p, const wxCStrData& cs) | |
2384 | { | |
2385 | return p - cs.AsChar(); | |
2386 | } | |
2387 | ||
2388 | inline size_t operator-(const wchar_t *p, const wxCStrData& cs) | |
2389 | { | |
2390 | return p - cs.AsWChar(); | |
2391 | } | |
2392 | ||
414b7b40 VZ |
2393 | // ---------------------------------------------------------------------------- |
2394 | // implementation of wx[W]CharBuffer inline methods using wxCStrData | |
2395 | // ---------------------------------------------------------------------------- | |
2396 | ||
11aac4ba VS |
2397 | // FIXME-UTF8: move this to buffer.h |
2398 | inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr) | |
2399 | : wxCharTypeBufferBase(cstr.AsChar()) | |
2400 | { | |
2401 | } | |
2402 | ||
2403 | inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr) | |
2404 | : wxCharTypeBufferBase(cstr.AsWChar()) | |
414b7b40 VZ |
2405 | { |
2406 | } | |
2407 | ||
11aac4ba | 2408 | #endif // _WX_WXSTRING_H_ |