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