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