]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
8898456d | 2 | // Name: src/common/string.cpp |
c801d85f | 3 | // Purpose: wxString class |
59059feb | 4 | // Author: Vadim Zeitlin, Ryan Norton |
c801d85f KB |
5 | // Modified by: |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
59059feb | 9 | // (c) 2004 Ryan Norton <wxprojects@comcast.net> |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
c801d85f KB |
13 | // =========================================================================== |
14 | // headers, declarations, constants | |
15 | // =========================================================================== | |
16 | ||
17 | // For compilers that support precompilation, includes "wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
8898456d | 21 | #pragma hdrstop |
c801d85f KB |
22 | #endif |
23 | ||
24 | #ifndef WX_PRECOMP | |
8898456d | 25 | #include "wx/string.h" |
2523e9b7 | 26 | #include "wx/wxcrtvararg.h" |
6b769f3d | 27 | #endif |
c801d85f KB |
28 | |
29 | #include <ctype.h> | |
92df97b8 WS |
30 | |
31 | #ifndef __WXWINCE__ | |
32 | #include <errno.h> | |
33 | #endif | |
34 | ||
c801d85f KB |
35 | #include <string.h> |
36 | #include <stdlib.h> | |
9a08c20e | 37 | |
ce3ed50d | 38 | #ifdef __SALFORDC__ |
8898456d | 39 | #include <clib.h> |
ce3ed50d JS |
40 | #endif |
41 | ||
8116a0c5 | 42 | #include "wx/hashmap.h" |
8f93a29f VS |
43 | |
44 | // string handling functions used by wxString: | |
45 | #if wxUSE_UNICODE_UTF8 | |
46 | #define wxStringMemcpy memcpy | |
47 | #define wxStringMemcmp memcmp | |
48 | #define wxStringMemchr memchr | |
49 | #define wxStringStrlen strlen | |
50 | #else | |
51 | #define wxStringMemcpy wxTmemcpy | |
52 | #define wxStringMemcmp wxTmemcmp | |
a7ea63e2 VS |
53 | #define wxStringMemchr wxTmemchr |
54 | #define wxStringStrlen wxStrlen | |
55 | #endif | |
8f93a29f | 56 | |
e87b7833 | 57 | |
a7ea63e2 VS |
58 | // --------------------------------------------------------------------------- |
59 | // static class variables definition | |
60 | // --------------------------------------------------------------------------- | |
e87b7833 | 61 | |
a7ea63e2 VS |
62 | //According to STL _must_ be a -1 size_t |
63 | const size_t wxString::npos = (size_t) -1; | |
8f93a29f | 64 | |
a7ea63e2 VS |
65 | // ---------------------------------------------------------------------------- |
66 | // global functions | |
67 | // ---------------------------------------------------------------------------- | |
e87b7833 | 68 | |
a7ea63e2 | 69 | #if wxUSE_STD_IOSTREAM |
8f93a29f | 70 | |
a7ea63e2 | 71 | #include <iostream> |
8f93a29f | 72 | |
a7ea63e2 | 73 | wxSTD ostream& operator<<(wxSTD ostream& os, const wxCStrData& str) |
8f93a29f | 74 | { |
04abe4bc | 75 | // FIXME-UTF8: always, not only if wxUSE_UNICODE |
a7ea63e2 | 76 | #if wxUSE_UNICODE && !defined(__BORLANDC__) |
681e4412 | 77 | return os << (const wchar_t*)str.AsWCharBuf(); |
a7ea63e2 | 78 | #else |
681e4412 | 79 | return os << (const char*)str.AsCharBuf(); |
a7ea63e2 | 80 | #endif |
8f93a29f VS |
81 | } |
82 | ||
04abe4bc VS |
83 | wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str) |
84 | { | |
85 | return os << str.c_str(); | |
86 | } | |
87 | ||
88 | wxSTD ostream& operator<<(wxSTD ostream& os, const wxCharBuffer& str) | |
89 | { | |
90 | return os << str.data(); | |
91 | } | |
92 | ||
93 | #ifndef __BORLANDC__ | |
94 | wxSTD ostream& operator<<(wxSTD ostream& os, const wxWCharBuffer& str) | |
95 | { | |
96 | return os << str.data(); | |
97 | } | |
98 | #endif | |
99 | ||
a7ea63e2 | 100 | #endif // wxUSE_STD_IOSTREAM |
e87b7833 | 101 | |
81727065 VS |
102 | // =========================================================================== |
103 | // wxString class core | |
104 | // =========================================================================== | |
105 | ||
106 | #if wxUSE_UNICODE_UTF8 | |
107 | ||
81727065 VS |
108 | void wxString::PosLenToImpl(size_t pos, size_t len, |
109 | size_t *implPos, size_t *implLen) const | |
110 | { | |
111 | if ( pos == npos ) | |
112 | *implPos = npos; | |
113 | else | |
114 | { | |
115 | const_iterator i = begin() + pos; | |
cf9a878b | 116 | *implPos = wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); |
81727065 VS |
117 | if ( len == npos ) |
118 | *implLen = npos; | |
119 | else | |
120 | { | |
121 | // too large length is interpreted as "to the end of the string" | |
122 | // FIXME-UTF8: verify this is the case in std::string, assert | |
123 | // otherwise | |
124 | if ( pos + len > length() ) | |
125 | len = length() - pos; | |
126 | ||
cf9a878b | 127 | *implLen = (i + len).impl() - i.impl(); |
81727065 VS |
128 | } |
129 | } | |
130 | } | |
131 | ||
132 | #endif // wxUSE_UNICODE_UTF8 | |
133 | ||
11aac4ba VS |
134 | // ---------------------------------------------------------------------------- |
135 | // wxCStrData converted strings caching | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
132276cf VS |
138 | // FIXME-UTF8: temporarily disabled because it doesn't work with global |
139 | // string objects; re-enable after fixing this bug and benchmarking | |
140 | // performance to see if using a hash is a good idea at all | |
141 | #if 0 | |
142 | ||
11aac4ba VS |
143 | // For backward compatibility reasons, it must be possible to assign the value |
144 | // returned by wxString::c_str() to a char* or wchar_t* variable and work with | |
145 | // it. Returning wxCharBuffer from (const char*)c_str() wouldn't do the trick, | |
146 | // because the memory would be freed immediately, but it has to be valid as long | |
147 | // as the string is not modified, so that code like this still works: | |
148 | // | |
149 | // const wxChar *s = str.c_str(); | |
150 | // while ( s ) { ... } | |
151 | ||
152 | // FIXME-UTF8: not thread safe! | |
153 | // FIXME-UTF8: we currently clear the cached conversion only when the string is | |
154 | // destroyed, but we should do it when the string is modified, to | |
155 | // keep memory usage down | |
156 | // FIXME-UTF8: we do the conversion every time As[W]Char() is called, but if we | |
157 | // invalidated the cache on every change, we could keep the previous | |
158 | // conversion | |
159 | // FIXME-UTF8: add tracing of usage of these two methods - new code is supposed | |
160 | // to use mb_str() or wc_str() instead of (const [w]char*)c_str() | |
161 | ||
162 | template<typename T> | |
163 | static inline void DeleteStringFromConversionCache(T& hash, const wxString *s) | |
164 | { | |
6c4ebcda | 165 | typename T::iterator i = hash.find(wxConstCast(s, wxString)); |
11aac4ba VS |
166 | if ( i != hash.end() ) |
167 | { | |
168 | free(i->second); | |
169 | hash.erase(i); | |
170 | } | |
171 | } | |
172 | ||
173 | #if wxUSE_UNICODE | |
6c4ebcda VS |
174 | // NB: non-STL implementation doesn't compile with "const wxString*" key type, |
175 | // so we have to use wxString* here and const-cast when used | |
11aac4ba VS |
176 | WX_DECLARE_HASH_MAP(wxString*, char*, wxPointerHash, wxPointerEqual, |
177 | wxStringCharConversionCache); | |
178 | static wxStringCharConversionCache gs_stringsCharCache; | |
179 | ||
180 | const char* wxCStrData::AsChar() const | |
181 | { | |
182 | // remove previously cache value, if any (see FIXMEs above): | |
183 | DeleteStringFromConversionCache(gs_stringsCharCache, m_str); | |
184 | ||
185 | // convert the string and keep it: | |
6c4ebcda VS |
186 | const char *s = gs_stringsCharCache[wxConstCast(m_str, wxString)] = |
187 | m_str->mb_str().release(); | |
11aac4ba VS |
188 | |
189 | return s + m_offset; | |
190 | } | |
191 | #endif // wxUSE_UNICODE | |
192 | ||
193 | #if !wxUSE_UNICODE_WCHAR | |
194 | WX_DECLARE_HASH_MAP(wxString*, wchar_t*, wxPointerHash, wxPointerEqual, | |
195 | wxStringWCharConversionCache); | |
196 | static wxStringWCharConversionCache gs_stringsWCharCache; | |
197 | ||
198 | const wchar_t* wxCStrData::AsWChar() const | |
199 | { | |
200 | // remove previously cache value, if any (see FIXMEs above): | |
201 | DeleteStringFromConversionCache(gs_stringsWCharCache, m_str); | |
202 | ||
203 | // convert the string and keep it: | |
6c4ebcda VS |
204 | const wchar_t *s = gs_stringsWCharCache[wxConstCast(m_str, wxString)] = |
205 | m_str->wc_str().release(); | |
11aac4ba VS |
206 | |
207 | return s + m_offset; | |
208 | } | |
209 | #endif // !wxUSE_UNICODE_WCHAR | |
210 | ||
11aac4ba VS |
211 | wxString::~wxString() |
212 | { | |
213 | #if wxUSE_UNICODE | |
214 | // FIXME-UTF8: do this only if locale is not UTF8 if wxUSE_UNICODE_UTF8 | |
215 | DeleteStringFromConversionCache(gs_stringsCharCache, this); | |
216 | #endif | |
217 | #if !wxUSE_UNICODE_WCHAR | |
218 | DeleteStringFromConversionCache(gs_stringsWCharCache, this); | |
219 | #endif | |
220 | } | |
132276cf VS |
221 | #endif |
222 | ||
111d9948 | 223 | #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY |
132276cf VS |
224 | const char* wxCStrData::AsChar() const |
225 | { | |
111d9948 VS |
226 | #if wxUSE_UNICODE_UTF8 |
227 | if ( wxLocaleIsUtf8 ) | |
228 | return AsInternal(); | |
229 | #endif | |
230 | // under non-UTF8 locales, we have to convert the internal UTF-8 | |
231 | // representation using wxConvLibc and cache the result | |
232 | ||
132276cf | 233 | wxString *str = wxConstCast(m_str, wxString); |
05f32fc3 VS |
234 | |
235 | // convert the string: | |
236 | wxCharBuffer buf(str->mb_str()); | |
237 | ||
238 | // FIXME-UTF8: do the conversion in-place in the existing buffer | |
239 | if ( str->m_convertedToChar && | |
240 | strlen(buf) == strlen(str->m_convertedToChar) ) | |
241 | { | |
242 | // keep the same buffer for as long as possible, so that several calls | |
243 | // to c_str() in a row still work: | |
244 | strcpy(str->m_convertedToChar, buf); | |
245 | } | |
246 | else | |
247 | { | |
248 | str->m_convertedToChar = buf.release(); | |
249 | } | |
250 | ||
251 | // and keep it: | |
132276cf VS |
252 | return str->m_convertedToChar + m_offset; |
253 | } | |
111d9948 | 254 | #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY |
132276cf VS |
255 | |
256 | #if !wxUSE_UNICODE_WCHAR | |
257 | const wchar_t* wxCStrData::AsWChar() const | |
258 | { | |
259 | wxString *str = wxConstCast(m_str, wxString); | |
05f32fc3 VS |
260 | |
261 | // convert the string: | |
262 | wxWCharBuffer buf(str->wc_str()); | |
263 | ||
264 | // FIXME-UTF8: do the conversion in-place in the existing buffer | |
265 | if ( str->m_convertedToWChar && | |
266 | wxWcslen(buf) == wxWcslen(str->m_convertedToWChar) ) | |
267 | { | |
268 | // keep the same buffer for as long as possible, so that several calls | |
269 | // to c_str() in a row still work: | |
270 | memcpy(str->m_convertedToWChar, buf, sizeof(wchar_t) * wxWcslen(buf)); | |
271 | } | |
272 | else | |
273 | { | |
274 | str->m_convertedToWChar = buf.release(); | |
275 | } | |
276 | ||
277 | // and keep it: | |
132276cf VS |
278 | return str->m_convertedToWChar + m_offset; |
279 | } | |
280 | #endif // !wxUSE_UNICODE_WCHAR | |
281 | ||
282 | // =========================================================================== | |
283 | // wxString class core | |
284 | // =========================================================================== | |
285 | ||
286 | // --------------------------------------------------------------------------- | |
287 | // construction and conversion | |
288 | // --------------------------------------------------------------------------- | |
11aac4ba | 289 | |
81727065 | 290 | #if wxUSE_UNICODE_WCHAR |
8f93a29f VS |
291 | /* static */ |
292 | wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength, | |
04abe4bc | 293 | const wxMBConv& conv) |
8f93a29f VS |
294 | { |
295 | // anything to do? | |
296 | if ( !psz || nLength == 0 ) | |
81727065 | 297 | return SubstrBufFromMB(L"", 0); |
8f93a29f VS |
298 | |
299 | if ( nLength == npos ) | |
300 | nLength = wxNO_LEN; | |
301 | ||
302 | size_t wcLen; | |
303 | wxWCharBuffer wcBuf(conv.cMB2WC(psz, nLength, &wcLen)); | |
304 | if ( !wcLen ) | |
81727065 | 305 | return SubstrBufFromMB(_T(""), 0); |
8f93a29f VS |
306 | else |
307 | return SubstrBufFromMB(wcBuf, wcLen); | |
308 | } | |
81727065 VS |
309 | #endif // wxUSE_UNICODE_WCHAR |
310 | ||
311 | #if wxUSE_UNICODE_UTF8 | |
312 | /* static */ | |
313 | wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength, | |
314 | const wxMBConv& conv) | |
315 | { | |
81727065 VS |
316 | // anything to do? |
317 | if ( !psz || nLength == 0 ) | |
318 | return SubstrBufFromMB("", 0); | |
319 | ||
111d9948 VS |
320 | // if psz is already in UTF-8, we don't have to do the roundtrip to |
321 | // wchar_t* and back: | |
322 | if ( conv.IsUTF8() ) | |
323 | { | |
324 | // we need to validate the input because UTF8 iterators assume valid | |
325 | // UTF-8 sequence and psz may be invalid: | |
326 | if ( wxStringOperations::IsValidUtf8String(psz, nLength) ) | |
327 | { | |
328 | return SubstrBufFromMB(wxCharBuffer::CreateNonOwned(psz), nLength); | |
329 | } | |
330 | // else: do the roundtrip through wchar_t* | |
331 | } | |
332 | ||
81727065 VS |
333 | if ( nLength == npos ) |
334 | nLength = wxNO_LEN; | |
335 | ||
336 | // first convert to wide string: | |
337 | size_t wcLen; | |
338 | wxWCharBuffer wcBuf(conv.cMB2WC(psz, nLength, &wcLen)); | |
339 | if ( !wcLen ) | |
340 | return SubstrBufFromMB("", 0); | |
341 | ||
342 | // and then to UTF-8: | |
4fdfe2f3 | 343 | SubstrBufFromMB buf(ConvertStr(wcBuf, wcLen, wxMBConvStrictUTF8())); |
81727065 VS |
344 | // widechar -> UTF-8 conversion isn't supposed to ever fail: |
345 | wxASSERT_MSG( buf.data, _T("conversion to UTF-8 failed") ); | |
346 | ||
347 | return buf; | |
348 | } | |
349 | #endif // wxUSE_UNICODE_UTF8 | |
350 | ||
351 | #if wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE | |
8f93a29f VS |
352 | /* static */ |
353 | wxString::SubstrBufFromWC wxString::ConvertStr(const wchar_t *pwz, size_t nLength, | |
04abe4bc | 354 | const wxMBConv& conv) |
8f93a29f VS |
355 | { |
356 | // anything to do? | |
357 | if ( !pwz || nLength == 0 ) | |
81727065 | 358 | return SubstrBufFromWC("", 0); |
8f93a29f VS |
359 | |
360 | if ( nLength == npos ) | |
361 | nLength = wxNO_LEN; | |
362 | ||
363 | size_t mbLen; | |
364 | wxCharBuffer mbBuf(conv.cWC2MB(pwz, nLength, &mbLen)); | |
365 | if ( !mbLen ) | |
81727065 | 366 | return SubstrBufFromWC("", 0); |
8f93a29f VS |
367 | else |
368 | return SubstrBufFromWC(mbBuf, mbLen); | |
369 | } | |
81727065 | 370 | #endif // wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE |
8f93a29f VS |
371 | |
372 | ||
81727065 | 373 | #if wxUSE_UNICODE_WCHAR |
e87b7833 | 374 | |
06386448 | 375 | //Convert wxString in Unicode mode to a multi-byte string |
830f8f11 | 376 | const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const |
265d5cce | 377 | { |
81727065 | 378 | return conv.cWC2MB(wx_str(), length() + 1 /* size, not length */, NULL); |
e87b7833 MB |
379 | } |
380 | ||
81727065 | 381 | #elif wxUSE_UNICODE_UTF8 |
e87b7833 | 382 | |
81727065 VS |
383 | const wxWCharBuffer wxString::wc_str() const |
384 | { | |
4fdfe2f3 VZ |
385 | return wxMBConvStrictUTF8().cMB2WC |
386 | ( | |
387 | m_impl.c_str(), | |
388 | m_impl.length() + 1, // size, not length | |
389 | NULL | |
390 | ); | |
81727065 VS |
391 | } |
392 | ||
393 | const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const | |
394 | { | |
111d9948 VS |
395 | if ( conv.IsUTF8() ) |
396 | return wxCharBuffer::CreateNonOwned(m_impl.c_str()); | |
397 | ||
81727065 VS |
398 | // FIXME-UTF8: use wc_str() here once we have buffers with length |
399 | ||
400 | size_t wcLen; | |
4fdfe2f3 VZ |
401 | wxWCharBuffer wcBuf(wxMBConvStrictUTF8().cMB2WC |
402 | ( | |
403 | m_impl.c_str(), | |
404 | m_impl.length() + 1, // size | |
405 | &wcLen | |
406 | )); | |
81727065 VS |
407 | if ( !wcLen ) |
408 | return wxCharBuffer(""); | |
409 | ||
410 | return conv.cWC2MB(wcBuf, wcLen, NULL); | |
411 | } | |
412 | ||
413 | #else // ANSI | |
eec47cc6 | 414 | |
7663d0d4 | 415 | //Converts this string to a wide character string if unicode |
06386448 | 416 | //mode is not enabled and wxUSE_WCHAR_T is enabled |
830f8f11 | 417 | const wxWCharBuffer wxString::wc_str(const wxMBConv& conv) const |
265d5cce | 418 | { |
81727065 | 419 | return conv.cMB2WC(wx_str(), length() + 1 /* size, not length */, NULL); |
265d5cce | 420 | } |
7663d0d4 | 421 | |
e87b7833 MB |
422 | #endif // Unicode/ANSI |
423 | ||
424 | // shrink to minimal size (releasing extra memory) | |
425 | bool wxString::Shrink() | |
426 | { | |
427 | wxString tmp(begin(), end()); | |
428 | swap(tmp); | |
429 | return tmp.length() == length(); | |
430 | } | |
431 | ||
d8a4b666 | 432 | // deprecated compatibility code: |
a7ea63e2 | 433 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
c87a0bc8 | 434 | wxStringCharType *wxString::GetWriteBuf(size_t nLen) |
d8a4b666 VS |
435 | { |
436 | return DoGetWriteBuf(nLen); | |
437 | } | |
438 | ||
439 | void wxString::UngetWriteBuf() | |
440 | { | |
441 | DoUngetWriteBuf(); | |
442 | } | |
443 | ||
444 | void wxString::UngetWriteBuf(size_t nLen) | |
445 | { | |
446 | DoUngetWriteBuf(nLen); | |
447 | } | |
a7ea63e2 | 448 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
e87b7833 | 449 | |
d8a4b666 | 450 | |
e87b7833 MB |
451 | // --------------------------------------------------------------------------- |
452 | // data access | |
453 | // --------------------------------------------------------------------------- | |
454 | ||
455 | // all functions are inline in string.h | |
456 | ||
457 | // --------------------------------------------------------------------------- | |
e8f59039 | 458 | // concatenation operators |
e87b7833 MB |
459 | // --------------------------------------------------------------------------- |
460 | ||
c801d85f | 461 | /* |
c801d85f KB |
462 | * concatenation functions come in 5 flavours: |
463 | * string + string | |
464 | * char + string and string + char | |
465 | * C str + string and string + C str | |
466 | */ | |
467 | ||
b1801e0e | 468 | wxString operator+(const wxString& str1, const wxString& str2) |
c801d85f | 469 | { |
992527a5 | 470 | #if !wxUSE_STL_BASED_WXSTRING |
8f93a29f VS |
471 | wxASSERT( str1.IsValid() ); |
472 | wxASSERT( str2.IsValid() ); | |
e87b7833 | 473 | #endif |
097c080b | 474 | |
3458e408 WS |
475 | wxString s = str1; |
476 | s += str2; | |
3168a13f | 477 | |
3458e408 | 478 | return s; |
c801d85f KB |
479 | } |
480 | ||
c9f78968 | 481 | wxString operator+(const wxString& str, wxUniChar ch) |
c801d85f | 482 | { |
992527a5 | 483 | #if !wxUSE_STL_BASED_WXSTRING |
8f93a29f | 484 | wxASSERT( str.IsValid() ); |
e87b7833 | 485 | #endif |
3168a13f | 486 | |
3458e408 WS |
487 | wxString s = str; |
488 | s += ch; | |
097c080b | 489 | |
3458e408 | 490 | return s; |
c801d85f KB |
491 | } |
492 | ||
c9f78968 | 493 | wxString operator+(wxUniChar ch, const wxString& str) |
c801d85f | 494 | { |
992527a5 | 495 | #if !wxUSE_STL_BASED_WXSTRING |
8f93a29f | 496 | wxASSERT( str.IsValid() ); |
e87b7833 | 497 | #endif |
097c080b | 498 | |
3458e408 WS |
499 | wxString s = ch; |
500 | s += str; | |
3168a13f | 501 | |
3458e408 | 502 | return s; |
c801d85f KB |
503 | } |
504 | ||
8f93a29f | 505 | wxString operator+(const wxString& str, const char *psz) |
c801d85f | 506 | { |
992527a5 | 507 | #if !wxUSE_STL_BASED_WXSTRING |
8f93a29f | 508 | wxASSERT( str.IsValid() ); |
e87b7833 | 509 | #endif |
097c080b | 510 | |
3458e408 | 511 | wxString s; |
8f93a29f | 512 | if ( !s.Alloc(strlen(psz) + str.length()) ) { |
3458e408 WS |
513 | wxFAIL_MSG( _T("out of memory in wxString::operator+") ); |
514 | } | |
515 | s += str; | |
516 | s += psz; | |
3168a13f | 517 | |
3458e408 | 518 | return s; |
c801d85f KB |
519 | } |
520 | ||
8f93a29f | 521 | wxString operator+(const wxString& str, const wchar_t *pwz) |
c801d85f | 522 | { |
992527a5 | 523 | #if !wxUSE_STL_BASED_WXSTRING |
8f93a29f VS |
524 | wxASSERT( str.IsValid() ); |
525 | #endif | |
526 | ||
527 | wxString s; | |
528 | if ( !s.Alloc(wxWcslen(pwz) + str.length()) ) { | |
529 | wxFAIL_MSG( _T("out of memory in wxString::operator+") ); | |
530 | } | |
531 | s += str; | |
532 | s += pwz; | |
533 | ||
534 | return s; | |
535 | } | |
536 | ||
537 | wxString operator+(const char *psz, const wxString& str) | |
538 | { | |
a7ea63e2 VS |
539 | #if !wxUSE_STL_BASED_WXSTRING |
540 | wxASSERT( str.IsValid() ); | |
541 | #endif | |
542 | ||
543 | wxString s; | |
544 | if ( !s.Alloc(strlen(psz) + str.length()) ) { | |
545 | wxFAIL_MSG( _T("out of memory in wxString::operator+") ); | |
546 | } | |
547 | s = psz; | |
548 | s += str; | |
549 | ||
550 | return s; | |
551 | } | |
552 | ||
553 | wxString operator+(const wchar_t *pwz, const wxString& str) | |
554 | { | |
555 | #if !wxUSE_STL_BASED_WXSTRING | |
556 | wxASSERT( str.IsValid() ); | |
557 | #endif | |
558 | ||
559 | wxString s; | |
560 | if ( !s.Alloc(wxWcslen(pwz) + str.length()) ) { | |
561 | wxFAIL_MSG( _T("out of memory in wxString::operator+") ); | |
562 | } | |
563 | s = pwz; | |
564 | s += str; | |
565 | ||
566 | return s; | |
567 | } | |
568 | ||
569 | // --------------------------------------------------------------------------- | |
570 | // string comparison | |
571 | // --------------------------------------------------------------------------- | |
572 | ||
52de37c7 VS |
573 | bool wxString::IsSameAs(wxUniChar c, bool compareWithCase) const |
574 | { | |
575 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c | |
576 | : wxToupper(GetChar(0u)) == wxToupper(c)); | |
577 | } | |
578 | ||
a7ea63e2 VS |
579 | #ifdef HAVE_STD_STRING_COMPARE |
580 | ||
581 | // NB: Comparison code (both if HAVE_STD_STRING_COMPARE and if not) works with | |
582 | // UTF-8 encoded strings too, thanks to UTF-8's design which allows us to | |
583 | // sort strings in characters code point order by sorting the byte sequence | |
584 | // in byte values order (i.e. what strcmp() and memcmp() do). | |
585 | ||
586 | int wxString::compare(const wxString& str) const | |
587 | { | |
588 | return m_impl.compare(str.m_impl); | |
589 | } | |
590 | ||
591 | int wxString::compare(size_t nStart, size_t nLen, | |
592 | const wxString& str) const | |
593 | { | |
594 | size_t pos, len; | |
595 | PosLenToImpl(nStart, nLen, &pos, &len); | |
596 | return m_impl.compare(pos, len, str.m_impl); | |
597 | } | |
598 | ||
599 | int wxString::compare(size_t nStart, size_t nLen, | |
600 | const wxString& str, | |
601 | size_t nStart2, size_t nLen2) const | |
602 | { | |
603 | size_t pos, len; | |
604 | PosLenToImpl(nStart, nLen, &pos, &len); | |
605 | ||
606 | size_t pos2, len2; | |
607 | str.PosLenToImpl(nStart2, nLen2, &pos2, &len2); | |
608 | ||
609 | return m_impl.compare(pos, len, str.m_impl, pos2, len2); | |
610 | } | |
611 | ||
612 | int wxString::compare(const char* sz) const | |
613 | { | |
614 | return m_impl.compare(ImplStr(sz)); | |
615 | } | |
616 | ||
617 | int wxString::compare(const wchar_t* sz) const | |
618 | { | |
619 | return m_impl.compare(ImplStr(sz)); | |
620 | } | |
621 | ||
622 | int wxString::compare(size_t nStart, size_t nLen, | |
623 | const char* sz, size_t nCount) const | |
624 | { | |
625 | size_t pos, len; | |
626 | PosLenToImpl(nStart, nLen, &pos, &len); | |
627 | ||
628 | SubstrBufFromMB str(ImplStr(sz, nCount)); | |
629 | ||
630 | return m_impl.compare(pos, len, str.data, str.len); | |
631 | } | |
632 | ||
633 | int wxString::compare(size_t nStart, size_t nLen, | |
634 | const wchar_t* sz, size_t nCount) const | |
635 | { | |
636 | size_t pos, len; | |
637 | PosLenToImpl(nStart, nLen, &pos, &len); | |
638 | ||
639 | SubstrBufFromWC str(ImplStr(sz, nCount)); | |
640 | ||
641 | return m_impl.compare(pos, len, str.data, str.len); | |
642 | } | |
643 | ||
644 | #else // !HAVE_STD_STRING_COMPARE | |
645 | ||
646 | static inline int wxDoCmp(const wxStringCharType* s1, size_t l1, | |
647 | const wxStringCharType* s2, size_t l2) | |
648 | { | |
649 | if( l1 == l2 ) | |
650 | return wxStringMemcmp(s1, s2, l1); | |
651 | else if( l1 < l2 ) | |
652 | { | |
653 | int ret = wxStringMemcmp(s1, s2, l1); | |
654 | return ret == 0 ? -1 : ret; | |
655 | } | |
656 | else | |
657 | { | |
658 | int ret = wxStringMemcmp(s1, s2, l2); | |
659 | return ret == 0 ? +1 : ret; | |
660 | } | |
661 | } | |
662 | ||
663 | int wxString::compare(const wxString& str) const | |
664 | { | |
665 | return ::wxDoCmp(m_impl.data(), m_impl.length(), | |
666 | str.m_impl.data(), str.m_impl.length()); | |
667 | } | |
668 | ||
669 | int wxString::compare(size_t nStart, size_t nLen, | |
670 | const wxString& str) const | |
671 | { | |
672 | wxASSERT(nStart <= length()); | |
673 | size_type strLen = length() - nStart; | |
674 | nLen = strLen < nLen ? strLen : nLen; | |
675 | ||
676 | size_t pos, len; | |
677 | PosLenToImpl(nStart, nLen, &pos, &len); | |
678 | ||
679 | return ::wxDoCmp(m_impl.data() + pos, len, | |
680 | str.m_impl.data(), str.m_impl.length()); | |
681 | } | |
682 | ||
683 | int wxString::compare(size_t nStart, size_t nLen, | |
684 | const wxString& str, | |
685 | size_t nStart2, size_t nLen2) const | |
686 | { | |
687 | wxASSERT(nStart <= length()); | |
688 | wxASSERT(nStart2 <= str.length()); | |
689 | size_type strLen = length() - nStart, | |
690 | strLen2 = str.length() - nStart2; | |
691 | nLen = strLen < nLen ? strLen : nLen; | |
692 | nLen2 = strLen2 < nLen2 ? strLen2 : nLen2; | |
693 | ||
694 | size_t pos, len; | |
695 | PosLenToImpl(nStart, nLen, &pos, &len); | |
696 | size_t pos2, len2; | |
697 | str.PosLenToImpl(nStart2, nLen2, &pos2, &len2); | |
698 | ||
699 | return ::wxDoCmp(m_impl.data() + pos, len, | |
700 | str.m_impl.data() + pos2, len2); | |
701 | } | |
702 | ||
703 | int wxString::compare(const char* sz) const | |
704 | { | |
705 | SubstrBufFromMB str(ImplStr(sz, npos)); | |
706 | if ( str.len == npos ) | |
707 | str.len = wxStringStrlen(str.data); | |
708 | return ::wxDoCmp(m_impl.data(), m_impl.length(), str.data, str.len); | |
709 | } | |
710 | ||
711 | int wxString::compare(const wchar_t* sz) const | |
712 | { | |
713 | SubstrBufFromWC str(ImplStr(sz, npos)); | |
714 | if ( str.len == npos ) | |
715 | str.len = wxStringStrlen(str.data); | |
716 | return ::wxDoCmp(m_impl.data(), m_impl.length(), str.data, str.len); | |
717 | } | |
718 | ||
719 | int wxString::compare(size_t nStart, size_t nLen, | |
720 | const char* sz, size_t nCount) const | |
721 | { | |
722 | wxASSERT(nStart <= length()); | |
723 | size_type strLen = length() - nStart; | |
724 | nLen = strLen < nLen ? strLen : nLen; | |
097c080b | 725 | |
a7ea63e2 VS |
726 | size_t pos, len; |
727 | PosLenToImpl(nStart, nLen, &pos, &len); | |
3168a13f | 728 | |
a7ea63e2 VS |
729 | SubstrBufFromMB str(ImplStr(sz, nCount)); |
730 | if ( str.len == npos ) | |
731 | str.len = wxStringStrlen(str.data); | |
732 | ||
733 | return ::wxDoCmp(m_impl.data() + pos, len, str.data, str.len); | |
c801d85f KB |
734 | } |
735 | ||
a7ea63e2 VS |
736 | int wxString::compare(size_t nStart, size_t nLen, |
737 | const wchar_t* sz, size_t nCount) const | |
8f93a29f | 738 | { |
a7ea63e2 VS |
739 | wxASSERT(nStart <= length()); |
740 | size_type strLen = length() - nStart; | |
741 | nLen = strLen < nLen ? strLen : nLen; | |
8f93a29f | 742 | |
a7ea63e2 VS |
743 | size_t pos, len; |
744 | PosLenToImpl(nStart, nLen, &pos, &len); | |
8f93a29f | 745 | |
a7ea63e2 VS |
746 | SubstrBufFromWC str(ImplStr(sz, nCount)); |
747 | if ( str.len == npos ) | |
748 | str.len = wxStringStrlen(str.data); | |
749 | ||
750 | return ::wxDoCmp(m_impl.data() + pos, len, str.data, str.len); | |
8f93a29f VS |
751 | } |
752 | ||
a7ea63e2 VS |
753 | #endif // HAVE_STD_STRING_COMPARE/!HAVE_STD_STRING_COMPARE |
754 | ||
755 | ||
8f93a29f VS |
756 | // --------------------------------------------------------------------------- |
757 | // find_{first,last}_[not]_of functions | |
758 | // --------------------------------------------------------------------------- | |
759 | ||
760 | #if !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 | |
c801d85f | 761 | |
8f93a29f VS |
762 | // NB: All these functions are implemented with the argument being wxChar*, |
763 | // i.e. widechar string in any Unicode build, even though native string | |
764 | // representation is char* in the UTF-8 build. This is because we couldn't | |
765 | // use memchr() to determine if a character is in a set encoded as UTF-8. | |
766 | ||
767 | size_t wxString::find_first_of(const wxChar* sz, size_t nStart) const | |
dcb68102 | 768 | { |
8f93a29f | 769 | return find_first_of(sz, nStart, wxStrlen(sz)); |
dcb68102 RN |
770 | } |
771 | ||
8f93a29f | 772 | size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart) const |
dcb68102 | 773 | { |
8f93a29f | 774 | return find_first_not_of(sz, nStart, wxStrlen(sz)); |
dcb68102 RN |
775 | } |
776 | ||
8f93a29f | 777 | size_t wxString::find_first_of(const wxChar* sz, size_t nStart, size_t n) const |
dcb68102 | 778 | { |
8f93a29f | 779 | wxASSERT_MSG( nStart <= length(), _T("invalid index") ); |
dcb68102 | 780 | |
8f93a29f VS |
781 | size_t idx = nStart; |
782 | for ( const_iterator i = begin() + nStart; i != end(); ++idx, ++i ) | |
dcb68102 | 783 | { |
8f93a29f VS |
784 | if ( wxTmemchr(sz, *i, n) ) |
785 | return idx; | |
dcb68102 | 786 | } |
8f93a29f VS |
787 | |
788 | return npos; | |
789 | } | |
790 | ||
791 | size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const | |
792 | { | |
793 | wxASSERT_MSG( nStart <= length(), _T("invalid index") ); | |
794 | ||
795 | size_t idx = nStart; | |
796 | for ( const_iterator i = begin() + nStart; i != end(); ++idx, ++i ) | |
dcb68102 | 797 | { |
8f93a29f VS |
798 | if ( !wxTmemchr(sz, *i, n) ) |
799 | return idx; | |
800 | } | |
801 | ||
802 | return npos; | |
803 | } | |
804 | ||
805 | ||
806 | size_t wxString::find_last_of(const wxChar* sz, size_t nStart) const | |
807 | { | |
808 | return find_last_of(sz, nStart, wxStrlen(sz)); | |
809 | } | |
810 | ||
811 | size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart) const | |
812 | { | |
813 | return find_last_not_of(sz, nStart, wxStrlen(sz)); | |
814 | } | |
815 | ||
816 | size_t wxString::find_last_of(const wxChar* sz, size_t nStart, size_t n) const | |
817 | { | |
818 | size_t len = length(); | |
819 | ||
820 | if ( nStart == npos ) | |
821 | { | |
822 | nStart = len - 1; | |
dcb68102 | 823 | } |
2c09fb3b | 824 | else |
dcb68102 | 825 | { |
8f93a29f | 826 | wxASSERT_MSG( nStart <= len, _T("invalid index") ); |
dcb68102 | 827 | } |
8f93a29f VS |
828 | |
829 | size_t idx = nStart; | |
830 | for ( const_reverse_iterator i = rbegin() + (len - nStart - 1); | |
831 | i != rend(); --idx, ++i ) | |
832 | { | |
833 | if ( wxTmemchr(sz, *i, n) ) | |
834 | return idx; | |
835 | } | |
836 | ||
837 | return npos; | |
dcb68102 RN |
838 | } |
839 | ||
8f93a29f | 840 | size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const |
dcb68102 | 841 | { |
8f93a29f VS |
842 | size_t len = length(); |
843 | ||
844 | if ( nStart == npos ) | |
845 | { | |
846 | nStart = len - 1; | |
847 | } | |
848 | else | |
849 | { | |
850 | wxASSERT_MSG( nStart <= len, _T("invalid index") ); | |
851 | } | |
852 | ||
853 | size_t idx = nStart; | |
854 | for ( const_reverse_iterator i = rbegin() + (len - nStart - 1); | |
855 | i != rend(); --idx, ++i ) | |
856 | { | |
857 | if ( !wxTmemchr(sz, *i, n) ) | |
858 | return idx; | |
859 | } | |
860 | ||
861 | return npos; | |
dcb68102 RN |
862 | } |
863 | ||
8f93a29f | 864 | size_t wxString::find_first_not_of(wxUniChar ch, size_t nStart) const |
dcb68102 | 865 | { |
8f93a29f VS |
866 | wxASSERT_MSG( nStart <= length(), _T("invalid index") ); |
867 | ||
868 | size_t idx = nStart; | |
869 | for ( const_iterator i = begin() + nStart; i != end(); ++idx, ++i ) | |
870 | { | |
871 | if ( *i != ch ) | |
872 | return idx; | |
873 | } | |
874 | ||
875 | return npos; | |
876 | } | |
877 | ||
878 | size_t wxString::find_last_not_of(wxUniChar ch, size_t nStart) const | |
879 | { | |
880 | size_t len = length(); | |
881 | ||
882 | if ( nStart == npos ) | |
883 | { | |
884 | nStart = len - 1; | |
885 | } | |
886 | else | |
887 | { | |
888 | wxASSERT_MSG( nStart <= len, _T("invalid index") ); | |
889 | } | |
890 | ||
891 | size_t idx = nStart; | |
892 | for ( const_reverse_iterator i = rbegin() + (len - nStart - 1); | |
893 | i != rend(); --idx, ++i ) | |
894 | { | |
895 | if ( *i != ch ) | |
896 | return idx; | |
897 | } | |
898 | ||
899 | return npos; | |
900 | } | |
901 | ||
902 | // the functions above were implemented for wchar_t* arguments in Unicode | |
903 | // build and char* in ANSI build; below are implementations for the other | |
904 | // version: | |
905 | #if wxUSE_UNICODE | |
906 | #define wxOtherCharType char | |
907 | #define STRCONV (const wxChar*)wxConvLibc.cMB2WC | |
908 | #else | |
909 | #define wxOtherCharType wchar_t | |
910 | #define STRCONV (const wxChar*)wxConvLibc.cWC2MB | |
911 | #endif | |
912 | ||
913 | size_t wxString::find_first_of(const wxOtherCharType* sz, size_t nStart) const | |
914 | { return find_first_of(STRCONV(sz), nStart); } | |
915 | ||
916 | size_t wxString::find_first_of(const wxOtherCharType* sz, size_t nStart, | |
917 | size_t n) const | |
918 | { return find_first_of(STRCONV(sz, n, NULL), nStart, n); } | |
919 | size_t wxString::find_last_of(const wxOtherCharType* sz, size_t nStart) const | |
920 | { return find_last_of(STRCONV(sz), nStart); } | |
921 | size_t wxString::find_last_of(const wxOtherCharType* sz, size_t nStart, | |
922 | size_t n) const | |
923 | { return find_last_of(STRCONV(sz, n, NULL), nStart, n); } | |
924 | size_t wxString::find_first_not_of(const wxOtherCharType* sz, size_t nStart) const | |
925 | { return find_first_not_of(STRCONV(sz), nStart); } | |
926 | size_t wxString::find_first_not_of(const wxOtherCharType* sz, size_t nStart, | |
927 | size_t n) const | |
928 | { return find_first_not_of(STRCONV(sz, n, NULL), nStart, n); } | |
929 | size_t wxString::find_last_not_of(const wxOtherCharType* sz, size_t nStart) const | |
930 | { return find_last_not_of(STRCONV(sz), nStart); } | |
931 | size_t wxString::find_last_not_of(const wxOtherCharType* sz, size_t nStart, | |
932 | size_t n) const | |
933 | { return find_last_not_of(STRCONV(sz, n, NULL), nStart, n); } | |
934 | ||
935 | #undef wxOtherCharType | |
936 | #undef STRCONV | |
937 | ||
938 | #endif // !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 | |
939 | ||
940 | // =========================================================================== | |
941 | // other common string functions | |
942 | // =========================================================================== | |
943 | ||
944 | int wxString::CmpNoCase(const wxString& s) const | |
945 | { | |
946 | // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added | |
947 | ||
948 | size_t idx = 0; | |
949 | const_iterator i1 = begin(); | |
950 | const_iterator end1 = end(); | |
951 | const_iterator i2 = s.begin(); | |
952 | const_iterator end2 = s.end(); | |
953 | ||
954 | for ( ; i1 != end1 && i2 != end2; ++idx, ++i1, ++i2 ) | |
955 | { | |
956 | wxUniChar lower1 = (wxChar)wxTolower(*i1); | |
957 | wxUniChar lower2 = (wxChar)wxTolower(*i2); | |
958 | if ( lower1 != lower2 ) | |
959 | return lower1 < lower2 ? -1 : 1; | |
960 | } | |
961 | ||
962 | size_t len1 = length(); | |
963 | size_t len2 = s.length(); | |
dcb68102 | 964 | |
8f93a29f VS |
965 | if ( len1 < len2 ) |
966 | return -1; | |
967 | else if ( len1 > len2 ) | |
968 | return 1; | |
969 | return 0; | |
dcb68102 RN |
970 | } |
971 | ||
972 | ||
b1ac3b56 | 973 | #if wxUSE_UNICODE |
e015c2a3 | 974 | |
cf6bedce SC |
975 | #ifdef __MWERKS__ |
976 | #ifndef __SCHAR_MAX__ | |
977 | #define __SCHAR_MAX__ 127 | |
978 | #endif | |
979 | #endif | |
980 | ||
e6310bbc | 981 | wxString wxString::FromAscii(const char *ascii, size_t len) |
b1ac3b56 | 982 | { |
e6310bbc | 983 | if (!ascii || len == 0) |
b1ac3b56 | 984 | return wxEmptyString; |
e015c2a3 | 985 | |
b1ac3b56 | 986 | wxString res; |
e015c2a3 | 987 | |
e6310bbc | 988 | { |
6798451b | 989 | wxStringInternalBuffer buf(res, len); |
602a857b | 990 | wxStringCharType *dest = buf; |
c1eada83 | 991 | |
602a857b VS |
992 | for ( ; len > 0; --len ) |
993 | { | |
994 | unsigned char c = (unsigned char)*ascii++; | |
995 | wxASSERT_MSG( c < 0x80, | |
996 | _T("Non-ASCII value passed to FromAscii().") ); | |
c1eada83 | 997 | |
602a857b VS |
998 | *dest++ = (wchar_t)c; |
999 | } | |
e015c2a3 VZ |
1000 | } |
1001 | ||
b1ac3b56 RR |
1002 | return res; |
1003 | } | |
1004 | ||
e6310bbc VS |
1005 | wxString wxString::FromAscii(const char *ascii) |
1006 | { | |
0081dd72 | 1007 | return FromAscii(ascii, wxStrlen(ascii)); |
e6310bbc VS |
1008 | } |
1009 | ||
2b5f62a0 VZ |
1010 | wxString wxString::FromAscii(const char ascii) |
1011 | { | |
1012 | // What do we do with '\0' ? | |
1013 | ||
c1eada83 | 1014 | unsigned char c = (unsigned char)ascii; |
8760bc65 | 1015 | |
c1eada83 VS |
1016 | wxASSERT_MSG( c < 0x80, _T("Non-ASCII value passed to FromAscii().") ); |
1017 | ||
1018 | // NB: the cast to wchar_t causes interpretation of 'ascii' as Latin1 value | |
1019 | return wxString(wxUniChar((wchar_t)c)); | |
2b5f62a0 VZ |
1020 | } |
1021 | ||
b1ac3b56 RR |
1022 | const wxCharBuffer wxString::ToAscii() const |
1023 | { | |
e015c2a3 VZ |
1024 | // this will allocate enough space for the terminating NUL too |
1025 | wxCharBuffer buffer(length()); | |
6e394fc6 | 1026 | char *dest = buffer.data(); |
e015c2a3 | 1027 | |
c1eada83 | 1028 | for ( const_iterator i = begin(); i != end(); ++i ) |
b1ac3b56 | 1029 | { |
c1eada83 VS |
1030 | wxUniChar c(*i); |
1031 | // FIXME-UTF8: unify substituted char ('_') with wxUniChar ('?') | |
1032 | *dest++ = c.IsAscii() ? (char)c : '_'; | |
e015c2a3 VZ |
1033 | |
1034 | // the output string can't have embedded NULs anyhow, so we can safely | |
1035 | // stop at first of them even if we do have any | |
c1eada83 | 1036 | if ( !c ) |
e015c2a3 | 1037 | break; |
b1ac3b56 | 1038 | } |
e015c2a3 | 1039 | |
b1ac3b56 RR |
1040 | return buffer; |
1041 | } | |
e015c2a3 | 1042 | |
c1eada83 | 1043 | #endif // wxUSE_UNICODE |
b1ac3b56 | 1044 | |
c801d85f | 1045 | // extract string of length nCount starting at nFirst |
c801d85f KB |
1046 | wxString wxString::Mid(size_t nFirst, size_t nCount) const |
1047 | { | |
73f507f5 | 1048 | size_t nLen = length(); |
30d9011f | 1049 | |
73f507f5 WS |
1050 | // default value of nCount is npos and means "till the end" |
1051 | if ( nCount == npos ) | |
1052 | { | |
1053 | nCount = nLen - nFirst; | |
1054 | } | |
30d9011f | 1055 | |
73f507f5 WS |
1056 | // out-of-bounds requests return sensible things |
1057 | if ( nFirst + nCount > nLen ) | |
1058 | { | |
1059 | nCount = nLen - nFirst; | |
1060 | } | |
c801d85f | 1061 | |
73f507f5 WS |
1062 | if ( nFirst > nLen ) |
1063 | { | |
1064 | // AllocCopy() will return empty string | |
1065 | return wxEmptyString; | |
1066 | } | |
c801d85f | 1067 | |
73f507f5 WS |
1068 | wxString dest(*this, nFirst, nCount); |
1069 | if ( dest.length() != nCount ) | |
1070 | { | |
1071 | wxFAIL_MSG( _T("out of memory in wxString::Mid") ); | |
1072 | } | |
30d9011f | 1073 | |
73f507f5 | 1074 | return dest; |
c801d85f KB |
1075 | } |
1076 | ||
e87b7833 | 1077 | // check that the string starts with prefix and return the rest of the string |
d775fa82 | 1078 | // in the provided pointer if it is not NULL, otherwise return false |
c5e7a7d7 | 1079 | bool wxString::StartsWith(const wxString& prefix, wxString *rest) const |
f6bcfd97 | 1080 | { |
c5e7a7d7 VS |
1081 | if ( compare(0, prefix.length(), prefix) != 0 ) |
1082 | return false; | |
f6bcfd97 BP |
1083 | |
1084 | if ( rest ) | |
1085 | { | |
1086 | // put the rest of the string into provided pointer | |
c5e7a7d7 | 1087 | rest->assign(*this, prefix.length(), npos); |
f6bcfd97 BP |
1088 | } |
1089 | ||
d775fa82 | 1090 | return true; |
f6bcfd97 BP |
1091 | } |
1092 | ||
3affcd07 VZ |
1093 | |
1094 | // check that the string ends with suffix and return the rest of it in the | |
1095 | // provided pointer if it is not NULL, otherwise return false | |
c5e7a7d7 | 1096 | bool wxString::EndsWith(const wxString& suffix, wxString *rest) const |
3affcd07 | 1097 | { |
c5e7a7d7 | 1098 | int start = length() - suffix.length(); |
81727065 VS |
1099 | |
1100 | if ( start < 0 || compare(start, npos, suffix) != 0 ) | |
3affcd07 VZ |
1101 | return false; |
1102 | ||
1103 | if ( rest ) | |
1104 | { | |
1105 | // put the rest of the string into provided pointer | |
1106 | rest->assign(*this, 0, start); | |
1107 | } | |
1108 | ||
1109 | return true; | |
1110 | } | |
1111 | ||
1112 | ||
c801d85f KB |
1113 | // extract nCount last (rightmost) characters |
1114 | wxString wxString::Right(size_t nCount) const | |
1115 | { | |
e87b7833 MB |
1116 | if ( nCount > length() ) |
1117 | nCount = length(); | |
c801d85f | 1118 | |
e87b7833 MB |
1119 | wxString dest(*this, length() - nCount, nCount); |
1120 | if ( dest.length() != nCount ) { | |
b1801e0e GD |
1121 | wxFAIL_MSG( _T("out of memory in wxString::Right") ); |
1122 | } | |
c801d85f KB |
1123 | return dest; |
1124 | } | |
1125 | ||
1126 | // get all characters after the last occurence of ch | |
1127 | // (returns the whole string if ch not found) | |
c9f78968 | 1128 | wxString wxString::AfterLast(wxUniChar ch) const |
c801d85f KB |
1129 | { |
1130 | wxString str; | |
d775fa82 | 1131 | int iPos = Find(ch, true); |
3c67202d | 1132 | if ( iPos == wxNOT_FOUND ) |
c801d85f KB |
1133 | str = *this; |
1134 | else | |
c9f78968 | 1135 | str = wx_str() + iPos + 1; |
c801d85f KB |
1136 | |
1137 | return str; | |
1138 | } | |
1139 | ||
1140 | // extract nCount first (leftmost) characters | |
1141 | wxString wxString::Left(size_t nCount) const | |
1142 | { | |
e87b7833 MB |
1143 | if ( nCount > length() ) |
1144 | nCount = length(); | |
c801d85f | 1145 | |
e87b7833 MB |
1146 | wxString dest(*this, 0, nCount); |
1147 | if ( dest.length() != nCount ) { | |
b1801e0e GD |
1148 | wxFAIL_MSG( _T("out of memory in wxString::Left") ); |
1149 | } | |
c801d85f KB |
1150 | return dest; |
1151 | } | |
1152 | ||
1153 | // get all characters before the first occurence of ch | |
1154 | // (returns the whole string if ch not found) | |
c9f78968 | 1155 | wxString wxString::BeforeFirst(wxUniChar ch) const |
c801d85f | 1156 | { |
e87b7833 MB |
1157 | int iPos = Find(ch); |
1158 | if ( iPos == wxNOT_FOUND ) iPos = length(); | |
1159 | return wxString(*this, 0, iPos); | |
c801d85f KB |
1160 | } |
1161 | ||
1162 | /// get all characters before the last occurence of ch | |
1163 | /// (returns empty string if ch not found) | |
c9f78968 | 1164 | wxString wxString::BeforeLast(wxUniChar ch) const |
c801d85f KB |
1165 | { |
1166 | wxString str; | |
d775fa82 | 1167 | int iPos = Find(ch, true); |
3c67202d | 1168 | if ( iPos != wxNOT_FOUND && iPos != 0 ) |
d1c9bbf6 | 1169 | str = wxString(c_str(), iPos); |
c801d85f KB |
1170 | |
1171 | return str; | |
1172 | } | |
1173 | ||
1174 | /// get all characters after the first occurence of ch | |
1175 | /// (returns empty string if ch not found) | |
c9f78968 | 1176 | wxString wxString::AfterFirst(wxUniChar ch) const |
c801d85f KB |
1177 | { |
1178 | wxString str; | |
1179 | int iPos = Find(ch); | |
3c67202d | 1180 | if ( iPos != wxNOT_FOUND ) |
c9f78968 | 1181 | str = wx_str() + iPos + 1; |
c801d85f KB |
1182 | |
1183 | return str; | |
1184 | } | |
1185 | ||
1186 | // replace first (or all) occurences of some substring with another one | |
8a540c88 VS |
1187 | size_t wxString::Replace(const wxString& strOld, |
1188 | const wxString& strNew, bool bReplaceAll) | |
c801d85f | 1189 | { |
a8f1f1b2 | 1190 | // if we tried to replace an empty string we'd enter an infinite loop below |
8a540c88 | 1191 | wxCHECK_MSG( !strOld.empty(), 0, |
a8f1f1b2 VZ |
1192 | _T("wxString::Replace(): invalid parameter") ); |
1193 | ||
510bb748 | 1194 | size_t uiCount = 0; // count of replacements made |
c801d85f | 1195 | |
8a540c88 VS |
1196 | size_t uiOldLen = strOld.length(); |
1197 | size_t uiNewLen = strNew.length(); | |
c801d85f | 1198 | |
510bb748 | 1199 | size_t dwPos = 0; |
c801d85f | 1200 | |
8a540c88 | 1201 | while ( (*this)[dwPos] != wxT('\0') ) |
510bb748 RN |
1202 | { |
1203 | //DO NOT USE STRSTR HERE | |
1204 | //this string can contain embedded null characters, | |
1205 | //so strstr will function incorrectly | |
8a540c88 | 1206 | dwPos = find(strOld, dwPos); |
ad5bb7d6 | 1207 | if ( dwPos == npos ) |
510bb748 | 1208 | break; // exit the loop |
ad5bb7d6 | 1209 | else |
510bb748 RN |
1210 | { |
1211 | //replace this occurance of the old string with the new one | |
8a540c88 | 1212 | replace(dwPos, uiOldLen, strNew, uiNewLen); |
510bb748 | 1213 | |
2df0258e RN |
1214 | //move up pos past the string that was replaced |
1215 | dwPos += uiNewLen; | |
510bb748 RN |
1216 | |
1217 | //increase replace count | |
1218 | ++uiCount; | |
ad5bb7d6 | 1219 | |
510bb748 | 1220 | // stop now? |
ad5bb7d6 | 1221 | if ( !bReplaceAll ) |
510bb748 RN |
1222 | break; // exit the loop |
1223 | } | |
c801d85f | 1224 | } |
c801d85f | 1225 | |
510bb748 | 1226 | return uiCount; |
c801d85f KB |
1227 | } |
1228 | ||
1229 | bool wxString::IsAscii() const | |
1230 | { | |
a4a44612 VS |
1231 | for ( const_iterator i = begin(); i != end(); ++i ) |
1232 | { | |
1233 | if ( !(*i).IsAscii() ) | |
1234 | return false; | |
1235 | } | |
1236 | ||
1237 | return true; | |
c801d85f | 1238 | } |
dd1eaa89 | 1239 | |
c801d85f KB |
1240 | bool wxString::IsWord() const |
1241 | { | |
a4a44612 VS |
1242 | for ( const_iterator i = begin(); i != end(); ++i ) |
1243 | { | |
1244 | if ( !wxIsalpha(*i) ) | |
1245 | return false; | |
1246 | } | |
1247 | ||
1248 | return true; | |
c801d85f | 1249 | } |
dd1eaa89 | 1250 | |
c801d85f KB |
1251 | bool wxString::IsNumber() const |
1252 | { | |
a4a44612 VS |
1253 | if ( empty() ) |
1254 | return true; | |
1255 | ||
1256 | const_iterator i = begin(); | |
1257 | ||
1258 | if ( *i == _T('-') || *i == _T('+') ) | |
1259 | ++i; | |
1260 | ||
1261 | for ( ; i != end(); ++i ) | |
1262 | { | |
1263 | if ( !wxIsdigit(*i) ) | |
1264 | return false; | |
1265 | } | |
1266 | ||
1267 | return true; | |
c801d85f KB |
1268 | } |
1269 | ||
c801d85f KB |
1270 | wxString wxString::Strip(stripType w) const |
1271 | { | |
1272 | wxString s = *this; | |
d775fa82 WS |
1273 | if ( w & leading ) s.Trim(false); |
1274 | if ( w & trailing ) s.Trim(true); | |
c801d85f KB |
1275 | return s; |
1276 | } | |
1277 | ||
c801d85f KB |
1278 | // --------------------------------------------------------------------------- |
1279 | // case conversion | |
1280 | // --------------------------------------------------------------------------- | |
1281 | ||
1282 | wxString& wxString::MakeUpper() | |
1283 | { | |
e87b7833 MB |
1284 | for ( iterator it = begin(), en = end(); it != en; ++it ) |
1285 | *it = (wxChar)wxToupper(*it); | |
c801d85f KB |
1286 | |
1287 | return *this; | |
1288 | } | |
1289 | ||
1290 | wxString& wxString::MakeLower() | |
1291 | { | |
e87b7833 MB |
1292 | for ( iterator it = begin(), en = end(); it != en; ++it ) |
1293 | *it = (wxChar)wxTolower(*it); | |
c801d85f KB |
1294 | |
1295 | return *this; | |
1296 | } | |
1297 | ||
1298 | // --------------------------------------------------------------------------- | |
1299 | // trimming and padding | |
1300 | // --------------------------------------------------------------------------- | |
1301 | ||
d775fa82 | 1302 | // some compilers (VC++ 6.0 not to name them) return true for a call to |
576c608d VZ |
1303 |