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