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