]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stringimpl.h
clean up wxBitmapButton::Create(); override MSWGetStyle() (#4804)
[wxWidgets.git] / include / wx / stringimpl.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stringimpl.h
3 // Purpose: wxStringImpl class, implementation of wxString
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>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 /*
13 This header implements std::string-like string class, wxStringImpl, that is
14 used by wxString to store the data. Alternatively, if wxUSE_STL=1,
15 wxStringImpl is just a typedef to std:: string class.
16 */
17
18 #ifndef _WX_WXSTRINGIMPL_H__
19 #define _WX_WXSTRINGIMPL_H__
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 #include "wx/defs.h" // everybody should include this
26 #include "wx/chartype.h" // for wxChar
27 #include "wx/wxcrtbase.h" // for wxStrlen() etc.
28
29 #include <stdlib.h>
30
31 // ---------------------------------------------------------------------------
32 // macros
33 // ---------------------------------------------------------------------------
34
35 // implementation only
36 #define wxASSERT_VALID_INDEX(i) \
37 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
38
39
40 // ----------------------------------------------------------------------------
41 // global data
42 // ----------------------------------------------------------------------------
43
44 // global pointer to empty string
45 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
46 #if wxUSE_UNICODE_UTF8
47 // FIXME-UTF8: we should have only one wxEmptyString
48 extern WXDLLIMPEXP_DATA_BASE(const wxStringCharType*) wxEmptyStringImpl;
49 #endif
50
51
52 // ----------------------------------------------------------------------------
53 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
54 // ----------------------------------------------------------------------------
55
56 // using STL implies using std::string
57 #if wxUSE_STL
58 #undef wxUSE_STD_STRING
59 #define wxUSE_STD_STRING 1
60 #endif
61
62 // we use STL-based string internally if we use std::string at all now, there
63 // should be no reason to prefer our internal implement but if you really need
64 // it you can predefine wxUSE_STL_BASED_WXSTRING as 0 when building the library
65 #ifndef wxUSE_STL_BASED_WXSTRING
66 #define wxUSE_STL_BASED_WXSTRING wxUSE_STD_STRING
67 #endif
68
69 // in both cases we need to define wxStdString
70 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
71
72 #include "wx/beforestd.h"
73 #include <string>
74 #include "wx/afterstd.h"
75
76 #ifdef HAVE_STD_WSTRING
77 typedef std::wstring wxStdWideString;
78 #else
79 typedef std::basic_string<wchar_t> wxStdWideString;
80 #endif
81
82 #if wxUSE_UNICODE_WCHAR
83 typedef wxStdWideString wxStdString;
84 #else
85 typedef std::string wxStdString;
86 #endif
87
88 #endif // wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
89
90
91 #if wxUSE_STL_BASED_WXSTRING
92
93 // we always want ctor from std::string when using std::string internally
94 #undef wxUSE_STD_STRING
95 #define wxUSE_STD_STRING 1
96
97 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
98 (defined(_MSC_VER) && (_MSC_VER <= 1200))
99 #define wxSTRING_BASE_HASNT_CLEAR
100 #endif
101
102 typedef wxStdString wxStringImpl;
103 #else // if !wxUSE_STL_BASED_WXSTRING
104
105 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
106 #undef HAVE_STD_STRING_COMPARE
107
108 // ---------------------------------------------------------------------------
109 // string data prepended with some housekeeping info (used by wxString class),
110 // is never used directly (but had to be put here to allow inlining)
111 // ---------------------------------------------------------------------------
112
113 struct WXDLLIMPEXP_BASE wxStringData
114 {
115 int nRefs; // reference count
116 size_t nDataLength, // actual string length
117 nAllocLength; // allocated memory size
118
119 // mimics declaration 'wxStringCharType data[nAllocLength]'
120 wxStringCharType* data() const { return (wxStringCharType*)(this + 1); }
121
122 // empty string has a special ref count so it's never deleted
123 bool IsEmpty() const { return (nRefs == -1); }
124 bool IsShared() const { return (nRefs > 1); }
125
126 // lock/unlock
127 void Lock() { if ( !IsEmpty() ) nRefs++; }
128
129 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
130 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
131 __forceinline
132 #endif
133 // VC++ free must take place in same DLL as allocation when using non dll
134 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
135 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
136 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
137 // we must not inline deallocation since allocation is not inlined
138 void Free();
139 #else
140 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
141 #endif
142
143 // if we had taken control over string memory (GetWriteBuf), it's
144 // intentionally put in invalid state
145 void Validate(bool b) { nRefs = (b ? 1 : 0); }
146 bool IsValid() const { return (nRefs != 0); }
147 };
148
149 class WXDLLIMPEXP_BASE wxStringImpl
150 {
151 public:
152 // an 'invalid' value for string index, moved to this place due to a CW bug
153 static const size_t npos;
154
155 protected:
156 // points to data preceded by wxStringData structure with ref count info
157 wxStringCharType *m_pchData;
158
159 // accessor to string data
160 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
161
162 // string (re)initialization functions
163 // initializes the string to the empty value (must be called only from
164 // ctors, use Reinit() otherwise)
165 #if wxUSE_UNICODE_UTF8
166 void Init() { m_pchData = (wxStringCharType *)wxEmptyStringImpl; } // FIXME-UTF8
167 #else
168 void Init() { m_pchData = (wxStringCharType *)wxEmptyString; }
169 #endif
170 // initializes the string with (a part of) C-string
171 void InitWith(const wxStringCharType *psz, size_t nPos = 0, size_t nLen = npos);
172 // as Init, but also frees old data
173 void Reinit() { GetStringData()->Unlock(); Init(); }
174
175 // memory allocation
176 // allocates memory for string of length nLen
177 bool AllocBuffer(size_t nLen);
178 // effectively copies data to string
179 bool AssignCopy(size_t, const wxStringCharType *);
180
181 // append a (sub)string
182 bool ConcatSelf(size_t nLen, const wxStringCharType *src, size_t nMaxLen);
183 bool ConcatSelf(size_t nLen, const wxStringCharType *src)
184 { return ConcatSelf(nLen, src, nLen); }
185
186 // functions called before writing to the string: they copy it if there
187 // are other references to our data (should be the only owner when writing)
188 bool CopyBeforeWrite();
189 bool AllocBeforeWrite(size_t);
190
191 // compatibility with wxString
192 bool Alloc(size_t nLen);
193
194 public:
195 // standard types
196 typedef wxStringCharType value_type;
197 typedef wxStringCharType char_type;
198 typedef size_t size_type;
199 typedef value_type& reference;
200 typedef const value_type& const_reference;
201 typedef value_type* pointer;
202 typedef const value_type* const_pointer;
203
204 // macro to define the bulk of iterator and const_iterator classes
205 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
206 public: \
207 typedef wxStringCharType value_type; \
208 typedef ref_type reference; \
209 typedef ptr_type pointer; \
210 typedef int difference_type; \
211 \
212 iterator_name() : m_ptr(NULL) { } \
213 iterator_name(pointer ptr) : m_ptr(ptr) { } \
214 \
215 reference operator*() const { return *m_ptr; } \
216 \
217 iterator_name& operator++() { m_ptr++; return *this; } \
218 iterator_name operator++(int) \
219 { \
220 const iterator_name tmp(*this); \
221 m_ptr++; \
222 return tmp; \
223 } \
224 \
225 iterator_name& operator--() { m_ptr--; return *this; } \
226 iterator_name operator--(int) \
227 { \
228 const iterator_name tmp(*this); \
229 m_ptr--; \
230 return tmp; \
231 } \
232 \
233 iterator_name operator+(ptrdiff_t n) const \
234 { return iterator_name(m_ptr + n); } \
235 iterator_name operator-(ptrdiff_t n) const \
236 { return iterator_name(m_ptr - n); } \
237 iterator_name& operator+=(ptrdiff_t n) \
238 { m_ptr += n; return *this; } \
239 iterator_name& operator-=(ptrdiff_t n) \
240 { m_ptr -= n; return *this; } \
241 \
242 difference_type operator-(const iterator_name& i) const \
243 { return m_ptr - i.m_ptr; } \
244 \
245 bool operator==(const iterator_name& i) const \
246 { return m_ptr == i.m_ptr; } \
247 bool operator!=(const iterator_name& i) const \
248 { return m_ptr != i.m_ptr; } \
249 \
250 bool operator<(const iterator_name& i) const \
251 { return m_ptr < i.m_ptr; } \
252 bool operator>(const iterator_name& i) const \
253 { return m_ptr > i.m_ptr; } \
254 bool operator<=(const iterator_name& i) const \
255 { return m_ptr <= i.m_ptr; } \
256 bool operator>=(const iterator_name& i) const \
257 { return m_ptr >= i.m_ptr; } \
258 \
259 private: \
260 /* for wxStringImpl use only */ \
261 pointer GetPtr() const { return m_ptr; } \
262 \
263 friend class wxStringImpl; \
264 \
265 pointer m_ptr
266
267 // we need to declare const_iterator in wxStringImpl scope, the friend
268 // declaration inside iterator class itself is not enough, or at least not
269 // for g++ 3.4 (g++ 4 is ok)
270 class WXDLLIMPEXP_FWD_BASE const_iterator;
271
272 class WXDLLIMPEXP_BASE iterator
273 {
274 WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
275 wxStringCharType&,
276 wxStringCharType*);
277
278 friend class const_iterator;
279 };
280
281 class WXDLLIMPEXP_BASE const_iterator
282 {
283 public:
284 const_iterator(iterator i) : m_ptr(i.m_ptr) { }
285
286 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator,
287 const wxStringCharType&,
288 const wxStringCharType*);
289 };
290
291 #undef WX_DEFINE_STRINGIMPL_ITERATOR
292
293
294 // constructors and destructor
295 // ctor for an empty string
296 wxStringImpl() { Init(); }
297 // copy ctor
298 wxStringImpl(const wxStringImpl& stringSrc)
299 {
300 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
301 _T("did you forget to call UngetWriteBuf()?") );
302
303 if ( stringSrc.empty() ) {
304 // nothing to do for an empty string
305 Init();
306 }
307 else {
308 m_pchData = stringSrc.m_pchData; // share same data
309 GetStringData()->Lock(); // => one more copy
310 }
311 }
312 // string containing nRepeat copies of ch
313 wxStringImpl(size_type nRepeat, wxStringCharType ch);
314 // ctor takes first nLength characters from C string
315 // (default value of npos means take all the string)
316 wxStringImpl(const wxStringCharType *psz)
317 { InitWith(psz, 0, npos); }
318 wxStringImpl(const wxStringCharType *psz, size_t nLength)
319 { InitWith(psz, 0, nLength); }
320 // take nLen chars starting at nPos
321 wxStringImpl(const wxStringImpl& str, size_t nPos, size_t nLen)
322 {
323 wxASSERT_MSG( str.GetStringData()->IsValid(),
324 _T("did you forget to call UngetWriteBuf()?") );
325 Init();
326 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
327 InitWith(str.c_str(), nPos, nLen);
328 }
329 // take everything between start and end
330 wxStringImpl(const_iterator start, const_iterator end);
331
332
333 // ctor from and conversion to std::string
334 #if wxUSE_STD_STRING
335 wxStringImpl(const wxStdString& impl)
336 { InitWith(impl.c_str(), 0, impl.length()); }
337
338 operator wxStdString() const
339 { return wxStdString(c_str(), length()); }
340 #endif
341
342
343 // dtor is not virtual, this class must not be inherited from!
344 ~wxStringImpl()
345 {
346 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
347 //RN - according to the above VC++ does indeed inline this,
348 //even though it spits out two warnings
349 #pragma warning (disable:4714)
350 #endif
351
352 GetStringData()->Unlock();
353 }
354
355 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
356 //re-enable inlining warning
357 #pragma warning (default:4714)
358 #endif
359 // overloaded assignment
360 // from another wxString
361 wxStringImpl& operator=(const wxStringImpl& stringSrc);
362 // from a character
363 wxStringImpl& operator=(wxStringCharType ch);
364 // from a C string
365 wxStringImpl& operator=(const wxStringCharType *psz);
366
367 // return the length of the string
368 size_type length() const { return GetStringData()->nDataLength; }
369 // return the length of the string
370 size_type size() const { return length(); }
371 // return the maximum size of the string
372 size_type max_size() const { return npos; }
373 // resize the string, filling the space with c if c != 0
374 void resize(size_t nSize, wxStringCharType ch = '\0');
375 // delete the contents of the string
376 void clear() { erase(0, npos); }
377 // returns true if the string is empty
378 bool empty() const { return length() == 0; }
379 // inform string about planned change in size
380 void reserve(size_t sz) { Alloc(sz); }
381 size_type capacity() const { return GetStringData()->nAllocLength; }
382
383 // lib.string.access
384 // return the character at position n
385 value_type at(size_type n) const
386 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
387 // returns the writable character at position n
388 reference at(size_type n)
389 {
390 wxASSERT_VALID_INDEX( n );
391 CopyBeforeWrite();
392 return m_pchData[n];
393 } // FIXME-UTF8: not useful for us...?
394
395 // lib.string.modifiers
396 // append elements str[pos], ..., str[pos+n]
397 wxStringImpl& append(const wxStringImpl& str, size_t pos, size_t n)
398 {
399 wxASSERT(pos <= str.length());
400 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
401 return *this;
402 }
403 // append a string
404 wxStringImpl& append(const wxStringImpl& str)
405 { ConcatSelf(str.length(), str.c_str()); return *this; }
406 // append first n (or all if n == npos) characters of sz
407 wxStringImpl& append(const wxStringCharType *sz)
408 { ConcatSelf(wxStrlen(sz), sz); return *this; }
409 wxStringImpl& append(const wxStringCharType *sz, size_t n)
410 { ConcatSelf(n, sz); return *this; }
411 // append n copies of ch
412 wxStringImpl& append(size_t n, wxStringCharType ch);
413 // append from first to last
414 wxStringImpl& append(const_iterator first, const_iterator last)
415 { ConcatSelf(last - first, first.GetPtr()); return *this; }
416
417 // same as `this_string = str'
418 wxStringImpl& assign(const wxStringImpl& str)
419 { return *this = str; }
420 // same as ` = str[pos..pos + n]
421 wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n)
422 { return replace(0, npos, str, pos, n); }
423 // same as `= first n (or all if n == npos) characters of sz'
424 wxStringImpl& assign(const wxStringCharType *sz)
425 { return replace(0, npos, sz, wxStrlen(sz)); }
426 wxStringImpl& assign(const wxStringCharType *sz, size_t n)
427 { return replace(0, npos, sz, n); }
428 // same as `= n copies of ch'
429 wxStringImpl& assign(size_t n, wxStringCharType ch)
430 { return replace(0, npos, n, ch); }
431 // assign from first to last
432 wxStringImpl& assign(const_iterator first, const_iterator last)
433 { return replace(begin(), end(), first, last); }
434
435 // first valid index position
436 const_iterator begin() const { return m_pchData; }
437 iterator begin();
438 // position one after the last valid one
439 const_iterator end() const { return m_pchData + length(); }
440 iterator end();
441
442 // insert another string
443 wxStringImpl& insert(size_t nPos, const wxStringImpl& str)
444 {
445 wxASSERT( str.GetStringData()->IsValid() );
446 return insert(nPos, str.c_str(), str.length());
447 }
448 // insert n chars of str starting at nStart (in str)
449 wxStringImpl& insert(size_t nPos, const wxStringImpl& str, size_t nStart, size_t n)
450 {
451 wxASSERT( str.GetStringData()->IsValid() );
452 wxASSERT( nStart < str.length() );
453 size_t strLen = str.length() - nStart;
454 n = strLen < n ? strLen : n;
455 return insert(nPos, str.c_str() + nStart, n);
456 }
457 // insert first n (or all if n == npos) characters of sz
458 wxStringImpl& insert(size_t nPos, const wxStringCharType *sz, size_t n = npos);
459 // insert n copies of ch
460 wxStringImpl& insert(size_t nPos, size_t n, wxStringCharType ch)
461 { return insert(nPos, wxStringImpl(n, ch)); }
462 iterator insert(iterator it, wxStringCharType ch)
463 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
464 void insert(iterator it, const_iterator first, const_iterator last)
465 { insert(it - begin(), first.GetPtr(), last - first); }
466 void insert(iterator it, size_type n, wxStringCharType ch)
467 { insert(it - begin(), n, ch); }
468
469 // delete characters from nStart to nStart + nLen
470 wxStringImpl& erase(size_type pos = 0, size_type n = npos);
471 iterator erase(iterator first, iterator last)
472 {
473 size_t idx = first - begin();
474 erase(idx, last - first);
475 return begin() + idx;
476 }
477 iterator erase(iterator first);
478
479 // explicit conversion to C string (use this with printf()!)
480 const wxStringCharType* c_str() const { return m_pchData; }
481 const wxStringCharType* data() const { return m_pchData; }
482
483 // replaces the substring of length nLen starting at nStart
484 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz)
485 { return replace(nStart, nLen, sz, npos); }
486 // replaces the substring of length nLen starting at nStart
487 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str)
488 { return replace(nStart, nLen, str.c_str(), str.length()); }
489 // replaces the substring with nCount copies of ch
490 wxStringImpl& replace(size_t nStart, size_t nLen,
491 size_t nCount, wxStringCharType ch)
492 { return replace(nStart, nLen, wxStringImpl(nCount, ch)); }
493 // replaces a substring with another substring
494 wxStringImpl& replace(size_t nStart, size_t nLen,
495 const wxStringImpl& str, size_t nStart2, size_t nLen2)
496 { return replace(nStart, nLen, str.substr(nStart2, nLen2)); }
497 // replaces the substring with first nCount chars of sz
498 wxStringImpl& replace(size_t nStart, size_t nLen,
499 const wxStringCharType* sz, size_t nCount);
500
501 wxStringImpl& replace(iterator first, iterator last, const_pointer s)
502 { return replace(first - begin(), last - first, s); }
503 wxStringImpl& replace(iterator first, iterator last, const_pointer s,
504 size_type n)
505 { return replace(first - begin(), last - first, s, n); }
506 wxStringImpl& replace(iterator first, iterator last, const wxStringImpl& s)
507 { return replace(first - begin(), last - first, s); }
508 wxStringImpl& replace(iterator first, iterator last, size_type n, wxStringCharType c)
509 { return replace(first - begin(), last - first, n, c); }
510 wxStringImpl& replace(iterator first, iterator last,
511 const_iterator first1, const_iterator last1)
512 { return replace(first - begin(), last - first, first1.GetPtr(), last1 - first1); }
513
514 // swap two strings
515 void swap(wxStringImpl& str);
516
517 // All find() functions take the nStart argument which specifies the
518 // position to start the search on, the default value is 0. All functions
519 // return npos if there were no match.
520
521 // find a substring
522 size_t find(const wxStringImpl& str, size_t nStart = 0) const;
523
524 // find first n characters of sz
525 size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
526
527 // find the first occurrence of character ch after nStart
528 size_t find(wxStringCharType ch, size_t nStart = 0) const;
529
530 // rfind() family is exactly like find() but works right to left
531
532 // as find, but from the end
533 size_t rfind(const wxStringImpl& str, size_t nStart = npos) const;
534
535 // as find, but from the end
536 size_t rfind(const wxStringCharType* sz, size_t nStart = npos,
537 size_t n = npos) const;
538 // as find, but from the end
539 size_t rfind(wxStringCharType ch, size_t nStart = npos) const;
540
541 size_type copy(wxStringCharType* s, size_type n, size_type pos = 0);
542
543 // substring extraction
544 wxStringImpl substr(size_t nStart = 0, size_t nLen = npos) const;
545
546 // string += string
547 wxStringImpl& operator+=(const wxStringImpl& s) { return append(s); }
548 // string += C string
549 wxStringImpl& operator+=(const wxStringCharType *psz) { return append(psz); }
550 // string += char
551 wxStringImpl& operator+=(wxStringCharType ch) { return append(1, ch); }
552
553 // helpers for wxStringBuffer and wxStringBufferLength
554 wxStringCharType *DoGetWriteBuf(size_t nLen);
555 void DoUngetWriteBuf();
556 void DoUngetWriteBuf(size_t nLen);
557
558 friend class WXDLLIMPEXP_FWD_BASE wxString;
559 };
560
561 #endif // !wxUSE_STL_BASED_WXSTRING
562
563 // don't pollute the library user's name space
564 #undef wxASSERT_VALID_INDEX
565
566 #endif // _WX_WXSTRINGIMPL_H__