]>
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 | ||
574 | ~wxString(); | |
8f93a29f VS |
575 | |
576 | public: | |
577 | // standard types | |
578 | typedef wxUniChar value_type; | |
579 | typedef wxUniChar char_type; | |
580 | typedef wxUniCharRef reference; | |
581 | typedef wxChar* pointer; | |
582 | typedef const wxChar* const_pointer; | |
583 | ||
584 | typedef size_t size_type; | |
585 | typedef wxUniChar const_reference; | |
586 | ||
a962cdf4 VS |
587 | #if wxUSE_STL |
588 | #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag | |
589 | #else | |
590 | #define WX_STR_ITERATOR_TAG void /* dummy type */ | |
591 | #endif | |
592 | ||
8f93a29f VS |
593 | #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \ |
594 | reference_type, reference_ctor) \ | |
595 | private: \ | |
596 | typedef wxStringImpl::iterator_name underlying_iterator; \ | |
597 | public: \ | |
a962cdf4 | 598 | typedef WX_STR_ITERATOR_TAG iterator_category; \ |
8f93a29f | 599 | typedef wxUniChar value_type; \ |
a962cdf4 | 600 | typedef int difference_type; \ |
8f93a29f VS |
601 | typedef reference_type reference; \ |
602 | typedef pointer_type pointer; \ | |
603 | \ | |
604 | iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \ | |
605 | \ | |
606 | reference operator*() const { return reference_ctor; } \ | |
a962cdf4 | 607 | reference operator[](size_t n) const { return *(*this + n); } \ |
8f93a29f VS |
608 | \ |
609 | iterator_name& operator++() \ | |
610 | { wxString::IncIter(m_cur); return *this; } \ | |
611 | iterator_name& operator--() \ | |
612 | { wxString::DecIter(m_cur); return *this; } \ | |
613 | iterator_name operator++(int) \ | |
614 | { \ | |
615 | iterator_name tmp = *this; \ | |
616 | wxString::IncIter(m_cur); \ | |
617 | return tmp; \ | |
618 | } \ | |
619 | iterator_name operator--(int) \ | |
620 | { \ | |
621 | iterator_name tmp = *this; \ | |
622 | wxString::DecIter(m_cur); \ | |
623 | return tmp; \ | |
624 | } \ | |
625 | \ | |
626 | iterator_name operator+(int n) const \ | |
627 | { return iterator_name(wxString::AddToIter(m_cur, n)); } \ | |
628 | iterator_name operator+(size_t n) const \ | |
629 | { return iterator_name(wxString::AddToIter(m_cur, (int)n)); } \ | |
630 | iterator_name operator-(int n) const \ | |
631 | { return iterator_name(wxString::AddToIter(m_cur, -n)); } \ | |
632 | iterator_name operator-(size_t n) const \ | |
633 | { return iterator_name(wxString::AddToIter(m_cur, -(int)n)); } \ | |
30d560f4 | 634 | iterator_name& operator+=(int n) \ |
8f93a29f | 635 | { m_cur = wxString::AddToIter(m_cur, n); return *this; } \ |
30d560f4 | 636 | iterator_name& operator+=(size_t n) \ |
8f93a29f | 637 | { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \ |
30d560f4 | 638 | iterator_name& operator-=(int n) \ |
8f93a29f | 639 | { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \ |
30d560f4 | 640 | iterator_name& operator-=(size_t n) \ |
8f93a29f VS |
641 | { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \ |
642 | \ | |
643 | unsigned operator-(const iterator_name& i) const \ | |
644 | { return wxString::DiffIters(m_cur, i.m_cur); } \ | |
645 | \ | |
f2a1b1bd | 646 | bool operator==(const iterator_name& i) const \ |
8f93a29f VS |
647 | { return m_cur == i.m_cur; } \ |
648 | bool operator!=(const iterator_name& i) const \ | |
649 | { return m_cur != i.m_cur; } \ | |
650 | \ | |
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 | bool operator>=(const iterator_name& i) const \ | |
658 | { return m_cur >= i.m_cur; } \ | |
659 | \ | |
660 | private: \ | |
661 | /* for internal wxString use only: */ \ | |
662 | iterator_name(underlying_iterator ptr) : m_cur(ptr) {} \ | |
663 | operator underlying_iterator() const { return m_cur; } \ | |
664 | \ | |
665 | friend class WXDLLIMPEXP_BASE wxString; \ | |
8f93a29f VS |
666 | friend class WXDLLIMPEXP_BASE wxCStrData; \ |
667 | \ | |
668 | private: \ | |
669 | underlying_iterator m_cur; | |
670 | ||
671 | class const_iterator; | |
672 | ||
673 | class iterator | |
674 | { | |
675 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, | |
676 | wxUniCharRef::CreateForString(m_cur)) | |
677 | ||
678 | friend class const_iterator; | |
679 | }; | |
680 | ||
681 | class const_iterator | |
682 | { | |
683 | // NB: reference_type is intentionally value, not reference, the character | |
684 | // may be encoded differently in wxString data: | |
685 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar, | |
686 | wxUniChar(*m_cur)) | |
687 | ||
688 | public: | |
689 | const_iterator(const iterator& i) : m_cur(i.m_cur) {} | |
690 | }; | |
691 | ||
a962cdf4 | 692 | #undef WX_STR_ITERATOR_TAG |
8f93a29f VS |
693 | #undef WX_STR_ITERATOR_IMPL |
694 | ||
695 | friend class iterator; | |
696 | friend class const_iterator; | |
697 | ||
698 | template <typename T> | |
699 | class reverse_iterator_impl | |
700 | { | |
701 | public: | |
702 | typedef T iterator_type; | |
a962cdf4 VS |
703 | |
704 | typedef typename T::iterator_category iterator_category; | |
8f93a29f | 705 | typedef typename T::value_type value_type; |
a962cdf4 | 706 | typedef typename T::difference_type difference_type; |
8f93a29f VS |
707 | typedef typename T::reference reference; |
708 | typedef typename T::pointer *pointer; | |
709 | ||
710 | reverse_iterator_impl(iterator_type i) : m_cur(i) {} | |
711 | reverse_iterator_impl(const reverse_iterator_impl& ri) | |
712 | : m_cur(ri.m_cur) {} | |
713 | ||
714 | iterator_type base() const { return m_cur; } | |
715 | ||
716 | reference operator*() const { return *(m_cur-1); } | |
a962cdf4 | 717 | reference operator[](size_t n) const { return *(*this + n); } |
8f93a29f VS |
718 | |
719 | reverse_iterator_impl& operator++() | |
720 | { --m_cur; return *this; } | |
721 | reverse_iterator_impl operator++(int) | |
722 | { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } | |
723 | reverse_iterator_impl& operator--() | |
724 | { ++m_cur; return *this; } | |
725 | reverse_iterator_impl operator--(int) | |
726 | { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } | |
727 | ||
50eeb96f | 728 | // NB: explicit <T> in the functions below is to keep BCC 5.5 happy |
8f93a29f | 729 | reverse_iterator_impl operator+(int n) const |
50eeb96f | 730 | { return reverse_iterator_impl<T>(m_cur - n); } |
8f93a29f | 731 | reverse_iterator_impl operator+(size_t n) const |
50eeb96f | 732 | { return reverse_iterator_impl<T>(m_cur - n); } |
8f93a29f | 733 | reverse_iterator_impl operator-(int n) const |
50eeb96f | 734 | { return reverse_iterator_impl<T>(m_cur + n); } |
8f93a29f | 735 | reverse_iterator_impl operator-(size_t n) const |
50eeb96f | 736 | { return reverse_iterator_impl<T>(m_cur + n); } |
8f93a29f VS |
737 | reverse_iterator_impl operator+=(int n) |
738 | { m_cur -= n; return *this; } | |
739 | reverse_iterator_impl operator+=(size_t n) | |
740 | { m_cur -= n; return *this; } | |
741 | reverse_iterator_impl operator-=(int n) | |
742 | { m_cur += n; return *this; } | |
743 | reverse_iterator_impl operator-=(size_t n) | |
744 | { m_cur += n; return *this; } | |
745 | ||
746 | unsigned operator-(const reverse_iterator_impl& i) const | |
747 | { return i.m_cur - m_cur; } | |
748 | ||
749 | bool operator==(const reverse_iterator_impl& ri) const | |
750 | { return m_cur == ri.m_cur; } | |
751 | bool operator!=(const reverse_iterator_impl& ri) const | |
752 | { return !(*this == ri); } | |
753 | ||
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 | bool operator>=(const reverse_iterator_impl& i) const | |
761 | { return m_cur <= i.m_cur; } | |
762 | ||
763 | private: | |
764 | iterator_type m_cur; | |
765 | }; | |
e87b7833 | 766 | |
8f93a29f VS |
767 | typedef reverse_iterator_impl<iterator> reverse_iterator; |
768 | typedef reverse_iterator_impl<const_iterator> const_reverse_iterator; | |
e90c1d2a | 769 | |
8f93a29f VS |
770 | // first valid index position |
771 | const_iterator begin() const { return const_iterator(m_impl.begin()); } | |
772 | iterator begin() { return iterator(m_impl.begin()); } | |
773 | // position one after the last valid one | |
774 | const_iterator end() const { return const_iterator(m_impl.end()); } | |
775 | iterator end() { return iterator(m_impl.end()); } | |
776 | ||
777 | // first element of the reversed string | |
778 | const_reverse_iterator rbegin() const | |
779 | { return const_reverse_iterator(end()); } | |
780 | reverse_iterator rbegin() | |
781 | { return reverse_iterator(end()); } | |
782 | // one beyond the end of the reversed string | |
783 | const_reverse_iterator rend() const | |
784 | { return const_reverse_iterator(begin()); } | |
785 | reverse_iterator rend() | |
786 | { return reverse_iterator(begin()); } | |
787 | ||
788 | // std::string methods: | |
789 | #if wxUSE_UNICODE_UTF8 | |
790 | size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize! | |
791 | #else | |
792 | size_t length() const { return m_impl.length(); } | |
793 | #endif | |
ce76f779 | 794 | |
8f93a29f VS |
795 | size_type size() const { return length(); } |
796 | size_type max_size() const { return npos; } | |
e90c1d2a | 797 | |
8f93a29f | 798 | bool empty() const { return m_impl.empty(); } |
b77afb41 | 799 | |
8f93a29f VS |
800 | size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8 |
801 | void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8 | |
b77afb41 | 802 | |
8f93a29f VS |
803 | void resize(size_t nSize, wxUniChar ch = wxT('\0')) |
804 | { | |
805 | #if wxUSE_UNICODE_UTF8 | |
806 | if ( !ch.IsAscii() ) | |
807 | { | |
808 | size_t len = length(); | |
809 | if ( nSize == len) | |
810 | return; | |
811 | else if ( nSize < len ) | |
812 | erase(nSize); | |
813 | else | |
814 | append(nSize - len, ch); | |
815 | } | |
816 | else | |
817 | #endif | |
818 | m_impl.resize(nSize, (wxStringCharType)ch); | |
819 | } | |
e90c1d2a | 820 | |
8f93a29f VS |
821 | wxString substr(size_t nStart = 0, size_t nLen = npos) const |
822 | { | |
823 | size_t pos, len; | |
824 | PosLenToImpl(nStart, nLen, &pos, &len); | |
825 | return m_impl.substr(pos, len); | |
826 | } | |
e90c1d2a | 827 | |
3c67202d VZ |
828 | // generic attributes & operations |
829 | // as standard strlen() | |
e87b7833 | 830 | size_t Len() const { return length(); } |
3c67202d | 831 | // string contains any characters? |
e87b7833 | 832 | bool IsEmpty() const { return empty(); } |
d775fa82 | 833 | // empty string is "false", so !str will return true |
20aa779e | 834 | bool operator!() const { return empty(); } |
735d1db6 VZ |
835 | // truncate the string to given length |
836 | wxString& Truncate(size_t uiLen); | |
3c67202d | 837 | // empty string contents |
dd1eaa89 VZ |
838 | void Empty() |
839 | { | |
735d1db6 | 840 | Truncate(0); |
dd1eaa89 | 841 | |
5a8231ef | 842 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); |
7be07660 | 843 | } |
3c67202d | 844 | // empty the string and free memory |
7be07660 VZ |
845 | void Clear() |
846 | { | |
e87b7833 MB |
847 | wxString tmp(wxEmptyString); |
848 | swap(tmp); | |
dd1eaa89 VZ |
849 | } |
850 | ||
3c67202d VZ |
851 | // contents test |
852 | // Is an ascii value | |
c801d85f | 853 | bool IsAscii() const; |
3c67202d | 854 | // Is a number |
c801d85f | 855 | bool IsNumber() const; |
3c67202d | 856 | // Is a word |
c801d85f | 857 | bool IsWord() const; |
c801d85f | 858 | |
3c67202d VZ |
859 | // data access (all indexes are 0 based) |
860 | // read access | |
8f93a29f VS |
861 | wxUniChar at(size_t n) const |
862 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
c9f78968 | 863 | wxUniChar GetChar(size_t n) const |
9d9ad673 | 864 | { return at(n); } |
3c67202d | 865 | // read/write access |
8f93a29f VS |
866 | wxUniCharRef at(size_t n) |
867 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
c9f78968 | 868 | wxUniCharRef GetWritableChar(size_t n) |
9d9ad673 | 869 | { return at(n); } |
3c67202d | 870 | // write access |
c9f78968 | 871 | void SetChar(size_t n, wxUniChar ch) |
9d9ad673 | 872 | { at(n) = ch; } |
c801d85f | 873 | |
3c67202d | 874 | // get last character |
8f93a29f | 875 | wxUniChar Last() const |
09443a26 | 876 | { |
5a8231ef | 877 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
09443a26 | 878 | |
9d9ad673 | 879 | return at(length() - 1); |
09443a26 VZ |
880 | } |
881 | ||
3c67202d | 882 | // get writable last character |
c9f78968 | 883 | wxUniCharRef Last() |
09443a26 | 884 | { |
5a8231ef | 885 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
9d9ad673 | 886 | return at(length() - 1); |
09443a26 | 887 | } |
c801d85f | 888 | |
d836ee96 | 889 | /* |
9d9ad673 | 890 | Note that we we must define all of the overloads below to avoid |
c9f78968 | 891 | ambiguity when using str[0]. |
d836ee96 | 892 | */ |
c9f78968 | 893 | wxUniChar operator[](int n) const |
8f93a29f | 894 | { return at(n); } |
c1df0a3b | 895 | wxUniChar operator[](long n) const |
8f93a29f | 896 | { return at(n); } |
c9f78968 | 897 | wxUniChar operator[](size_t n) const |
8f93a29f | 898 | { return at(n); } |
01398433 | 899 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 900 | wxUniChar operator[](unsigned int n) const |
8f93a29f | 901 | { return at(n); } |
01398433 | 902 | #endif // size_t != unsigned int |
01398433 | 903 | |
9d9ad673 | 904 | // operator versions of GetWriteableChar() |
c9f78968 | 905 | wxUniCharRef operator[](int n) |
8f93a29f | 906 | { return at(n); } |
c1df0a3b | 907 | wxUniCharRef operator[](long n) |
8f93a29f | 908 | { return at(n); } |
c9f78968 | 909 | wxUniCharRef operator[](size_t n) |
8f93a29f | 910 | { return at(n); } |
d836ee96 | 911 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 912 | wxUniCharRef operator[](unsigned int n) |
8f93a29f | 913 | { return at(n); } |
d836ee96 | 914 | #endif // size_t != unsigned int |
c801d85f | 915 | |
c9f78968 VS |
916 | // explicit conversion to C string (use this with printf()!) |
917 | wxCStrData c_str() const { return wxCStrData(this); } | |
8f93a29f | 918 | wxCStrData data() const { return c_str(); } |
c9f78968 | 919 | |
3c67202d | 920 | // implicit conversion to C string |
c9f78968 | 921 | operator wxCStrData() const { return c_str(); } |
11aac4ba VS |
922 | operator const char*() const { return c_str(); } |
923 | operator const wchar_t*() const { return c_str(); } | |
e015c2a3 | 924 | |
e015c2a3 | 925 | // identical to c_str(), for MFC compatibility |
c9f78968 VS |
926 | const wxCStrData GetData() const { return c_str(); } |
927 | ||
928 | // explicit conversion to C string in internal representation (char*, | |
929 | // wchar_t*, UTF-8-encoded char*, depending on the build): | |
8f93a29f | 930 | const_pointer wx_str() const { return m_impl.c_str(); } |
e90c1d2a | 931 | |
ef0f1387 VS |
932 | // conversion to *non-const* multibyte or widestring buffer; modifying |
933 | // returned buffer won't affect the string, these methods are only useful | |
934 | // for passing values to const-incorrect functions | |
935 | wxWritableCharBuffer char_str() const { return mb_str(); } | |
936 | wxWritableWCharBuffer wchar_str() const { return wc_str(); } | |
937 | ||
e015c2a3 VZ |
938 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
939 | // converting numbers or strings which are certain not to contain special | |
940 | // chars (typically system functions, X atoms, environment variables etc.) | |
941 | // | |
942 | // the behaviour of these functions with the strings containing anything | |
943 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 944 | #if wxUSE_UNICODE |
2b5f62a0 VZ |
945 | static wxString FromAscii(const char *ascii); // string |
946 | static wxString FromAscii(const char ascii); // char | |
b1ac3b56 | 947 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
948 | #else // ANSI |
949 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
2b5f62a0 | 950 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
951 | const char *ToAscii() const { return c_str(); } |
952 | #endif // Unicode/!Unicode | |
b1ac3b56 | 953 | |
2b5f62a0 | 954 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 955 | // buffer with temporary data |
f6bcfd97 BP |
956 | // |
957 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
958 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
959 | // fn_str() to return a string which should be used with the OS APIs | |
960 | // accepting the file names. The return value is always the same, but the | |
961 | // type differs because a function may either return pointer to the buffer | |
962 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 963 | #if wxUSE_UNICODE |
830f8f11 | 964 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; |
f6bcfd97 | 965 | |
e90c1d2a VZ |
966 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
967 | ||
e87b7833 | 968 | const wxChar* wc_str() const { return c_str(); } |
f6bcfd97 BP |
969 | |
970 | // for compatibility with !wxUSE_UNICODE version | |
830f8f11 | 971 | const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
e90c1d2a | 972 | |
2bb67b80 | 973 | #if wxMBFILES |
5f709e67 | 974 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 975 | #else // !wxMBFILES |
e87b7833 | 976 | const wxChar* fn_str() const { return c_str(); } |
e90c1d2a VZ |
977 | #endif // wxMBFILES/!wxMBFILES |
978 | #else // ANSI | |
e87b7833 | 979 | const wxChar* mb_str() const { return c_str(); } |
f6bcfd97 BP |
980 | |
981 | // for compatibility with wxUSE_UNICODE version | |
830f8f11 | 982 | const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
f6bcfd97 | 983 | |
e90c1d2a | 984 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 985 | |
6f841509 | 986 | #if wxUSE_WCHAR_T |
ef0f1387 | 987 | const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const; |
e90c1d2a | 988 | #endif // wxUSE_WCHAR_T |
d5c8817c SC |
989 | #ifdef __WXOSX__ |
990 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
991 | #else | |
e87b7833 | 992 | const wxChar* fn_str() const { return c_str(); } |
d5c8817c | 993 | #endif |
e90c1d2a | 994 | #endif // Unicode/ANSI |
c801d85f | 995 | |
3c67202d VZ |
996 | // overloaded assignment |
997 | // from another wxString | |
04abe4bc VS |
998 | wxString& operator=(const wxString& stringSrc) |
999 | { m_impl = stringSrc.m_impl; return *this; } | |
8f93a29f VS |
1000 | wxString& operator=(const wxCStrData& cstr) |
1001 | { return *this = cstr.AsString(); } | |
3c67202d | 1002 | // from a character |
c9f78968 | 1003 | wxString& operator=(wxUniChar ch) |
8f93a29f | 1004 | { m_impl = EncodeChar(ch); return *this; } |
c9f78968 | 1005 | wxString& operator=(wxUniCharRef ch) |
8f93a29f | 1006 | { return operator=((wxUniChar)ch); } |
c9f78968 | 1007 | wxString& operator=(char ch) |
8f93a29f | 1008 | { return operator=(wxUniChar(ch)); } |
50e02008 VS |
1009 | wxString& operator=(unsigned char ch) |
1010 | { return operator=(wxUniChar(ch)); } | |
c9f78968 | 1011 | wxString& operator=(wchar_t ch) |
8f93a29f | 1012 | { return operator=(wxUniChar(ch)); } |
c57e3bd5 RN |
1013 | // from a C string - STL probably will crash on NULL, |
1014 | // so we need to compensate in that case | |
992527a5 | 1015 | #if wxUSE_STL_BASED_WXSTRING |
04abe4bc VS |
1016 | wxString& operator=(const char *psz) |
1017 | { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; } | |
1018 | wxString& operator=(const wchar_t *pwz) | |
1019 | { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; } | |
c57e3bd5 | 1020 | #else |
04abe4bc VS |
1021 | wxString& operator=(const char *psz) |
1022 | { m_impl = ImplStr(psz); return *this; } | |
1023 | wxString& operator=(const wchar_t *pwz) | |
1024 | { m_impl = ImplStr(pwz); return *this; } | |
c57e3bd5 | 1025 | #endif |
04abe4bc VS |
1026 | wxString& operator=(const unsigned char *psz) |
1027 | { return operator=((const char*)psz); } | |
c57e3bd5 | 1028 | |
111bb7f2 | 1029 | // from wxWCharBuffer |
8f93a29f | 1030 | wxString& operator=(const wxWCharBuffer& s) |
04abe4bc | 1031 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs |
111bb7f2 | 1032 | // from wxCharBuffer |
04abe4bc VS |
1033 | wxString& operator=(const wxCharBuffer& s) |
1034 | { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs | |
3c67202d VZ |
1035 | |
1036 | // string concatenation | |
1037 | // in place concatenation | |
1038 | /* | |
1039 | Concatenate and return the result. Note that the left to right | |
1040 | associativity of << allows to write things like "str << str1 << str2 | |
1041 | << ..." (unlike with +=) | |
1042 | */ | |
1043 | // string += string | |
dd1eaa89 VZ |
1044 | wxString& operator<<(const wxString& s) |
1045 | { | |
8f93a29f VS |
1046 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
1047 | wxASSERT_MSG( s.IsValid(), | |
09443a26 | 1048 | _T("did you forget to call UngetWriteBuf()?") ); |
e87b7833 | 1049 | #endif |
dd1eaa89 | 1050 | |
e87b7833 | 1051 | append(s); |
dd1eaa89 VZ |
1052 | return *this; |
1053 | } | |
3c67202d | 1054 | // string += C string |
8f93a29f | 1055 | wxString& operator<<(const char *psz) |
c9f78968 | 1056 | { append(psz); return *this; } |
8f93a29f VS |
1057 | wxString& operator<<(const wchar_t *pwz) |
1058 | { append(pwz); return *this; } | |
c9f78968 | 1059 | wxString& operator<<(const wxCStrData& psz) |
8f93a29f | 1060 | { append(psz.AsString()); return *this; } |
3c67202d | 1061 | // string += char |
c9f78968 VS |
1062 | wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } |
1063 | wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } | |
1064 | wxString& operator<<(char ch) { append(1, ch); return *this; } | |
50e02008 | 1065 | wxString& operator<<(unsigned char ch) { append(1, ch); return *this; } |
c9f78968 | 1066 | wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } |
2bb67b80 OK |
1067 | |
1068 | // string += buffer (i.e. from wxGetString) | |
09443a26 | 1069 | wxString& operator<<(const wxWCharBuffer& s) |
d7330233 VS |
1070 | { return operator<<((const wchar_t *)s); } |
1071 | wxString& operator+=(const wxWCharBuffer& s) | |
1072 | { return operator<<((const wchar_t *)s); } | |
8f93a29f | 1073 | |
09443a26 | 1074 | wxString& operator<<(const wxCharBuffer& s) |
d7330233 VS |
1075 | { return operator<<((const char *)s); } |
1076 | wxString& operator+=(const wxCharBuffer& s) | |
1077 | { return operator<<((const char *)s); } | |
d7330233 | 1078 | |
3c67202d | 1079 | // string += C string |
09443a26 VZ |
1080 | wxString& Append(const wxString& s) |
1081 | { | |
5a8231ef WS |
1082 | // test for empty() to share the string if possible |
1083 | if ( empty() ) | |
09443a26 VZ |
1084 | *this = s; |
1085 | else | |
e87b7833 | 1086 | append(s); |
09443a26 VZ |
1087 | return *this; |
1088 | } | |
c9f78968 VS |
1089 | wxString& Append(const wxCStrData& psz) |
1090 | { append(psz); return *this; } | |
8f93a29f | 1091 | wxString& Append(const char* psz) |
e87b7833 | 1092 | { append(psz); return *this; } |
8f93a29f VS |
1093 | wxString& Append(const wchar_t* pwz) |
1094 | { append(pwz); return *this; } | |
3c67202d | 1095 | // append count copies of given character |
c9f78968 VS |
1096 | wxString& Append(wxUniChar ch, size_t count = 1u) |
1097 | { append(count, ch); return *this; } | |
1098 | wxString& Append(wxUniCharRef ch, size_t count = 1u) | |
1099 | { append(count, ch); return *this; } | |
1100 | wxString& Append(char ch, size_t count = 1u) | |
1101 | { append(count, ch); return *this; } | |
50e02008 VS |
1102 | wxString& Append(unsigned char ch, size_t count = 1u) |
1103 | { append(count, ch); return *this; } | |
c9f78968 | 1104 | wxString& Append(wchar_t ch, size_t count = 1u) |
e87b7833 | 1105 | { append(count, ch); return *this; } |
8f93a29f | 1106 | wxString& Append(const char* psz, size_t nLen) |
e87b7833 | 1107 | { append(psz, nLen); return *this; } |
8f93a29f VS |
1108 | wxString& Append(const wchar_t* pwz, size_t nLen) |
1109 | { append(pwz, nLen); return *this; } | |
3c67202d VZ |
1110 | |
1111 | // prepend a string, return the string itself | |
1112 | wxString& Prepend(const wxString& str) | |
1113 | { *this = str + *this; return *this; } | |
1114 | ||
1115 | // non-destructive concatenation | |
1fc8cfbd VZ |
1116 | // two strings |
1117 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
1118 | const wxString& string2); | |
1119 | // string with a single char | |
c9f78968 | 1120 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
1fc8cfbd | 1121 | // char with a string |
c9f78968 | 1122 | friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); |
1fc8cfbd VZ |
1123 | // string with C string |
1124 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
8f93a29f VS |
1125 | const char *psz); |
1126 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1127 | const wchar_t *pwz); | |
1fc8cfbd | 1128 | // C string with string |
8f93a29f VS |
1129 | friend wxString WXDLLIMPEXP_BASE operator+(const char *psz, |
1130 | const wxString& string); | |
1131 | friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, | |
1fc8cfbd | 1132 | const wxString& string); |
3c67202d VZ |
1133 | |
1134 | // stream-like functions | |
1135 | // insert an int into string | |
3ce65f6c VZ |
1136 | wxString& operator<<(int i) |
1137 | { return (*this) << Format(_T("%d"), i); } | |
1138 | // insert an unsigned int into string | |
1139 | wxString& operator<<(unsigned int ui) | |
1140 | { return (*this) << Format(_T("%u"), ui); } | |
1141 | // insert a long into string | |
1142 | wxString& operator<<(long l) | |
1143 | { return (*this) << Format(_T("%ld"), l); } | |
1144 | // insert an unsigned long into string | |
1145 | wxString& operator<<(unsigned long ul) | |
1146 | { return (*this) << Format(_T("%lu"), ul); } | |
3fc1adbd MW |
1147 | #if defined wxLongLong_t && !defined wxLongLongIsLong |
1148 | // insert a long long if they exist and aren't longs | |
1149 | wxString& operator<<(wxLongLong_t ll) | |
b7859132 MW |
1150 | { |
1151 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
1152 | return (*this) << Format(fmt, ll); | |
1153 | } | |
3fc1adbd MW |
1154 | // insert an unsigned long long |
1155 | wxString& operator<<(wxULongLong_t ull) | |
b7859132 MW |
1156 | { |
1157 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
1158 | return (*this) << Format(fmt , ull); | |
1159 | } | |
3fc1adbd | 1160 | #endif |
3c67202d | 1161 | // insert a float into string |
3ce65f6c VZ |
1162 | wxString& operator<<(float f) |
1163 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 1164 | // insert a double into string |
3ce65f6c VZ |
1165 | wxString& operator<<(double d) |
1166 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 1167 | |
3c67202d | 1168 | // string comparison |
30b21f9a | 1169 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
8f93a29f VS |
1170 | int Cmp(const char *psz) const |
1171 | { return compare(psz); } | |
1172 | int Cmp(const wchar_t *pwz) const | |
1173 | { return compare(pwz); } | |
1174 | int Cmp(const wxString& s) const | |
1175 | { return compare(s); } | |
3c67202d | 1176 | // same as Cmp() but not case-sensitive |
dcb68102 | 1177 | int CmpNoCase(const wxString& s) const; |
8f93a29f VS |
1178 | int CmpNoCase(const char *psz) const |
1179 | { return CmpNoCase(wxString(psz)); } | |
1180 | int CmpNoCase(const wchar_t *pwz) const | |
1181 | { return CmpNoCase(wxString(pwz)); } | |
3c67202d VZ |
1182 | // test for the string equality, either considering case or not |
1183 | // (if compareWithCase then the case matters) | |
11aac4ba VS |
1184 | bool IsSameAs(const wxString& str, bool compareWithCase = true) const |
1185 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
1186 | bool IsSameAs(const char *str, bool compareWithCase = true) const | |
1187 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
1188 | bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const | |
1189 | { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; } | |
20aa779e | 1190 | // comparison with a single character: returns true if equal |
c9f78968 | 1191 | bool IsSameAs(wxUniChar c, bool compareWithCase = true) const |
f33fee2a | 1192 | { |
e87b7833 | 1193 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c |
f33fee2a VZ |
1194 | : wxToupper(GetChar(0u)) == wxToupper(c)); |
1195 | } | |
11aac4ba VS |
1196 | // FIXME-UTF8: remove these overloads |
1197 | bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const | |
1198 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1199 | bool IsSameAs(char c, bool compareWithCase = true) const | |
1200 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1201 | bool IsSameAs(unsigned char c, bool compareWithCase = true) const | |
1202 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1203 | bool IsSameAs(wchar_t c, bool compareWithCase = true) const | |
1204 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
1205 | bool IsSameAs(int c, bool compareWithCase = true) const | |
1206 | { return IsSameAs(wxUniChar(c), compareWithCase); } | |
3c67202d VZ |
1207 | |
1208 | // simple sub-string extraction | |
1209 | // return substring starting at nFirst of length nCount (or till the end | |
1210 | // if nCount = default value) | |
e87b7833 | 1211 | wxString Mid(size_t nFirst, size_t nCount = npos) const; |
3c67202d | 1212 | |
f6bcfd97 | 1213 | // operator version of Mid() |
3c67202d VZ |
1214 | wxString operator()(size_t start, size_t len) const |
1215 | { return Mid(start, len); } | |
1216 | ||
3affcd07 VZ |
1217 | // check if the string starts with the given prefix and return the rest |
1218 | // of the string in the provided pointer if it is not NULL; otherwise | |
1219 | // return false | |
f6bcfd97 | 1220 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; |
3affcd07 VZ |
1221 | // check if the string ends with the given suffix and return the |
1222 | // beginning of the string before the suffix in the provided pointer if | |
1223 | // it is not NULL; otherwise return false | |
1224 | bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const; | |
f6bcfd97 | 1225 | |
3c67202d | 1226 | // get first nCount characters |
c801d85f | 1227 | wxString Left(size_t nCount) const; |
3c67202d | 1228 | // get last nCount characters |
c801d85f | 1229 | wxString Right(size_t nCount) const; |
c0881dc3 | 1230 | // get all characters before the first occurance of ch |
3c67202d | 1231 | // (returns the whole string if ch not found) |
c9f78968 | 1232 | wxString BeforeFirst(wxUniChar ch) const; |
3c67202d VZ |
1233 | // get all characters before the last occurence of ch |
1234 | // (returns empty string if ch not found) | |
c9f78968 | 1235 | wxString BeforeLast(wxUniChar ch) const; |
3c67202d VZ |
1236 | // get all characters after the first occurence of ch |
1237 | // (returns empty string if ch not found) | |
c9f78968 | 1238 | wxString AfterFirst(wxUniChar ch) const; |
3c67202d VZ |
1239 | // get all characters after the last occurence of ch |
1240 | // (returns the whole string if ch not found) | |
c9f78968 | 1241 | wxString AfterLast(wxUniChar ch) const; |
3c67202d VZ |
1242 | |
1243 | // for compatibility only, use more explicitly named functions above | |
c9f78968 VS |
1244 | wxString Before(wxUniChar ch) const { return BeforeLast(ch); } |
1245 | wxString After(wxUniChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
1246 | |
1247 | // case conversion | |
c84c52de | 1248 | // convert to upper case in place, return the string itself |
c801d85f | 1249 | wxString& MakeUpper(); |
c84c52de | 1250 | // convert to upper case, return the copy of the string |
03ab016d JS |
1251 | // Here's something to remember: BC++ doesn't like returns in inlines. |
1252 | wxString Upper() const ; | |
c84c52de | 1253 | // convert to lower case in place, return the string itself |
c801d85f | 1254 | wxString& MakeLower(); |
c84c52de | 1255 | // convert to lower case, return the copy of the string |
03ab016d | 1256 | wxString Lower() const ; |
c801d85f | 1257 | |
3c67202d VZ |
1258 | // trimming/padding whitespace (either side) and truncating |
1259 | // remove spaces from left or from right (default) side | |
d775fa82 | 1260 | wxString& Trim(bool bFromRight = true); |
3c67202d | 1261 | // add nCount copies chPad in the beginning or at the end (default) |
c9f78968 | 1262 | wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); |
dd1eaa89 | 1263 | |
3c67202d VZ |
1264 | // searching and replacing |
1265 | // searching (return starting index, or -1 if not found) | |
c9f78968 | 1266 | int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr |
8a540c88 VS |
1267 | int Find(wxUniCharRef ch, bool bFromEnd = false) const |
1268 | { return Find(wxUniChar(ch), bFromEnd); } | |
1269 | int Find(char ch, bool bFromEnd = false) const | |
1270 | { return Find(wxUniChar(ch), bFromEnd); } | |
1271 | int Find(unsigned char ch, bool bFromEnd = false) const | |
1272 | { return Find(wxUniChar(ch), bFromEnd); } | |
1273 | int Find(wchar_t ch, bool bFromEnd = false) const | |
1274 | { return Find(wxUniChar(ch), bFromEnd); } | |
3c67202d | 1275 | // searching (return starting index, or -1 if not found) |
8a540c88 VS |
1276 | // FIXME-UTF8: keep wxString overload only |
1277 | int Find(const wxString& sub) const // like strstr | |
1278 | { | |
1279 | size_type idx = find(sub); | |
1280 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1281 | } | |
1282 | int Find(const char *sub) const // like strstr | |
1283 | { | |
1284 | size_type idx = find(sub); | |
1285 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1286 | } | |
1287 | int Find(const wchar_t *sub) const // like strstr | |
1288 | { | |
1289 | size_type idx = find(sub); | |
1290 | return (idx == npos) ? wxNOT_FOUND : (int)idx; | |
1291 | } | |
1292 | ||
3c67202d VZ |
1293 | // replace first (or all of bReplaceAll) occurences of substring with |
1294 | // another string, returns the number of replacements made | |
8a540c88 VS |
1295 | size_t Replace(const wxString& strOld, |
1296 | const wxString& strNew, | |
d775fa82 | 1297 | bool bReplaceAll = true); |
3c67202d VZ |
1298 | |
1299 | // check if the string contents matches a mask containing '*' and '?' | |
8a540c88 | 1300 | bool Matches(const wxString& mask) const; |
c801d85f | 1301 | |
d775fa82 | 1302 | // conversion to numbers: all functions return true only if the whole |
538f35cc VZ |
1303 | // string is a number and put the value of this number into the pointer |
1304 | // provided, the base is the numeric base in which the conversion should be | |
1305 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1306 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
cd0b1709 | 1307 | // convert to a signed integer |
538f35cc | 1308 | bool ToLong(long *val, int base = 10) const; |
cd0b1709 | 1309 | // convert to an unsigned integer |
538f35cc | 1310 | bool ToULong(unsigned long *val, int base = 10) const; |
d6718dd1 VZ |
1311 | // convert to wxLongLong |
1312 | #if defined(wxLongLong_t) | |
1313 | bool ToLongLong(wxLongLong_t *val, int base = 10) const; | |
1314 | // convert to wxULongLong | |
1315 | bool ToULongLong(wxULongLong_t *val, int base = 10) const; | |
1316 | #endif // wxLongLong_t | |
cd0b1709 VZ |
1317 | // convert to a double |
1318 | bool ToDouble(double *val) const; | |
1319 | ||
d6718dd1 | 1320 | |
c9f78968 | 1321 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
90e572f1 | 1322 | // formatted input/output |
3c67202d | 1323 | // as sprintf(), returns the number of characters written or < 0 on error |
7357f981 | 1324 | // (take 'this' into account in attribute parameter count) |
c9f78968 VS |
1325 | // int Printf(const wxChar *pszFormat, ...); |
1326 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
1327 | #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN | |
3c67202d | 1328 | // as vprintf(), returns the number of characters written or < 0 on error |
c9f78968 | 1329 | int PrintfV(const wxString& format, va_list argptr); |
dd1eaa89 | 1330 | |
c9f78968 | 1331 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
341e7d28 | 1332 | // returns the string containing the result of Printf() to it |
c9f78968 VS |
1333 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; |
1334 | WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat) | |
1335 | #endif | |
341e7d28 | 1336 | // the same as above, but takes a va_list |
c9f78968 | 1337 | static wxString FormatV(const wxString& format, va_list argptr); |
341e7d28 | 1338 | |
3c67202d VZ |
1339 | // raw access to string memory |
1340 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 1341 | // only works if the data of this string is not shared |
e87b7833 | 1342 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } |
3c67202d | 1343 | // minimize the string's memory |
dd1eaa89 | 1344 | // only works if the data of this string is not shared |
b1801e0e | 1345 | bool Shrink(); |
8f93a29f | 1346 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
d8a4b666 VS |
1347 | // These are deprecated, use wxStringBuffer or wxStringBufferLength instead |
1348 | // | |
3c67202d VZ |
1349 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
1350 | // a.s.a.p. to put string back in a reasonable state! | |
d8a4b666 | 1351 | wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) ); |
3c67202d | 1352 | // call this immediately after GetWriteBuf() has been used |
d8a4b666 VS |
1353 | wxDEPRECATED( void UngetWriteBuf() ); |
1354 | wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); | |
8f93a29f | 1355 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8 |
c801d85f | 1356 | |
77ffb593 | 1357 | // wxWidgets version 1 compatibility functions |
3c67202d VZ |
1358 | |
1359 | // use Mid() | |
1360 | wxString SubString(size_t from, size_t to) const | |
1361 | { return Mid(from, (to - from + 1)); } | |
1362 | // values for second parameter of CompareTo function | |
c801d85f | 1363 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 1364 | // values for first parameter of Strip function |
c801d85f | 1365 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 1366 | |
c9f78968 | 1367 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
7357f981 GD |
1368 | // use Printf() |
1369 | // (take 'this' into account in attribute parameter count) | |
c9f78968 VS |
1370 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; |
1371 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
1372 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
c801d85f | 1373 | |
3c67202d | 1374 | // use Cmp() |
2bb67b80 | 1375 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 1376 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 1377 | |
8f93a29f | 1378 | // use length() |
e87b7833 | 1379 | size_t Length() const { return length(); } |
3c67202d | 1380 | // Count the number of characters |
c9f78968 | 1381 | int Freq(wxUniChar ch) const; |
3c67202d | 1382 | // use MakeLower |
c801d85f | 1383 | void LowerCase() { MakeLower(); } |
3c67202d | 1384 | // use MakeUpper |
c801d85f | 1385 | void UpperCase() { MakeUpper(); } |
3c67202d | 1386 | // use Trim except that it doesn't change this string |
c801d85f KB |
1387 | wxString Strip(stripType w = trailing) const; |
1388 | ||
3c67202d | 1389 | // use Find (more general variants not yet supported) |
2bb67b80 | 1390 | size_t Index(const wxChar* psz) const { return Find(psz); } |
c9f78968 | 1391 | size_t Index(wxUniChar ch) const { return Find(ch); } |
3c67202d | 1392 | // use Truncate |
c801d85f | 1393 | wxString& Remove(size_t pos) { return Truncate(pos); } |
e87b7833 | 1394 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } |
c801d85f | 1395 | |
e87b7833 MB |
1396 | wxString& Remove(size_t nStart, size_t nLen) |
1397 | { return (wxString&)erase( nStart, nLen ); } | |
dd1eaa89 | 1398 | |
3c67202d | 1399 | // use Find() |
8f93a29f | 1400 | int First( wxUniChar ch ) const { return Find(ch); } |
8a540c88 | 1401 | int First( wxUniCharRef ch ) const { return Find(ch); } |
c9f78968 | 1402 | int First( char ch ) const { return Find(ch); } |
50e02008 | 1403 | int First( unsigned char ch ) const { return Find(ch); } |
c9f78968 | 1404 | int First( wchar_t ch ) const { return Find(ch); } |
2bb67b80 | 1405 | int First( const wxChar* psz ) const { return Find(psz); } |
8a540c88 | 1406 | int First( const wxString& str ) const { return Find(str); } |
8f93a29f | 1407 | int Last( wxUniChar ch ) const { return Find(ch, true); } |
d775fa82 | 1408 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } |
c801d85f | 1409 | |
5a8231ef WS |
1410 | // use empty() |
1411 | bool IsNull() const { return empty(); } | |
c801d85f | 1412 | |
3c67202d | 1413 | // std::string compatibility functions |
dd1eaa89 | 1414 | |
3c67202d VZ |
1415 | // take nLen chars starting at nPos |
1416 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
8f93a29f | 1417 | : m_impl(str.m_impl, nPos, nLen) { } |
f2a1b1bd | 1418 | // take all characters from first to last |
e87b7833 | 1419 | wxString(const_iterator first, const_iterator last) |
8f93a29f | 1420 | : m_impl(first, last) { } |
f2a1b1bd VZ |
1421 | wxString(const char *first, const char *last) |
1422 | { | |
1423 | SubstrBufFromMB str(ImplStr(first, last - first)); | |
1424 | m_impl.assign(str.data, str.len); | |
1425 | } | |
1426 | wxString(const wchar_t *first, const wchar_t *last) | |
1427 | { | |
1428 | SubstrBufFromWC str(ImplStr(first, last - first)); | |
1429 | m_impl.assign(str.data, str.len); | |
1430 | } | |
5460b9e3 | 1431 | |
3c67202d | 1432 | // lib.string.modifiers |
3c67202d VZ |
1433 | // append elements str[pos], ..., str[pos+n] |
1434 | wxString& append(const wxString& str, size_t pos, size_t n) | |
8f93a29f VS |
1435 | { |
1436 | size_t from, len; | |
1437 | str.PosLenToImpl(pos, n, &from, &len); | |
1438 | m_impl.append(str.m_impl, from, len); | |
1439 | return *this; | |
1440 | } | |
e87b7833 MB |
1441 | // append a string |
1442 | wxString& append(const wxString& str) | |
8f93a29f | 1443 | { m_impl.append(str.m_impl); return *this; } |
c9f78968 | 1444 | wxString& append(const wxCStrData& str) |
8f93a29f | 1445 | { m_impl.append(str.AsString().m_impl); return *this; } |
3c67202d | 1446 | // append first n (or all if n == npos) characters of sz |
8f93a29f VS |
1447 | wxString& append(const char *sz) |
1448 | { m_impl.append(ImplStr(sz)); return *this; } | |
1449 | wxString& append(const wchar_t *sz) | |
1450 | { m_impl.append(ImplStr(sz)); return *this; } | |
1451 | wxString& append(const char *sz, size_t n) | |
1452 | { | |
1453 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1454 | m_impl.append(str.data, str.len); | |
1455 | return *this; | |
1456 | } | |
1457 | wxString& append(const wchar_t *sz, size_t n) | |
1458 | { | |
1459 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1460 | m_impl.append(str.data, str.len); | |
1461 | return *this; | |
1462 | } | |
3c67202d | 1463 | // append n copies of ch |
c9f78968 | 1464 | wxString& append(size_t n, wxUniChar ch) |
8f93a29f VS |
1465 | { |
1466 | #if wxUSE_UNICODE_UTF8 | |
1467 | if ( !ch.IsAscii() ) | |
1468 | m_impl.append(EncodeNChars(n, ch)); | |
1469 | else | |
1470 | #endif | |
1471 | m_impl.append(n, (wxStringCharType)ch); | |
1472 | return *this; | |
1473 | } | |
e87b7833 MB |
1474 | // append from first to last |
1475 | wxString& append(const_iterator first, const_iterator last) | |
8f93a29f | 1476 | { m_impl.append(first, last); return *this; } |
f2a1b1bd VZ |
1477 | wxString& append(const char *first, const char *last) |
1478 | { return append(first, last - first); } | |
1479 | wxString& append(const wchar_t *first, const wchar_t *last) | |
1480 | { return append(first, last - first); } | |
3c67202d VZ |
1481 | |
1482 | // same as `this_string = str' | |
735d1db6 | 1483 | wxString& assign(const wxString& str) |
8f93a29f | 1484 | { m_impl = str.m_impl; return *this; } |
11aac4ba VS |
1485 | wxString& assign(const wxString& str, size_t len) |
1486 | { | |
1487 | m_impl.assign(str.m_impl, 0, str.LenToImpl(len)); | |
1488 | return *this; | |
1489 | } | |
3c67202d VZ |
1490 | // same as ` = str[pos..pos + n] |
1491 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
8f93a29f VS |
1492 | { |
1493 | size_t from, len; | |
1494 | str.PosLenToImpl(pos, n, &from, &len); | |
1495 | m_impl.assign(str.m_impl, from, len); | |
1496 | return *this; | |
1497 | } | |
3c67202d | 1498 | // same as `= first n (or all if n == npos) characters of sz' |
8f93a29f VS |
1499 | wxString& assign(const char *sz) |
1500 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1501 | wxString& assign(const wchar_t *sz) | |
1502 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1503 | wxString& assign(const char *sz, size_t n) | |
1504 | { | |
1505 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1506 | m_impl.assign(str.data, str.len); | |
1507 | return *this; | |
1508 | } | |
1509 | wxString& assign(const wchar_t *sz, size_t n) | |
1510 | { | |
1511 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1512 | m_impl.assign(str.data, str.len); | |
1513 | return *this; | |
1514 | } | |
3c67202d | 1515 | // same as `= n copies of ch' |
c9f78968 | 1516 | wxString& assign(size_t n, wxUniChar ch) |
8f93a29f VS |
1517 | { |
1518 | #if wxUSE_UNICODE_UTF8 | |
1519 | if ( !ch.IsAscii() ) | |
1520 | m_impl.assign(EncodeNChars(n, ch)); | |
1521 | else | |
1522 | #endif | |
1523 | m_impl.assign(n, (wxStringCharType)ch); | |
1524 | return *this; | |
1525 | } | |
11aac4ba VS |
1526 | |
1527 | wxString& assign(size_t n, wxUniCharRef ch) | |
1528 | { return assign(n, wxUniChar(ch)); } | |
1529 | wxString& assign(size_t n, char ch) | |
1530 | { return assign(n, wxUniChar(ch)); } | |
1531 | wxString& assign(size_t n, unsigned char ch) | |
1532 | { return assign(n, wxUniChar(ch)); } | |
1533 | wxString& assign(size_t n, wchar_t ch) | |
1534 | { return assign(n, wxUniChar(ch)); } | |
1535 | ||
e87b7833 MB |
1536 | // assign from first to last |
1537 | wxString& assign(const_iterator first, const_iterator last) | |
8f93a29f | 1538 | { m_impl.assign(first, last); return *this; } |
f2a1b1bd VZ |
1539 | wxString& assign(const char *first, const char *last) |
1540 | { return assign(first, last - first); } | |
1541 | wxString& assign(const wchar_t *first, const wchar_t *last) | |
1542 | { return assign(first, last - first); } | |
e87b7833 MB |
1543 | |
1544 | // string comparison | |
8f93a29f | 1545 | int compare(const wxString& str) const; |
e87b7833 | 1546 | // comparison with a substring |
8f93a29f | 1547 | int compare(size_t nStart, size_t nLen, const wxString& str) const; |
e87b7833 MB |
1548 | // comparison of 2 substrings |
1549 | int compare(size_t nStart, size_t nLen, | |
8f93a29f | 1550 | const wxString& str, size_t nStart2, size_t nLen2) const; |
e87b7833 | 1551 | // just like strcmp() |
8f93a29f VS |
1552 | int compare(const char* sz) const; |
1553 | int compare(const wchar_t* sz) const; | |
e87b7833 MB |
1554 | // substring comparison with first nCount characters of sz |
1555 | int compare(size_t nStart, size_t nLen, | |
8f93a29f VS |
1556 | const char* sz, size_t nCount = npos) const; |
1557 | int compare(size_t nStart, size_t nLen, | |
1558 | const wchar_t* sz, size_t nCount = npos) const; | |
3c67202d VZ |
1559 | |
1560 | // insert another string | |
e87b7833 | 1561 | wxString& insert(size_t nPos, const wxString& str) |
8f93a29f | 1562 | { insert(begin() + nPos, str.begin(), str.end()); return *this; } |
3c67202d VZ |
1563 | // insert n chars of str starting at nStart (in str) |
1564 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
8f93a29f VS |
1565 | { |
1566 | size_t from, len; | |
1567 | str.PosLenToImpl(nStart, n, &from, &len); | |
1568 | m_impl.insert(PosToImpl(nPos), str.m_impl, from, len); | |
1569 | return *this; | |
1570 | } | |
3c67202d | 1571 | // insert first n (or all if n == npos) characters of sz |
8f93a29f VS |
1572 | wxString& insert(size_t nPos, const char *sz) |
1573 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1574 | wxString& insert(size_t nPos, const wchar_t *sz) | |
1575 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1576 | wxString& insert(size_t nPos, const char *sz, size_t n) | |
1577 | { | |
1578 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1579 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1580 | return *this; | |
1581 | } | |
1582 | wxString& insert(size_t nPos, const wchar_t *sz, size_t n) | |
1583 | { | |
1584 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1585 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1586 | return *this; | |
1587 | } | |
3c67202d | 1588 | // insert n copies of ch |
c9f78968 | 1589 | wxString& insert(size_t nPos, size_t n, wxUniChar ch) |
8f93a29f VS |
1590 | { |
1591 | #if wxUSE_UNICODE_UTF8 | |
1592 | if ( !ch.IsAscii() ) | |
1593 | m_impl.insert(begin() + nPos, EncodeNChars(n, ch)); | |
1594 | else | |
1595 | #endif | |
1596 | m_impl.insert(begin() + nPos, n, (wxStringCharType)ch); | |
1597 | return *this; | |
1598 | } | |
c9f78968 | 1599 | iterator insert(iterator it, wxUniChar ch) |
8f93a29f | 1600 | { return iterator(m_impl.insert(it, EncodeChar(ch))); } |
e87b7833 | 1601 | void insert(iterator it, const_iterator first, const_iterator last) |
8f93a29f | 1602 | { m_impl.insert(it, first, last); } |
f2a1b1bd VZ |
1603 | void insert(iterator it, const char *first, const char *last) |
1604 | { insert(it - begin(), first, last - first); } | |
1605 | void append(iterator it, const wchar_t *first, const wchar_t *last) | |
1606 | { insert(it - begin(), first, last - first); } | |
c9f78968 | 1607 | void insert(iterator it, size_type n, wxUniChar ch) |
8f93a29f VS |
1608 | { |
1609 | #if wxUSE_UNICODE_UTF8 | |
1610 | if ( !ch.IsAscii() ) | |
1611 | m_impl.insert(it, EncodeNChars(n, ch)); | |
1612 | else | |
1613 | #endif | |
1614 | m_impl.insert(it, n, (wxStringCharType)ch); | |
1615 | } | |
3c67202d VZ |
1616 | |
1617 | // delete characters from nStart to nStart + nLen | |
e87b7833 | 1618 | wxString& erase(size_type pos = 0, size_type n = npos) |
8f93a29f VS |
1619 | { |
1620 | size_t from, len; | |
1621 | PosLenToImpl(pos, n, &from, &len); | |
1622 | m_impl.erase(from, len); | |
1623 | return *this; | |
1624 | } | |
f2a1b1bd | 1625 | // delete characters from first up to last |
e87b7833 | 1626 | iterator erase(iterator first, iterator last) |
8f93a29f | 1627 | { return iterator(m_impl.erase(first, last)); } |
e87b7833 | 1628 | iterator erase(iterator first) |
8f93a29f | 1629 | { return iterator(m_impl.erase(first)); } |
e87b7833 MB |
1630 | |
1631 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
1632 | void clear() { erase(); } | |
8f93a29f VS |
1633 | #else |
1634 | void clear() { m_impl.clear(); } | |
e87b7833 | 1635 | #endif |
3c67202d VZ |
1636 | |
1637 | // replaces the substring of length nLen starting at nStart | |
8f93a29f VS |
1638 | wxString& replace(size_t nStart, size_t nLen, const char* sz) |
1639 | { | |
1640 | size_t from, len; | |
1641 | PosLenToImpl(nStart, nLen, &from, &len); | |
1642 | m_impl.replace(from, len, ImplStr(sz)); | |
1643 | return *this; | |
1644 | } | |
1645 | wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz) | |
1646 | { | |
1647 | size_t from, len; | |
1648 | PosLenToImpl(nStart, nLen, &from, &len); | |
1649 | m_impl.replace(from, len, ImplStr(sz)); | |
1650 | return *this; | |
1651 | } | |
e87b7833 MB |
1652 | // replaces the substring of length nLen starting at nStart |
1653 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
8f93a29f VS |
1654 | { |
1655 | size_t from, len; | |
1656 | PosLenToImpl(nStart, nLen, &from, &len); | |
1657 | m_impl.replace(from, len, str.m_impl); | |
1658 | return *this; | |
1659 | } | |
3c67202d | 1660 | // replaces the substring with nCount copies of ch |
c9f78968 | 1661 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) |
8f93a29f VS |
1662 | { |
1663 | size_t from, len; | |
1664 | PosLenToImpl(nStart, nLen, &from, &len); | |
1665 | #if wxUSE_UNICODE_UTF8 | |
1666 | if ( !ch.IsAscii() ) | |
1667 | m_impl.replace(from, len, EncodeNChars(nCount, ch)); | |
1668 | else | |
1669 | #endif | |
1670 | m_impl.replace(from, len, nCount, (wxStringCharType)ch); | |
1671 | return *this; | |
1672 | } | |
3c67202d VZ |
1673 | // replaces a substring with another substring |
1674 | wxString& replace(size_t nStart, size_t nLen, | |
e87b7833 | 1675 | const wxString& str, size_t nStart2, size_t nLen2) |
8f93a29f VS |
1676 | { |
1677 | size_t from, len; | |
1678 | PosLenToImpl(nStart, nLen, &from, &len); | |
1679 | ||
1680 | size_t from2, len2; | |
1681 | str.PosLenToImpl(nStart2, nLen2, &from2, &len2); | |
1682 | ||
1683 | m_impl.replace(from, len, str.m_impl, from2, len2); | |
1684 | return *this; | |
1685 | } | |
e87b7833 | 1686 | // replaces the substring with first nCount chars of sz |
fb20fa43 | 1687 | wxString& replace(size_t nStart, size_t nLen, |
8f93a29f VS |
1688 | const char* sz, size_t nCount) |
1689 | { | |
1690 | size_t from, len; | |
1691 | PosLenToImpl(nStart, nLen, &from, &len); | |
1692 | ||
1693 | SubstrBufFromMB str(ImplStr(sz, nCount)); | |
1694 | ||
1695 | m_impl.replace(from, len, str.data, str.len); | |
1696 | return *this; | |
1697 | } | |
1698 | wxString& replace(size_t nStart, size_t nLen, | |
1699 | const wchar_t* sz, size_t nCount) | |
1700 | { | |
1701 | size_t from, len; | |
1702 | PosLenToImpl(nStart, nLen, &from, &len); | |
1703 | ||
1704 | SubstrBufFromWC str(ImplStr(sz, nCount)); | |
1705 | ||
1706 | m_impl.replace(from, len, str.data, str.len); | |
1707 | return *this; | |
1708 | } | |
11aac4ba VS |
1709 | wxString& replace(size_t nStart, size_t nLen, |
1710 | const wxString& s, size_t nCount) | |
1711 | { | |
1712 | size_t from, len; | |
1713 | PosLenToImpl(nStart, nLen, &from, &len); | |
1714 | m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount)); | |
1715 | return *this; | |
1716 | } | |
1717 | ||
8f93a29f VS |
1718 | wxString& replace(iterator first, iterator last, const char* s) |
1719 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
1720 | wxString& replace(iterator first, iterator last, const wchar_t* s) | |
1721 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
1722 | wxString& replace(iterator first, iterator last, const char* s, size_type n) | |
1723 | { | |
1724 | SubstrBufFromMB str(ImplStr(s, n)); | |
1725 | m_impl.replace(first, last, str.data, str.len); | |
1726 | return *this; | |
1727 | } | |
1728 | wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n) | |
1729 | { | |
1730 | SubstrBufFromWC str(ImplStr(s, n)); | |
1731 | m_impl.replace(first, last, str.data, str.len); | |
1732 | return *this; | |
1733 | } | |
e87b7833 | 1734 | wxString& replace(iterator first, iterator last, const wxString& s) |
8f93a29f VS |
1735 | { m_impl.replace(first, last, s.m_impl); return *this; } |
1736 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch) | |
1737 | { | |
1738 | #if wxUSE_UNICODE_UTF8 | |
1739 | if ( !ch.IsAscii() ) | |
1740 | m_impl.replace(first, last, EncodeNChars(n, ch)); | |
1741 | else | |
1742 | #endif | |
1743 | m_impl.replace(first, last, n, (wxStringCharType)ch); | |
1744 | return *this; | |
1745 | } | |
e87b7833 MB |
1746 | wxString& replace(iterator first, iterator last, |
1747 | const_iterator first1, const_iterator last1) | |
8f93a29f | 1748 | { m_impl.replace(first, last, first1, last1); return *this; } |
f2a1b1bd VZ |
1749 | wxString& replace(iterator first, iterator last, |
1750 | const char *first1, const char *last1) | |
1751 | { replace(first, last, first1, last1 - first1); return *this; } | |
1752 | wxString& replace(iterator first, iterator last, | |
1753 | const wchar_t *first1, const wchar_t *last1) | |
1754 | { replace(first, last, first1, last1 - first1); return *this; } | |
8f93a29f VS |
1755 | |
1756 | // swap two strings | |
1757 | void swap(wxString& str) | |
1758 | { m_impl.swap(str.m_impl); } | |
1759 | ||
1760 | // find a substring | |
1761 | size_t find(const wxString& str, size_t nStart = 0) const | |
1762 | { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); } | |
1763 | ||
1764 | // find first n characters of sz | |
1765 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const | |
1766 | { | |
1767 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1768 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
1769 | } | |
1770 | size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const | |
1771 | { | |
1772 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1773 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
1774 | } | |
1775 | ||
1776 | // find the first occurence of character ch after nStart | |
1777 | size_t find(wxUniChar ch, size_t nStart = 0) const | |
1778 | { return PosFromImpl(m_impl.find(EncodeChar(ch), PosToImpl(nStart))); } | |
1779 | size_t find(wxUniCharRef ch, size_t nStart = 0) const | |
1780 | { return find(wxUniChar(ch), nStart); } | |
1781 | size_t find(char ch, size_t nStart = 0) const | |
1782 | { return find(wxUniChar(ch), nStart); } | |
50e02008 VS |
1783 | size_t find(unsigned char ch, size_t nStart = 0) const |
1784 | { return find(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1785 | size_t find(wchar_t ch, size_t nStart = 0) const |
1786 | { return find(wxUniChar(ch), nStart); } | |
1787 | ||
1788 | // rfind() family is exactly like find() but works right to left | |
1789 | ||
1790 | // as find, but from the end | |
1791 | size_t rfind(const wxString& str, size_t nStart = npos) const | |
1792 | { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); } | |
1793 | ||
1794 | // as find, but from the end | |
1795 | size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const | |
1796 | { | |
1797 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1798 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
1799 | } | |
1800 | size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const | |
1801 | { | |
1802 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1803 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
1804 | } | |
1805 | // as find, but from the end | |
1806 | size_t rfind(wxUniChar ch, size_t nStart = npos) const | |
1807 | { return PosFromImpl(m_impl.rfind(EncodeChar(ch), PosToImpl(nStart))); } | |
1808 | size_t rfind(wxUniCharRef ch, size_t nStart = npos) const | |
1809 | { return rfind(wxUniChar(ch), nStart); } | |
1810 | size_t rfind(char ch, size_t nStart = npos) const | |
1811 | { return rfind(wxUniChar(ch), nStart); } | |
50e02008 VS |
1812 | size_t rfind(unsigned char ch, size_t nStart = npos) const |
1813 | { return rfind(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1814 | size_t rfind(wchar_t ch, size_t nStart = npos) const |
1815 | { return rfind(wxUniChar(ch), nStart); } | |
1816 | ||
1817 | // find first/last occurence of any character (not) in the set: | |
1818 | #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
1819 | // FIXME-UTF8: this is not entirely correct, because it doesn't work if | |
1820 | // sizeof(wchar_t)==2 and surrogates are present in the string; | |
1821 | // should we care? Probably not. | |
1822 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
a962cdf4 | 1823 | { return m_impl.find_first_of(str.m_impl, nStart); } |
8f93a29f VS |
1824 | size_t find_first_of(const char* sz, size_t nStart = 0) const |
1825 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
1826 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const | |
1827 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
a962cdf4 | 1828 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 1829 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1830 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f VS |
1831 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
1832 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
1833 | { return m_impl.find_first_of((wxChar)c, nStart); } | |
1834 | ||
a962cdf4 VS |
1835 | size_t find_last_of(const wxString& str, size_t nStart = npos) const |
1836 | { return m_impl.find_last_of(str.m_impl, nStart); } | |
8f93a29f VS |
1837 | size_t find_last_of(const char* sz, size_t nStart = npos) const |
1838 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
1839 | size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const | |
1840 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
1841 | size_t find_last_of(const char* 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(const wchar_t* sz, size_t nStart, size_t n) const | |
1844 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
1845 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
1846 | { return m_impl.find_last_of((wxChar)c, nStart); } | |
1847 | ||
a962cdf4 | 1848 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const |
8f93a29f | 1849 | { return m_impl.find_first_not_of(str.m_impl, nStart); } |
a962cdf4 | 1850 | size_t find_first_not_of(const char* 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 wchar_t* sz, size_t nStart = 0) const |
8f93a29f | 1853 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1854 | size_t find_first_not_of(const char* 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(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 1857 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1858 | size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const |
8f93a29f VS |
1859 | { return m_impl.find_first_not_of((wxChar)c, nStart); } |
1860 | ||
a962cdf4 | 1861 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const |
8f93a29f | 1862 | { return m_impl.find_last_not_of(str.m_impl, nStart); } |
a962cdf4 | 1863 | size_t find_last_not_of(const char* 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 wchar_t* sz, size_t nStart = npos) const |
8f93a29f | 1866 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1867 | size_t find_last_not_of(const char* 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(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 1870 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1871 | size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const |
8f93a29f VS |
1872 | { return m_impl.find_last_not_of((wxChar)c, nStart); } |
1873 | #else | |
1874 | // we can't use std::string implementation in UTF-8 build, because the | |
1875 | // character sets would be interpreted wrongly: | |
1876 | ||
1877 | // as strpbrk() but starts at nStart, returns npos if not found | |
1878 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
1879 | { return find_first_of((const wxChar*)str.c_str(), nStart); } | |
1880 | // same as above | |
1881 | size_t find_first_of(const char* sz, size_t nStart = 0) const; | |
1882 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const; | |
1883 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const; | |
1884 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1885 | // same as find(char, size_t) | |
1886 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
1887 | { return find(c, nStart); } | |
1888 | // find the last (starting from nStart) char from str in this string | |
1889 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
1890 | { return find_last_of((const wxChar*)str.c_str(), nStart); } | |
1891 | // same as above | |
1892 | size_t find_last_of (const char* sz, size_t nStart = npos) const; | |
1893 | size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const; | |
1894 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const; | |
1895 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1896 | // same as above | |
1897 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
1898 | { return rfind(c, nStart); } | |
1899 | ||
1900 | // find first/last occurence of any character not in the set | |
1901 | ||
1902 | // as strspn() (starting from nStart), returns npos on failure | |
1903 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
1904 | { return find_first_not_of((const wxChar*)str.c_str(), nStart); } | |
1905 | // same as above | |
1906 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const; | |
1907 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const; | |
1908 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const; | |
1909 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1910 | // same as above | |
1911 | size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; | |
1912 | // as strcspn() | |
1913 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const | |
1914 | { return find_last_not_of((const wxChar*)str.c_str(), nStart); } | |
1915 | // same as above | |
1916 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const; | |
1917 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const; | |
1918 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const; | |
1919 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1920 | // same as above | |
1921 | size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; | |
1922 | #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not | |
1923 | ||
1924 | // provide char/wchar_t/wxUniCharRef overloads for char-finding functions | |
1925 | // above to resolve ambiguities: | |
1926 | size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const | |
1927 | { return find_first_of(wxUniChar(ch), nStart); } | |
1928 | size_t find_first_of(char ch, size_t nStart = 0) const | |
1929 | { return find_first_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1930 | size_t find_first_of(unsigned char ch, size_t nStart = 0) const |
1931 | { return find_first_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1932 | size_t find_first_of(wchar_t ch, size_t nStart = 0) const |
1933 | { return find_first_of(wxUniChar(ch), nStart); } | |
1934 | size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const | |
1935 | { return find_last_of(wxUniChar(ch), nStart); } | |
1936 | size_t find_last_of(char ch, size_t nStart = npos) const | |
1937 | { return find_last_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1938 | size_t find_last_of(unsigned char ch, size_t nStart = npos) const |
1939 | { return find_last_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1940 | size_t find_last_of(wchar_t ch, size_t nStart = npos) const |
1941 | { return find_last_of(wxUniChar(ch), nStart); } | |
1942 | size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const | |
1943 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
1944 | size_t find_first_not_of(char ch, size_t nStart = 0) const | |
1945 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1946 | size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const |
1947 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1948 | size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const |
1949 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
1950 | size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const | |
1951 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
1952 | size_t find_last_not_of(char ch, size_t nStart = npos) const | |
1953 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1954 | size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const |
1955 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1956 | size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const |
1957 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
3c67202d | 1958 | |
e87b7833 MB |
1959 | // string += string |
1960 | wxString& operator+=(const wxString& s) | |
8f93a29f | 1961 | { m_impl += s.m_impl; return *this; } |
e87b7833 | 1962 | // string += C string |
8f93a29f VS |
1963 | wxString& operator+=(const char *psz) |
1964 | { m_impl += ImplStr(psz); return *this; } | |
1965 | wxString& operator+=(const wchar_t *pwz) | |
1966 | { m_impl += ImplStr(pwz); return *this; } | |
c9f78968 | 1967 | wxString& operator+=(const wxCStrData& s) |
8f93a29f | 1968 | { m_impl += s.AsString().m_impl; return *this; } |
e87b7833 | 1969 | // string += char |
c9f78968 | 1970 | wxString& operator+=(wxUniChar ch) |
8f93a29f | 1971 | { m_impl += EncodeChar(ch); return *this; } |
c9f78968 | 1972 | wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } |
b7d40341 | 1973 | wxString& operator+=(int ch) { return *this += wxUniChar(ch); } |
c9f78968 | 1974 | wxString& operator+=(char ch) { return *this += wxUniChar(ch); } |
b77afb41 | 1975 | wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); } |
c9f78968 | 1976 | wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } |
d8a4b666 VS |
1977 | |
1978 | private: | |
8f93a29f | 1979 | #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
d8a4b666 | 1980 | // helpers for wxStringBuffer and wxStringBufferLength |
8f93a29f VS |
1981 | wxStringCharType *DoGetWriteBuf(size_t nLen) |
1982 | { return m_impl.DoGetWriteBuf(nLen); } | |
1983 | void DoUngetWriteBuf() | |
1984 | { m_impl.DoUngetWriteBuf(); } | |
1985 | void DoUngetWriteBuf(size_t nLen) | |
1986 | { m_impl.DoUngetWriteBuf(nLen); } | |
d8a4b666 VS |
1987 | |
1988 | friend class WXDLLIMPEXP_BASE wxStringBuffer; | |
1989 | friend class WXDLLIMPEXP_BASE wxStringBufferLength; | |
8f93a29f | 1990 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
c9f78968 VS |
1991 | |
1992 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1993 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
1994 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
1995 | #endif | |
8f93a29f VS |
1996 | |
1997 | #if !wxUSE_STL_BASED_WXSTRING | |
1998 | // check string's data validity | |
1999 | bool IsValid() const { return m_impl.GetStringData()->IsValid(); } | |
2000 | #endif | |
2001 | ||
2002 | private: | |
2003 | wxStringImpl m_impl; | |
c801d85f KB |
2004 | }; |
2005 | ||
c9f78968 VS |
2006 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
2007 | #pragma warning (default:4275) | |
2008 | #endif | |
2009 | ||
a962cdf4 VS |
2010 | // string iterator operators that satisfy STL Random Access Iterator |
2011 | // requirements: | |
2012 | inline wxString::iterator operator+(int n, wxString::iterator i) | |
2013 | { return i + n; } | |
2014 | inline wxString::iterator operator+(size_t n, wxString::iterator i) | |
2015 | { return i + n; } | |
2016 | inline wxString::const_iterator operator+(int n, wxString::const_iterator i) | |
2017 | { return i + n; } | |
2018 | inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i) | |
2019 | { return i + n; } | |
2020 | inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i) | |
2021 | { return i + n; } | |
2022 | inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i) | |
2023 | { return i + n; } | |
2024 | inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i) | |
2025 | { return i + n; } | |
2026 | inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i) | |
2027 | { return i + n; } | |
2028 | ||
1fc8cfbd VZ |
2029 | // notice that even though for many compilers the friend declarations above are |
2030 | // enough, from the point of view of C++ standard we must have the declarations | |
2031 | // here as friend ones are not injected in the enclosing namespace and without | |
2032 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
c9f78968 | 2033 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
8f93a29f VS |
2034 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz); |
2035 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz); | |
2036 | wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string); | |
2037 | wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string); | |
1fc8cfbd | 2038 | |
c9f78968 VS |
2039 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
2040 | wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
2041 | ||
2042 | inline wxString operator+(const wxString& string, wxUniCharRef ch) | |
2043 | { return string + (wxUniChar)ch; } | |
2044 | inline wxString operator+(const wxString& string, char ch) | |
2045 | { return string + wxUniChar(ch); } | |
2046 | inline wxString operator+(const wxString& string, wchar_t ch) | |
2047 | { return string + wxUniChar(ch); } | |
2048 | inline wxString operator+(wxUniCharRef ch, const wxString& string) | |
2049 | { return (wxUniChar)ch + string; } | |
2050 | inline wxString operator+(char ch, const wxString& string) | |
2051 | { return wxUniChar(ch) + string; } | |
2052 | inline wxString operator+(wchar_t ch, const wxString& string) | |
2053 | { return wxUniChar(ch) + string; } | |
2054 | ||
b7146cbe | 2055 | |
992527a5 | 2056 | #if wxUSE_STL_BASED_WXSTRING |
07170120 | 2057 | // return an empty wxString (not very useful with wxUSE_STL == 1) |
12f99210 | 2058 | inline const wxString wxGetEmptyString() { return wxString(); } |
992527a5 | 2059 | #else // !wxUSE_STL_BASED_WXSTRING |
07170120 VZ |
2060 | // return an empty wxString (more efficient than wxString() here) |
2061 | inline const wxString& wxGetEmptyString() | |
2062 | { | |
2063 | return *(wxString *)&wxEmptyString; | |
2064 | } | |
992527a5 | 2065 | #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING |
07170120 | 2066 | |
1d218550 VZ |
2067 | // ---------------------------------------------------------------------------- |
2068 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
2069 | // ---------------------------------------------------------------------------- | |
2070 | ||
8f93a29f | 2071 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 |
941b78cc MB |
2072 | |
2073 | class WXDLLIMPEXP_BASE wxStringBuffer | |
2074 | { | |
2075 | public: | |
2076 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
e87b7833 | 2077 | : m_str(str), m_buf(lenWanted) |
941b78cc MB |
2078 | { } |
2079 | ||
fe5fc682 | 2080 | ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); } |
941b78cc MB |
2081 | |
2082 | operator wxChar*() { return m_buf.data(); } | |
2083 | ||
2084 | private: | |
2085 | wxString& m_str; | |
2086 | #if wxUSE_UNICODE | |
2087 | wxWCharBuffer m_buf; | |
2088 | #else | |
2089 | wxCharBuffer m_buf; | |
2090 | #endif | |
941b78cc MB |
2091 | |
2092 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
2093 | }; | |
2094 | ||
2095 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
2096 | { | |
2097 | public: | |
2098 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2099 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
2100 | { } | |
2101 | ||
2102 | ~wxStringBufferLength() | |
2103 | { | |
2104 | wxASSERT(m_lenSet); | |
2105 | m_str.assign(m_buf.data(), m_len); | |
2106 | } | |
2107 | ||
2108 | operator wxChar*() { return m_buf.data(); } | |
2109 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2110 | ||
2111 | private: | |
2112 | wxString& m_str; | |
2113 | #if wxUSE_UNICODE | |
2114 | wxWCharBuffer m_buf; | |
2115 | #else | |
2116 | wxCharBuffer m_buf; | |
2117 | #endif | |
2118 | size_t m_len; | |
2119 | bool m_lenSet; | |
2120 | ||
2121 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2122 | }; | |
2123 | ||
8f93a29f | 2124 | #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
941b78cc | 2125 | |
bddd7a8d | 2126 | class WXDLLIMPEXP_BASE wxStringBuffer |
1d218550 VZ |
2127 | { |
2128 | public: | |
2129 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
b1801e0e | 2130 | : m_str(str), m_buf(NULL) |
d8a4b666 | 2131 | { m_buf = m_str.DoGetWriteBuf(lenWanted); } |
e015c2a3 | 2132 | |
d8a4b666 | 2133 | ~wxStringBuffer() { m_str.DoUngetWriteBuf(); } |
e015c2a3 | 2134 | |
1d218550 | 2135 | operator wxChar*() const { return m_buf; } |
e015c2a3 | 2136 | |
1d218550 VZ |
2137 | private: |
2138 | wxString& m_str; | |
2139 | wxChar *m_buf; | |
e015c2a3 VZ |
2140 | |
2141 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1d218550 VZ |
2142 | }; |
2143 | ||
941b78cc MB |
2144 | class WXDLLIMPEXP_BASE wxStringBufferLength |
2145 | { | |
2146 | public: | |
2147 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2148 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
4055ed82 | 2149 | { |
d8a4b666 | 2150 | m_buf = m_str.DoGetWriteBuf(lenWanted); |
494b978f RN |
2151 | wxASSERT(m_buf != NULL); |
2152 | } | |
941b78cc MB |
2153 | |
2154 | ~wxStringBufferLength() | |
2155 | { | |
2156 | wxASSERT(m_lenSet); | |
d8a4b666 | 2157 | m_str.DoUngetWriteBuf(m_len); |
941b78cc MB |
2158 | } |
2159 | ||
2160 | operator wxChar*() const { return m_buf; } | |
2161 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2162 | ||
2163 | private: | |
2164 | wxString& m_str; | |
2165 | wxChar *m_buf; | |
2166 | size_t m_len; | |
2167 | bool m_lenSet; | |
2168 | ||
2169 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2170 | }; | |
2171 | ||
8f93a29f | 2172 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
941b78cc | 2173 | |
c801d85f | 2174 | // --------------------------------------------------------------------------- |
3c67202d | 2175 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 2176 | // --------------------------------------------------------------------------- |
f33fee2a | 2177 | |
fb52c2b6 VZ |
2178 | #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0 |
2179 | ||
2180 | wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) | |
2181 | ||
2182 | #undef wxCMP_WXCHAR_STRING | |
2183 | ||
2184 | // note that there is an optimization in operator==() and !=(): we (quickly) | |
2185 | // checks the strings length first, before comparing their data | |
0cbe5ee3 VZ |
2186 | inline bool operator==(const wxString& s1, const wxString& s2) |
2187 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
0cbe5ee3 VZ |
2188 | inline bool operator!=(const wxString& s1, const wxString& s2) |
2189 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
0cbe5ee3 VZ |
2190 | inline bool operator< (const wxString& s1, const wxString& s2) |
2191 | { return s1.Cmp(s2) < 0; } | |
0cbe5ee3 VZ |
2192 | inline bool operator> (const wxString& s1, const wxString& s2) |
2193 | { return s1.Cmp(s2) > 0; } | |
0cbe5ee3 VZ |
2194 | inline bool operator<=(const wxString& s1, const wxString& s2) |
2195 | { return s1.Cmp(s2) <= 0; } | |
0cbe5ee3 VZ |
2196 | inline bool operator>=(const wxString& s1, const wxString& s2) |
2197 | { return s1.Cmp(s2) >= 0; } | |
3c67202d | 2198 | |
5ca07d0f OK |
2199 | #if wxUSE_UNICODE |
2200 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 2201 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 2202 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 2203 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
2204 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
2205 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
2206 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
2207 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
0cbe5ee3 | 2208 | #else // !wxUSE_UNICODE |
5ca07d0f | 2209 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 2210 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 2211 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 2212 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
2213 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
2214 | { return (s1.Cmp((const char *)s2) != 0); } | |
2215 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
2216 | { return (s2.Cmp((const char *)s1) != 0); } | |
0cbe5ee3 | 2217 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
5ca07d0f | 2218 | |
028a2b5d | 2219 | #if wxUSE_UNICODE |
6d56eb5c | 2220 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 2221 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 2222 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 2223 | { return (const wchar_t *)buf + string; } |
0cbe5ee3 | 2224 | #else // !wxUSE_UNICODE |
6d56eb5c | 2225 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 2226 | { return string + (const char *)buf; } |
6d56eb5c | 2227 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 2228 | { return (const char *)buf + string; } |
0cbe5ee3 | 2229 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
f04f3991 | 2230 | |
a962cdf4 | 2231 | // comparison with char |
c9f78968 VS |
2232 | inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } |
2233 | inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } | |
2234 | inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } | |
2235 | inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } | |
2236 | inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } | |
2237 | inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } | |
2238 | inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } | |
2239 | inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } | |
2240 | inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } | |
2241 | inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } | |
2242 | inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } | |
2243 | inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } | |
2244 | inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } | |
2245 | inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } | |
2246 | inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } | |
2247 | inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } | |
2248 | inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } | |
2249 | inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } | |
e51b17a1 | 2250 | |
d7330233 VS |
2251 | // comparison with C string in Unicode build |
2252 | #if wxUSE_UNICODE | |
fb52c2b6 VZ |
2253 | |
2254 | #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s | |
2255 | ||
2256 | wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) | |
2257 | ||
2258 | #undef wxCMP_CHAR_STRING | |
2259 | ||
d7330233 VS |
2260 | #endif // wxUSE_UNICODE |
2261 | ||
fb52c2b6 VZ |
2262 | // we also need to provide the operators for comparison with wxCStrData to |
2263 | // resolve ambiguity between operator(const wxChar *,const wxString &) and | |
2264 | // operator(const wxChar *, const wxChar *) for "p == s.c_str()" | |
2265 | // | |
2266 | // notice that these are (shallow) pointer comparisons, not (deep) string ones | |
2267 | #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() | |
2268 | #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() | |
2269 | ||
11aac4ba VS |
2270 | wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) |
2271 | wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) | |
fb52c2b6 VZ |
2272 | |
2273 | #undef wxCMP_CHAR_CSTRDATA | |
2274 | #undef wxCMP_WCHAR_CSTRDATA | |
2275 | ||
c801d85f | 2276 | // --------------------------------------------------------------------------- |
3c67202d | 2277 | // Implementation only from here until the end of file |
c801d85f KB |
2278 | // --------------------------------------------------------------------------- |
2279 | ||
e87b7833 | 2280 | #if wxUSE_STD_IOSTREAM |
c801d85f | 2281 | |
65f19af1 | 2282 | #include "wx/iosfwrap.h" |
c801d85f | 2283 | |
bddd7a8d | 2284 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); |
c9f78968 | 2285 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); |
04abe4bc VS |
2286 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&); |
2287 | #ifndef __BORLANDC__ | |
2288 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&); | |
2289 | #endif | |
c801d85f | 2290 | |
3c67202d | 2291 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 2292 | |
c9f78968 VS |
2293 | // --------------------------------------------------------------------------- |
2294 | // wxCStrData implementation | |
2295 | // --------------------------------------------------------------------------- | |
2296 | ||
2297 | inline wxCStrData::wxCStrData(char *buf) | |
2298 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2299 | inline wxCStrData::wxCStrData(wchar_t *buf) | |
2300 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2301 | ||
2302 | inline wxCStrData::~wxCStrData() | |
2303 | { | |
2304 | if ( m_owned ) | |
2305 | delete m_str; | |
2306 | } | |
2307 | ||
11aac4ba VS |
2308 | inline wxCStrData::operator bool() const |
2309 | { | |
2310 | return !m_str->empty(); | |
2311 | }; | |
2312 | ||
2313 | // simple cases for AsChar() and AsWChar(), the complicated ones are | |
2314 | // in string.cpp | |
2315 | #if wxUSE_UNICODE_WCHAR | |
c9f78968 | 2316 | inline const wchar_t* wxCStrData::AsWChar() const |
11aac4ba VS |
2317 | { |
2318 | return m_str->wx_str() + m_offset; | |
2319 | } | |
2320 | #endif // wxUSE_UNICODE_WCHAR | |
2321 | ||
2322 | #if !wxUSE_UNICODE | |
c9f78968 | 2323 | inline const char* wxCStrData::AsChar() const |
c9f78968 | 2324 | { |
a962cdf4 | 2325 | return m_str->wx_str() + m_offset; |
c9f78968 | 2326 | } |
11aac4ba | 2327 | #endif // !wxUSE_UNICODE |
c9f78968 VS |
2328 | |
2329 | inline wxString wxCStrData::AsString() const | |
2330 | { | |
2331 | if ( m_offset == 0 ) | |
2332 | return *m_str; | |
2333 | else | |
2334 | return m_str->Mid(m_offset); | |
2335 | } | |
2336 | ||
c9f78968 VS |
2337 | inline wxUniChar wxCStrData::operator*() const |
2338 | { | |
2339 | if ( m_str->empty() ) | |
2340 | return wxUniChar(_T('\0')); | |
2341 | else | |
2342 | return (*m_str)[m_offset]; | |
2343 | } | |
2344 | ||
2345 | inline wxUniChar wxCStrData::operator[](size_t n) const | |
2346 | { | |
2347 | return m_str->at(m_offset + n); | |
2348 | } | |
2349 | ||
cc5bd48e VZ |
2350 | // ---------------------------------------------------------------------------- |
2351 | // more wxCStrData operators | |
2352 | // ---------------------------------------------------------------------------- | |
2353 | ||
2354 | // we need to define those to allow "size_t pos = p - s.c_str()" where p is | |
2355 | // some pointer into the string | |
2356 | inline size_t operator-(const char *p, const wxCStrData& cs) | |
2357 | { | |
2358 | return p - cs.AsChar(); | |
2359 | } | |
2360 | ||
2361 | inline size_t operator-(const wchar_t *p, const wxCStrData& cs) | |
2362 | { | |
2363 | return p - cs.AsWChar(); | |
2364 | } | |
2365 | ||
414b7b40 VZ |
2366 | // ---------------------------------------------------------------------------- |
2367 | // implementation of wx[W]CharBuffer inline methods using wxCStrData | |
2368 | // ---------------------------------------------------------------------------- | |
2369 | ||
11aac4ba VS |
2370 | // FIXME-UTF8: move this to buffer.h |
2371 | inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr) | |
2372 | : wxCharTypeBufferBase(cstr.AsChar()) | |
2373 | { | |
2374 | } | |
2375 | ||
2376 | inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr) | |
2377 | : wxCharTypeBufferBase(cstr.AsWChar()) | |
414b7b40 VZ |
2378 | { |
2379 | } | |
2380 | ||
11aac4ba | 2381 | #endif // _WX_WXSTRING_H_ |