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