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