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