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