]>
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 KB |
58 | // (as sizeof(StringData) is unknown here we substract 100) |
59 | #define STRING_MAXLEN (UINT_MAX - 100) | |
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 | |
209 | void InitWith(const char *psz, size_t nPos = 0, size_t nLen = STRING_MAXLEN); | |
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 | ||
c801d85f | 229 | public: |
3c67202d VZ |
230 | // constructors and destructor |
231 | // ctor for an empty string | |
6b95b20d | 232 | wxString() { Init(); } |
3c67202d | 233 | // copy ctor |
6b95b20d VZ |
234 | wxString(const wxString& stringSrc) |
235 | { | |
236 | wxASSERT( stringSrc.GetStringData()->IsValid() ); | |
237 | ||
238 | if ( stringSrc.IsEmpty() ) { | |
239 | // nothing to do for an empty string | |
240 | Init(); | |
241 | } | |
242 | else { | |
243 | m_pchData = stringSrc.m_pchData; // share same data | |
244 | GetStringData()->Lock(); // => one more copy | |
245 | } | |
246 | } | |
3c67202d | 247 | // string containing nRepeat copies of ch |
dd1eaa89 | 248 | wxString(char ch, size_t nRepeat = 1); |
3c67202d | 249 | // ctor takes first nLength characters from C string |
6b95b20d VZ |
250 | // (default value of STRING_MAXLEN means take all the string) |
251 | wxString(const char *psz, size_t nLength = STRING_MAXLEN) | |
252 | { InitWith(psz, 0, nLength); } | |
3c67202d | 253 | // from C string (for compilers using unsigned char) |
c801d85f | 254 | wxString(const unsigned char* psz, size_t nLength = STRING_MAXLEN); |
3c67202d | 255 | // from wide (UNICODE) string |
c801d85f | 256 | wxString(const wchar_t *pwz); |
3c67202d | 257 | // dtor is not virtual, this class must not be inherited from! |
6b95b20d | 258 | ~wxString() { GetStringData()->Unlock(); } |
c801d85f | 259 | |
3c67202d VZ |
260 | // generic attributes & operations |
261 | // as standard strlen() | |
47d67540 | 262 | size_t Len() const { return GetStringData()->nDataLength; } |
3c67202d | 263 | // string contains any characters? |
dd1eaa89 | 264 | bool IsEmpty() const { return Len() == 0; } |
dcfde592 VZ |
265 | // empty string is "FALSE", so !str will return TRUE |
266 | bool operator!() const { return IsEmpty(); } | |
3c67202d | 267 | // empty string contents |
dd1eaa89 VZ |
268 | void Empty() |
269 | { | |
2c3b684c | 270 | if ( !IsEmpty() ) |
dd1eaa89 VZ |
271 | Reinit(); |
272 | ||
7be07660 | 273 | // should be empty |
dd1eaa89 | 274 | wxASSERT( GetStringData()->nDataLength == 0 ); |
7be07660 | 275 | } |
3c67202d | 276 | // empty the string and free memory |
7be07660 VZ |
277 | void Clear() |
278 | { | |
279 | if ( !GetStringData()->IsEmpty() ) | |
280 | Reinit(); | |
281 | ||
282 | wxASSERT( GetStringData()->nDataLength == 0 ); // should be empty | |
283 | wxASSERT( GetStringData()->nAllocLength == 0 ); // and not own any memory | |
dd1eaa89 VZ |
284 | } |
285 | ||
3c67202d VZ |
286 | // contents test |
287 | // Is an ascii value | |
c801d85f | 288 | bool IsAscii() const; |
3c67202d | 289 | // Is a number |
c801d85f | 290 | bool IsNumber() const; |
3c67202d | 291 | // Is a word |
c801d85f | 292 | bool IsWord() const; |
c801d85f | 293 | |
3c67202d VZ |
294 | // data access (all indexes are 0 based) |
295 | // read access | |
c801d85f | 296 | char GetChar(size_t n) const |
dd1eaa89 | 297 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
3c67202d | 298 | // read/write access |
c801d85f | 299 | char& GetWritableChar(size_t n) |
dd1eaa89 | 300 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
3c67202d | 301 | // write access |
c801d85f KB |
302 | void SetChar(size_t n, char ch) |
303 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; } | |
304 | ||
3c67202d | 305 | // get last character |
c801d85f KB |
306 | char Last() const |
307 | { wxASSERT( !IsEmpty() ); return m_pchData[Len() - 1]; } | |
3c67202d | 308 | // get writable last character |
dd1eaa89 | 309 | char& Last() |
c801d85f KB |
310 | { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData[Len()-1]; } |
311 | ||
e0e680d2 | 312 | // on alpha-linux this gives overload problems: |
c5248639 | 313 | // Also on Solaris, so removing for now (JACS) |
e0e680d2 | 314 | #if ! defined(__ALPHA__) |
3c67202d | 315 | // operator version of GetChar |
c801d85f KB |
316 | char operator[](size_t n) const |
317 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } | |
22552603 | 318 | #endif |
c5248639 | 319 | |
3c67202d | 320 | // operator version of GetChar |
c801d85f KB |
321 | char operator[](int n) const |
322 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } | |
3c67202d | 323 | // operator version of GetWritableChar |
c801d85f KB |
324 | char& operator[](size_t n) |
325 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } | |
326 | ||
3c67202d | 327 | // implicit conversion to C string |
dd1eaa89 | 328 | operator const char*() const { return m_pchData; } |
3c67202d | 329 | // explicit conversion to C string (use this with printf()!) |
c801d85f | 330 | const char* c_str() const { return m_pchData; } |
3c67202d | 331 | // |
c801d85f | 332 | const char* GetData() const { return m_pchData; } |
c801d85f | 333 | |
3c67202d VZ |
334 | // overloaded assignment |
335 | // from another wxString | |
c801d85f | 336 | wxString& operator=(const wxString& stringSrc); |
3c67202d | 337 | // from a character |
c801d85f | 338 | wxString& operator=(char ch); |
3c67202d | 339 | // from a C string |
c801d85f | 340 | wxString& operator=(const char *psz); |
3c67202d | 341 | // from another kind of C string |
c801d85f | 342 | wxString& operator=(const unsigned char* psz); |
3c67202d | 343 | // from a wide string |
c801d85f | 344 | wxString& operator=(const wchar_t *pwz); |
3c67202d VZ |
345 | |
346 | // string concatenation | |
347 | // in place concatenation | |
348 | /* | |
349 | Concatenate and return the result. Note that the left to right | |
350 | associativity of << allows to write things like "str << str1 << str2 | |
351 | << ..." (unlike with +=) | |
352 | */ | |
353 | // string += string | |
dd1eaa89 VZ |
354 | wxString& operator<<(const wxString& s) |
355 | { | |
356 | wxASSERT( s.GetStringData()->IsValid() ); | |
357 | ||
358 | ConcatSelf(s.Len(), s); | |
359 | return *this; | |
360 | } | |
3c67202d | 361 | // string += C string |
dd1eaa89 VZ |
362 | wxString& operator<<(const char *psz) |
363 | { ConcatSelf(Strlen(psz), psz); return *this; } | |
3c67202d | 364 | // string += char |
dd1eaa89 | 365 | wxString& operator<<(char ch) { ConcatSelf(1, &ch); return *this; } |
dd1eaa89 | 366 | |
3c67202d | 367 | // string += string |
6b95b20d | 368 | void operator+=(const wxString& s) { (void)operator<<(s); } |
3c67202d | 369 | // string += C string |
6b95b20d | 370 | void operator+=(const char *psz) { (void)operator<<(psz); } |
3c67202d | 371 | // string += char |
6b95b20d | 372 | void operator+=(char ch) { (void)operator<<(ch); } |
6b95b20d | 373 | |
3c67202d VZ |
374 | // string += C string |
375 | wxString& Append(const char* psz) | |
376 | { ConcatSelf(Strlen(psz), psz); return *this; } | |
377 | // append count copies of given character | |
378 | wxString& Append(char ch, size_t count = 1u) | |
379 | { wxString str(ch, count); return *this << str; } | |
380 | ||
381 | // prepend a string, return the string itself | |
382 | wxString& Prepend(const wxString& str) | |
383 | { *this = str + *this; return *this; } | |
384 | ||
385 | // non-destructive concatenation | |
386 | // | |
c33534e5 | 387 | friend wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2); |
3c67202d | 388 | // |
c33534e5 | 389 | friend wxString WXDLLEXPORT operator+(const wxString& string, char ch); |
3c67202d | 390 | // |
c33534e5 | 391 | friend wxString WXDLLEXPORT operator+(char ch, const wxString& string); |
3c67202d | 392 | // |
c33534e5 | 393 | friend wxString WXDLLEXPORT operator+(const wxString& string, const char *psz); |
3c67202d | 394 | // |
c33534e5 | 395 | friend wxString WXDLLEXPORT operator+(const char *psz, const wxString& string); |
3c67202d VZ |
396 | |
397 | // stream-like functions | |
398 | // insert an int into string | |
399 | wxString& operator<<(int i); | |
400 | // insert a float into string | |
401 | wxString& operator<<(float f); | |
402 | // insert a double into string | |
403 | wxString& operator<<(double d); | |
c84c52de | 404 | |
3c67202d VZ |
405 | // string comparison |
406 | // case-sensitive comparison: return 0 if =, +1 if > or -1 if < | |
c801d85f | 407 | int Cmp(const char *psz) const { return strcmp(c_str(), psz); } |
3c67202d | 408 | // same as Cmp() but not case-sensitive |
c801d85f | 409 | int CmpNoCase(const char *psz) const { return Stricmp(c_str(), psz); } |
3c67202d VZ |
410 | // test for the string equality, either considering case or not |
411 | // (if compareWithCase then the case matters) | |
412 | bool IsSameAs(const char *psz, bool compareWithCase = TRUE) const | |
413 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } | |
414 | ||
415 | // simple sub-string extraction | |
416 | // return substring starting at nFirst of length nCount (or till the end | |
417 | // if nCount = default value) | |
dd1eaa89 | 418 | wxString Mid(size_t nFirst, size_t nCount = STRING_MAXLEN) const; |
3c67202d VZ |
419 | |
420 | // operator version of Mid() | |
421 | wxString operator()(size_t start, size_t len) const | |
422 | { return Mid(start, len); } | |
423 | ||
424 | // get first nCount characters | |
c801d85f | 425 | wxString Left(size_t nCount) const; |
3c67202d | 426 | // get last nCount characters |
c801d85f | 427 | wxString Right(size_t nCount) const; |
3c67202d VZ |
428 | // get all characters before the first occurence of ch |
429 | // (returns the whole string if ch not found) | |
430 | wxString BeforeFirst(char ch) const; | |
431 | // get all characters before the last occurence of ch | |
432 | // (returns empty string if ch not found) | |
433 | wxString BeforeLast(char ch) const; | |
434 | // get all characters after the first occurence of ch | |
435 | // (returns empty string if ch not found) | |
436 | wxString AfterFirst(char ch) const; | |
437 | // get all characters after the last occurence of ch | |
438 | // (returns the whole string if ch not found) | |
439 | wxString AfterLast(char ch) const; | |
440 | ||
441 | // for compatibility only, use more explicitly named functions above | |
c84c52de VZ |
442 | wxString Before(char ch) const { return BeforeLast(ch); } |
443 | wxString After(char ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
444 | |
445 | // case conversion | |
c84c52de | 446 | // convert to upper case in place, return the string itself |
c801d85f | 447 | wxString& MakeUpper(); |
c84c52de | 448 | // convert to upper case, return the copy of the string |
03ab016d JS |
449 | // Here's something to remember: BC++ doesn't like returns in inlines. |
450 | wxString Upper() const ; | |
c84c52de | 451 | // convert to lower case in place, return the string itself |
c801d85f | 452 | wxString& MakeLower(); |
c84c52de | 453 | // convert to lower case, return the copy of the string |
03ab016d | 454 | wxString Lower() const ; |
c801d85f | 455 | |
3c67202d VZ |
456 | // trimming/padding whitespace (either side) and truncating |
457 | // remove spaces from left or from right (default) side | |
c801d85f | 458 | wxString& Trim(bool bFromRight = TRUE); |
3c67202d | 459 | // add nCount copies chPad in the beginning or at the end (default) |
c801d85f | 460 | wxString& Pad(size_t nCount, char chPad = ' ', bool bFromRight = TRUE); |
3c67202d | 461 | // truncate string to given length |
c801d85f | 462 | wxString& Truncate(size_t uiLen); |
dd1eaa89 | 463 | |
3c67202d VZ |
464 | // searching and replacing |
465 | // searching (return starting index, or -1 if not found) | |
c801d85f | 466 | int Find(char ch, bool bFromEnd = FALSE) const; // like strchr/strrchr |
3c67202d | 467 | // searching (return starting index, or -1 if not found) |
c801d85f | 468 | int Find(const char *pszSub) const; // like strstr |
3c67202d VZ |
469 | // replace first (or all of bReplaceAll) occurences of substring with |
470 | // another string, returns the number of replacements made | |
471 | size_t Replace(const char *szOld, | |
472 | const char *szNew, | |
473 | bool bReplaceAll = TRUE); | |
474 | ||
475 | // check if the string contents matches a mask containing '*' and '?' | |
8fd0f20b | 476 | bool Matches(const char *szMask) const; |
c801d85f | 477 | |
3c67202d VZ |
478 | // formated input/output |
479 | // as sprintf(), returns the number of characters written or < 0 on error | |
c801d85f | 480 | int Printf(const char *pszFormat, ...); |
3c67202d | 481 | // as vprintf(), returns the number of characters written or < 0 on error |
c801d85f | 482 | int PrintfV(const char* pszFormat, va_list argptr); |
dd1eaa89 | 483 | |
3c67202d VZ |
484 | // raw access to string memory |
485 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 486 | // only works if the data of this string is not shared |
c86f1403 | 487 | void Alloc(size_t nLen); |
3c67202d | 488 | // minimize the string's memory |
dd1eaa89 VZ |
489 | // only works if the data of this string is not shared |
490 | void Shrink(); | |
3c67202d VZ |
491 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
492 | // a.s.a.p. to put string back in a reasonable state! | |
c86f1403 | 493 | char *GetWriteBuf(size_t nLen); |
3c67202d | 494 | // call this immediately after GetWriteBuf() has been used |
8fd0f20b | 495 | void UngetWriteBuf(); |
c801d85f | 496 | |
3c67202d VZ |
497 | // wxWindows version 1 compatibility functions |
498 | ||
499 | // use Mid() | |
500 | wxString SubString(size_t from, size_t to) const | |
501 | { return Mid(from, (to - from + 1)); } | |
502 | // values for second parameter of CompareTo function | |
c801d85f | 503 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 504 | // values for first parameter of Strip function |
c801d85f | 505 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 506 | |
3c67202d | 507 | // use Printf() |
8870c26e | 508 | int sprintf(const char *pszFormat, ...); |
c801d85f | 509 | |
3c67202d | 510 | // use Cmp() |
c801d85f | 511 | inline int CompareTo(const char* psz, caseCompare cmp = exact) const |
6b95b20d | 512 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 513 | |
3c67202d | 514 | // use Len |
c801d85f | 515 | size_t Length() const { return Len(); } |
3c67202d | 516 | // Count the number of characters |
1fc5dd6f | 517 | int Freq(char ch) const; |
3c67202d | 518 | // use MakeLower |
c801d85f | 519 | void LowerCase() { MakeLower(); } |
3c67202d | 520 | // use MakeUpper |
c801d85f | 521 | void UpperCase() { MakeUpper(); } |
3c67202d | 522 | // use Trim except that it doesn't change this string |
c801d85f KB |
523 | wxString Strip(stripType w = trailing) const; |
524 | ||
3c67202d | 525 | // use Find (more general variants not yet supported) |
c801d85f KB |
526 | size_t Index(const char* psz) const { return Find(psz); } |
527 | size_t Index(char ch) const { return Find(ch); } | |
3c67202d | 528 | // use Truncate |
c801d85f KB |
529 | wxString& Remove(size_t pos) { return Truncate(pos); } |
530 | wxString& RemoveLast() { return Truncate(Len() - 1); } | |
531 | ||
3ed358cb | 532 | wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); } |
dd1eaa89 | 533 | |
3c67202d | 534 | // use Find() |
3ed358cb VZ |
535 | int First( const char ch ) const { return Find(ch); } |
536 | int First( const char* psz ) const { return Find(psz); } | |
537 | int First( const wxString &str ) const { return Find(str); } | |
3ed358cb | 538 | int Last( const char ch ) const { return Find(ch, TRUE); } |
3c67202d | 539 | bool Contains(const wxString& str) const { return Find(str) != -1; } |
c801d85f | 540 | |
3c67202d | 541 | // use IsEmpty() |
c801d85f | 542 | bool IsNull() const { return IsEmpty(); } |
c801d85f | 543 | |
3c67202d VZ |
544 | #ifdef wxSTD_STRING_COMPATIBILITY |
545 | // std::string compatibility functions | |
dd1eaa89 | 546 | |
3c67202d | 547 | // an 'invalid' value for string index |
c801d85f | 548 | static const size_t npos; |
dd1eaa89 | 549 | |
3c67202d VZ |
550 | // constructors |
551 | // take nLen chars starting at nPos | |
552 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
553 | { | |
554 | wxASSERT( str.GetStringData()->IsValid() ); | |
555 | InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen); | |
556 | } | |
557 | // take all characters from pStart to pEnd | |
558 | wxString(const void *pStart, const void *pEnd); | |
559 | ||
560 | // lib.string.capacity | |
561 | // return the length of the string | |
562 | size_t size() const { return Len(); } | |
563 | // return the length of the string | |
564 | size_t length() const { return Len(); } | |
565 | // return the maximum size of the string | |
566 | size_t max_size() const { return STRING_MAXLEN; } | |
567 | // resize the string, filling the space with c if c != 0 | |
568 | void resize(size_t nSize, char ch = '\0'); | |
569 | // delete the contents of the string | |
570 | void clear() { Empty(); } | |
571 | // returns true if the string is empty | |
572 | bool empty() const { return IsEmpty(); } | |
573 | ||
574 | // lib.string.access | |
575 | // return the character at position n | |
576 | char at(size_t n) const { return GetChar(n); } | |
577 | // returns the writable character at position n | |
578 | char& at(size_t n) { return GetWritableChar(n); } | |
579 | ||
580 | // lib.string.modifiers | |
581 | // append a string | |
582 | wxString& append(const wxString& str) | |
583 | { *this += str; return *this; } | |
584 | // append elements str[pos], ..., str[pos+n] | |
585 | wxString& append(const wxString& str, size_t pos, size_t n) | |
586 | { ConcatSelf(n, str.c_str() + pos); return *this; } | |
587 | // append first n (or all if n == npos) characters of sz | |
588 | wxString& append(const char *sz, size_t n = npos) | |
589 | { ConcatSelf(n == npos ? Strlen(sz) : n, sz); return *this; } | |
590 | ||
591 | // append n copies of ch | |
592 | wxString& append(size_t n, char ch) { return Pad(n, ch); } | |
593 | ||
594 | // same as `this_string = str' | |
595 | wxString& assign(const wxString& str) { return (*this) = str; } | |
596 | // same as ` = str[pos..pos + n] | |
597 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
598 | { return *this = wxString((const char *)str + pos, n); } | |
599 | // same as `= first n (or all if n == npos) characters of sz' | |
600 | wxString& assign(const char *sz, size_t n = npos) | |
601 | { return *this = wxString(sz, n); } | |
602 | // same as `= n copies of ch' | |
603 | wxString& assign(size_t n, char ch) | |
604 | { return *this = wxString(ch, n); } | |
605 | ||
606 | // insert another string | |
607 | wxString& insert(size_t nPos, const wxString& str); | |
608 | // insert n chars of str starting at nStart (in str) | |
609 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
610 | { return insert(nPos, wxString((const char *)str + nStart, n)); } | |
611 | ||
612 | // insert first n (or all if n == npos) characters of sz | |
613 | wxString& insert(size_t nPos, const char *sz, size_t n = npos) | |
614 | { return insert(nPos, wxString(sz, n)); } | |
615 | // insert n copies of ch | |
616 | wxString& insert(size_t nPos, size_t n, char ch) | |
617 | { return insert(nPos, wxString(ch, n)); } | |
618 | ||
619 | // delete characters from nStart to nStart + nLen | |
620 | wxString& erase(size_t nStart = 0, size_t nLen = npos); | |
621 | ||
622 | // replaces the substring of length nLen starting at nStart | |
623 | wxString& replace(size_t nStart, size_t nLen, const char* sz); | |
624 | // replaces the substring with nCount copies of ch | |
625 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, char ch); | |
626 | // replaces a substring with another substring | |
627 | wxString& replace(size_t nStart, size_t nLen, | |
628 | const wxString& str, size_t nStart2, size_t nLen2); | |
629 | // replaces the substring with first nCount chars of sz | |
630 | wxString& replace(size_t nStart, size_t nLen, | |
631 | const char* sz, size_t nCount); | |
632 | ||
633 | // swap two strings | |
634 | void swap(wxString& str); | |
635 | ||
636 | // All find() functions take the nStart argument which specifies the | |
637 | // position to start the search on, the default value is 0. All functions | |
638 | // return npos if there were no match. | |
639 | ||
640 | // find a substring | |
641 | size_t find(const wxString& str, size_t nStart = 0) const; | |
642 | ||
643 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 644 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d VZ |
645 | // find first n characters of sz |
646 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const; | |
6b0eb19f | 647 | #endif |
3c67202d VZ |
648 | |
649 | // Gives a duplicate symbol (presumably a case-insensitivity problem) | |
62448488 | 650 | #if !defined(__BORLANDC__) |
3c67202d VZ |
651 | // find the first occurence of character ch after nStart |
652 | size_t find(char ch, size_t nStart = 0) const; | |
62448488 | 653 | #endif |
3c67202d VZ |
654 | // rfind() family is exactly like find() but works right to left |
655 | ||
656 | // as find, but from the end | |
657 | size_t rfind(const wxString& str, size_t nStart = npos) const; | |
658 | ||
659 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 660 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d VZ |
661 | // as find, but from the end |
662 | size_t rfind(const char* sz, size_t nStart = npos, | |
663 | size_t n = npos) const; | |
664 | // as find, but from the end | |
665 | size_t rfind(char ch, size_t nStart = npos) const; | |
c801d85f | 666 | #endif |
3c67202d VZ |
667 | |
668 | // find first/last occurence of any character in the set | |
669 | ||
670 | // | |
671 | size_t find_first_of(const wxString& str, size_t nStart = 0) const; | |
672 | // | |
673 | size_t find_first_of(const char* sz, size_t nStart = 0) const; | |
674 | // same as find(char, size_t) | |
675 | size_t find_first_of(char c, size_t nStart = 0) const; | |
676 | // | |
677 | size_t find_last_of (const wxString& str, size_t nStart = npos) const; | |
678 | // | |
679 | size_t find_last_of (const char* s, size_t nStart = npos) const; | |
680 | // same as rfind(char, size_t) | |
681 | size_t find_last_of (char c, size_t nStart = npos) const; | |
682 | ||
683 | // find first/last occurence of any character not in the set | |
684 | ||
685 | // | |
686 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const; | |
687 | // | |
688 | size_t find_first_not_of(const char* s, size_t nStart = 0) const; | |
689 | // | |
690 | size_t find_first_not_of(char ch, size_t nStart = 0) const; | |
691 | // | |
692 | size_t find_last_not_of(const wxString& str, size_t nStart=npos) const; | |
693 | // | |
694 | size_t find_last_not_of(const char* s, size_t nStart = npos) const; | |
695 | // | |
696 | size_t find_last_not_of(char ch, size_t nStart = npos) const; | |
697 | ||
698 | // All compare functions return -1, 0 or 1 if the [sub]string is less, | |
699 | // equal or greater than the compare() argument. | |
700 | ||
701 | // just like strcmp() | |
702 | int compare(const wxString& str) const { return Cmp(str); } | |
703 | // comparison with a substring | |
704 | int compare(size_t nStart, size_t nLen, const wxString& str) const; | |
705 | // comparison of 2 substrings | |
706 | int compare(size_t nStart, size_t nLen, | |
707 | const wxString& str, size_t nStart2, size_t nLen2) const; | |
708 | // just like strcmp() | |
709 | int compare(const char* sz) const { return Cmp(sz); } | |
710 | // substring comparison with first nCount characters of sz | |
711 | int compare(size_t nStart, size_t nLen, | |
712 | const char* sz, size_t nCount = npos) const; | |
713 | ||
714 | // substring extraction | |
715 | wxString substr(size_t nStart = 0, size_t nLen = npos) const; | |
716 | #endif // wxSTD_STRING_COMPATIBILITY | |
c801d85f KB |
717 | }; |
718 | ||
719 | // ---------------------------------------------------------------------------- | |
3c67202d VZ |
720 | // The string array uses it's knowledge of internal structure of the wxString |
721 | // class to optimize string storage. Normally, we would store pointers to | |
722 | // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is | |
723 | // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is | |
724 | // really all we need to turn such pointer into a string! | |
725 | // | |
726 | // Of course, it can be called a dirty hack, but we use twice less memory and | |
727 | // this approach is also more speed efficient, so it's probably worth it. | |
728 | // | |
729 | // Usage notes: when a string is added/inserted, a new copy of it is created, | |
730 | // so the original string may be safely deleted. When a string is retrieved | |
731 | // from the array (operator[] or Item() method), a reference is returned. | |
c801d85f | 732 | // ---------------------------------------------------------------------------- |
fbcb4166 | 733 | class WXDLLEXPORT wxArrayString |
c801d85f KB |
734 | { |
735 | public: | |
3c67202d VZ |
736 | // constructors and destructor |
737 | // default ctor | |
c801d85f | 738 | wxArrayString(); |
3c67202d | 739 | // copy ctor |
c801d85f | 740 | wxArrayString(const wxArrayString& array); |
3c67202d | 741 | // assignment operator |
c801d85f | 742 | wxArrayString& operator=(const wxArrayString& src); |
3c67202d | 743 | // not virtual, this class should not be derived from |
c801d85f | 744 | ~wxArrayString(); |
c801d85f | 745 | |
3c67202d VZ |
746 | // memory management |
747 | // empties the list, but doesn't release memory | |
c801d85f | 748 | void Empty(); |
3c67202d | 749 | // empties the list and releases memory |
c801d85f | 750 | void Clear(); |
3c67202d | 751 | // preallocates memory for given number of items |
c801d85f | 752 | void Alloc(size_t nCount); |
3c67202d | 753 | // minimzes the memory usage (by freeing all extra memory) |
dd1eaa89 | 754 | void Shrink(); |
3c67202d VZ |
755 | |
756 | // simple accessors | |
757 | // number of elements in the array | |
758 | size_t GetCount() const { return m_nCount; } | |
759 | // is it empty? | |
760 | bool IsEmpty() const { return m_nCount == 0; } | |
761 | // number of elements in the array (GetCount is preferred API) | |
762 | size_t Count() const { return m_nCount; } | |
763 | ||
764 | // items access (range checking is done in debug version) | |
765 | // get item at position uiIndex | |
c801d85f KB |
766 | wxString& Item(size_t nIndex) const |
767 | { wxASSERT( nIndex < m_nCount ); return *(wxString *)&(m_pItems[nIndex]); } | |
3c67202d | 768 | // same as Item() |
c801d85f | 769 | wxString& operator[](size_t nIndex) const { return Item(nIndex); } |
3c67202d | 770 | // get last item |
c801d85f | 771 | wxString& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); } |
3c67202d VZ |
772 | |
773 | // item management | |
774 | // Search the element in the array, starting from the beginning if | |
775 | // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case | |
776 | // sensitive (default). Returns index of the first item matched or | |
777 | // wxNOT_FOUND | |
c801d85f | 778 | int Index (const char *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const; |
3c67202d VZ |
779 | // add new element at the end |
780 | void Add(const wxString& str); | |
781 | // add new element at given position | |
c86f1403 | 782 | void Insert(const wxString& str, size_t uiIndex); |
3c67202d | 783 | // remove first item matching this value |
c801d85f | 784 | void Remove(const char *sz); |
3c67202d | 785 | // remove item by index |
c801d85f | 786 | void Remove(size_t nIndex); |
c801d85f | 787 | |
3c67202d | 788 | // sort array elements |
c801d85f KB |
789 | void Sort(bool bCase = TRUE, bool bReverse = FALSE); |
790 | ||
791 | private: | |
792 | void Grow(); // makes array bigger if needed | |
793 | void Free(); // free the string stored | |
794 | ||
3c67202d | 795 | size_t m_nSize, // current size of the array |
c801d85f KB |
796 | m_nCount; // current number of elements |
797 | ||
798 | char **m_pItems; // pointer to data | |
799 | }; | |
800 | ||
c801d85f | 801 | // --------------------------------------------------------------------------- |
3c67202d | 802 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 803 | // --------------------------------------------------------------------------- |
3c67202d | 804 | // |
a3ef5bf5 | 805 | inline bool operator==(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) == 0); } |
3c67202d | 806 | // |
a3ef5bf5 | 807 | inline bool operator==(const wxString& s1, const char * s2) { return (s1.Cmp(s2) == 0); } |
3c67202d | 808 | // |
a3ef5bf5 | 809 | inline bool operator==(const char * s1, const wxString& s2) { return (s2.Cmp(s1) == 0); } |
3c67202d | 810 | // |
a3ef5bf5 | 811 | inline bool operator!=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) != 0); } |
3c67202d | 812 | // |
a3ef5bf5 | 813 | inline bool operator!=(const wxString& s1, const char * s2) { return (s1.Cmp(s2) != 0); } |
3c67202d | 814 | // |
a3ef5bf5 | 815 | inline bool operator!=(const char * s1, const wxString& s2) { return (s2.Cmp(s1) != 0); } |
3c67202d | 816 | // |
a3ef5bf5 | 817 | inline bool operator< (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) < 0); } |
3c67202d | 818 | // |
a3ef5bf5 | 819 | inline bool operator< (const wxString& s1, const char * s2) { return (s1.Cmp(s2) < 0); } |
3c67202d | 820 | // |
a3ef5bf5 | 821 | inline bool operator< (const char * s1, const wxString& s2) { return (s2.Cmp(s1) > 0); } |
3c67202d | 822 | // |
a3ef5bf5 | 823 | inline bool operator> (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) > 0); } |
3c67202d | 824 | // |
a3ef5bf5 | 825 | inline bool operator> (const wxString& s1, const char * s2) { return (s1.Cmp(s2) > 0); } |
3c67202d | 826 | // |
a3ef5bf5 | 827 | inline bool operator> (const char * s1, const wxString& s2) { return (s2.Cmp(s1) < 0); } |
3c67202d | 828 | // |
a3ef5bf5 | 829 | inline bool operator<=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) <= 0); } |
3c67202d | 830 | // |
a3ef5bf5 | 831 | inline bool operator<=(const wxString& s1, const char * s2) { return (s1.Cmp(s2) <= 0); } |
3c67202d | 832 | // |
a3ef5bf5 | 833 | inline bool operator<=(const char * s1, const wxString& s2) { return (s2.Cmp(s1) >= 0); } |
3c67202d | 834 | // |
a3ef5bf5 | 835 | inline bool operator>=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) >= 0); } |
3c67202d | 836 | // |
a3ef5bf5 | 837 | inline bool operator>=(const wxString& s1, const char * s2) { return (s1.Cmp(s2) >= 0); } |
3c67202d | 838 | // |
a3ef5bf5 | 839 | inline bool operator>=(const char * s1, const wxString& s2) { return (s2.Cmp(s1) <= 0); } |
3c67202d | 840 | |
ed7174ba UU |
841 | wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2); |
842 | wxString WXDLLEXPORT operator+(const wxString& string, char ch); | |
843 | wxString WXDLLEXPORT operator+(char ch, const wxString& string); | |
844 | wxString WXDLLEXPORT operator+(const wxString& string, const char *psz); | |
845 | wxString WXDLLEXPORT operator+(const char *psz, const wxString& string); | |
f04f3991 | 846 | |
c801d85f | 847 | // --------------------------------------------------------------------------- |
3c67202d | 848 | // Implementation only from here until the end of file |
c801d85f KB |
849 | // --------------------------------------------------------------------------- |
850 | ||
3c67202d | 851 | #ifdef wxSTD_STRING_COMPATIBILITY |
c801d85f | 852 | |
3f4a0c5b | 853 | #include "wx/ioswrap.h" |
c801d85f | 854 | |
184b5d99 | 855 | WXDLLEXPORT istream& operator>>(istream& is, wxString& str); |
c801d85f | 856 | |
3c67202d | 857 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 858 | |
34138703 | 859 | #endif // _WX_WXSTRINGH__ |