]>
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> | |
dd1eaa89 | 9 | // Licence: wxWindows license |
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 KB |
20 | |
21 | #ifdef __GNUG__ | |
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 | ||
32 | // define to derive wxString from wxObject (deprecated!) | |
33 | #ifdef WXSTRING_IS_WXOBJECT | |
34 | #undef WXSTRING_IS_WXOBJECT | |
c801d85f KB |
35 | #endif |
36 | ||
e90c1d2a VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // headers | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
17dff81c | 41 | #ifdef __WXMAC__ |
3f4a0c5b | 42 | #include <ctype.h> |
17dff81c | 43 | #endif |
3f4a0c5b | 44 | |
91b8de8d RR |
45 | #ifdef __EMX__ |
46 | #include <std.h> | |
47 | #endif | |
48 | ||
c801d85f KB |
49 | #include <string.h> |
50 | #include <stdio.h> | |
51 | #include <stdarg.h> | |
52 | #include <limits.h> | |
dd1eaa89 | 53 | #include <stdlib.h> |
c801d85f | 54 | |
57493f9f | 55 | #ifdef HAVE_STRINGS_H |
1bfcb0b6 VZ |
56 | #include <strings.h> // for strcasecmp() |
57 | #endif // AIX | |
58 | ||
e90c1d2a VZ |
59 | #include "wx/defs.h" // everybody should include this |
60 | #include "wx/debug.h" // for wxASSERT() | |
61 | #include "wx/wxchar.h" // for wxChar | |
62 | #include "wx/buffer.h" // for wxCharBuffer | |
63 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
3f4a0c5b | 64 | |
e90c1d2a | 65 | #ifndef WX_PRECOMP |
dd1eaa89 | 66 | #ifdef WXSTRING_IS_WXOBJECT |
e90c1d2a | 67 | #include "wx/object.h" // base class |
dd1eaa89 | 68 | #endif |
3f4a0c5b | 69 | #endif // !PCH |
8fd0f20b | 70 | |
c801d85f KB |
71 | // --------------------------------------------------------------------------- |
72 | // macros | |
73 | // --------------------------------------------------------------------------- | |
74 | ||
c801d85f | 75 | // 'naughty' cast |
2bb67b80 | 76 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
e90c1d2a VZ |
77 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) |
78 | #define wxMBSTRINGCAST (char *)(const char *) | |
79 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 80 | |
3c67202d | 81 | // implementation only |
b1269691 | 82 | #define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) <= Len() ) |
c801d85f | 83 | |
e90c1d2a VZ |
84 | // ---------------------------------------------------------------------------- |
85 | // constants | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | // maximum possible length for a string means "take all string" everywhere | |
89 | // (as sizeof(StringData) is unknown here, we substract 100) | |
90 | const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100; | |
91 | ||
92 | // ---------------------------------------------------------------------------- | |
93 | // global data | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | // global pointer to empty string | |
97 | WXDLLEXPORT_DATA(extern 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 |
dbda9e86 | 108 | inline bool WXDLLEXPORT IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 109 | |
3c67202d VZ |
110 | // safe version of strlen() (returns 0 if passed NULL pointer) |
111 | inline size_t WXDLLEXPORT Strlen(const char *psz) | |
c801d85f KB |
112 | { return psz ? strlen(psz) : 0; } |
113 | ||
3c67202d | 114 | // portable strcasecmp/_stricmp |
dd1eaa89 VZ |
115 | inline int WXDLLEXPORT Stricmp(const char *psz1, const char *psz2) |
116 | { | |
91b8de8d | 117 | #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) |
dd1eaa89 | 118 | return _stricmp(psz1, psz2); |
91b8de8d | 119 | #elif defined(__SC__) |
2432b92d | 120 | return _stricmp(psz1, psz2); |
91b8de8d | 121 | #elif defined(__SALFORDC__) |
a3ef5bf5 | 122 | return stricmp(psz1, psz2); |
dd1eaa89 VZ |
123 | #elif defined(__BORLANDC__) |
124 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
125 | #elif defined(__WATCOMC__) |
126 | return stricmp(psz1, psz2); | |
91b8de8d RR |
127 | #elif defined(__EMX__) |
128 | return stricmp(psz1, psz2); | |
e2c87f4c | 129 | #elif defined(__WXPM__) |
1777b9bb | 130 | return stricmp(psz1, psz2); |
91b8de8d | 131 | #elif defined(__UNIX__) || defined(__GNUWIN32__) |
dd1eaa89 | 132 | return strcasecmp(psz1, psz2); |
8be97d65 | 133 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
134 | register char c1, c2; |
135 | do { | |
136 | c1 = tolower(*psz1++); | |
137 | c2 = tolower(*psz2++); | |
138 | } while ( c1 && (c1 == c2) ); | |
139 | ||
140 | return c1 - c2; | |
dd1eaa89 VZ |
141 | #else |
142 | // almost all compilers/libraries provide this function (unfortunately under | |
143 | // different names), that's why we don't implement our own which will surely | |
144 | // be more efficient than this code (uncomment to use): | |
145 | /* | |
146 | register char c1, c2; | |
147 | do { | |
148 | c1 = tolower(*psz1++); | |
149 | c2 = tolower(*psz2++); | |
150 | } while ( c1 && (c1 == c2) ); | |
151 | ||
152 | return c1 - c2; | |
153 | */ | |
154 | ||
155 | #error "Please define string case-insensitive compare for your OS/compiler" | |
156 | #endif // OS/compiler | |
157 | } | |
c801d85f | 158 | |
f04f3991 | 159 | // return an empty wxString |
f7bd2698 | 160 | class WXDLLEXPORT wxString; // not yet defined |
e90c1d2a | 161 | inline const wxString& wxGetEmptyString() { return *(wxString *)&wxEmptyString; } |
f04f3991 | 162 | |
c801d85f | 163 | // --------------------------------------------------------------------------- |
f04f3991 | 164 | // string data prepended with some housekeeping info (used by wxString class), |
c801d85f KB |
165 | // is never used directly (but had to be put here to allow inlining) |
166 | // --------------------------------------------------------------------------- | |
e90c1d2a | 167 | |
c801d85f KB |
168 | struct WXDLLEXPORT wxStringData |
169 | { | |
170 | int nRefs; // reference count | |
3c024cc2 | 171 | size_t nDataLength, // actual string length |
c801d85f KB |
172 | nAllocLength; // allocated memory size |
173 | ||
2bb67b80 OK |
174 | // mimics declaration 'wxChar data[nAllocLength]' |
175 | wxChar* data() const { return (wxChar*)(this + 1); } | |
c801d85f KB |
176 | |
177 | // empty string has a special ref count so it's never deleted | |
dbda9e86 JS |
178 | bool IsEmpty() const { return (nRefs == -1); } |
179 | bool IsShared() const { return (nRefs > 1); } | |
c801d85f KB |
180 | |
181 | // lock/unlock | |
dd1eaa89 VZ |
182 | void Lock() { if ( !IsEmpty() ) nRefs++; } |
183 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); } | |
8fd0f20b | 184 | |
dd1eaa89 | 185 | // if we had taken control over string memory (GetWriteBuf), it's |
8fd0f20b | 186 | // intentionally put in invalid state |
dbda9e86 JS |
187 | void Validate(bool b) { nRefs = (b ? 1 : 0); } |
188 | bool IsValid() const { return (nRefs != 0); } | |
c801d85f KB |
189 | }; |
190 | ||
c801d85f | 191 | // --------------------------------------------------------------------------- |
3c67202d VZ |
192 | // This is (yet another one) String class for C++ programmers. It doesn't use |
193 | // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...) | |
194 | // thus you should be able to compile it with practicaly any C++ compiler. | |
195 | // This class uses copy-on-write technique, i.e. identical strings share the | |
196 | // same memory as long as neither of them is changed. | |
197 | // | |
198 | // This class aims to be as compatible as possible with the new standard | |
199 | // std::string class, but adds some additional functions and should be at | |
200 | // least as efficient than the standard implementation. | |
201 | // | |
202 | // Performance note: it's more efficient to write functions which take "const | |
203 | // String&" arguments than "const char *" if you assign the argument to | |
204 | // another string. | |
205 | // | |
206 | // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5. | |
207 | // | |
208 | // To do: | |
209 | // - ressource support (string tables in ressources) | |
210 | // - more wide character (UNICODE) support | |
211 | // - regular expressions support | |
c801d85f | 212 | // --------------------------------------------------------------------------- |
3c67202d | 213 | |
c801d85f | 214 | #ifdef WXSTRING_IS_WXOBJECT |
3c67202d VZ |
215 | class WXDLLEXPORT wxString : public wxObject |
216 | { | |
c801d85f KB |
217 | DECLARE_DYNAMIC_CLASS(wxString) |
218 | #else //WXSTRING_IS_WXOBJECT | |
3c67202d VZ |
219 | class WXDLLEXPORT wxString |
220 | { | |
c801d85f KB |
221 | #endif //WXSTRING_IS_WXOBJECT |
222 | ||
fbcb4166 | 223 | friend class WXDLLEXPORT wxArrayString; |
c801d85f | 224 | |
3c67202d VZ |
225 | // NB: special care was taken in arranging the member functions in such order |
226 | // that all inline functions can be effectively inlined, verify that all | |
227 | // performace critical functions are still inlined if you change order! | |
dd1eaa89 VZ |
228 | private: |
229 | // points to data preceded by wxStringData structure with ref count info | |
2bb67b80 | 230 | wxChar *m_pchData; |
dd1eaa89 VZ |
231 | |
232 | // accessor to string data | |
233 | wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; } | |
234 | ||
6b95b20d VZ |
235 | // string (re)initialization functions |
236 | // initializes the string to the empty value (must be called only from | |
237 | // ctors, use Reinit() otherwise) | |
e90c1d2a | 238 | void Init() { m_pchData = (wxChar *)wxEmptyString; } |
6b95b20d | 239 | // initializaes the string with (a part of) C-string |
2bb67b80 | 240 | void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = wxSTRING_MAXLEN); |
6b95b20d VZ |
241 | // as Init, but also frees old data |
242 | void Reinit() { GetStringData()->Unlock(); Init(); } | |
243 | ||
244 | // memory allocation | |
245 | // allocates memory for string of lenght nLen | |
246 | void AllocBuffer(size_t nLen); | |
247 | // copies data to another string | |
248 | void AllocCopy(wxString&, int, int) const; | |
249 | // effectively copies data to string | |
2bb67b80 | 250 | void AssignCopy(size_t, const wxChar *); |
6b95b20d VZ |
251 | |
252 | // append a (sub)string | |
2bb67b80 | 253 | void ConcatSelf(int nLen, const wxChar *src); |
6b95b20d VZ |
254 | |
255 | // functions called before writing to the string: they copy it if there | |
256 | // are other references to our data (should be the only owner when writing) | |
257 | void CopyBeforeWrite(); | |
258 | void AllocBeforeWrite(size_t); | |
259 | ||
c606a9a4 VZ |
260 | // this method is not implemented - there is _no_ conversion from int to |
261 | // string, you're doing something wrong if the compiler wants to call it! | |
262 | // | |
263 | // try `s << i' or `s.Printf("%d", i)' instead | |
264 | wxString(int); | |
265 | wxString(long); | |
266 | ||
c801d85f | 267 | public: |
3c67202d VZ |
268 | // constructors and destructor |
269 | // ctor for an empty string | |
6b95b20d | 270 | wxString() { Init(); } |
3c67202d | 271 | // copy ctor |
6b95b20d VZ |
272 | wxString(const wxString& stringSrc) |
273 | { | |
274 | wxASSERT( stringSrc.GetStringData()->IsValid() ); | |
275 | ||
276 | if ( stringSrc.IsEmpty() ) { | |
277 | // nothing to do for an empty string | |
278 | Init(); | |
279 | } | |
280 | else { | |
281 | m_pchData = stringSrc.m_pchData; // share same data | |
282 | GetStringData()->Lock(); // => one more copy | |
283 | } | |
284 | } | |
3c67202d | 285 | // string containing nRepeat copies of ch |
2bb67b80 | 286 | wxString(wxChar ch, size_t nRepeat = 1); |
3c67202d | 287 | // ctor takes first nLength characters from C string |
566b84d2 | 288 | // (default value of wxSTRING_MAXLEN means take all the string) |
2bb67b80 | 289 | wxString(const wxChar *psz, size_t nLength = wxSTRING_MAXLEN) |
6b95b20d | 290 | { InitWith(psz, 0, nLength); } |
e90c1d2a | 291 | |
2bb67b80 OK |
292 | #if wxUSE_UNICODE |
293 | // from multibyte string | |
294 | // (NB: nLength is right now number of Unicode characters, not | |
295 | // characters in psz! So try not to use it yet!) | |
5f709e67 | 296 | wxString(const char *psz, wxMBConv& conv = wxConvLibc, size_t nLength = wxSTRING_MAXLEN); |
2bb67b80 OK |
297 | // from wxWCharBuffer (i.e. return from wxGetString) |
298 | wxString(const wxWCharBuffer& psz) | |
299 | { InitWith(psz, 0, wxSTRING_MAXLEN); } | |
e90c1d2a | 300 | #else // ANSI |
3c67202d | 301 | // from C string (for compilers using unsigned char) |
2bb67b80 OK |
302 | wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN) |
303 | { InitWith((const char*)psz, 0, nLength); } | |
3a309a64 OK |
304 | // from multibyte string |
305 | wxString(const char *psz, wxMBConv& WXUNUSED(conv), size_t nLength = wxSTRING_MAXLEN) | |
306 | { InitWith(psz, 0, nLength); } | |
e90c1d2a | 307 | |
6f841509 | 308 | #if wxUSE_WCHAR_T |
2bb67b80 | 309 | // from wide (Unicode) string |
c801d85f | 310 | wxString(const wchar_t *pwz); |
e90c1d2a VZ |
311 | #endif // !wxUSE_WCHAR_T |
312 | ||
2bb67b80 OK |
313 | // from wxCharBuffer |
314 | wxString(const wxCharBuffer& psz) | |
315 | { InitWith(psz, 0, wxSTRING_MAXLEN); } | |
e90c1d2a VZ |
316 | #endif // Unicode/ANSI |
317 | ||
3c67202d | 318 | // dtor is not virtual, this class must not be inherited from! |
6b95b20d | 319 | ~wxString() { GetStringData()->Unlock(); } |
c801d85f | 320 | |
3c67202d VZ |
321 | // generic attributes & operations |
322 | // as standard strlen() | |
47d67540 | 323 | size_t Len() const { return GetStringData()->nDataLength; } |
3c67202d | 324 | // string contains any characters? |
dd1eaa89 | 325 | bool IsEmpty() const { return Len() == 0; } |
dcfde592 VZ |
326 | // empty string is "FALSE", so !str will return TRUE |
327 | bool operator!() const { return IsEmpty(); } | |
3c67202d | 328 | // empty string contents |
dd1eaa89 VZ |
329 | void Empty() |
330 | { | |
2c3b684c | 331 | if ( !IsEmpty() ) |
dd1eaa89 VZ |
332 | Reinit(); |
333 | ||
7be07660 | 334 | // should be empty |
dd1eaa89 | 335 | wxASSERT( GetStringData()->nDataLength == 0 ); |
7be07660 | 336 | } |
3c67202d | 337 | // empty the string and free memory |
7be07660 VZ |
338 | void Clear() |
339 | { | |
340 | if ( !GetStringData()->IsEmpty() ) | |
341 | Reinit(); | |
342 | ||
343 | wxASSERT( GetStringData()->nDataLength == 0 ); // should be empty | |
344 | wxASSERT( GetStringData()->nAllocLength == 0 ); // and not own any memory | |
dd1eaa89 VZ |
345 | } |
346 | ||
3c67202d VZ |
347 | // contents test |
348 | // Is an ascii value | |
c801d85f | 349 | bool IsAscii() const; |
3c67202d | 350 | // Is a number |
c801d85f | 351 | bool IsNumber() const; |
3c67202d | 352 | // Is a word |
c801d85f | 353 | bool IsWord() const; |
c801d85f | 354 | |
3c67202d VZ |
355 | // data access (all indexes are 0 based) |
356 | // read access | |
2bb67b80 | 357 | wxChar GetChar(size_t n) const |
dd1eaa89 | 358 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
3c67202d | 359 | // read/write access |
2bb67b80 | 360 | wxChar& GetWritableChar(size_t n) |
dd1eaa89 | 361 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
3c67202d | 362 | // write access |
2bb67b80 | 363 | void SetChar(size_t n, wxChar ch) |
c801d85f KB |
364 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; } |
365 | ||
3c67202d | 366 | // get last character |
2bb67b80 | 367 | wxChar Last() const |
c801d85f | 368 | { wxASSERT( !IsEmpty() ); return m_pchData[Len() - 1]; } |
3c67202d | 369 | // get writable last character |
2bb67b80 | 370 | wxChar& Last() |
c801d85f KB |
371 | { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData[Len()-1]; } |
372 | ||
57493f9f VZ |
373 | // under Unix it is tested with configure, assume it works on other |
374 | // platforms (there might be overloading problems if size_t and int are | |
375 | // the same type) | |
227b5cd7 | 376 | #if !defined(__UNIX__) || wxUSE_SIZE_T_STRING_OPERATOR |
3c67202d | 377 | // operator version of GetChar |
2bb67b80 | 378 | wxChar operator[](size_t n) const |
c801d85f | 379 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
22552603 | 380 | #endif |
c5248639 | 381 | |
3c67202d | 382 | // operator version of GetChar |
2bb67b80 | 383 | wxChar operator[](int n) const |
c801d85f | 384 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
3c67202d | 385 | // operator version of GetWritableChar |
2bb67b80 | 386 | wxChar& operator[](size_t n) |
c801d85f KB |
387 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
388 | ||
3c67202d | 389 | // implicit conversion to C string |
2bb67b80 | 390 | operator const wxChar*() const { return m_pchData; } |
3c67202d | 391 | // explicit conversion to C string (use this with printf()!) |
2bb67b80 | 392 | const wxChar* c_str() const { return m_pchData; } |
6f841509 OK |
393 | // (and this with [wx]Printf()!) |
394 | const wxChar* wx_str() const { return m_pchData; } | |
e90c1d2a | 395 | // identical to c_str() |
2bb67b80 | 396 | const wxChar* GetData() const { return m_pchData; } |
e90c1d2a VZ |
397 | |
398 | // conversions with (possible) format convertions: have to return a | |
399 | // buffer with temporary data | |
2bb67b80 | 400 | #if wxUSE_UNICODE |
5f709e67 | 401 | const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const { return conv.cWC2MB(m_pchData); } |
e90c1d2a VZ |
402 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
403 | ||
5f709e67 | 404 | const wxChar* wc_str(wxMBConv& WXUNUSED(conv) = wxConvLibc) const { return m_pchData; } |
e90c1d2a | 405 | |
2bb67b80 | 406 | #if wxMBFILES |
5f709e67 | 407 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 408 | #else // !wxMBFILES |
2bb67b80 | 409 | const wxChar* fn_str() const { return m_pchData; } |
e90c1d2a VZ |
410 | #endif // wxMBFILES/!wxMBFILES |
411 | #else // ANSI | |
412 | #if wxUSE_MULTIBYTE | |
413 | const wxChar* mb_str(wxMBConv& WXUNUSED(conv) = wxConvLibc) const | |
414 | { return m_pchData; } | |
415 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } | |
416 | #else // !mmultibyte | |
417 | const wxChar* mb_str() const { return m_pchData; } | |
418 | const wxWX2MBbuf mbc_str() const { return mb_str(); } | |
419 | #endif // multibyte/!multibyte | |
6f841509 | 420 | #if wxUSE_WCHAR_T |
2bb67b80 | 421 | const wxWCharBuffer wc_str(wxMBConv& conv) const { return conv.cMB2WC(m_pchData); } |
e90c1d2a | 422 | #endif // wxUSE_WCHAR_T |
2bb67b80 | 423 | const wxChar* fn_str() const { return m_pchData; } |
e90c1d2a | 424 | #endif // Unicode/ANSI |
c801d85f | 425 | |
3c67202d VZ |
426 | // overloaded assignment |
427 | // from another wxString | |
c801d85f | 428 | wxString& operator=(const wxString& stringSrc); |
3c67202d | 429 | // from a character |
2bb67b80 | 430 | wxString& operator=(wxChar ch); |
3c67202d | 431 | // from a C string |
2bb67b80 | 432 | wxString& operator=(const wxChar *psz); |
111bb7f2 OK |
433 | #if wxUSE_UNICODE |
434 | // from wxWCharBuffer | |
435 | wxString& operator=(const wxWCharBuffer& psz) { return operator=((const wchar_t *)psz); } | |
e90c1d2a | 436 | #else // ANSI |
3c67202d | 437 | // from another kind of C string |
c801d85f | 438 | wxString& operator=(const unsigned char* psz); |
6f841509 | 439 | #if wxUSE_WCHAR_T |
3c67202d | 440 | // from a wide string |
c801d85f | 441 | wxString& operator=(const wchar_t *pwz); |
6f841509 | 442 | #endif |
111bb7f2 OK |
443 | // from wxCharBuffer |
444 | wxString& operator=(const wxCharBuffer& psz) { return operator=((const char *)psz); } | |
e90c1d2a | 445 | #endif // Unicode/ANSI |
3c67202d VZ |
446 | |
447 | // string concatenation | |
448 | // in place concatenation | |
449 | /* | |
450 | Concatenate and return the result. Note that the left to right | |
451 | associativity of << allows to write things like "str << str1 << str2 | |
452 | << ..." (unlike with +=) | |
453 | */ | |
454 | // string += string | |
dd1eaa89 VZ |
455 | wxString& operator<<(const wxString& s) |
456 | { | |
457 | wxASSERT( s.GetStringData()->IsValid() ); | |
458 | ||
459 | ConcatSelf(s.Len(), s); | |
460 | return *this; | |
461 | } | |
3c67202d | 462 | // string += C string |
2bb67b80 OK |
463 | wxString& operator<<(const wxChar *psz) |
464 | { ConcatSelf(wxStrlen(psz), psz); return *this; } | |
3c67202d | 465 | // string += char |
2bb67b80 | 466 | wxString& operator<<(wxChar ch) { ConcatSelf(1, &ch); return *this; } |
dd1eaa89 | 467 | |
3c67202d | 468 | // string += string |
6b95b20d | 469 | void operator+=(const wxString& s) { (void)operator<<(s); } |
3c67202d | 470 | // string += C string |
2bb67b80 | 471 | void operator+=(const wxChar *psz) { (void)operator<<(psz); } |
3c67202d | 472 | // string += char |
2bb67b80 OK |
473 | void operator+=(wxChar ch) { (void)operator<<(ch); } |
474 | ||
475 | // string += buffer (i.e. from wxGetString) | |
476 | #if wxUSE_UNICODE | |
477 | wxString& operator<<(const wxWCharBuffer& s) { (void)operator<<((const wchar_t *)s); return *this; } | |
478 | void operator+=(const wxWCharBuffer& s) { (void)operator<<((const wchar_t *)s); } | |
479 | #else | |
480 | wxString& operator<<(const wxCharBuffer& s) { (void)operator<<((const char *)s); return *this; } | |
481 | void operator+=(const wxCharBuffer& s) { (void)operator<<((const char *)s); } | |
482 | #endif | |
6b95b20d | 483 | |
3c67202d | 484 | // string += C string |
2bb67b80 OK |
485 | wxString& Append(const wxChar* psz) |
486 | { ConcatSelf(wxStrlen(psz), psz); return *this; } | |
3c67202d | 487 | // append count copies of given character |
2bb67b80 | 488 | wxString& Append(wxChar ch, size_t count = 1u) |
3c67202d VZ |
489 | { wxString str(ch, count); return *this << str; } |
490 | ||
491 | // prepend a string, return the string itself | |
492 | wxString& Prepend(const wxString& str) | |
493 | { *this = str + *this; return *this; } | |
494 | ||
495 | // non-destructive concatenation | |
496 | // | |
c33534e5 | 497 | friend wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2); |
3c67202d | 498 | // |
2bb67b80 | 499 | friend wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch); |
3c67202d | 500 | // |
2bb67b80 | 501 | friend wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string); |
3c67202d | 502 | // |
2bb67b80 | 503 | friend wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz); |
3c67202d | 504 | // |
2bb67b80 | 505 | friend wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string); |
3c67202d VZ |
506 | |
507 | // stream-like functions | |
508 | // insert an int into string | |
509 | wxString& operator<<(int i); | |
510 | // insert a float into string | |
511 | wxString& operator<<(float f); | |
512 | // insert a double into string | |
513 | wxString& operator<<(double d); | |
c84c52de | 514 | |
3c67202d | 515 | // string comparison |
30b21f9a | 516 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
f33fee2a | 517 | int Cmp(const wxChar *psz) const { return wxStrcmp(c_str(), psz); } |
3c67202d | 518 | // same as Cmp() but not case-sensitive |
f33fee2a | 519 | int CmpNoCase(const wxChar *psz) const { return wxStricmp(c_str(), psz); } |
3c67202d VZ |
520 | // test for the string equality, either considering case or not |
521 | // (if compareWithCase then the case matters) | |
2bb67b80 | 522 | bool IsSameAs(const wxChar *psz, bool compareWithCase = TRUE) const |
3c67202d | 523 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } |
f33fee2a VZ |
524 | // comparison with a signle character: returns TRUE if equal |
525 | bool IsSameAs(wxChar c, bool compareWithCase = TRUE) const | |
526 | { | |
527 | return (Len() == 1) && (compareWithCase ? GetChar(0u) == c | |
528 | : wxToupper(GetChar(0u)) == wxToupper(c)); | |
529 | } | |
3c67202d VZ |
530 | |
531 | // simple sub-string extraction | |
532 | // return substring starting at nFirst of length nCount (or till the end | |
533 | // if nCount = default value) | |
566b84d2 | 534 | wxString Mid(size_t nFirst, size_t nCount = wxSTRING_MAXLEN) const; |
3c67202d VZ |
535 | |
536 | // operator version of Mid() | |
537 | wxString operator()(size_t start, size_t len) const | |
538 | { return Mid(start, len); } | |
539 | ||
540 | // get first nCount characters | |
c801d85f | 541 | wxString Left(size_t nCount) const; |
3c67202d | 542 | // get last nCount characters |
c801d85f | 543 | wxString Right(size_t nCount) const; |
3c67202d VZ |
544 | // get all characters before the first occurence of ch |
545 | // (returns the whole string if ch not found) | |
2bb67b80 | 546 | wxString BeforeFirst(wxChar ch) const; |
3c67202d VZ |
547 | // get all characters before the last occurence of ch |
548 | // (returns empty string if ch not found) | |
2bb67b80 | 549 | wxString BeforeLast(wxChar ch) const; |
3c67202d VZ |
550 | // get all characters after the first occurence of ch |
551 | // (returns empty string if ch not found) | |
2bb67b80 | 552 | wxString AfterFirst(wxChar ch) const; |
3c67202d VZ |
553 | // get all characters after the last occurence of ch |
554 | // (returns the whole string if ch not found) | |
2bb67b80 | 555 | wxString AfterLast(wxChar ch) const; |
3c67202d VZ |
556 | |
557 | // for compatibility only, use more explicitly named functions above | |
2bb67b80 OK |
558 | wxString Before(wxChar ch) const { return BeforeLast(ch); } |
559 | wxString After(wxChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
560 | |
561 | // case conversion | |
c84c52de | 562 | // convert to upper case in place, return the string itself |
c801d85f | 563 | wxString& MakeUpper(); |
c84c52de | 564 | // convert to upper case, return the copy of the string |
03ab016d JS |
565 | // Here's something to remember: BC++ doesn't like returns in inlines. |
566 | wxString Upper() const ; | |
c84c52de | 567 | // convert to lower case in place, return the string itself |
c801d85f | 568 | wxString& MakeLower(); |
c84c52de | 569 | // convert to lower case, return the copy of the string |
03ab016d | 570 | wxString Lower() const ; |
c801d85f | 571 | |
3c67202d VZ |
572 | // trimming/padding whitespace (either side) and truncating |
573 | // remove spaces from left or from right (default) side | |
c801d85f | 574 | wxString& Trim(bool bFromRight = TRUE); |
3c67202d | 575 | // add nCount copies chPad in the beginning or at the end (default) |
223d09f6 | 576 | wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = TRUE); |
3c67202d | 577 | // truncate string to given length |
c801d85f | 578 | wxString& Truncate(size_t uiLen); |
dd1eaa89 | 579 | |
3c67202d VZ |
580 | // searching and replacing |
581 | // searching (return starting index, or -1 if not found) | |
2bb67b80 | 582 | int Find(wxChar ch, bool bFromEnd = FALSE) const; // like strchr/strrchr |
3c67202d | 583 | // searching (return starting index, or -1 if not found) |
2bb67b80 | 584 | int Find(const wxChar *pszSub) const; // like strstr |
3c67202d VZ |
585 | // replace first (or all of bReplaceAll) occurences of substring with |
586 | // another string, returns the number of replacements made | |
2bb67b80 OK |
587 | size_t Replace(const wxChar *szOld, |
588 | const wxChar *szNew, | |
3c67202d VZ |
589 | bool bReplaceAll = TRUE); |
590 | ||
591 | // check if the string contents matches a mask containing '*' and '?' | |
2bb67b80 | 592 | bool Matches(const wxChar *szMask) const; |
c801d85f | 593 | |
3c67202d VZ |
594 | // formated input/output |
595 | // as sprintf(), returns the number of characters written or < 0 on error | |
2bb67b80 | 596 | int Printf(const wxChar *pszFormat, ...); |
3c67202d | 597 | // as vprintf(), returns the number of characters written or < 0 on error |
2bb67b80 | 598 | int PrintfV(const wxChar* pszFormat, va_list argptr); |
dd1eaa89 | 599 | |
3c67202d VZ |
600 | // raw access to string memory |
601 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 602 | // only works if the data of this string is not shared |
c86f1403 | 603 | void Alloc(size_t nLen); |
3c67202d | 604 | // minimize the string's memory |
dd1eaa89 VZ |
605 | // only works if the data of this string is not shared |
606 | void Shrink(); | |
3c67202d VZ |
607 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
608 | // a.s.a.p. to put string back in a reasonable state! | |
2bb67b80 | 609 | wxChar *GetWriteBuf(size_t nLen); |
3c67202d | 610 | // call this immediately after GetWriteBuf() has been used |
8fd0f20b | 611 | void UngetWriteBuf(); |
c801d85f | 612 | |
3c67202d VZ |
613 | // wxWindows version 1 compatibility functions |
614 | ||
615 | // use Mid() | |
616 | wxString SubString(size_t from, size_t to) const | |
617 | { return Mid(from, (to - from + 1)); } | |
618 | // values for second parameter of CompareTo function | |
c801d85f | 619 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 620 | // values for first parameter of Strip function |
c801d85f | 621 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 622 | |
3c67202d | 623 | // use Printf() |
2bb67b80 | 624 | int sprintf(const wxChar *pszFormat, ...); |
c801d85f | 625 | |
3c67202d | 626 | // use Cmp() |
2bb67b80 | 627 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 628 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 629 | |
3c67202d | 630 | // use Len |
c801d85f | 631 | size_t Length() const { return Len(); } |
3c67202d | 632 | // Count the number of characters |
2bb67b80 | 633 | int Freq(wxChar ch) const; |
3c67202d | 634 | // use MakeLower |
c801d85f | 635 | void LowerCase() { MakeLower(); } |
3c67202d | 636 | // use MakeUpper |
c801d85f | 637 | void UpperCase() { MakeUpper(); } |
3c67202d | 638 | // use Trim except that it doesn't change this string |
c801d85f KB |
639 | wxString Strip(stripType w = trailing) const; |
640 | ||
3c67202d | 641 | // use Find (more general variants not yet supported) |
2bb67b80 OK |
642 | size_t Index(const wxChar* psz) const { return Find(psz); } |
643 | size_t Index(wxChar ch) const { return Find(ch); } | |
3c67202d | 644 | // use Truncate |
c801d85f KB |
645 | wxString& Remove(size_t pos) { return Truncate(pos); } |
646 | wxString& RemoveLast() { return Truncate(Len() - 1); } | |
647 | ||
3ed358cb | 648 | wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); } |
dd1eaa89 | 649 | |
3c67202d | 650 | // use Find() |
2bb67b80 OK |
651 | int First( const wxChar ch ) const { return Find(ch); } |
652 | int First( const wxChar* psz ) const { return Find(psz); } | |
3ed358cb | 653 | int First( const wxString &str ) const { return Find(str); } |
2bb67b80 | 654 | int Last( const wxChar ch ) const { return Find(ch, TRUE); } |
3c67202d | 655 | bool Contains(const wxString& str) const { return Find(str) != -1; } |
c801d85f | 656 | |
3c67202d | 657 | // use IsEmpty() |
c801d85f | 658 | bool IsNull() const { return IsEmpty(); } |
c801d85f | 659 | |
3c67202d VZ |
660 | #ifdef wxSTD_STRING_COMPATIBILITY |
661 | // std::string compatibility functions | |
dd1eaa89 | 662 | |
2bc07607 VZ |
663 | // standard types |
664 | typedef wxChar value_type; | |
665 | typedef const value_type *const_iterator; | |
666 | ||
3c67202d | 667 | // an 'invalid' value for string index |
c801d85f | 668 | static const size_t npos; |
dd1eaa89 | 669 | |
3c67202d VZ |
670 | // constructors |
671 | // take nLen chars starting at nPos | |
672 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
673 | { | |
674 | wxASSERT( str.GetStringData()->IsValid() ); | |
675 | InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen); | |
676 | } | |
677 | // take all characters from pStart to pEnd | |
678 | wxString(const void *pStart, const void *pEnd); | |
679 | ||
680 | // lib.string.capacity | |
681 | // return the length of the string | |
682 | size_t size() const { return Len(); } | |
683 | // return the length of the string | |
684 | size_t length() const { return Len(); } | |
685 | // return the maximum size of the string | |
566b84d2 | 686 | size_t max_size() const { return wxSTRING_MAXLEN; } |
3c67202d | 687 | // resize the string, filling the space with c if c != 0 |
223d09f6 | 688 | void resize(size_t nSize, wxChar ch = wxT('\0')); |
3c67202d VZ |
689 | // delete the contents of the string |
690 | void clear() { Empty(); } | |
691 | // returns true if the string is empty | |
692 | bool empty() const { return IsEmpty(); } | |
693 | ||
694 | // lib.string.access | |
695 | // return the character at position n | |
2bb67b80 | 696 | wxChar at(size_t n) const { return GetChar(n); } |
3c67202d | 697 | // returns the writable character at position n |
2bb67b80 | 698 | wxChar& at(size_t n) { return GetWritableChar(n); } |
3c67202d | 699 | |
2bc07607 VZ |
700 | // first valid index position |
701 | const_iterator begin() const { return wx_str(); } | |
702 | // position one after the last valid one | |
703 | const_iterator end() const { return wx_str() + length(); } | |
704 | ||
3c67202d VZ |
705 | // lib.string.modifiers |
706 | // append a string | |
707 | wxString& append(const wxString& str) | |
708 | { *this += str; return *this; } | |
709 | // append elements str[pos], ..., str[pos+n] | |
710 | wxString& append(const wxString& str, size_t pos, size_t n) | |
711 | { ConcatSelf(n, str.c_str() + pos); return *this; } | |
712 | // append first n (or all if n == npos) characters of sz | |
2bb67b80 OK |
713 | wxString& append(const wxChar *sz, size_t n = npos) |
714 | { ConcatSelf(n == npos ? wxStrlen(sz) : n, sz); return *this; } | |
3c67202d VZ |
715 | |
716 | // append n copies of ch | |
2bb67b80 | 717 | wxString& append(size_t n, wxChar ch) { return Pad(n, ch); } |
3c67202d VZ |
718 | |
719 | // same as `this_string = str' | |
720 | wxString& assign(const wxString& str) { return (*this) = str; } | |
721 | // same as ` = str[pos..pos + n] | |
722 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
2bb67b80 | 723 | { return *this = wxString((const wxChar *)str + pos, n); } |
3c67202d | 724 | // same as `= first n (or all if n == npos) characters of sz' |
2bb67b80 | 725 | wxString& assign(const wxChar *sz, size_t n = npos) |
3c67202d VZ |
726 | { return *this = wxString(sz, n); } |
727 | // same as `= n copies of ch' | |
2bb67b80 | 728 | wxString& assign(size_t n, wxChar ch) |
3c67202d VZ |
729 | { return *this = wxString(ch, n); } |
730 | ||
731 | // insert another string | |
732 | wxString& insert(size_t nPos, const wxString& str); | |
733 | // insert n chars of str starting at nStart (in str) | |
734 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
2bb67b80 | 735 | { return insert(nPos, wxString((const wxChar *)str + nStart, n)); } |
3c67202d VZ |
736 | |
737 | // insert first n (or all if n == npos) characters of sz | |
2bb67b80 | 738 | wxString& insert(size_t nPos, const wxChar *sz, size_t n = npos) |
3c67202d VZ |
739 | { return insert(nPos, wxString(sz, n)); } |
740 | // insert n copies of ch | |
2bb67b80 | 741 | wxString& insert(size_t nPos, size_t n, wxChar ch) |
3c67202d VZ |
742 | { return insert(nPos, wxString(ch, n)); } |
743 | ||
744 | // delete characters from nStart to nStart + nLen | |
745 | wxString& erase(size_t nStart = 0, size_t nLen = npos); | |
746 | ||
747 | // replaces the substring of length nLen starting at nStart | |
2bb67b80 | 748 | wxString& replace(size_t nStart, size_t nLen, const wxChar* sz); |
3c67202d | 749 | // replaces the substring with nCount copies of ch |
2bb67b80 | 750 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch); |
3c67202d VZ |
751 | // replaces a substring with another substring |
752 | wxString& replace(size_t nStart, size_t nLen, | |
753 | const wxString& str, size_t nStart2, size_t nLen2); | |
754 | // replaces the substring with first nCount chars of sz | |
755 | wxString& replace(size_t nStart, size_t nLen, | |
2bb67b80 | 756 | const wxChar* sz, size_t nCount); |
3c67202d VZ |
757 | |
758 | // swap two strings | |
759 | void swap(wxString& str); | |
760 | ||
761 | // All find() functions take the nStart argument which specifies the | |
762 | // position to start the search on, the default value is 0. All functions | |
763 | // return npos if there were no match. | |
764 | ||
765 | // find a substring | |
766 | size_t find(const wxString& str, size_t nStart = 0) const; | |
767 | ||
768 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 769 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d | 770 | // find first n characters of sz |
2bb67b80 | 771 | size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; |
6b0eb19f | 772 | #endif |
3c67202d VZ |
773 | |
774 | // Gives a duplicate symbol (presumably a case-insensitivity problem) | |
62448488 | 775 | #if !defined(__BORLANDC__) |
3c67202d | 776 | // find the first occurence of character ch after nStart |
2bb67b80 | 777 | size_t find(wxChar ch, size_t nStart = 0) const; |
62448488 | 778 | #endif |
3c67202d VZ |
779 | // rfind() family is exactly like find() but works right to left |
780 | ||
781 | // as find, but from the end | |
782 | size_t rfind(const wxString& str, size_t nStart = npos) const; | |
783 | ||
784 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 785 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d | 786 | // as find, but from the end |
2bb67b80 | 787 | size_t rfind(const wxChar* sz, size_t nStart = npos, |
3c67202d VZ |
788 | size_t n = npos) const; |
789 | // as find, but from the end | |
2bb67b80 | 790 | size_t rfind(wxChar ch, size_t nStart = npos) const; |
c801d85f | 791 | #endif |
3c67202d VZ |
792 | |
793 | // find first/last occurence of any character in the set | |
794 | ||
969d318c VZ |
795 | // as strpbrk() but starts at nStart, returns npos if not found |
796 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
797 | { return find_first_of(str.c_str(), nStart); } | |
798 | // same as above | |
2bb67b80 | 799 | size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; |
3c67202d | 800 | // same as find(char, size_t) |
969d318c VZ |
801 | size_t find_first_of(wxChar c, size_t nStart = 0) const |
802 | { return find(c, nStart); } | |
803 | // find the last (starting from nStart) char from str in this string | |
804 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
805 | { return find_last_of(str.c_str(), nStart); } | |
806 | // same as above | |
807 | size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; | |
808 | // same as above | |
809 | size_t find_last_of(wxChar c, size_t nStart = npos) const | |
810 | { return rfind(c, nStart); } | |
3c67202d VZ |
811 | |
812 | // find first/last occurence of any character not in the set | |
813 | ||
969d318c VZ |
814 | // as strspn() (starting from nStart), returns npos on failure |
815 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
816 | { return find_first_not_of(str.c_str(), nStart); } | |
817 | // same as above | |
818 | size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; | |
819 | // same as above | |
2bb67b80 | 820 | size_t find_first_not_of(wxChar ch, size_t nStart = 0) const; |
969d318c | 821 | // as strcspn() |
3c67202d | 822 | size_t find_last_not_of(const wxString& str, size_t nStart=npos) const; |
969d318c VZ |
823 | // same as above |
824 | size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; | |
825 | // same as above | |
2bb67b80 | 826 | size_t find_last_not_of(wxChar ch, size_t nStart = npos) const; |
3c67202d VZ |
827 | |
828 | // All compare functions return -1, 0 or 1 if the [sub]string is less, | |
829 | // equal or greater than the compare() argument. | |
830 | ||
831 | // just like strcmp() | |
832 | int compare(const wxString& str) const { return Cmp(str); } | |
833 | // comparison with a substring | |
834 | int compare(size_t nStart, size_t nLen, const wxString& str) const; | |
835 | // comparison of 2 substrings | |
836 | int compare(size_t nStart, size_t nLen, | |
837 | const wxString& str, size_t nStart2, size_t nLen2) const; | |
838 | // just like strcmp() | |
2bb67b80 | 839 | int compare(const wxChar* sz) const { return Cmp(sz); } |
3c67202d VZ |
840 | // substring comparison with first nCount characters of sz |
841 | int compare(size_t nStart, size_t nLen, | |
2bb67b80 | 842 | const wxChar* sz, size_t nCount = npos) const; |
3c67202d VZ |
843 | |
844 | // substring extraction | |
845 | wxString substr(size_t nStart = 0, size_t nLen = npos) const; | |
846 | #endif // wxSTD_STRING_COMPATIBILITY | |
c801d85f KB |
847 | }; |
848 | ||
849 | // ---------------------------------------------------------------------------- | |
3c67202d VZ |
850 | // The string array uses it's knowledge of internal structure of the wxString |
851 | // class to optimize string storage. Normally, we would store pointers to | |
852 | // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is | |
853 | // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is | |
854 | // really all we need to turn such pointer into a string! | |
855 | // | |
856 | // Of course, it can be called a dirty hack, but we use twice less memory and | |
857 | // this approach is also more speed efficient, so it's probably worth it. | |
858 | // | |
859 | // Usage notes: when a string is added/inserted, a new copy of it is created, | |
860 | // so the original string may be safely deleted. When a string is retrieved | |
861 | // from the array (operator[] or Item() method), a reference is returned. | |
c801d85f | 862 | // ---------------------------------------------------------------------------- |
e90c1d2a | 863 | |
fbcb4166 | 864 | class WXDLLEXPORT wxArrayString |
c801d85f KB |
865 | { |
866 | public: | |
30b21f9a VZ |
867 | // type of function used by wxArrayString::Sort() |
868 | typedef int (*CompareFunction)(const wxString& first, | |
869 | const wxString& second); | |
870 | ||
3c67202d VZ |
871 | // constructors and destructor |
872 | // default ctor | |
c801d85f | 873 | wxArrayString(); |
3c67202d | 874 | // copy ctor |
c801d85f | 875 | wxArrayString(const wxArrayString& array); |
3c67202d | 876 | // assignment operator |
c801d85f | 877 | wxArrayString& operator=(const wxArrayString& src); |
3c67202d | 878 | // not virtual, this class should not be derived from |
c801d85f | 879 | ~wxArrayString(); |
c801d85f | 880 | |
3c67202d VZ |
881 | // memory management |
882 | // empties the list, but doesn't release memory | |
c801d85f | 883 | void Empty(); |
3c67202d | 884 | // empties the list and releases memory |
c801d85f | 885 | void Clear(); |
3c67202d | 886 | // preallocates memory for given number of items |
c801d85f | 887 | void Alloc(size_t nCount); |
3c67202d | 888 | // minimzes the memory usage (by freeing all extra memory) |
dd1eaa89 | 889 | void Shrink(); |
3c67202d VZ |
890 | |
891 | // simple accessors | |
892 | // number of elements in the array | |
893 | size_t GetCount() const { return m_nCount; } | |
894 | // is it empty? | |
895 | bool IsEmpty() const { return m_nCount == 0; } | |
896 | // number of elements in the array (GetCount is preferred API) | |
897 | size_t Count() const { return m_nCount; } | |
898 | ||
899 | // items access (range checking is done in debug version) | |
900 | // get item at position uiIndex | |
c801d85f KB |
901 | wxString& Item(size_t nIndex) const |
902 | { wxASSERT( nIndex < m_nCount ); return *(wxString *)&(m_pItems[nIndex]); } | |
3c67202d | 903 | // same as Item() |
c801d85f | 904 | wxString& operator[](size_t nIndex) const { return Item(nIndex); } |
3c67202d | 905 | // get last item |
c801d85f | 906 | wxString& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); } |
3c67202d VZ |
907 | |
908 | // item management | |
909 | // Search the element in the array, starting from the beginning if | |
910 | // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case | |
911 | // sensitive (default). Returns index of the first item matched or | |
912 | // wxNOT_FOUND | |
2bb67b80 | 913 | int Index (const wxChar *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const; |
3c67202d VZ |
914 | // add new element at the end |
915 | void Add(const wxString& str); | |
916 | // add new element at given position | |
c86f1403 | 917 | void Insert(const wxString& str, size_t uiIndex); |
3c67202d | 918 | // remove first item matching this value |
2bb67b80 | 919 | void Remove(const wxChar *sz); |
3c67202d | 920 | // remove item by index |
c801d85f | 921 | void Remove(size_t nIndex); |
c801d85f | 922 | |
30b21f9a VZ |
923 | // sorting |
924 | // sort array elements in alphabetical order (or reversed alphabetical | |
925 | // order if reverseOrder parameter is TRUE) | |
926 | void Sort(bool reverseOrder = FALSE); | |
927 | // sort array elements using specified comparaison function | |
928 | void Sort(CompareFunction compareFunction); | |
c801d85f KB |
929 | |
930 | private: | |
931 | void Grow(); // makes array bigger if needed | |
932 | void Free(); // free the string stored | |
933 | ||
30b21f9a VZ |
934 | void DoSort(); // common part of all Sort() variants |
935 | ||
3c67202d | 936 | size_t m_nSize, // current size of the array |
c801d85f KB |
937 | m_nCount; // current number of elements |
938 | ||
2bb67b80 | 939 | wxChar **m_pItems; // pointer to data |
c801d85f KB |
940 | }; |
941 | ||
c801d85f | 942 | // --------------------------------------------------------------------------- |
3c67202d | 943 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 944 | // --------------------------------------------------------------------------- |
f33fee2a | 945 | |
3c67202d | 946 | // |
a3ef5bf5 | 947 | inline bool operator==(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) == 0); } |
3c67202d | 948 | // |
2bb67b80 | 949 | inline bool operator==(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) == 0); } |
3c67202d | 950 | // |
2bb67b80 | 951 | inline bool operator==(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) == 0); } |
3c67202d | 952 | // |
a3ef5bf5 | 953 | inline bool operator!=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) != 0); } |
3c67202d | 954 | // |
2bb67b80 | 955 | inline bool operator!=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) != 0); } |
3c67202d | 956 | // |
2bb67b80 | 957 | inline bool operator!=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) != 0); } |
3c67202d | 958 | // |
a3ef5bf5 | 959 | inline bool operator< (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) < 0); } |
3c67202d | 960 | // |
2bb67b80 | 961 | inline bool operator< (const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) < 0); } |
3c67202d | 962 | // |
2bb67b80 | 963 | inline bool operator< (const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) > 0); } |
3c67202d | 964 | // |
a3ef5bf5 | 965 | inline bool operator> (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) > 0); } |
3c67202d | 966 | // |
2bb67b80 | 967 | inline bool operator> (const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) > 0); } |
3c67202d | 968 | // |
2bb67b80 | 969 | inline bool operator> (const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) < 0); } |
3c67202d | 970 | // |
a3ef5bf5 | 971 | inline bool operator<=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) <= 0); } |
3c67202d | 972 | // |
2bb67b80 | 973 | inline bool operator<=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) <= 0); } |
3c67202d | 974 | // |
2bb67b80 | 975 | inline bool operator<=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) >= 0); } |
3c67202d | 976 | // |
a3ef5bf5 | 977 | inline bool operator>=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) >= 0); } |
3c67202d | 978 | // |
2bb67b80 | 979 | inline bool operator>=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) >= 0); } |
3c67202d | 980 | // |
2bb67b80 | 981 | inline bool operator>=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) <= 0); } |
3c67202d | 982 | |
f33fee2a VZ |
983 | // comparison with char |
984 | inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); } | |
985 | inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); } | |
986 | inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); } | |
987 | inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } | |
988 | ||
5ca07d0f OK |
989 | #if wxUSE_UNICODE |
990 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 991 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 992 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 993 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
5ca07d0f OK |
994 | #else |
995 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) | |
f33fee2a | 996 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 997 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 998 | { return (s2.Cmp((const char *)s1) == 0); } |
5ca07d0f OK |
999 | #endif |
1000 | ||
ed7174ba | 1001 | wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2); |
2bb67b80 OK |
1002 | wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch); |
1003 | wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string); | |
1004 | wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz); | |
1005 | wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string); | |
028a2b5d | 1006 | #if wxUSE_UNICODE |
c6cee672 | 1007 | inline wxString WXDLLEXPORT operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 1008 | { return string + (const wchar_t *)buf; } |
c6cee672 | 1009 | inline wxString WXDLLEXPORT operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 1010 | { return (const wchar_t *)buf + string; } |
028a2b5d | 1011 | #else |
c6cee672 | 1012 | inline wxString WXDLLEXPORT operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 1013 | { return string + (const char *)buf; } |
c6cee672 | 1014 | inline wxString WXDLLEXPORT operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 1015 | { return (const char *)buf + string; } |
028a2b5d | 1016 | #endif |
f04f3991 | 1017 | |
c801d85f | 1018 | // --------------------------------------------------------------------------- |
3c67202d | 1019 | // Implementation only from here until the end of file |
c801d85f KB |
1020 | // --------------------------------------------------------------------------- |
1021 | ||
e90c1d2a VZ |
1022 | // don't pollute the library user's name space |
1023 | #undef ASSERT_VALID_INDEX | |
1024 | ||
38830220 | 1025 | #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM |
c801d85f | 1026 | |
3f4a0c5b | 1027 | #include "wx/ioswrap.h" |
c801d85f | 1028 | |
184b5d99 | 1029 | WXDLLEXPORT istream& operator>>(istream& is, wxString& str); |
c801d85f | 1030 | |
3c67202d | 1031 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 1032 | |
34138703 | 1033 | #endif // _WX_WXSTRINGH__ |