]>
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 | |
91b8de8d RR |
23 | #ifdef __EMX__ |
24 | #include <std.h> | |
25 | #endif | |
26 | ||
c801d85f KB |
27 | #include <string.h> |
28 | #include <stdio.h> | |
29 | #include <stdarg.h> | |
30 | #include <limits.h> | |
dd1eaa89 | 31 | #include <stdlib.h> |
c801d85f | 32 | |
57493f9f | 33 | #ifdef HAVE_STRINGS_H |
1bfcb0b6 VZ |
34 | #include <strings.h> // for strcasecmp() |
35 | #endif // AIX | |
36 | ||
8fd0f20b | 37 | #ifndef WX_PRECOMP |
3f4a0c5b VZ |
38 | #include "wx/defs.h" |
39 | ||
dd1eaa89 VZ |
40 | #ifdef WXSTRING_IS_WXOBJECT |
41 | #include "wx/object.h" | |
42 | #endif | |
3f4a0c5b | 43 | #endif // !PCH |
8fd0f20b | 44 | |
c801d85f | 45 | #include "wx/debug.h" |
2bb67b80 OK |
46 | #include "wx/wxchar.h" |
47 | #include "wx/buffer.h" | |
c801d85f | 48 | |
3c67202d VZ |
49 | /* |
50 | Efficient string class [more or less] compatible with MFC CString, | |
51 | wxWindows version 1 wxString and std::string and some handy functions | |
52 | missing from string.h. | |
53 | */ | |
c801d85f KB |
54 | |
55 | // --------------------------------------------------------------------------- | |
56 | // macros | |
57 | // --------------------------------------------------------------------------- | |
58 | ||
3c67202d VZ |
59 | // compile the std::string compatibility functions if defined |
60 | #define wxSTD_STRING_COMPATIBILITY | |
c801d85f | 61 | |
3c67202d | 62 | // define to derive wxString from wxObject |
a3ef5bf5 | 63 | #ifdef WXSTRING_IS_WXOBJECT |
c801d85f | 64 | #undef WXSTRING_IS_WXOBJECT |
a3ef5bf5 | 65 | #endif |
c801d85f | 66 | |
3c67202d | 67 | // maximum possible length for a string means "take all string" everywhere |
c801d85f | 68 | // (as sizeof(StringData) is unknown here we substract 100) |
566b84d2 | 69 | const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100; |
c801d85f KB |
70 | |
71 | // 'naughty' cast | |
2bb67b80 | 72 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
6f841509 | 73 | #define WXCSTRINGCAST (wxChar *)(const wxChar *) |
2bb67b80 OK |
74 | #define MBSTRINGCAST (char *)(const char *) |
75 | #define WCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 76 | |
3c67202d | 77 | // implementation only |
b1269691 | 78 | #define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) <= Len() ) |
c801d85f KB |
79 | |
80 | // --------------------------------------------------------------------------- | |
3c67202d VZ |
81 | // Global functions complementing standard C string library replacements for |
82 | // strlen() and portable strcasecmp() | |
83 | //--------------------------------------------------------------------------- | |
2bb67b80 | 84 | // USE wx* FUNCTIONS IN wx/wxchar.h INSTEAD - THIS IS ONLY FOR BINARY COMPATIBILITY |
88150e60 | 85 | |
3c67202d | 86 | // checks whether the passed in pointer is NULL and if the string is empty |
dbda9e86 | 87 | inline bool WXDLLEXPORT IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 88 | |
3c67202d VZ |
89 | // safe version of strlen() (returns 0 if passed NULL pointer) |
90 | inline size_t WXDLLEXPORT Strlen(const char *psz) | |
c801d85f KB |
91 | { return psz ? strlen(psz) : 0; } |
92 | ||
3c67202d | 93 | // portable strcasecmp/_stricmp |
dd1eaa89 VZ |
94 | inline int WXDLLEXPORT Stricmp(const char *psz1, const char *psz2) |
95 | { | |
91b8de8d | 96 | #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) |
dd1eaa89 | 97 | return _stricmp(psz1, psz2); |
91b8de8d | 98 | #elif defined(__SC__) |
2432b92d | 99 | return _stricmp(psz1, psz2); |
91b8de8d | 100 | #elif defined(__SALFORDC__) |
a3ef5bf5 | 101 | return stricmp(psz1, psz2); |
dd1eaa89 VZ |
102 | #elif defined(__BORLANDC__) |
103 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
104 | #elif defined(__WATCOMC__) |
105 | return stricmp(psz1, psz2); | |
91b8de8d RR |
106 | #elif defined(__EMX__) |
107 | return stricmp(psz1, psz2); | |
108 | #elif defined(__UNIX__) || defined(__GNUWIN32__) | |
dd1eaa89 | 109 | return strcasecmp(psz1, psz2); |
8be97d65 | 110 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
111 | register char c1, c2; |
112 | do { | |
113 | c1 = tolower(*psz1++); | |
114 | c2 = tolower(*psz2++); | |
115 | } while ( c1 && (c1 == c2) ); | |
116 | ||
117 | return c1 - c2; | |
dd1eaa89 VZ |
118 | #else |
119 | // almost all compilers/libraries provide this function (unfortunately under | |
120 | // different names), that's why we don't implement our own which will surely | |
121 | // be more efficient than this code (uncomment to use): | |
122 | /* | |
123 | register char c1, c2; | |
124 | do { | |
125 | c1 = tolower(*psz1++); | |
126 | c2 = tolower(*psz2++); | |
127 | } while ( c1 && (c1 == c2) ); | |
128 | ||
129 | return c1 - c2; | |
130 | */ | |
131 | ||
132 | #error "Please define string case-insensitive compare for your OS/compiler" | |
133 | #endif // OS/compiler | |
134 | } | |
c801d85f | 135 | |
f04f3991 VZ |
136 | // ---------------------------------------------------------------------------- |
137 | // global data | |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
2bb67b80 | 140 | WXDLLEXPORT_DATA(extern const wxChar*) wxEmptyString; |
3c67202d | 141 | |
f04f3991 | 142 | // global pointer to empty string |
2bb67b80 | 143 | WXDLLEXPORT_DATA(extern const wxChar*) g_szNul; |
f04f3991 VZ |
144 | |
145 | // return an empty wxString | |
f7bd2698 | 146 | class WXDLLEXPORT wxString; // not yet defined |
f04f3991 VZ |
147 | inline const wxString& wxGetEmptyString() { return *(wxString *)&g_szNul; } |
148 | ||
c801d85f | 149 | // --------------------------------------------------------------------------- |
f04f3991 | 150 | // string data prepended with some housekeeping info (used by wxString class), |
c801d85f KB |
151 | // is never used directly (but had to be put here to allow inlining) |
152 | // --------------------------------------------------------------------------- | |
153 | struct WXDLLEXPORT wxStringData | |
154 | { | |
155 | int nRefs; // reference count | |
3c024cc2 | 156 | size_t nDataLength, // actual string length |
c801d85f KB |
157 | nAllocLength; // allocated memory size |
158 | ||
2bb67b80 OK |
159 | // mimics declaration 'wxChar data[nAllocLength]' |
160 | wxChar* data() const { return (wxChar*)(this + 1); } | |
c801d85f KB |
161 | |
162 | // empty string has a special ref count so it's never deleted | |
dbda9e86 JS |
163 | bool IsEmpty() const { return (nRefs == -1); } |
164 | bool IsShared() const { return (nRefs > 1); } | |
c801d85f KB |
165 | |
166 | // lock/unlock | |
dd1eaa89 VZ |
167 | void Lock() { if ( !IsEmpty() ) nRefs++; } |
168 | void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); } | |
8fd0f20b | 169 | |
dd1eaa89 | 170 | // if we had taken control over string memory (GetWriteBuf), it's |
8fd0f20b | 171 | // intentionally put in invalid state |
dbda9e86 JS |
172 | void Validate(bool b) { nRefs = (b ? 1 : 0); } |
173 | bool IsValid() const { return (nRefs != 0); } | |
c801d85f KB |
174 | }; |
175 | ||
2bb67b80 OK |
176 | // --------------------------------------------------------------------------- |
177 | // types of multibyte<->Unicode conversions | |
178 | // --------------------------------------------------------------------------- | |
6f841509 | 179 | #if wxUSE_WCHAR_T |
2bb67b80 OK |
180 | class WXDLLEXPORT wxMBConv |
181 | { | |
182 | public: | |
183 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const; | |
184 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const; | |
0f3e3e0c JS |
185 | // No longer inline since BC++ complains. |
186 | const wxWCharBuffer cMB2WC(const char *psz) const; | |
187 | const wxCharBuffer cWC2MB(const wchar_t *psz) const; | |
2bb67b80 OK |
188 | #if wxUSE_UNICODE |
189 | const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); } | |
190 | const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); } | |
191 | const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; } | |
192 | const wchar_t* cMB2WC(const wchar_t *psz) const { return psz; } | |
193 | #else | |
194 | const char* cMB2WX(const char *psz) const { return psz; } | |
195 | const char* cWX2MB(const char *psz) const { return psz; } | |
196 | const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); } | |
197 | const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); } | |
198 | #endif | |
199 | }; | |
5f709e67 | 200 | WXDLLEXPORT_DATA(extern wxMBConv) wxConvLibc; |
2bb67b80 | 201 | |
d41bba91 OK |
202 | #define wxANOTHER_MBCONV(type) \ |
203 | class type : public wxMBConv { \ | |
204 | public: \ | |
205 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const; \ | |
206 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const; \ | |
207 | } | |
208 | ||
5f709e67 | 209 | WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvFile)) wxConvFile; |
5f709e67 OK |
210 | WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvUTF7)) wxConvUTF7; |
211 | WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvUTF8)) wxConvUTF8; | |
dcf924a3 | 212 | #ifdef __WXGTK12__ |
5f709e67 | 213 | WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvGdk)) wxConvGdk; |
dcf924a3 | 214 | #endif |
d41bba91 | 215 | |
a69f7aa8 | 216 | class wxCharacterSet; |
d41bba91 | 217 | class WXDLLEXPORT wxCSConv : public wxMBConv |
2bb67b80 OK |
218 | { |
219 | private: | |
a86f1d4a OK |
220 | wxChar *m_name; |
221 | wxCharacterSet *m_cset; | |
222 | bool m_deferred; | |
551df6f2 | 223 | void SetName(const wxChar *charset); |
2bb67b80 OK |
224 | public: |
225 | wxCSConv(const wxChar *charset); | |
a86f1d4a OK |
226 | virtual ~wxCSConv(); |
227 | void LoadNow(); | |
4300d6cb OK |
228 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const; |
229 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const; | |
2bb67b80 OK |
230 | }; |
231 | ||
5f709e67 OK |
232 | WXDLLEXPORT_DATA(extern wxCSConv) wxConvLocal; |
233 | #define wxConv_local wxConvLocal | |
a69f7aa8 | 234 | |
5f709e67 OK |
235 | WXDLLEXPORT_DATA(extern wxMBConv *) wxConvCurrent; |
236 | #define wxConv_current wxConvCurrent | |
a69f7aa8 | 237 | |
2bb67b80 | 238 | // filenames are multibyte on Unix and probably widechar on Windows? |
111edd48 | 239 | #if defined(__UNIX__) || defined(__BORLANDC__) |
2bb67b80 OK |
240 | #define wxMBFILES 1 |
241 | #else | |
242 | #define wxMBFILES 0 | |
243 | #endif | |
244 | ||
245 | #if wxMBFILES | |
5f709e67 | 246 | #define wxFNCONV(name) wxConvFile.cWX2MB(name) |
2bb67b80 OK |
247 | #define FNSTRINGCAST MBSTRINGCAST |
248 | #else | |
249 | #define wxFNCONV(name) name | |
250 | #define FNSTRINGCAST WXSTRINGCAST | |
251 | #endif | |
6f841509 | 252 | #else//!wxUSE_WCHAR_T |
a55268db | 253 | class WXDLLEXPORT wxMBConv { |
0f3e3e0c | 254 | public: |
a55268db OK |
255 | const char* cMB2WX(const char *psz) const { return psz; } |
256 | const char* cWX2MB(const char *psz) const { return psz; } | |
257 | }; | |
c50c1ddc | 258 | WXDLLEXPORT_DATA(extern wxMBConv) wxConvLibc, wxConvFile; |
c50c1ddc | 259 | WXDLLEXPORT_DATA(extern wxMBConv *) wxConvCurrent; |
6776a0b2 JS |
260 | #define wxFNCONV(name) name |
261 | #define FNSTRINGCAST WXSTRINGCAST | |
6f841509 | 262 | #endif//wxUSE_WCHAR_T |
2bb67b80 | 263 | |
c801d85f | 264 | // --------------------------------------------------------------------------- |
3c67202d VZ |
265 | // This is (yet another one) String class for C++ programmers. It doesn't use |
266 | // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...) | |
267 | // thus you should be able to compile it with practicaly any C++ compiler. | |
268 | // This class uses copy-on-write technique, i.e. identical strings share the | |
269 | // same memory as long as neither of them is changed. | |
270 | // | |
271 | // This class aims to be as compatible as possible with the new standard | |
272 | // std::string class, but adds some additional functions and should be at | |
273 | // least as efficient than the standard implementation. | |
274 | // | |
275 | // Performance note: it's more efficient to write functions which take "const | |
276 | // String&" arguments than "const char *" if you assign the argument to | |
277 | // another string. | |
278 | // | |
279 | // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5. | |
280 | // | |
281 | // To do: | |
282 | // - ressource support (string tables in ressources) | |
283 | // - more wide character (UNICODE) support | |
284 | // - regular expressions support | |
c801d85f | 285 | // --------------------------------------------------------------------------- |
3c67202d | 286 | |
c801d85f | 287 | #ifdef WXSTRING_IS_WXOBJECT |
3c67202d VZ |
288 | class WXDLLEXPORT wxString : public wxObject |
289 | { | |
c801d85f KB |
290 | DECLARE_DYNAMIC_CLASS(wxString) |
291 | #else //WXSTRING_IS_WXOBJECT | |
3c67202d VZ |
292 | class WXDLLEXPORT wxString |
293 | { | |
c801d85f KB |
294 | #endif //WXSTRING_IS_WXOBJECT |
295 | ||
fbcb4166 | 296 | friend class WXDLLEXPORT wxArrayString; |
c801d85f | 297 | |
3c67202d VZ |
298 | // NB: special care was taken in arranging the member functions in such order |
299 | // that all inline functions can be effectively inlined, verify that all | |
300 | // performace critical functions are still inlined if you change order! | |
dd1eaa89 VZ |
301 | private: |
302 | // points to data preceded by wxStringData structure with ref count info | |
2bb67b80 | 303 | wxChar *m_pchData; |
dd1eaa89 VZ |
304 | |
305 | // accessor to string data | |
306 | wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; } | |
307 | ||
6b95b20d VZ |
308 | // string (re)initialization functions |
309 | // initializes the string to the empty value (must be called only from | |
310 | // ctors, use Reinit() otherwise) | |
2bb67b80 | 311 | void Init() { m_pchData = (wxChar *)g_szNul; } |
6b95b20d | 312 | // initializaes the string with (a part of) C-string |
2bb67b80 | 313 | void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = wxSTRING_MAXLEN); |
6b95b20d VZ |
314 | // as Init, but also frees old data |
315 | void Reinit() { GetStringData()->Unlock(); Init(); } | |
316 | ||
317 | // memory allocation | |
318 | // allocates memory for string of lenght nLen | |
319 | void AllocBuffer(size_t nLen); | |
320 | // copies data to another string | |
321 | void AllocCopy(wxString&, int, int) const; | |
322 | // effectively copies data to string | |
2bb67b80 | 323 | void AssignCopy(size_t, const wxChar *); |
6b95b20d VZ |
324 | |
325 | // append a (sub)string | |
2bb67b80 | 326 | void ConcatSelf(int nLen, const wxChar *src); |
6b95b20d VZ |
327 | |
328 | // functions called before writing to the string: they copy it if there | |
329 | // are other references to our data (should be the only owner when writing) | |
330 | void CopyBeforeWrite(); | |
331 | void AllocBeforeWrite(size_t); | |
332 | ||
c606a9a4 VZ |
333 | // this method is not implemented - there is _no_ conversion from int to |
334 | // string, you're doing something wrong if the compiler wants to call it! | |
335 | // | |
336 | // try `s << i' or `s.Printf("%d", i)' instead | |
337 | wxString(int); | |
338 | wxString(long); | |
339 | ||
c801d85f | 340 | public: |
3c67202d VZ |
341 | // constructors and destructor |
342 | // ctor for an empty string | |
6b95b20d | 343 | wxString() { Init(); } |
3c67202d | 344 | // copy ctor |
6b95b20d VZ |
345 | wxString(const wxString& stringSrc) |
346 | { | |
347 | wxASSERT( stringSrc.GetStringData()->IsValid() ); | |
348 | ||
349 | if ( stringSrc.IsEmpty() ) { | |
350 | // nothing to do for an empty string | |
351 | Init(); | |
352 | } | |
353 | else { | |
354 | m_pchData = stringSrc.m_pchData; // share same data | |
355 | GetStringData()->Lock(); // => one more copy | |
356 | } | |
357 | } | |
3c67202d | 358 | // string containing nRepeat copies of ch |
2bb67b80 | 359 | wxString(wxChar ch, size_t nRepeat = 1); |
3c67202d | 360 | // ctor takes first nLength characters from C string |
566b84d2 | 361 | // (default value of wxSTRING_MAXLEN means take all the string) |
2bb67b80 | 362 | wxString(const wxChar *psz, size_t nLength = wxSTRING_MAXLEN) |
6b95b20d | 363 | { InitWith(psz, 0, nLength); } |
2bb67b80 OK |
364 | #if wxUSE_UNICODE |
365 | // from multibyte string | |
366 | // (NB: nLength is right now number of Unicode characters, not | |
367 | // characters in psz! So try not to use it yet!) | |
5f709e67 | 368 | wxString(const char *psz, wxMBConv& conv = wxConvLibc, size_t nLength = wxSTRING_MAXLEN); |
2bb67b80 OK |
369 | // from wxWCharBuffer (i.e. return from wxGetString) |
370 | wxString(const wxWCharBuffer& psz) | |
371 | { InitWith(psz, 0, wxSTRING_MAXLEN); } | |
372 | #else | |
3c67202d | 373 | // from C string (for compilers using unsigned char) |
2bb67b80 OK |
374 | wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN) |
375 | { InitWith((const char*)psz, 0, nLength); } | |
3a309a64 OK |
376 | // from multibyte string |
377 | wxString(const char *psz, wxMBConv& WXUNUSED(conv), size_t nLength = wxSTRING_MAXLEN) | |
378 | { InitWith(psz, 0, nLength); } | |
6f841509 | 379 | #if wxUSE_WCHAR_T |
2bb67b80 | 380 | // from wide (Unicode) string |
c801d85f | 381 | wxString(const wchar_t *pwz); |
6f841509 | 382 | #endif |
2bb67b80 OK |
383 | // from wxCharBuffer |
384 | wxString(const wxCharBuffer& psz) | |
385 | { InitWith(psz, 0, wxSTRING_MAXLEN); } | |
386 | #endif | |
3c67202d | 387 | // dtor is not virtual, this class must not be inherited from! |
6b95b20d | 388 | ~wxString() { GetStringData()->Unlock(); } |
c801d85f | 389 | |
3c67202d VZ |
390 | // generic attributes & operations |
391 | // as standard strlen() | |
47d67540 | 392 | size_t Len() const { return GetStringData()->nDataLength; } |
3c67202d | 393 | // string contains any characters? |
dd1eaa89 | 394 | bool IsEmpty() const { return Len() == 0; } |
dcfde592 VZ |
395 | // empty string is "FALSE", so !str will return TRUE |
396 | bool operator!() const { return IsEmpty(); } | |
3c67202d | 397 | // empty string contents |
dd1eaa89 VZ |
398 | void Empty() |
399 | { | |
2c3b684c | 400 | if ( !IsEmpty() ) |
dd1eaa89 VZ |
401 | Reinit(); |
402 | ||
7be07660 | 403 | // should be empty |
dd1eaa89 | 404 | wxASSERT( GetStringData()->nDataLength == 0 ); |
7be07660 | 405 | } |
3c67202d | 406 | // empty the string and free memory |
7be07660 VZ |
407 | void Clear() |
408 | { | |
409 | if ( !GetStringData()->IsEmpty() ) | |
410 | Reinit(); | |
411 | ||
412 | wxASSERT( GetStringData()->nDataLength == 0 ); // should be empty | |
413 | wxASSERT( GetStringData()->nAllocLength == 0 ); // and not own any memory | |
dd1eaa89 VZ |
414 | } |
415 | ||
3c67202d VZ |
416 | // contents test |
417 | // Is an ascii value | |
c801d85f | 418 | bool IsAscii() const; |
3c67202d | 419 | // Is a number |
c801d85f | 420 | bool IsNumber() const; |
3c67202d | 421 | // Is a word |
c801d85f | 422 | bool IsWord() const; |
c801d85f | 423 | |
3c67202d VZ |
424 | // data access (all indexes are 0 based) |
425 | // read access | |
2bb67b80 | 426 | wxChar GetChar(size_t n) const |
dd1eaa89 | 427 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
3c67202d | 428 | // read/write access |
2bb67b80 | 429 | wxChar& GetWritableChar(size_t n) |
dd1eaa89 | 430 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
3c67202d | 431 | // write access |
2bb67b80 | 432 | void SetChar(size_t n, wxChar ch) |
c801d85f KB |
433 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; } |
434 | ||
3c67202d | 435 | // get last character |
2bb67b80 | 436 | wxChar Last() const |
c801d85f | 437 | { wxASSERT( !IsEmpty() ); return m_pchData[Len() - 1]; } |
3c67202d | 438 | // get writable last character |
2bb67b80 | 439 | wxChar& Last() |
c801d85f KB |
440 | { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData[Len()-1]; } |
441 | ||
57493f9f VZ |
442 | // under Unix it is tested with configure, assume it works on other |
443 | // platforms (there might be overloading problems if size_t and int are | |
444 | // the same type) | |
227b5cd7 | 445 | #if !defined(__UNIX__) || wxUSE_SIZE_T_STRING_OPERATOR |
3c67202d | 446 | // operator version of GetChar |
2bb67b80 | 447 | wxChar operator[](size_t n) const |
c801d85f | 448 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
22552603 | 449 | #endif |
c5248639 | 450 | |
3c67202d | 451 | // operator version of GetChar |
2bb67b80 | 452 | wxChar operator[](int n) const |
c801d85f | 453 | { ASSERT_VALID_INDEX( n ); return m_pchData[n]; } |
3c67202d | 454 | // operator version of GetWritableChar |
2bb67b80 | 455 | wxChar& operator[](size_t n) |
c801d85f KB |
456 | { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } |
457 | ||
3c67202d | 458 | // implicit conversion to C string |
2bb67b80 | 459 | operator const wxChar*() const { return m_pchData; } |
3c67202d | 460 | // explicit conversion to C string (use this with printf()!) |
2bb67b80 | 461 | const wxChar* c_str() const { return m_pchData; } |
6f841509 OK |
462 | // (and this with [wx]Printf()!) |
463 | const wxChar* wx_str() const { return m_pchData; } | |
3c67202d | 464 | // |
2bb67b80 OK |
465 | const wxChar* GetData() const { return m_pchData; } |
466 | #if wxUSE_UNICODE | |
5f709e67 OK |
467 | const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const { return conv.cWC2MB(m_pchData); } |
468 | const wxChar* wc_str(wxMBConv& WXUNUSED(conv) = wxConvLibc) const { return m_pchData; } | |
2bb67b80 | 469 | #if wxMBFILES |
5f709e67 | 470 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
2bb67b80 OK |
471 | #else |
472 | const wxChar* fn_str() const { return m_pchData; } | |
473 | #endif | |
474 | #else | |
5f709e67 | 475 | const wxChar* mb_str(wxMBConv& WXUNUSED(conv) = wxConvLibc ) const { return m_pchData; } |
6f841509 | 476 | #if wxUSE_WCHAR_T |
2bb67b80 | 477 | const wxWCharBuffer wc_str(wxMBConv& conv) const { return conv.cMB2WC(m_pchData); } |
6f841509 | 478 | #endif |
2bb67b80 OK |
479 | const wxChar* fn_str() const { return m_pchData; } |
480 | #endif | |
a69f7aa8 | 481 | // for convenience |
5f709e67 | 482 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
c801d85f | 483 | |
3c67202d VZ |
484 | // overloaded assignment |
485 | // from another wxString | |
c801d85f | 486 | wxString& operator=(const wxString& stringSrc); |
3c67202d | 487 | // from a character |
2bb67b80 | 488 | wxString& operator=(wxChar ch); |
3c67202d | 489 | // from a C string |
2bb67b80 | 490 | wxString& operator=(const wxChar *psz); |
111bb7f2 OK |
491 | #if wxUSE_UNICODE |
492 | // from wxWCharBuffer | |
493 | wxString& operator=(const wxWCharBuffer& psz) { return operator=((const wchar_t *)psz); } | |
494 | #else | |
3c67202d | 495 | // from another kind of C string |
c801d85f | 496 | wxString& operator=(const unsigned char* psz); |
6f841509 | 497 | #if wxUSE_WCHAR_T |
3c67202d | 498 | // from a wide string |
c801d85f | 499 | wxString& operator=(const wchar_t *pwz); |
6f841509 | 500 | #endif |
111bb7f2 OK |
501 | // from wxCharBuffer |
502 | wxString& operator=(const wxCharBuffer& psz) { return operator=((const char *)psz); } | |
2bb67b80 | 503 | #endif |
3c67202d VZ |
504 | |
505 | // string concatenation | |
506 | // in place concatenation | |
507 | /* | |
508 | Concatenate and return the result. Note that the left to right | |
509 | associativity of << allows to write things like "str << str1 << str2 | |
510 | << ..." (unlike with +=) | |
511 | */ | |
512 | // string += string | |
dd1eaa89 VZ |
513 | wxString& operator<<(const wxString& s) |
514 | { | |
515 | wxASSERT( s.GetStringData()->IsValid() ); | |
516 | ||
517 | ConcatSelf(s.Len(), s); | |
518 | return *this; | |
519 | } | |
3c67202d | 520 | // string += C string |
2bb67b80 OK |
521 | wxString& operator<<(const wxChar *psz) |
522 | { ConcatSelf(wxStrlen(psz), psz); return *this; } | |
3c67202d | 523 | // string += char |
2bb67b80 | 524 | wxString& operator<<(wxChar ch) { ConcatSelf(1, &ch); return *this; } |
dd1eaa89 | 525 | |
3c67202d | 526 | // string += string |
6b95b20d | 527 | void operator+=(const wxString& s) { (void)operator<<(s); } |
3c67202d | 528 | // string += C string |
2bb67b80 | 529 | void operator+=(const wxChar *psz) { (void)operator<<(psz); } |
3c67202d | 530 | // string += char |
2bb67b80 OK |
531 | void operator+=(wxChar ch) { (void)operator<<(ch); } |
532 | ||
533 | // string += buffer (i.e. from wxGetString) | |
534 | #if wxUSE_UNICODE | |
535 | wxString& operator<<(const wxWCharBuffer& s) { (void)operator<<((const wchar_t *)s); return *this; } | |
536 | void operator+=(const wxWCharBuffer& s) { (void)operator<<((const wchar_t *)s); } | |
537 | #else | |
538 | wxString& operator<<(const wxCharBuffer& s) { (void)operator<<((const char *)s); return *this; } | |
539 | void operator+=(const wxCharBuffer& s) { (void)operator<<((const char *)s); } | |
540 | #endif | |
6b95b20d | 541 | |
3c67202d | 542 | // string += C string |
2bb67b80 OK |
543 | wxString& Append(const wxChar* psz) |
544 | { ConcatSelf(wxStrlen(psz), psz); return *this; } | |
3c67202d | 545 | // append count copies of given character |
2bb67b80 | 546 | wxString& Append(wxChar ch, size_t count = 1u) |
3c67202d VZ |
547 | { wxString str(ch, count); return *this << str; } |
548 | ||
549 | // prepend a string, return the string itself | |
550 | wxString& Prepend(const wxString& str) | |
551 | { *this = str + *this; return *this; } | |
552 | ||
553 | // non-destructive concatenation | |
554 | // | |
c33534e5 | 555 | friend wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2); |
3c67202d | 556 | // |
2bb67b80 | 557 | friend wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch); |
3c67202d | 558 | // |
2bb67b80 | 559 | friend wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string); |
3c67202d | 560 | // |
2bb67b80 | 561 | friend wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz); |
3c67202d | 562 | // |
2bb67b80 | 563 | friend wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string); |
3c67202d VZ |
564 | |
565 | // stream-like functions | |
566 | // insert an int into string | |
567 | wxString& operator<<(int i); | |
568 | // insert a float into string | |
569 | wxString& operator<<(float f); | |
570 | // insert a double into string | |
571 | wxString& operator<<(double d); | |
c84c52de | 572 | |
3c67202d | 573 | // string comparison |
30b21f9a | 574 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
f33fee2a | 575 | int Cmp(const wxChar *psz) const { return wxStrcmp(c_str(), psz); } |
3c67202d | 576 | // same as Cmp() but not case-sensitive |
f33fee2a | 577 | int CmpNoCase(const wxChar *psz) const { return wxStricmp(c_str(), psz); } |
3c67202d VZ |
578 | // test for the string equality, either considering case or not |
579 | // (if compareWithCase then the case matters) | |
2bb67b80 | 580 | bool IsSameAs(const wxChar *psz, bool compareWithCase = TRUE) const |
3c67202d | 581 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } |
f33fee2a VZ |
582 | // comparison with a signle character: returns TRUE if equal |
583 | bool IsSameAs(wxChar c, bool compareWithCase = TRUE) const | |
584 | { | |
585 | return (Len() == 1) && (compareWithCase ? GetChar(0u) == c | |
586 | : wxToupper(GetChar(0u)) == wxToupper(c)); | |
587 | } | |
3c67202d VZ |
588 | |
589 | // simple sub-string extraction | |
590 | // return substring starting at nFirst of length nCount (or till the end | |
591 | // if nCount = default value) | |
566b84d2 | 592 | wxString Mid(size_t nFirst, size_t nCount = wxSTRING_MAXLEN) const; |
3c67202d VZ |
593 | |
594 | // operator version of Mid() | |
595 | wxString operator()(size_t start, size_t len) const | |
596 | { return Mid(start, len); } | |
597 | ||
598 | // get first nCount characters | |
c801d85f | 599 | wxString Left(size_t nCount) const; |
3c67202d | 600 | // get last nCount characters |
c801d85f | 601 | wxString Right(size_t nCount) const; |
3c67202d VZ |
602 | // get all characters before the first occurence of ch |
603 | // (returns the whole string if ch not found) | |
2bb67b80 | 604 | wxString BeforeFirst(wxChar ch) const; |
3c67202d VZ |
605 | // get all characters before the last occurence of ch |
606 | // (returns empty string if ch not found) | |
2bb67b80 | 607 | wxString BeforeLast(wxChar ch) const; |
3c67202d VZ |
608 | // get all characters after the first occurence of ch |
609 | // (returns empty string if ch not found) | |
2bb67b80 | 610 | wxString AfterFirst(wxChar ch) const; |
3c67202d VZ |
611 | // get all characters after the last occurence of ch |
612 | // (returns the whole string if ch not found) | |
2bb67b80 | 613 | wxString AfterLast(wxChar ch) const; |
3c67202d VZ |
614 | |
615 | // for compatibility only, use more explicitly named functions above | |
2bb67b80 OK |
616 | wxString Before(wxChar ch) const { return BeforeLast(ch); } |
617 | wxString After(wxChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
618 | |
619 | // case conversion | |
c84c52de | 620 | // convert to upper case in place, return the string itself |
c801d85f | 621 | wxString& MakeUpper(); |
c84c52de | 622 | // convert to upper case, return the copy of the string |
03ab016d JS |
623 | // Here's something to remember: BC++ doesn't like returns in inlines. |
624 | wxString Upper() const ; | |
c84c52de | 625 | // convert to lower case in place, return the string itself |
c801d85f | 626 | wxString& MakeLower(); |
c84c52de | 627 | // convert to lower case, return the copy of the string |
03ab016d | 628 | wxString Lower() const ; |
c801d85f | 629 | |
3c67202d VZ |
630 | // trimming/padding whitespace (either side) and truncating |
631 | // remove spaces from left or from right (default) side | |
c801d85f | 632 | wxString& Trim(bool bFromRight = TRUE); |
3c67202d | 633 | // add nCount copies chPad in the beginning or at the end (default) |
2bb67b80 | 634 | wxString& Pad(size_t nCount, wxChar chPad = _T(' '), bool bFromRight = TRUE); |
3c67202d | 635 | // truncate string to given length |
c801d85f | 636 | wxString& Truncate(size_t uiLen); |
dd1eaa89 | 637 | |
3c67202d VZ |
638 | // searching and replacing |
639 | // searching (return starting index, or -1 if not found) | |
2bb67b80 | 640 | int Find(wxChar ch, bool bFromEnd = FALSE) const; // like strchr/strrchr |
3c67202d | 641 | // searching (return starting index, or -1 if not found) |
2bb67b80 | 642 | int Find(const wxChar *pszSub) const; // like strstr |
3c67202d VZ |
643 | // replace first (or all of bReplaceAll) occurences of substring with |
644 | // another string, returns the number of replacements made | |
2bb67b80 OK |
645 | size_t Replace(const wxChar *szOld, |
646 | const wxChar *szNew, | |
3c67202d VZ |
647 | bool bReplaceAll = TRUE); |
648 | ||
649 | // check if the string contents matches a mask containing '*' and '?' | |
2bb67b80 | 650 | bool Matches(const wxChar *szMask) const; |
c801d85f | 651 | |
3c67202d VZ |
652 | // formated input/output |
653 | // as sprintf(), returns the number of characters written or < 0 on error | |
2bb67b80 | 654 | int Printf(const wxChar *pszFormat, ...); |
3c67202d | 655 | // as vprintf(), returns the number of characters written or < 0 on error |
2bb67b80 | 656 | int PrintfV(const wxChar* pszFormat, va_list argptr); |
dd1eaa89 | 657 | |
3c67202d VZ |
658 | // raw access to string memory |
659 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 660 | // only works if the data of this string is not shared |
c86f1403 | 661 | void Alloc(size_t nLen); |
3c67202d | 662 | // minimize the string's memory |
dd1eaa89 VZ |
663 | // only works if the data of this string is not shared |
664 | void Shrink(); | |
3c67202d VZ |
665 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
666 | // a.s.a.p. to put string back in a reasonable state! | |
2bb67b80 | 667 | wxChar *GetWriteBuf(size_t nLen); |
3c67202d | 668 | // call this immediately after GetWriteBuf() has been used |
8fd0f20b | 669 | void UngetWriteBuf(); |
c801d85f | 670 | |
3c67202d VZ |
671 | // wxWindows version 1 compatibility functions |
672 | ||
673 | // use Mid() | |
674 | wxString SubString(size_t from, size_t to) const | |
675 | { return Mid(from, (to - from + 1)); } | |
676 | // values for second parameter of CompareTo function | |
c801d85f | 677 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 678 | // values for first parameter of Strip function |
c801d85f | 679 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 680 | |
3c67202d | 681 | // use Printf() |
2bb67b80 | 682 | int sprintf(const wxChar *pszFormat, ...); |
c801d85f | 683 | |
3c67202d | 684 | // use Cmp() |
2bb67b80 | 685 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 686 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 687 | |
3c67202d | 688 | // use Len |
c801d85f | 689 | size_t Length() const { return Len(); } |
3c67202d | 690 | // Count the number of characters |
2bb67b80 | 691 | int Freq(wxChar ch) const; |
3c67202d | 692 | // use MakeLower |
c801d85f | 693 | void LowerCase() { MakeLower(); } |
3c67202d | 694 | // use MakeUpper |
c801d85f | 695 | void UpperCase() { MakeUpper(); } |
3c67202d | 696 | // use Trim except that it doesn't change this string |
c801d85f KB |
697 | wxString Strip(stripType w = trailing) const; |
698 | ||
3c67202d | 699 | // use Find (more general variants not yet supported) |
2bb67b80 OK |
700 | size_t Index(const wxChar* psz) const { return Find(psz); } |
701 | size_t Index(wxChar ch) const { return Find(ch); } | |
3c67202d | 702 | // use Truncate |
c801d85f KB |
703 | wxString& Remove(size_t pos) { return Truncate(pos); } |
704 | wxString& RemoveLast() { return Truncate(Len() - 1); } | |
705 | ||
3ed358cb | 706 | wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); } |
dd1eaa89 | 707 | |
3c67202d | 708 | // use Find() |
2bb67b80 OK |
709 | int First( const wxChar ch ) const { return Find(ch); } |
710 | int First( const wxChar* psz ) const { return Find(psz); } | |
3ed358cb | 711 | int First( const wxString &str ) const { return Find(str); } |
2bb67b80 | 712 | int Last( const wxChar ch ) const { return Find(ch, TRUE); } |
3c67202d | 713 | bool Contains(const wxString& str) const { return Find(str) != -1; } |
c801d85f | 714 | |
3c67202d | 715 | // use IsEmpty() |
c801d85f | 716 | bool IsNull() const { return IsEmpty(); } |
c801d85f | 717 | |
3c67202d VZ |
718 | #ifdef wxSTD_STRING_COMPATIBILITY |
719 | // std::string compatibility functions | |
dd1eaa89 | 720 | |
2bc07607 VZ |
721 | // standard types |
722 | typedef wxChar value_type; | |
723 | typedef const value_type *const_iterator; | |
724 | ||
3c67202d | 725 | // an 'invalid' value for string index |
c801d85f | 726 | static const size_t npos; |
dd1eaa89 | 727 | |
3c67202d VZ |
728 | // constructors |
729 | // take nLen chars starting at nPos | |
730 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
731 | { | |
732 | wxASSERT( str.GetStringData()->IsValid() ); | |
733 | InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen); | |
734 | } | |
735 | // take all characters from pStart to pEnd | |
736 | wxString(const void *pStart, const void *pEnd); | |
737 | ||
738 | // lib.string.capacity | |
739 | // return the length of the string | |
740 | size_t size() const { return Len(); } | |
741 | // return the length of the string | |
742 | size_t length() const { return Len(); } | |
743 | // return the maximum size of the string | |
566b84d2 | 744 | size_t max_size() const { return wxSTRING_MAXLEN; } |
3c67202d | 745 | // resize the string, filling the space with c if c != 0 |
2bb67b80 | 746 | void resize(size_t nSize, wxChar ch = _T('\0')); |
3c67202d VZ |
747 | // delete the contents of the string |
748 | void clear() { Empty(); } | |
749 | // returns true if the string is empty | |
750 | bool empty() const { return IsEmpty(); } | |
751 | ||
752 | // lib.string.access | |
753 | // return the character at position n | |
2bb67b80 | 754 | wxChar at(size_t n) const { return GetChar(n); } |
3c67202d | 755 | // returns the writable character at position n |
2bb67b80 | 756 | wxChar& at(size_t n) { return GetWritableChar(n); } |
3c67202d | 757 | |
2bc07607 VZ |
758 | // first valid index position |
759 | const_iterator begin() const { return wx_str(); } | |
760 | // position one after the last valid one | |
761 | const_iterator end() const { return wx_str() + length(); } | |
762 | ||
3c67202d VZ |
763 | // lib.string.modifiers |
764 | // append a string | |
765 | wxString& append(const wxString& str) | |
766 | { *this += str; return *this; } | |
767 | // append elements str[pos], ..., str[pos+n] | |
768 | wxString& append(const wxString& str, size_t pos, size_t n) | |
769 | { ConcatSelf(n, str.c_str() + pos); return *this; } | |
770 | // append first n (or all if n == npos) characters of sz | |
2bb67b80 OK |
771 | wxString& append(const wxChar *sz, size_t n = npos) |
772 | { ConcatSelf(n == npos ? wxStrlen(sz) : n, sz); return *this; } | |
3c67202d VZ |
773 | |
774 | // append n copies of ch | |
2bb67b80 | 775 | wxString& append(size_t n, wxChar ch) { return Pad(n, ch); } |
3c67202d VZ |
776 | |
777 | // same as `this_string = str' | |
778 | wxString& assign(const wxString& str) { return (*this) = str; } | |
779 | // same as ` = str[pos..pos + n] | |
780 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
2bb67b80 | 781 | { return *this = wxString((const wxChar *)str + pos, n); } |
3c67202d | 782 | // same as `= first n (or all if n == npos) characters of sz' |
2bb67b80 | 783 | wxString& assign(const wxChar *sz, size_t n = npos) |
3c67202d VZ |
784 | { return *this = wxString(sz, n); } |
785 | // same as `= n copies of ch' | |
2bb67b80 | 786 | wxString& assign(size_t n, wxChar ch) |
3c67202d VZ |
787 | { return *this = wxString(ch, n); } |
788 | ||
789 | // insert another string | |
790 | wxString& insert(size_t nPos, const wxString& str); | |
791 | // insert n chars of str starting at nStart (in str) | |
792 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
2bb67b80 | 793 | { return insert(nPos, wxString((const wxChar *)str + nStart, n)); } |
3c67202d VZ |
794 | |
795 | // insert first n (or all if n == npos) characters of sz | |
2bb67b80 | 796 | wxString& insert(size_t nPos, const wxChar *sz, size_t n = npos) |
3c67202d VZ |
797 | { return insert(nPos, wxString(sz, n)); } |
798 | // insert n copies of ch | |
2bb67b80 | 799 | wxString& insert(size_t nPos, size_t n, wxChar ch) |
3c67202d VZ |
800 | { return insert(nPos, wxString(ch, n)); } |
801 | ||
802 | // delete characters from nStart to nStart + nLen | |
803 | wxString& erase(size_t nStart = 0, size_t nLen = npos); | |
804 | ||
805 | // replaces the substring of length nLen starting at nStart | |
2bb67b80 | 806 | wxString& replace(size_t nStart, size_t nLen, const wxChar* sz); |
3c67202d | 807 | // replaces the substring with nCount copies of ch |
2bb67b80 | 808 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch); |
3c67202d VZ |
809 | // replaces a substring with another substring |
810 | wxString& replace(size_t nStart, size_t nLen, | |
811 | const wxString& str, size_t nStart2, size_t nLen2); | |
812 | // replaces the substring with first nCount chars of sz | |
813 | wxString& replace(size_t nStart, size_t nLen, | |
2bb67b80 | 814 | const wxChar* sz, size_t nCount); |
3c67202d VZ |
815 | |
816 | // swap two strings | |
817 | void swap(wxString& str); | |
818 | ||
819 | // All find() functions take the nStart argument which specifies the | |
820 | // position to start the search on, the default value is 0. All functions | |
821 | // return npos if there were no match. | |
822 | ||
823 | // find a substring | |
824 | size_t find(const wxString& str, size_t nStart = 0) const; | |
825 | ||
826 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 827 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d | 828 | // find first n characters of sz |
2bb67b80 | 829 | size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; |
6b0eb19f | 830 | #endif |
3c67202d VZ |
831 | |
832 | // Gives a duplicate symbol (presumably a case-insensitivity problem) | |
62448488 | 833 | #if !defined(__BORLANDC__) |
3c67202d | 834 | // find the first occurence of character ch after nStart |
2bb67b80 | 835 | size_t find(wxChar ch, size_t nStart = 0) const; |
62448488 | 836 | #endif |
3c67202d VZ |
837 | // rfind() family is exactly like find() but works right to left |
838 | ||
839 | // as find, but from the end | |
840 | size_t rfind(const wxString& str, size_t nStart = npos) const; | |
841 | ||
842 | // VC++ 1.5 can't cope with this syntax. | |
3f4a0c5b | 843 | #if !defined(__VISUALC__) || defined(__WIN32__) |
3c67202d | 844 | // as find, but from the end |
2bb67b80 | 845 | size_t rfind(const wxChar* sz, size_t nStart = npos, |
3c67202d VZ |
846 | size_t n = npos) const; |
847 | // as find, but from the end | |
2bb67b80 | 848 | size_t rfind(wxChar ch, size_t nStart = npos) const; |
c801d85f | 849 | #endif |
3c67202d VZ |
850 | |
851 | // find first/last occurence of any character in the set | |
852 | ||
969d318c VZ |
853 | // as strpbrk() but starts at nStart, returns npos if not found |
854 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
855 | { return find_first_of(str.c_str(), nStart); } | |
856 | // same as above | |
2bb67b80 | 857 | size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; |
3c67202d | 858 | // same as find(char, size_t) |
969d318c VZ |
859 | size_t find_first_of(wxChar c, size_t nStart = 0) const |
860 | { return find(c, nStart); } | |
861 | // find the last (starting from nStart) char from str in this string | |
862 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
863 | { return find_last_of(str.c_str(), nStart); } | |
864 | // same as above | |
865 | size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; | |
866 | // same as above | |
867 | size_t find_last_of(wxChar c, size_t nStart = npos) const | |
868 | { return rfind(c, nStart); } | |
3c67202d VZ |
869 | |
870 | // find first/last occurence of any character not in the set | |
871 | ||
969d318c VZ |
872 | // as strspn() (starting from nStart), returns npos on failure |
873 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
874 | { return find_first_not_of(str.c_str(), nStart); } | |
875 | // same as above | |
876 | size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; | |
877 | // same as above | |
2bb67b80 | 878 | size_t find_first_not_of(wxChar ch, size_t nStart = 0) const; |
969d318c | 879 | // as strcspn() |
3c67202d | 880 | size_t find_last_not_of(const wxString& str, size_t nStart=npos) const; |
969d318c VZ |
881 | // same as above |
882 | size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; | |
883 | // same as above | |
2bb67b80 | 884 | size_t find_last_not_of(wxChar ch, size_t nStart = npos) const; |
3c67202d VZ |
885 | |
886 | // All compare functions return -1, 0 or 1 if the [sub]string is less, | |
887 | // equal or greater than the compare() argument. | |
888 | ||
889 | // just like strcmp() | |
890 | int compare(const wxString& str) const { return Cmp(str); } | |
891 | // comparison with a substring | |
892 | int compare(size_t nStart, size_t nLen, const wxString& str) const; | |
893 | // comparison of 2 substrings | |
894 | int compare(size_t nStart, size_t nLen, | |
895 | const wxString& str, size_t nStart2, size_t nLen2) const; | |
896 | // just like strcmp() | |
2bb67b80 | 897 | int compare(const wxChar* sz) const { return Cmp(sz); } |
3c67202d VZ |
898 | // substring comparison with first nCount characters of sz |
899 | int compare(size_t nStart, size_t nLen, | |
2bb67b80 | 900 | const wxChar* sz, size_t nCount = npos) const; |
3c67202d VZ |
901 | |
902 | // substring extraction | |
903 | wxString substr(size_t nStart = 0, size_t nLen = npos) const; | |
904 | #endif // wxSTD_STRING_COMPATIBILITY | |
c801d85f KB |
905 | }; |
906 | ||
907 | // ---------------------------------------------------------------------------- | |
3c67202d VZ |
908 | // The string array uses it's knowledge of internal structure of the wxString |
909 | // class to optimize string storage. Normally, we would store pointers to | |
910 | // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is | |
911 | // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is | |
912 | // really all we need to turn such pointer into a string! | |
913 | // | |
914 | // Of course, it can be called a dirty hack, but we use twice less memory and | |
915 | // this approach is also more speed efficient, so it's probably worth it. | |
916 | // | |
917 | // Usage notes: when a string is added/inserted, a new copy of it is created, | |
918 | // so the original string may be safely deleted. When a string is retrieved | |
919 | // from the array (operator[] or Item() method), a reference is returned. | |
c801d85f | 920 | // ---------------------------------------------------------------------------- |
fbcb4166 | 921 | class WXDLLEXPORT wxArrayString |
c801d85f KB |
922 | { |
923 | public: | |
30b21f9a VZ |
924 | // type of function used by wxArrayString::Sort() |
925 | typedef int (*CompareFunction)(const wxString& first, | |
926 | const wxString& second); | |
927 | ||
3c67202d VZ |
928 | // constructors and destructor |
929 | // default ctor | |
c801d85f | 930 | wxArrayString(); |
3c67202d | 931 | // copy ctor |
c801d85f | 932 | wxArrayString(const wxArrayString& array); |
3c67202d | 933 | // assignment operator |
c801d85f | 934 | wxArrayString& operator=(const wxArrayString& src); |
3c67202d | 935 | // not virtual, this class should not be derived from |
c801d85f | 936 | ~wxArrayString(); |
c801d85f | 937 | |
3c67202d VZ |
938 | // memory management |
939 | // empties the list, but doesn't release memory | |
c801d85f | 940 | void Empty(); |
3c67202d | 941 | // empties the list and releases memory |
c801d85f | 942 | void Clear(); |
3c67202d | 943 | // preallocates memory for given number of items |
c801d85f | 944 | void Alloc(size_t nCount); |
3c67202d | 945 | // minimzes the memory usage (by freeing all extra memory) |
dd1eaa89 | 946 | void Shrink(); |
3c67202d VZ |
947 | |
948 | // simple accessors | |
949 | // number of elements in the array | |
950 | size_t GetCount() const { return m_nCount; } | |
951 | // is it empty? | |
952 | bool IsEmpty() const { return m_nCount == 0; } | |
953 | // number of elements in the array (GetCount is preferred API) | |
954 | size_t Count() const { return m_nCount; } | |
955 | ||
956 | // items access (range checking is done in debug version) | |
957 | // get item at position uiIndex | |
c801d85f KB |
958 | wxString& Item(size_t nIndex) const |
959 | { wxASSERT( nIndex < m_nCount ); return *(wxString *)&(m_pItems[nIndex]); } | |
3c67202d | 960 | // same as Item() |
c801d85f | 961 | wxString& operator[](size_t nIndex) const { return Item(nIndex); } |
3c67202d | 962 | // get last item |
c801d85f | 963 | wxString& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); } |
3c67202d VZ |
964 | |
965 | // item management | |
966 | // Search the element in the array, starting from the beginning if | |
967 | // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case | |
968 | // sensitive (default). Returns index of the first item matched or | |
969 | // wxNOT_FOUND | |
2bb67b80 | 970 | int Index (const wxChar *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const; |
3c67202d VZ |
971 | // add new element at the end |
972 | void Add(const wxString& str); | |
973 | // add new element at given position | |
c86f1403 | 974 | void Insert(const wxString& str, size_t uiIndex); |
3c67202d | 975 | // remove first item matching this value |
2bb67b80 | 976 | void Remove(const wxChar *sz); |
3c67202d | 977 | // remove item by index |
c801d85f | 978 | void Remove(size_t nIndex); |
c801d85f | 979 | |
30b21f9a VZ |
980 | // sorting |
981 | // sort array elements in alphabetical order (or reversed alphabetical | |
982 | // order if reverseOrder parameter is TRUE) | |
983 | void Sort(bool reverseOrder = FALSE); | |
984 | // sort array elements using specified comparaison function | |
985 | void Sort(CompareFunction compareFunction); | |
c801d85f KB |
986 | |
987 | private: | |
988 | void Grow(); // makes array bigger if needed | |
989 | void Free(); // free the string stored | |
990 | ||
30b21f9a VZ |
991 | void DoSort(); // common part of all Sort() variants |
992 | ||
3c67202d | 993 | size_t m_nSize, // current size of the array |
c801d85f KB |
994 | m_nCount; // current number of elements |
995 | ||
2bb67b80 | 996 | wxChar **m_pItems; // pointer to data |
c801d85f KB |
997 | }; |
998 | ||
c801d85f | 999 | // --------------------------------------------------------------------------- |
3c67202d | 1000 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 1001 | // --------------------------------------------------------------------------- |
f33fee2a | 1002 | |
3c67202d | 1003 | // |
a3ef5bf5 | 1004 | inline bool operator==(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) == 0); } |
3c67202d | 1005 | // |
2bb67b80 | 1006 | inline bool operator==(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) == 0); } |
3c67202d | 1007 | // |
2bb67b80 | 1008 | inline bool operator==(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) == 0); } |
3c67202d | 1009 | // |
a3ef5bf5 | 1010 | inline bool operator!=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) != 0); } |
3c67202d | 1011 | // |
2bb67b80 | 1012 | inline bool operator!=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) != 0); } |
3c67202d | 1013 | // |
2bb67b80 | 1014 | inline bool operator!=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) != 0); } |
3c67202d | 1015 | // |
a3ef5bf5 | 1016 | inline bool operator< (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) < 0); } |
3c67202d | 1017 | // |
2bb67b80 | 1018 | inline bool operator< (const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) < 0); } |
3c67202d | 1019 | // |
2bb67b80 | 1020 | inline bool operator< (const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) > 0); } |
3c67202d | 1021 | // |
a3ef5bf5 | 1022 | inline bool operator> (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) > 0); } |
3c67202d | 1023 | // |
2bb67b80 | 1024 | inline bool operator> (const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) > 0); } |
3c67202d | 1025 | // |
2bb67b80 | 1026 | inline bool operator> (const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) < 0); } |
3c67202d | 1027 | // |
a3ef5bf5 | 1028 | inline bool operator<=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) <= 0); } |
3c67202d | 1029 | // |
2bb67b80 | 1030 | inline bool operator<=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) <= 0); } |
3c67202d | 1031 | // |
2bb67b80 | 1032 | inline bool operator<=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) >= 0); } |
3c67202d | 1033 | // |
a3ef5bf5 | 1034 | inline bool operator>=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) >= 0); } |
3c67202d | 1035 | // |
2bb67b80 | 1036 | inline bool operator>=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) >= 0); } |
3c67202d | 1037 | // |
2bb67b80 | 1038 | inline bool operator>=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) <= 0); } |
3c67202d | 1039 | |
f33fee2a VZ |
1040 | // comparison with char |
1041 | inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); } | |
1042 | inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); } | |
1043 | inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); } | |
1044 | inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } | |
1045 | ||
5ca07d0f OK |
1046 | #if wxUSE_UNICODE |
1047 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 1048 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 1049 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 1050 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
5ca07d0f OK |
1051 | #else |
1052 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) | |
f33fee2a | 1053 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 1054 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 1055 | { return (s2.Cmp((const char *)s1) == 0); } |
5ca07d0f OK |
1056 | #endif |
1057 | ||
ed7174ba | 1058 | wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2); |
2bb67b80 OK |
1059 | wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch); |
1060 | wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string); | |
1061 | wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz); | |
1062 | wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string); | |
028a2b5d | 1063 | #if wxUSE_UNICODE |
c6cee672 | 1064 | inline wxString WXDLLEXPORT operator+(const wxString& string, const wxWCharBuffer& buf) |
028a2b5d | 1065 | { return string + (const wchar_t *)buf; } |
c6cee672 | 1066 | inline wxString WXDLLEXPORT operator+(const wxWCharBuffer& buf, const wxString& string) |
bf88baa6 | 1067 | { return (const wchar_t *)buf + string; } |
028a2b5d | 1068 | #else |
c6cee672 | 1069 | inline wxString WXDLLEXPORT operator+(const wxString& string, const wxCharBuffer& buf) |
028a2b5d | 1070 | { return string + (const char *)buf; } |
c6cee672 | 1071 | inline wxString WXDLLEXPORT operator+(const wxCharBuffer& buf, const wxString& string) |
bf88baa6 | 1072 | { return (const char *)buf + string; } |
028a2b5d | 1073 | #endif |
f04f3991 | 1074 | |
c801d85f | 1075 | // --------------------------------------------------------------------------- |
3c67202d | 1076 | // Implementation only from here until the end of file |
c801d85f KB |
1077 | // --------------------------------------------------------------------------- |
1078 | ||
38830220 | 1079 | #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM |
c801d85f | 1080 | |
3f4a0c5b | 1081 | #include "wx/ioswrap.h" |
c801d85f | 1082 | |
184b5d99 | 1083 | WXDLLEXPORT istream& operator>>(istream& is, wxString& str); |
c801d85f | 1084 | |
3c67202d | 1085 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 1086 | |
34138703 | 1087 | #endif // _WX_WXSTRINGH__ |