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