]>
Commit | Line | Data |
---|---|---|
3c67202d | 1 | /////////////////////////////////////////////////////////////////////////////// |
c801d85f | 2 | // Name: string.h |
3c67202d | 3 | // Purpose: wxString and wxArrayString classes |
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> | |
371a5b4e | 9 | // Licence: wxWindows licence |
3c67202d | 10 | /////////////////////////////////////////////////////////////////////////////// |
c801d85f | 11 | |
e90c1d2a VZ |
12 | /* |
13 | Efficient string class [more or less] compatible with MFC CString, | |
14 | wxWindows version 1 wxString and std::string and some handy functions | |
15 | missing from string.h. | |
16 | */ | |
17 | ||
34138703 JS |
18 | #ifndef _WX_WXSTRINGH__ |
19 | #define _WX_WXSTRINGH__ | |
c801d85f | 20 | |
af49c4b8 | 21 | #if defined(__GNUG__) && !defined(__APPLE__) |
e90c1d2a VZ |
22 | #pragma interface "string.h" |
23 | #endif | |
24 | ||
25 | // ---------------------------------------------------------------------------- | |
26 | // conditinal compilation | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
29 | // compile the std::string compatibility functions if defined | |
30 | #define wxSTD_STRING_COMPATIBILITY | |
31 | ||
e90c1d2a VZ |
32 | // ---------------------------------------------------------------------------- |
33 | // headers | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
5737d05f VZ |
36 | #include "wx/defs.h" // everybody should include this |
37 | ||
9dea36ef | 38 | #if defined(__WXMAC__) || defined(__VISAGECPP__) |
3f4a0c5b | 39 | #include <ctype.h> |
17dff81c | 40 | #endif |
3f4a0c5b | 41 | |
91b8de8d RR |
42 | #ifdef __EMX__ |
43 | #include <std.h> | |
44 | #endif | |
45 | ||
9dea36ef DW |
46 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
47 | // problem in VACPP V4 with including stdlib.h multiple times | |
48 | // strconv includes it anyway | |
49 | # include <stdio.h> | |
50 | # include <string.h> | |
51 | # include <stdarg.h> | |
52 | # include <limits.h> | |
53 | #else | |
54 | # include <string.h> | |
55 | # include <stdio.h> | |
56 | # include <stdarg.h> | |
57 | # include <limits.h> | |
58 | # include <stdlib.h> | |
59 | #endif | |
c801d85f | 60 | |
57493f9f | 61 | #ifdef HAVE_STRINGS_H |
1bfcb0b6 | 62 | #include <strings.h> // for strcasecmp() |
d739313f | 63 | #endif // HAVE_STRINGS_H |
1bfcb0b6 | 64 | |
e90c1d2a VZ |
65 | #include "wx/wxchar.h" // for wxChar |
66 | #include "wx/buffer.h" // for wxCharBuffer | |
67 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
3f4a0c5b | 68 | |
c801d85f KB |
69 | // --------------------------------------------------------------------------- |
70 | // macros | |
71 | // --------------------------------------------------------------------------- | |
72 | ||
5737d05f VZ |
73 | // casts [unfortunately!] needed to call some broken functions which require |
74 | // "char *" instead of "const char *" | |
2bb67b80 | 75 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
e90c1d2a VZ |
76 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) |
77 | #define wxMBSTRINGCAST (char *)(const char *) | |
78 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 79 | |
3c67202d | 80 | // implementation only |
5737d05f | 81 | #define wxASSERT_VALID_INDEX(i) \ |
b1801e0e | 82 | wxASSERT_MSG( (size_t)(i) <= Len(), _T("invalid index in wxString") ) |
c801d85f | 83 | |
e90c1d2a VZ |
84 | // ---------------------------------------------------------------------------- |
85 | // constants | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
66b6b045 | 88 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
67b81440 | 89 | // must define this static for VA or else you get multiply defined symbols everywhere |
5d33ed2c | 90 | extern const unsigned int wxSTRING_MAXLEN; |
66b6b045 DW |
91 | |
92 | #else | |
e90c1d2a VZ |
93 | // maximum possible length for a string means "take all string" everywhere |
94 | // (as sizeof(StringData) is unknown here, we substract 100) | |
95 | const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100; | |
96 | ||
66b6b045 DW |
97 | #endif |
98 | ||
e90c1d2a VZ |
99 | // ---------------------------------------------------------------------------- |
100 | // global data | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
103 | // global pointer to empty string | |
bddd7a8d | 104 | extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString; |
6001e347 | 105 | |
c801d85f | 106 | // --------------------------------------------------------------------------- |
e90c1d2a | 107 | // global functions complementing standard C string library replacements for |
3c67202d VZ |
108 | // strlen() and portable strcasecmp() |
109 | //--------------------------------------------------------------------------- | |
e90c1d2a VZ |
110 | |
111 | // Use wxXXX() functions from wxchar.h instead! These functions are for | |
112 | // backwards compatibility only. | |
88150e60 | 113 | |
3c67202d | 114 | // checks whether the passed in pointer is NULL and if the string is empty |
6d56eb5c | 115 | inline bool IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 116 | |
3c67202d | 117 | // safe version of strlen() (returns 0 if passed NULL pointer) |
6d56eb5c | 118 | inline size_t Strlen(const char *psz) |
c801d85f KB |
119 | { return psz ? strlen(psz) : 0; } |
120 | ||
3c67202d | 121 | // portable strcasecmp/_stricmp |
6d56eb5c | 122 | inline int Stricmp(const char *psz1, const char *psz2) |
dd1eaa89 | 123 | { |
91b8de8d | 124 | #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) |
dd1eaa89 | 125 | return _stricmp(psz1, psz2); |
91b8de8d | 126 | #elif defined(__SC__) |
2432b92d | 127 | return _stricmp(psz1, psz2); |
91b8de8d | 128 | #elif defined(__SALFORDC__) |
a3ef5bf5 | 129 | return stricmp(psz1, psz2); |
dd1eaa89 VZ |
130 | #elif defined(__BORLANDC__) |
131 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
132 | #elif defined(__WATCOMC__) |
133 | return stricmp(psz1, psz2); | |
14704334 VS |
134 | #elif defined(__DJGPP__) |
135 | return stricmp(psz1, psz2); | |
91b8de8d RR |
136 | #elif defined(__EMX__) |
137 | return stricmp(psz1, psz2); | |
e2c87f4c | 138 | #elif defined(__WXPM__) |
1777b9bb | 139 | return stricmp(psz1, psz2); |
91b8de8d | 140 | #elif defined(__UNIX__) || defined(__GNUWIN32__) |
dd1eaa89 | 141 | return strcasecmp(psz1, psz2); |
8be97d65 | 142 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
143 | register char c1, c2; |
144 | do { | |
145 | c1 = tolower(*psz1++); | |
146 | c2 = tolower(*psz2++); | |
147 | } while ( c1 && (c1 == c2) ); | |
148 | ||
149 | return c1 - c2; | |
dd1eaa89 VZ |
150 | #else |
151 | // almost all compilers/libraries provide this function (unfortunately under | |
152 | // different names), that's why we don't implement our own which will surely | |
153 | // be more efficient than this code (uncomment to use): | |
154 | /* | |
155 | register char c1, c2; | |
156 | do { | |
157 | c1 = tolower(*psz1++); | |
158 | c2 = tolower(*psz2++); | |
159 | } while ( c1 && (c1 == c2) ); | |
160 | ||
161 | return c1 - c2; | |
162 | */ | |
163 | ||
164 | #error "Please define string case-insensitive compare for your OS/compiler" | |
165 | #endif // OS/compiler | |
166 | } | |
c801d85f | 167 | |
f04f3991 | 168 | // return an empty wxString |
bddd7a8d | 169 | class WXDLLIMPEXP_BASE wxString; // not yet defined |
e90c1d2a | 170 | inline const wxString& wxGetEmptyString() { return *(wxString *)&wxEmptyString; } |
f04f3991 | 171 | |
c801d85f | 172 | // --------------------------------------------------------------------------- |
f04f3991 | 173 | // string data prepended with some housekeeping info (used by wxString class), |
c801d85f KB |
174 | // is never used directly (but had to be put here to allow inlining) |
175 | // --------------------------------------------------------------------------- | |
e90c1d2a | 176 | |
bddd7a8d | 177 | struct WXDLLIMPEXP_BASE wxStringData |
c801d85f KB |
178 | { |
179 | int nRefs; // reference count | |
3c024cc2 | 180 | size_t nDataLength, // actual string length |
c801d85f KB |
181 | nAllocLength; // allocated memory size |
182 | ||
2bb67b80 OK |
183 | // mimics declaration 'wxChar data[nAllocLength]' |
184 | wxChar* data() const { return (wxChar*)(this + 1); } | |
c801d85f KB |
185 | |
186 | // empty string has a special ref count so it's never deleted | |
dbda9e86 JS |
187 | bool IsEmpty() const { return (nRefs == -1); } |
188 | bool IsShared() const { return (nRefs > 1); } | |
c801d85f KB |
189 | |
190 | // lock/unlock | |
dd1eaa89 | 191 | void Lock() { if ( !IsEmpty() ) nRefs++; } |
f6bcfd97 | 192 | |
8ecf21b7 | 193 | // VC++ will refuse to inline Unlock but profiling shows that it is wrong |
f6bcfd97 BP |
194 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
195 | __forceinline | |
196 | #endif | |
ca5e07c7 GD |
197 | // VC++ free must take place in same DLL as allocation when using non dll |
198 | // run-time library (e.g. Multithreaded instead of Multithreaded DLL) | |
8ecf21b7 GD |
199 | #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL) |
200 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); } | |
ca5e07c7 GD |
201 | // we must not inline deallocation since allocation is not inlined |
202 | void Free(); | |
8ecf21b7 | 203 | #else |
f1317a5d | 204 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); } |
8ecf21b7 | 205 | #endif |
8fd0f20b | 206 | |
dd1eaa89 | 207 | // if we had taken control over string memory (GetWriteBuf), it's |
8fd0f20b | 208 | // intentionally put in invalid state |
dbda9e86 JS |
209 | void Validate(bool b) { nRefs = (b ? 1 : 0); } |
210 | bool IsValid() const { return (nRefs != 0); } | |
c801d85f KB |
211 | }; |
212 | ||
c801d85f | 213 | // --------------------------------------------------------------------------- |
3c67202d VZ |
214 | // This is (yet another one) String class for C++ programmers. It doesn't use |
215 | // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...) | |
216 | // thus you should be able to compile it with practicaly any C++ compiler. | |
217 | // This class uses copy-on-write technique, i.e. identical strings share the | |
218 | // same memory as long as neither of them is changed. | |
219 | // | |
220 | // This class aims to be as compatible as possible with the new standard | |
221 | // std::string class, but adds some additional functions and should be at | |
222 | // least as efficient than the standard implementation. | |
223 | // | |
224 | // Performance note: it's more efficient to write functions which take "const | |
225 | // String&" arguments than "const char *" if you assign the argument to | |
226 | // another string. | |
227 | // | |
228 | // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5. | |
229 | // | |
230 | // To do: | |
231 | // - ressource support (string tables in ressources) | |
232 | // - more wide character (UNICODE) support | |
233 | // - regular expressions support | |
c801d85f | 234 | // --------------------------------------------------------------------------- |
3c67202d | 235 | |
bddd7a8d | 236 | class WXDLLIMPEXP_BASE wxString |
3c67202d | 237 | { |
df5168c4 | 238 | #if !wxUSE_STL |
bddd7a8d | 239 | friend class WXDLLIMPEXP_BASE wxArrayString; |
df5168c4 | 240 | #endif |
c801d85f | 241 | |
3c67202d VZ |
242 | // NB: special care was taken in arranging the member functions in such order |
243 | // that all inline functions can be effectively inlined, verify that all | |
244 | // performace critical functions are still inlined if you change order! | |
dd1eaa89 VZ |
245 | private: |
246 | // points to data preceded by wxStringData structure with ref count info | |
2bb67b80 | 247 | wxChar *m_pchData; |
dd1eaa89 VZ |
248 | |
249 | // accessor to string data | |
250 | wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; } | |
251 | ||
6b95b20d VZ |
252 | // string (re)initialization functions |
253 | // initializes the string to the empty value (must be called only from | |
254 | // ctors, use Reinit() otherwise) | |
e90c1d2a | 255 | void Init() { m_pchData = (wxChar *)wxEmptyString; } |
6b95b20d | 256 | // initializaes the string with (a part of) C-string |
2bb67b80 | 257 | void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = wxSTRING_MAXLEN); |
6b95b20d VZ |
258 | // as Init, but also frees old data |
259 | void Reinit() { GetStringData()->Unlock(); Init(); } | |
260 | ||
261 | // memory allocation | |
b1801e0e GD |
262 | // allocates memory for string of length nLen |
263 | bool AllocBuffer(size_t nLen); | |
6b95b20d | 264 | // copies data to another string |
b1801e0e | 265 | bool AllocCopy(wxString&, int, int) const; |
6b95b20d | 266 | // effectively copies data to string |
b1801e0e | 267 | bool AssignCopy(size_t, const wxChar *); |
6b95b20d VZ |
268 | |
269 | // append a (sub)string | |
083f7497 | 270 | bool ConcatSelf(size_t nLen, const wxChar *src); |
6b95b20d VZ |
271 | |
272 | // functions called before writing to the string: they copy it if there | |
273 | // are other references to our data (should be the only owner when writing) | |
b1801e0e GD |
274 | bool CopyBeforeWrite(); |
275 | bool AllocBeforeWrite(size_t); | |
6b95b20d | 276 | |
00b4a13e VZ |
277 | // if we hadn't made these operators private, it would be possible to |
278 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
279 | // converted to char in C and we do have operator=(char) | |
280 | // | |
281 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
282 | // to assign another numeric type to wxString will now result in | |
283 | // ambiguity between operator=(char) and operator=(int) | |
284 | wxString& operator=(int); | |
285 | ||
286 | // these methods are not implemented - there is _no_ conversion from int to | |
c606a9a4 VZ |
287 | // string, you're doing something wrong if the compiler wants to call it! |
288 | // | |
289 | // try `s << i' or `s.Printf("%d", i)' instead | |
290 | wxString(int); | |
c606a9a4 | 291 | |
c801d85f | 292 | public: |
3c67202d VZ |
293 | // constructors and destructor |
294 | // ctor for an empty string | |
b1801e0e | 295 | wxString() : m_pchData(NULL) { Init(); } |
3c67202d | 296 | // copy ctor |
b1801e0e | 297 | wxString(const wxString& stringSrc) : m_pchData(NULL) |
6b95b20d | 298 | { |
09443a26 VZ |
299 | wxASSERT_MSG( stringSrc.GetStringData()->IsValid(), |
300 | _T("did you forget to call UngetWriteBuf()?") ); | |
6b95b20d VZ |
301 | |
302 | if ( stringSrc.IsEmpty() ) { | |
303 | // nothing to do for an empty string | |
304 | Init(); | |
305 | } | |
306 | else { | |
307 | m_pchData = stringSrc.m_pchData; // share same data | |
308 | GetStringData()->Lock(); // => one more copy | |
309 | } | |
310 | } | |
3c67202d | 311 | // string containing nRepeat copies of ch |
2bb67b80 | 312 | wxString(wxChar ch, size_t nRepeat = 1); |
3c67202d | 313 | // ctor takes first nLength characters from C string |
566b84d2 | 314 | // (default value of wxSTRING_MAXLEN means take all the string) |
2bb67b80 | 315 | wxString(const wxChar *psz, size_t nLength = wxSTRING_MAXLEN) |
b1801e0e GD |
316 | : m_pchData(NULL) |
317 | { InitWith(psz, 0, nLength); } | |
f6bcfd97 | 318 | wxString(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = wxSTRING_MAXLEN) |
b1801e0e GD |
319 | : m_pchData(NULL) |
320 | { InitWith(psz, 0, nLength); } | |
e90c1d2a | 321 | |
2bb67b80 OK |
322 | #if wxUSE_UNICODE |
323 | // from multibyte string | |
324 | // (NB: nLength is right now number of Unicode characters, not | |
325 | // characters in psz! So try not to use it yet!) | |
2b5f62a0 | 326 | wxString(const char *psz, wxMBConv& conv, size_t nLength = wxSTRING_MAXLEN); |
2bb67b80 OK |
327 | // from wxWCharBuffer (i.e. return from wxGetString) |
328 | wxString(const wxWCharBuffer& psz) | |
329 | { InitWith(psz, 0, wxSTRING_MAXLEN); } | |
e90c1d2a | 330 | #else // ANSI |
3c67202d | 331 | // from C string (for compilers using unsigned char) |
2bb67b80 | 332 | wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN) |
b1801e0e GD |
333 | : m_pchData(NULL) |
334 | { InitWith((const char*)psz, 0, nLength); } | |
e90c1d2a | 335 | |
6f841509 | 336 | #if wxUSE_WCHAR_T |
2bb67b80 | 337 | // from wide (Unicode) string |
1c2e6a28 | 338 | wxString(const wchar_t *pwz, wxMBConv& conv = wxConvLibc, size_t nLength = wxSTRING_MAXLEN); |
e90c1d2a VZ |
339 | #endif // !wxUSE_WCHAR_T |
340 | ||
2bb67b80 OK |
341 | // from wxCharBuffer |
342 | wxString(const wxCharBuffer& psz) | |
b1801e0e GD |
343 | : m_pchData(NULL) |
344 | { InitWith(psz, 0, wxSTRING_MAXLEN); } | |
e90c1d2a VZ |
345 | #endif // Unicode/ANSI |
346 | ||
3c67202d | 347 | // dtor is not virtual, this class must not be inherited from! |
6b95b20d | 348 | ~wxString() { GetStringData()->Unlock(); } |
c801d85f | 349 | |
3c67202d VZ |
350 | // generic attributes & operations |
351 | // as standard strlen() | |
47d67540 | 352 | size_t Len() const { return GetStringData()->nDataLength; } |
3c67202d | 353 | // string contains any characters? |
dd1eaa89 | 354 | bool IsEmpty() const { return Len() == 0; } |
dcfde592 VZ |
355 | // empty string is "FALSE", so !str will return TRUE |
356 | bool operator!() const { return IsEmpty(); } | |
735d1db6 VZ |
357 | // truncate the string to given length |
358 | wxString& Truncate(size_t uiLen); | |
3c67202d | 359 | // empty string contents |
dd1eaa89 VZ |
360 | void Empty() |
361 | { | |
735d1db6 | 362 | Truncate(0); |
dd1eaa89 | 363 | |
735d1db6 | 364 | wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") ); |
7be07660 | 365 | } |
3c67202d | 366 | // empty the string and free memory |
7be07660 VZ |
367 | void Clear() |
368 | { | |
369 | if ( !GetStringData()->IsEmpty() ) | |
370 | Reinit(); | |
371 | ||
09443a26 VZ |
372 | wxASSERT_MSG( !GetStringData()->nDataLength && |
373 | !GetStringData()->nAllocLength, | |
374 | _T("string should be empty after Clear()") ); | |
dd1eaa89 VZ |
375 | } |
376 | ||
3c67202d VZ |
377 | // contents test |
378 | // Is an ascii value | |
c801d85f | 379 | bool IsAscii() const; |
3c67202d | 380 | // Is a number |
c801d85f | 381 | bool IsNumber() const; |
3c67202d | 382 | // Is a word |
c801d85f | 383 | bool IsWord() const; |
c801d85f | 384 | |
3c67202d VZ |
385 | // data access (all indexes are 0 based) |
386 | // read access | |
2bb67b80 | 387 | wxChar GetChar(size_t n) const |
5737d05f | 388 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
3c67202d | 389 | // read/write access |
2bb67b80 | 390 | wxChar& GetWritableChar(size_t n) |
5737d05f | 391 | { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
3c67202d | 392 | // write access |
2bb67b80 | 393 | void SetChar(size_t n, wxChar ch) |
5737d05f | 394 | { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; } |
c801d85f | 395 | |
3c67202d | 396 | // get last character |
2bb67b80 | 397 | wxChar Last() const |
09443a26 VZ |
398 | { |
399 | wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") ); | |
400 | ||
401 | return m_pchData[Len() - 1]; | |
402 | } | |
403 | ||
3c67202d | 404 | // get writable last character |
2bb67b80 | 405 | wxChar& Last() |
09443a26 VZ |
406 | { |
407 | wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") ); | |
408 | CopyBeforeWrite(); | |
409 | return m_pchData[Len()-1]; | |
410 | } | |
c801d85f | 411 | |
d836ee96 VZ |
412 | /* |
413 | So why do we have all these overloaded operator[]s? A bit of history: | |
414 | initially there was only one of them, taking size_t. Then people | |
415 | started complaining because they wanted to use ints as indices (I | |
416 | wonder why) and compilers were giving warnings about it, so we had to | |
417 | add the operator[](int). Then it became apparent that you couldn't | |
418 | write str[0] any longer because there was ambiguity between two | |
419 | overloads and so you now had to write str[0u] (or, of course, use the | |
420 | explicit casts to either int or size_t but nobody did this). | |
421 | ||
422 | Finally, someone decided to compile wxWin on an Alpha machine and got | |
423 | a surprize: str[0u] didn't compile there because it is of type | |
424 | unsigned int and size_t is unsigned _long_ on Alpha and so there was | |
425 | ambiguity between converting uint to int or ulong. To fix this one we | |
426 | now add operator[](uint) for the machines where size_t is not already | |
427 | the same as unsigned int - hopefully this fixes the problem (for some | |
428 | time) | |
429 | ||
430 | The only real fix is, of course, to remove all versions but the one | |
431 | taking size_t... | |
432 | */ | |
433 | ||
3c67202d | 434 | // operator version of GetChar |
2bb67b80 | 435 | wxChar operator[](size_t n) const |
5737d05f | 436 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
c5248639 | 437 | |
3c67202d | 438 | // operator version of GetChar |
2bb67b80 | 439 | wxChar operator[](int n) const |
5737d05f | 440 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
cd0b1709 | 441 | |
f83db661 | 442 | // operator version of GetWriteableChar |
2bb67b80 | 443 | wxChar& operator[](size_t n) |
5737d05f | 444 | { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
d836ee96 VZ |
445 | |
446 | #ifndef wxSIZE_T_IS_UINT | |
447 | // operator version of GetChar | |
448 | wxChar operator[](unsigned int n) const | |
5737d05f | 449 | { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
d836ee96 | 450 | |
f83db661 | 451 | // operator version of GetWriteableChar |
d836ee96 | 452 | wxChar& operator[](unsigned int n) |
5737d05f | 453 | { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
d836ee96 | 454 | #endif // size_t != unsigned int |
c801d85f | 455 | |
3c67202d | 456 | // implicit conversion to C string |
2bb67b80 | 457 | operator const wxChar*() const { return m_pchData; } |
e015c2a3 | 458 | |
3c67202d | 459 | // explicit conversion to C string (use this with printf()!) |
2bb67b80 | 460 | const wxChar* c_str() const { return m_pchData; } |
e015c2a3 | 461 | // identical to c_str(), for wxWin 1.6x compatibility |
6f841509 | 462 | const wxChar* wx_str() const { return m_pchData; } |
e015c2a3 | 463 | // identical to c_str(), for MFC compatibility |
2bb67b80 | 464 | const wxChar* GetData() const { return m_pchData; } |
e90c1d2a | 465 | |
e015c2a3 VZ |
466 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
467 | // converting numbers or strings which are certain not to contain special | |
468 | // chars (typically system functions, X atoms, environment variables etc.) | |
469 | // | |
470 | // the behaviour of these functions with the strings containing anything | |
471 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 472 | #if wxUSE_UNICODE |
2b5f62a0 VZ |
473 | static wxString FromAscii(const char *ascii); // string |
474 | static wxString FromAscii(const char ascii); // char | |
b1ac3b56 | 475 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
476 | #else // ANSI |
477 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
2b5f62a0 | 478 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
479 | const char *ToAscii() const { return c_str(); } |
480 | #endif // Unicode/!Unicode | |
b1ac3b56 | 481 | |
2b5f62a0 | 482 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 483 | // buffer with temporary data |
f6bcfd97 BP |
484 | // |
485 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
486 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
487 | // fn_str() to return a string which should be used with the OS APIs | |
488 | // accepting the file names. The return value is always the same, but the | |
489 | // type differs because a function may either return pointer to the buffer | |
490 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 491 | #if wxUSE_UNICODE |
f6bcfd97 BP |
492 | const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const |
493 | { return conv.cWC2MB(m_pchData); } | |
494 | ||
e90c1d2a VZ |
495 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
496 | ||
f6bcfd97 BP |
497 | const wxChar* wc_str() const { return m_pchData; } |
498 | ||
499 | // for compatibility with !wxUSE_UNICODE version | |
500 | const wxChar* wc_str(wxMBConv& WXUNUSED(conv)) const { return m_pchData; } | |
e90c1d2a | 501 | |
2bb67b80 | 502 | #if wxMBFILES |
5f709e67 | 503 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 504 | #else // !wxMBFILES |
2bb67b80 | 505 | const wxChar* fn_str() const { return m_pchData; } |
e90c1d2a VZ |
506 | #endif // wxMBFILES/!wxMBFILES |
507 | #else // ANSI | |
e90c1d2a | 508 | const wxChar* mb_str() const { return m_pchData; } |
f6bcfd97 BP |
509 | |
510 | // for compatibility with wxUSE_UNICODE version | |
511 | const wxChar* mb_str(wxMBConv& WXUNUSED(conv)) const { return m_pchData; } | |
512 | ||
e90c1d2a | 513 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 514 | |
6f841509 | 515 | #if wxUSE_WCHAR_T |
f6bcfd97 BP |
516 | const wxWCharBuffer wc_str(wxMBConv& conv) const |
517 | { return conv.cMB2WC(m_pchData); } | |
e90c1d2a | 518 | #endif // wxUSE_WCHAR_T |
f6bcfd97 | 519 | |
2bb67b80 | 520 | const wxChar* fn_str() const { return m_pchData; } |
e90c1d2a | 521 | #endif // Unicode/ANSI |
c801d85f | 522 | |
3c67202d VZ |
523 | // overloaded assignment |
524 | // from another wxString | |
c801d85f | 525 | wxString& operator=(const wxString& stringSrc); |
3c67202d | 526 | // from a character |
2bb67b80 | 527 | wxString& operator=(wxChar ch); |
3c67202d | 528 | // from a C string |
2bb67b80 | 529 | wxString& operator=(const wxChar *psz); |
111bb7f2 OK |
530 | #if wxUSE_UNICODE |
531 | // from wxWCharBuffer | |
b1801e0e GD |
532 | wxString& operator=(const wxWCharBuffer& psz) |
533 | { (void) operator=((const wchar_t *)psz); return *this; } | |
e90c1d2a | 534 | #else // ANSI |
3c67202d | 535 | // from another kind of C string |
c801d85f | 536 | wxString& operator=(const unsigned char* psz); |
6f841509 | 537 | #if wxUSE_WCHAR_T |
3c67202d | 538 | // from a wide string |
c801d85f | 539 | wxString& operator=(const wchar_t *pwz); |
6f841509 | 540 | #endif |
111bb7f2 | 541 | // from wxCharBuffer |
b1801e0e GD |
542 | wxString& operator=(const wxCharBuffer& psz) |
543 | { (void) operator=((const char *)psz); return *this; } | |
e90c1d2a | 544 | #endif // Unicode/ANSI |
3c67202d VZ |
545 | |
546 | // string concatenation | |
547 | // in place concatenation | |
548 | /* | |
549 | Concatenate and return the result. Note that the left to right | |
550 | associativity of << allows to write things like "str << str1 << str2 | |
551 | << ..." (unlike with +=) | |
552 | */ | |
553 | // string += string | |
dd1eaa89 VZ |
554 | wxString& operator<<(const wxString& s) |
555 | { | |
09443a26 VZ |
556 | wxASSERT_MSG( s.GetStringData()->IsValid(), |
557 | _T("did you forget to call UngetWriteBuf()?") ); | |
dd1eaa89 VZ |
558 | |
559 | ConcatSelf(s.Len(), s); | |
560 | return *this; | |
561 | } | |
3c67202d | 562 | // string += C string |
2bb67b80 OK |
563 | wxString& operator<<(const wxChar *psz) |
564 | { ConcatSelf(wxStrlen(psz), psz); return *this; } | |
3c67202d | 565 | // string += char |
2bb67b80 | 566 | wxString& operator<<(wxChar ch) { ConcatSelf(1, &ch); return *this; } |
dd1eaa89 | 567 | |
3c67202d | 568 | // string += string |
6b95b20d | 569 | void operator+=(const wxString& s) { (void)operator<<(s); } |
3c67202d | 570 | // string += C string |
2bb67b80 | 571 | void operator+=(const wxChar *psz) { (void)operator<<(psz); } |
3c67202d | 572 | // string += char |
2bb67b80 OK |
573 | void operator+=(wxChar ch) { (void)operator<<(ch); } |
574 | ||
575 | // string += buffer (i.e. from wxGetString) | |
576 | #if wxUSE_UNICODE | |
09443a26 VZ |
577 | wxString& operator<<(const wxWCharBuffer& s) |
578 | { (void)operator<<((const wchar_t *)s); return *this; } | |
579 | void operator+=(const wxWCharBuffer& s) | |
580 | { (void)operator<<((const wchar_t *)s); } | |
581 | #else // !wxUSE_UNICODE | |
582 | wxString& operator<<(const wxCharBuffer& s) | |
583 | { (void)operator<<((const char *)s); return *this; } | |
584 | void operator+=(const wxCharBuffer& s) | |
585 | { (void)operator<<((const char *)s); } | |
586 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
6b95b20d | 587 | |
3c67202d | 588 | // string += C string |
09443a26 VZ |
589 | wxString& Append(const wxString& s) |
590 | { | |
591 | // test for IsEmpty() to share the string if possible | |
592 | if ( IsEmpty() ) | |
593 | *this = s; | |
594 | else | |
595 | ConcatSelf(s.Length(), s.c_str()); | |
596 | return *this; | |
597 | } | |
2bb67b80 OK |
598 | wxString& Append(const wxChar* psz) |
599 | { ConcatSelf(wxStrlen(psz), psz); return *this; } | |
3c67202d | 600 | // append count copies of given character |
2bb67b80 | 601 | wxString& Append(wxChar ch, size_t count = 1u) |
3c67202d | 602 | { wxString str(ch, count); return *this << str; } |
8f06a017 RD |
603 | wxString& Append(const wxChar* psz, size_t nLen) |
604 | { ConcatSelf(nLen, psz); return *this; } | |
3c67202d VZ |
605 | |
606 | // prepend a string, return the string itself | |
607 | wxString& Prepend(const wxString& str) | |
608 | { *this = str + *this; return *this; } | |
609 | ||
610 | // non-destructive concatenation | |
611 | // | |
bddd7a8d | 612 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
3c67202d | 613 | // |
bddd7a8d | 614 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); |
3c67202d | 615 | // |
bddd7a8d | 616 | friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); |
3c67202d | 617 | // |
bddd7a8d | 618 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); |
3c67202d | 619 | // |
bddd7a8d | 620 | friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); |
3c67202d VZ |
621 | |
622 | // stream-like functions | |
623 | // insert an int into string | |
3ce65f6c VZ |
624 | wxString& operator<<(int i) |
625 | { return (*this) << Format(_T("%d"), i); } | |
626 | // insert an unsigned int into string | |
627 | wxString& operator<<(unsigned int ui) | |
628 | { return (*this) << Format(_T("%u"), ui); } | |
629 | // insert a long into string | |
630 | wxString& operator<<(long l) | |
631 | { return (*this) << Format(_T("%ld"), l); } | |
632 | // insert an unsigned long into string | |
633 | wxString& operator<<(unsigned long ul) | |
634 | { return (*this) << Format(_T("%lu"), ul); } | |
3c67202d | 635 | // insert a float into string |
3ce65f6c VZ |
636 | wxString& operator<<(float f) |
637 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 638 | // insert a double into string |
3ce65f6c VZ |
639 | wxString& operator<<(double d) |
640 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 641 | |
3c67202d | 642 | // string comparison |
30b21f9a | 643 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
f33fee2a | 644 | int Cmp(const wxChar *psz) const { return wxStrcmp(c_str(), psz); } |
3c67202d | 645 | // same as Cmp() but not case-sensitive |
f33fee2a | 646 | int CmpNoCase(const wxChar *psz) const { return wxStricmp(c_str(), psz); } |
3c67202d VZ |
647 | // test for the string equality, either considering case or not |
648 | // (if compareWithCase then the case matters) | |
2bb67b80 | 649 | bool IsSameAs(const wxChar *psz, bool compareWithCase = TRUE) const |
3c67202d | 650 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } |
f33fee2a VZ |
651 | // comparison with a signle character: returns TRUE if equal |
652 | bool IsSameAs(wxChar c, bool compareWithCase = TRUE) const | |
653 | { | |
654 | return (Len() == 1) && (compareWithCase ? GetChar(0u) == c | |
655 | : wxToupper(GetChar(0u)) == wxToupper(c)); | |
656 | } | |
3c67202d VZ |
657 | |
658 | // simple sub-string extraction | |
659 | // return substring starting at nFirst of length nCount (or till the end | |
660 | // if nCount = default value) | |
566b84d2 | 661 | wxString Mid(size_t nFirst, size_t nCount = wxSTRING_MAXLEN) const; |
3c67202d | 662 | |
f6bcfd97 | 663 | // operator version of Mid() |
3c67202d VZ |
664 | wxString operator()(size_t start, size_t len) const |
665 | { return Mid(start, len); } | |
666 | ||
c0881dc3 | 667 | // check that the string starts with prefix and return the rest of the |
f6bcfd97 BP |
668 | // string in the provided pointer if it is not NULL, otherwise return |
669 | // FALSE | |
670 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; | |
671 | ||
3c67202d | 672 | // get first nCount characters |
c801d85f | 673 | wxString Left(size_t nCount) const; |
3c67202d | 674 | // get last nCount characters |
c801d85f | 675 | wxString Right(size_t nCount) const; |
c0881dc3 | 676 | // get all characters before the first occurance of ch |
3c67202d | 677 | // (returns the whole string if ch not found) |
2bb67b80 | 678 | wxString BeforeFirst(wxChar ch) const; |
3c67202d VZ |
679 | // get all characters before the last occurence of ch |
680 | // (returns empty string if ch not found) | |
2bb67b80 | 681 | wxString BeforeLast(wxChar ch) const; |
3c67202d VZ |
682 | // get all characters after the first occurence of ch |
683 | // (returns empty string if ch not found) | |
2bb67b80 | 684 | wxString AfterFirst(wxChar ch) const; |
3c67202d VZ |
685 | // get all characters after the last occurence of ch |
686 | // (returns the whole string if ch not found) | |
2bb67b80 | 687 | wxString AfterLast(wxChar ch) const; |
3c67202d VZ |
688 | |
689 | // for compatibility only, use more explicitly named functions above | |
2bb67b80 OK |
690 | wxString Before(wxChar ch) const { return BeforeLast(ch); } |
691 | wxString After(wxChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
692 | |
693 | // case conversion | |
c84c52de | 694 | // convert to upper case in place, return the string itself |
c801d85f | 695 | wxString& MakeUpper(); |
c84c52de | 696 | // convert to upper case, return the copy of the string |
03ab016d JS |
697 | // Here's something to remember: BC++ doesn't like returns in inlines. |
698 | wxString Upper() const ; | |
c84c52de | 699 | // convert to lower case in place, return the string itself |
c801d85f | 700 | wxString& MakeLower(); |
c84c52de | 701 | // convert to lower case, return the copy of the string |
03ab016d | 702 | wxString Lower() const ; |
c801d85f | 703 | |
3c67202d VZ |
704 | // trimming/padding whitespace (either side) and truncating |
705 | // remove spaces from left or from right (default) side | |
c801d85f | 706 | wxString& Trim(bool bFromRight = TRUE); |
3c67202d | 707 | // add nCount copies chPad in the beginning or at the end (default) |
223d09f6 | 708 | wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = TRUE); |
dd1eaa89 | 709 | |
3c67202d VZ |
710 | // searching and replacing |
711 | // searching (return starting index, or -1 if not found) | |
2bb67b80 | 712 | int Find(wxChar ch, bool bFromEnd = FALSE) const; // like strchr/strrchr |
3c67202d | 713 | // searching (return starting index, or -1 if not found) |
2bb67b80 | 714 | int Find(const wxChar *pszSub) const; // like strstr |
3c67202d VZ |
715 | // replace first (or all of bReplaceAll) occurences of substring with |
716 | // another string, returns the number of replacements made | |
2bb67b80 OK |
717 | size_t Replace(const wxChar *szOld, |
718 | const wxChar *szNew, | |
3c67202d VZ |
719 | bool bReplaceAll = TRUE); |
720 | ||
721 | // check if the string contents matches a mask containing '*' and '?' | |
2bb67b80 | 722 | bool Matches(const wxChar *szMask) const; |
c801d85f | 723 | |
538f35cc VZ |
724 | // conversion to numbers: all functions return TRUE only if the whole |
725 | // string is a number and put the value of this number into the pointer | |
726 | // provided, the base is the numeric base in which the conversion should be | |
727 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
728 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
cd0b1709 | 729 | // convert to a signed integer |
538f35cc | 730 | bool ToLong(long *val, int base = 10) const; |
cd0b1709 | 731 | // convert to an unsigned integer |
538f35cc | 732 | bool ToULong(unsigned long *val, int base = 10) const; |
cd0b1709 VZ |
733 | // convert to a double |
734 | bool ToDouble(double *val) const; | |
735 | ||
3c67202d VZ |
736 | // formated input/output |
737 | // as sprintf(), returns the number of characters written or < 0 on error | |
7357f981 GD |
738 | // (take 'this' into account in attribute parameter count) |
739 | int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
3c67202d | 740 | // as vprintf(), returns the number of characters written or < 0 on error |
2bb67b80 | 741 | int PrintfV(const wxChar* pszFormat, va_list argptr); |
dd1eaa89 | 742 | |
341e7d28 | 743 | // returns the string containing the result of Printf() to it |
7357f981 | 744 | static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; |
341e7d28 VZ |
745 | // the same as above, but takes a va_list |
746 | static wxString FormatV(const wxChar *pszFormat, va_list argptr); | |
747 | ||
3c67202d VZ |
748 | // raw access to string memory |
749 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 750 | // only works if the data of this string is not shared |
b1801e0e | 751 | bool Alloc(size_t nLen); |
3c67202d | 752 | // minimize the string's memory |
dd1eaa89 | 753 | // only works if the data of this string is not shared |
b1801e0e | 754 | bool Shrink(); |
3c67202d VZ |
755 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
756 | // a.s.a.p. to put string back in a reasonable state! | |
2bb67b80 | 757 | wxChar *GetWriteBuf(size_t nLen); |
3c67202d | 758 | // call this immediately after GetWriteBuf() has been used |
8fd0f20b | 759 | void UngetWriteBuf(); |
8f06a017 | 760 | void UngetWriteBuf(size_t nLen); |
c801d85f | 761 | |
3c67202d VZ |
762 | // wxWindows version 1 compatibility functions |
763 | ||
764 | // use Mid() | |
765 | wxString SubString(size_t from, size_t to) const | |
766 | { return Mid(from, (to - from + 1)); } | |
767 | // values for second parameter of CompareTo function | |
c801d85f | 768 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 769 | // values for first parameter of Strip function |
c801d85f | 770 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 771 | |
7357f981 GD |
772 | // use Printf() |
773 | // (take 'this' into account in attribute parameter count) | |
774 | int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
c801d85f | 775 | |
3c67202d | 776 | // use Cmp() |
2bb67b80 | 777 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 778 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 779 | |
3c67202d | 780 | // use Len |
c801d85f | 781 | size_t Length() const { return Len(); } |
3c67202d | 782 | // Count the number of characters |
2bb67b80 | 783 | int Freq(wxChar ch) const; |
3c67202d | 784 | // use MakeLower |
c801d85f | 785 | void LowerCase() { MakeLower(); } |
3c67202d | 786 | // use MakeUpper |
c801d85f | 787 | void UpperCase() { MakeUpper(); } |
3c67202d | 788 | // use Trim except that it doesn't change this string |
c801d85f KB |
789 | wxString Strip(stripType w = trailing) const; |
790 | ||
3c67202d | 791 | // use Find (more general variants not yet supported) |
2bb67b80 OK |
792 | size_t Index(const wxChar* psz) const { return Find(psz); } |
793 | size_t Index(wxChar ch) const { return Find(ch); } | |
3c67202d | 794 | // use Truncate |
c801d85f | 795 | wxString& Remove(size_t pos) { return Truncate(pos); } |
28a156b1 | 796 | wxString& RemoveLast(size_t n = 1) { return Truncate(Len() - n); } |
c801d85f | 797 | |
3ed358cb | 798 | wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); } |
dd1eaa89 | 799 | |
3c67202d | 800 | // use Find() |
2bb67b80 OK |
801 | int First( const wxChar ch ) const { return Find(ch); } |
802 | int First( const wxChar* psz ) const { return Find(psz); } | |
3ed358cb | 803 | int First( const wxString &str ) const { return Find(str); } |
2bb67b80 | 804 | int Last( const wxChar ch ) const { return Find(ch, TRUE); } |
3c67202d | 805 | bool Contains(const wxString& str) const { return Find(str) != -1; } |
c801d85f | 806 | |
3c67202d | 807 | // use IsEmpty() |
c801d85f | 808 | bool IsNull() const { return IsEmpty(); } |
c801d85f | 809 | |
3c67202d VZ |
810 | #ifdef wxSTD_STRING_COMPATIBILITY |
811 | // std::string compatibility functions | |
dd1eaa89 | 812 | |
2bc07607 VZ |
813 | // standard types |
814 | typedef wxChar value_type; | |
5460b9e3 VZ |
815 | typedef size_t size_type; |
816 | typedef value_type *iterator; | |
2bc07607 VZ |
817 | typedef const value_type *const_iterator; |
818 | ||
3c67202d | 819 | // an 'invalid' value for string index |
c801d85f | 820 | static const size_t npos; |
dd1eaa89 | 821 | |
3c67202d VZ |
822 | // constructors |
823 | // take nLen chars starting at nPos | |
824 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
b1801e0e | 825 | : m_pchData(NULL) |
3c67202d | 826 | { |
09443a26 VZ |
827 | wxASSERT_MSG( str.GetStringData()->IsValid(), |
828 | _T("did you forget to call UngetWriteBuf()?") ); | |
829 | ||
3c67202d VZ |
830 | InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen); |
831 | } | |
832 | // take all characters from pStart to pEnd | |
833 | wxString(const void *pStart, const void *pEnd); | |
834 | ||
835 | // lib.string.capacity | |
836 | // return the length of the string | |
837 | size_t size() const { return Len(); } | |
838 | // return the length of the string | |
839 | size_t length() const { return Len(); } | |
840 | // return the maximum size of the string | |
566b84d2 | 841 | size_t max_size() const { return wxSTRING_MAXLEN; } |
3c67202d | 842 | // resize the string, filling the space with c if c != 0 |
223d09f6 | 843 | void resize(size_t nSize, wxChar ch = wxT('\0')); |
3c67202d VZ |
844 | // delete the contents of the string |
845 | void clear() { Empty(); } | |
846 | // returns true if the string is empty | |
847 | bool empty() const { return IsEmpty(); } | |
252a752e | 848 | // inform string about planned change in size |
2b5f62a0 | 849 | void reserve(size_t sz) { Alloc(sz); } |
3c67202d VZ |
850 | |
851 | // lib.string.access | |
852 | // return the character at position n | |
2bb67b80 | 853 | wxChar at(size_t n) const { return GetChar(n); } |
3c67202d | 854 | // returns the writable character at position n |
2bb67b80 | 855 | wxChar& at(size_t n) { return GetWritableChar(n); } |
3c67202d | 856 | |
2bc07607 VZ |
857 | // first valid index position |
858 | const_iterator begin() const { return wx_str(); } | |
859 | // position one after the last valid one | |
860 | const_iterator end() const { return wx_str() + length(); } | |
861 | ||
5460b9e3 VZ |
862 | // first valid index position |
863 | iterator begin() { CopyBeforeWrite(); return m_pchData; } | |
864 | // position one after the last valid one | |
865 | iterator end() { CopyBeforeWrite(); return m_pchData + length(); } | |
866 | ||
3c67202d VZ |
867 | // lib.string.modifiers |
868 | // append a string | |
869 | wxString& append(const wxString& str) | |
870 | { *this += str; return *this; } | |
871 | // append elements str[pos], ..., str[pos+n] | |
872 | wxString& append(const wxString& str, size_t pos, size_t n) | |
873 | { ConcatSelf(n, str.c_str() + pos); return *this; } | |
874 | // append first n (or all if n == npos) characters of sz | |
2bb67b80 OK |
875 | wxString& append(const wxChar *sz, size_t n = npos) |
876 | { ConcatSelf(n == npos ? wxStrlen(sz) : n, sz); return *this; } | |
3c67202d VZ |
877 | |
878 | // append n copies of ch | |
2bb67b80 | 879 | wxString& append(size_t n, wxChar ch) { return Pad(n, ch); } |
3c67202d VZ |
880 | |
881 | // same as `this_string = str' | |
735d1db6 VZ |
882 | wxString& assign(const wxString& str) |
883 | { return *this = str; } | |
3c67202d VZ |
884 | // same as ` = str[pos..pos + n] |
885 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
735d1db6 | 886 | { Empty(); return Append(str.c_str() + pos, n); } |
3c67202d | 887 | // same as `= first n (or all if n == npos) characters of sz' |
2bb67b80 | 888 | wxString& assign(const wxChar *sz, size_t n = npos) |
735d1db6 | 889 | { Empty(); return Append(sz, n == npos ? wxStrlen(sz) : n); } |
3c67202d | 890 | // same as `= n copies of ch' |
2bb67b80 | 891 | wxString& assign(size_t n, wxChar ch) |
735d1db6 | 892 | { Empty(); return Append(ch, n); } |
3c67202d VZ |
893 | |
894 | // insert another string | |
895 | wxString& insert(size_t nPos, const wxString& str); | |
896 | // insert n chars of str starting at nStart (in str) | |
897 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
2bb67b80 | 898 | { return insert(nPos, wxString((const wxChar *)str + nStart, n)); } |
3c67202d VZ |
899 | |
900 | // insert first n (or all if n == npos) characters of sz | |
2bb67b80 | 901 | wxString& insert(size_t nPos, const wxChar *sz, size_t n = npos) |
3c67202d VZ |
902 | { return insert(nPos, wxString(sz, n)); } |
903 | // insert n copies of ch | |
2bb67b80 | 904 | wxString& insert(size_t nPos, size_t n, wxChar ch) |
3c67202d VZ |
905 | { return insert(nPos, wxString(ch, n)); } |
906 | ||
907 | // delete characters from nStart to nStart + nLen | |
908 | wxString& erase(size_t nStart = 0, size_t nLen = npos); | |
909 | ||
910 | // replaces the substring of length nLen starting at nStart | |
2bb67b80 | 911 | wxString& replace(size_t nStart, size_t nLen, const wxChar* sz); |
3c67202d | 912 | // replaces the substring with nCount copies of ch |
2bb67b80 | 913 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch); |
3c67202d VZ |
914 | // replaces a substring with another substring |
915 | wxString& replace(size_t nStart, size_t nLen, | |
916 | const wxString& str, size_t nStart2, size_t nLen2); | |
917 | // replaces the substring with first nCount chars of sz | |
fb20fa43 VZ |
918 | wxString& replace(size_t nStart, size_t nLen, |
919 | const wxChar* sz, size_t nCount); | |
3c67202d VZ |
920 | |
921 | // swap two strings | |
922 | void swap(wxString& str); | |
923 | ||
924 | // All find() functions take the nStart argument which specifies the | |
925 | // position to start the search on, the default value is 0. All functions | |
926 | // return npos if there were no match. | |
927 | ||
928 | // find a substring | |
929 | size_t find(const wxString& str, size_t nStart = 0) const; | |
930 | ||
931 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 932 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d | 933 | // find first n characters of sz |
2bb67b80 | 934 | size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; |
fb20fa43 | 935 | #endif // VC++ 1.5 |
3c67202d VZ |
936 | |
937 | // Gives a duplicate symbol (presumably a case-insensitivity problem) | |
62448488 | 938 | #if !defined(__BORLANDC__) |
3c67202d | 939 | // find the first occurence of character ch after nStart |
2bb67b80 | 940 | size_t find(wxChar ch, size_t nStart = 0) const; |
62448488 | 941 | #endif |
3c67202d VZ |
942 | // rfind() family is exactly like find() but works right to left |
943 | ||
944 | // as find, but from the end | |
945 | size_t rfind(const wxString& str, size_t nStart = npos) const; | |
946 | ||
947 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 948 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d | 949 | // as find, but from the end |
2bb67b80 | 950 | size_t rfind(const wxChar* sz, size_t nStart = npos, |
3c67202d VZ |
951 | size_t n = npos) const; |
952 | // as find, but from the end | |
2bb67b80 | 953 | size_t rfind(wxChar ch, size_t nStart = npos) const; |
fb20fa43 | 954 | #endif // VC++ 1.5 |
3c67202d VZ |
955 | |
956 | // find first/last occurence of any character in the set | |
957 | ||
969d318c VZ |
958 | // as strpbrk() but starts at nStart, returns npos if not found |
959 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
960 | { return find_first_of(str.c_str(), nStart); } | |
961 | // same as above | |
2bb67b80 | 962 | size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; |
3c67202d | 963 | // same as find(char, size_t) |
969d318c VZ |
964 | size_t find_first_of(wxChar c, size_t nStart = 0) const |
965 | { return find(c, nStart); } | |
966 | // find the last (starting from nStart) char from str in this string | |
967 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
968 | { return find_last_of(str.c_str(), nStart); } | |
969 | // same as above | |
970 | size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; | |
971 | // same as above | |
972 | size_t find_last_of(wxChar c, size_t nStart = npos) const | |
973 | { return rfind(c, nStart); } | |
3c67202d VZ |
974 | |
975 | // find first/last occurence of any character not in the set | |
976 | ||
969d318c VZ |
977 | // as strspn() (starting from nStart), returns npos on failure |
978 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
979 | { return find_first_not_of(str.c_str(), nStart); } | |
980 | // same as above | |
981 | size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; | |
982 | // same as above | |
2bb67b80 | 983 | size_t find_first_not_of(wxChar ch, size_t nStart = 0) const; |
969d318c | 984 | // as strcspn() |
fb20fa43 VZ |
985 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const |
986 | { return find_first_not_of(str.c_str(), nStart); } | |
969d318c VZ |
987 | // same as above |
988 | size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; | |
989 | // same as above | |
2bb67b80 | 990 | size_t find_last_not_of(wxChar ch, size_t nStart = npos) const; |
3c67202d VZ |
991 | |
992 | // All compare functions return -1, 0 or 1 if the [sub]string is less, | |
993 | // equal or greater than the compare() argument. | |
994 | ||
995 | // just like strcmp() | |
996 | int compare(const wxString& str) const { return Cmp(str); } | |
997 | // comparison with a substring | |
fb20fa43 VZ |
998 | int compare(size_t nStart, size_t nLen, const wxString& str) const |
999 | { return Mid(nStart, nLen).Cmp(str); } | |
3c67202d VZ |
1000 | // comparison of 2 substrings |
1001 | int compare(size_t nStart, size_t nLen, | |
fb20fa43 VZ |
1002 | const wxString& str, size_t nStart2, size_t nLen2) const |
1003 | { return Mid(nStart, nLen).Cmp(str.Mid(nStart2, nLen2)); } | |
3c67202d | 1004 | // just like strcmp() |
2bb67b80 | 1005 | int compare(const wxChar* sz) const { return Cmp(sz); } |
3c67202d VZ |
1006 | // substring comparison with first nCount characters of sz |
1007 | int compare(size_t nStart, size_t nLen, | |
fb20fa43 VZ |
1008 | const wxChar* sz, size_t nCount = npos) const |
1009 | { return Mid(nStart, nLen).Cmp(wxString(sz, nCount)); } | |
3c67202d VZ |
1010 | |
1011 | // substring extraction | |
8c264b23 VZ |
1012 | wxString substr(size_t nStart = 0, size_t nLen = npos) const |
1013 | { return Mid(nStart, nLen); } | |
3c67202d | 1014 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f KB |
1015 | }; |
1016 | ||
df5168c4 MB |
1017 | // define wxArrayString, for compatibility |
1018 | #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL | |
1019 | #include "wx/arrstr.h" | |
ba8c1601 | 1020 | #endif |
9d155f50 | 1021 | |
1d218550 VZ |
1022 | // ---------------------------------------------------------------------------- |
1023 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
1024 | // ---------------------------------------------------------------------------- | |
1025 | ||
bddd7a8d | 1026 | class WXDLLIMPEXP_BASE wxStringBuffer |
1d218550 VZ |
1027 | { |
1028 | public: | |
1029 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
b1801e0e GD |
1030 | : m_str(str), m_buf(NULL) |
1031 | { m_buf = m_str.GetWriteBuf(lenWanted); } | |
e015c2a3 | 1032 | |
1d218550 | 1033 | ~wxStringBuffer() { m_str.UngetWriteBuf(); } |
e015c2a3 | 1034 | |
1d218550 | 1035 | operator wxChar*() const { return m_buf; } |
e015c2a3 | 1036 | |
1d218550 VZ |
1037 | private: |
1038 | wxString& m_str; | |
1039 | wxChar *m_buf; | |
e015c2a3 VZ |
1040 | |
1041 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1d218550 VZ |
1042 | }; |
1043 | ||
c801d85f | 1044 | // --------------------------------------------------------------------------- |
3c67202d | 1045 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 1046 | // --------------------------------------------------------------------------- |
f33fee2a | 1047 | |
0cbe5ee3 VZ |
1048 | inline bool operator==(const wxString& s1, const wxString& s2) |
1049 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
1050 | inline bool operator==(const wxString& s1, const wxChar * s2) | |
1051 | { return s1.Cmp(s2) == 0; } | |
1052 | inline bool operator==(const wxChar * s1, const wxString& s2) | |
1053 | { return s2.Cmp(s1) == 0; } | |
1054 | inline bool operator!=(const wxString& s1, const wxString& s2) | |
1055 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
1056 | inline bool operator!=(const wxString& s1, const wxChar * s2) | |
1057 | { return s1.Cmp(s2) != 0; } | |
1058 | inline bool operator!=(const wxChar * s1, const wxString& s2) | |
1059 | { return s2.Cmp(s1) != 0; } | |
1060 | inline bool operator< (const wxString& s1, const wxString& s2) | |
1061 | { return s1.Cmp(s2) < 0; } | |
1062 | inline bool operator< (const wxString& s1, const wxChar * s2) | |
1063 | { return s1.Cmp(s2) < 0; } | |
1064 | inline bool operator< (const wxChar * s1, const wxString& s2) | |
1065 | { return s2.Cmp(s1) > 0; } | |
1066 | inline bool operator> (const wxString& s1, const wxString& s2) | |
1067 | { return s1.Cmp(s2) > 0; } | |
1068 | inline bool operator> (const wxString& s1, const wxChar * s2) | |
1069 | { return s1.Cmp(s2) > 0; } | |
1070 | inline bool operator> (const wxChar * s1, const wxString& s2) | |
1071 | { return s2.Cmp(s1) < 0; } | |
1072 | inline bool operator<=(const wxString& s1, const wxString& s2) | |
1073 | { return s1.Cmp(s2) <= 0; } | |
1074 | inline bool operator<=(const wxString& s1, const wxChar * s2) | |
1075 | { return s1.Cmp(s2) <= 0; } | |
1076 | inline bool operator<=(const wxChar * s1, const wxString& s2) | |
1077 | { return s2.Cmp(s1) >= 0; } | |
1078 | inline bool operator>=(const wxString& s1, const wxString& s2) | |
1079 | { return s1.Cmp(s2) >= 0; } | |
1080 | inline bool operator>=(const wxString& s1, const wxChar * s2) | |
1081 | { return s1.Cmp(s2) >= 0; } | |
1082 | inline bool operator>=(const wxChar * s1, const wxString& s2) | |
1083 | { return s2.Cmp(s1) <= 0; } | |
3c67202d | 1084 | |
f33fee2a VZ |
1085 | // comparison with char |
1086 | inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); } | |
1087 | inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); } | |
1088 | inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); } | |
1089 | inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } | |
1090 | ||
5ca07d0f OK |
1091 | #if wxUSE_UNICODE |
1092 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 1093 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 1094 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 1095 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
1096 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
1097 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
1098 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
1099 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
0cbe5ee3 | 1100 | #else // !wxUSE_UNICODE |
5ca07d0f | 1101 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 1102 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 1103 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 1104 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
1105 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
1106 | { return (s1.Cmp((const char *)s2) != 0); } | |
1107 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
1108 | { return (s2.Cmp((const char *)s1) != 0); } | |
0cbe5ee3 | 1109 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
5ca07d0f | 1110 | |
bddd7a8d VZ |
1111 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
1112 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); | |
1113 | wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); | |
1114 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); | |
1115 | wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); | |
028a2b5d | 1116 | #if wxUSE_UNICODE |
6d56eb5c | 1117 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 1118 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 1119 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 1120 | { return (const wchar_t *)buf + string; } |
0cbe5ee3 | 1121 | #else // !wxUSE_UNICODE |
6d56eb5c | 1122 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 1123 | { return string + (const char *)buf; } |
6d56eb5c | 1124 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 1125 | { return (const char *)buf + string; } |
0cbe5ee3 | 1126 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
f04f3991 | 1127 | |
c801d85f | 1128 | // --------------------------------------------------------------------------- |
3c67202d | 1129 | // Implementation only from here until the end of file |
c801d85f KB |
1130 | // --------------------------------------------------------------------------- |
1131 | ||
e90c1d2a | 1132 | // don't pollute the library user's name space |
5737d05f | 1133 | #undef wxASSERT_VALID_INDEX |
e90c1d2a | 1134 | |
38830220 | 1135 | #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM |
c801d85f | 1136 | |
65f19af1 | 1137 | #include "wx/iosfwrap.h" |
c801d85f | 1138 | |
bddd7a8d VZ |
1139 | WXDLLIMPEXP_BASE wxSTD istream& operator>>(wxSTD istream&, wxString&); |
1140 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); | |
c801d85f | 1141 | |
3c67202d | 1142 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 1143 | |
34138703 | 1144 | #endif // _WX_WXSTRINGH__ |