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