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