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