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