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