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