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