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