]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stringimpl.h
only provide ptrdiff_t versions of verious operator+/- working with iterators instead...
[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+(ptrdiff_t n) const \
223 { return iterator_name(m_ptr + n); } \
224 iterator_name operator-(ptrdiff_t n) const \
225 { return iterator_name(m_ptr - n); } \
226 iterator_name& operator+=(ptrdiff_t n) \
227 { m_ptr += n; return *this; } \
228 iterator_name& operator-=(ptrdiff_t n) \
229 { m_ptr -= n; return *this; } \
230 \
231 difference_type operator-(const iterator_name& i) const \
232 { return m_ptr - i.m_ptr; } \
233 \
234 bool operator==(const iterator_name& i) const \
235 { return m_ptr == i.m_ptr; } \
236 bool operator!=(const iterator_name& i) const \
237 { return m_ptr != i.m_ptr; } \
238 \
239 bool operator<(const iterator_name& i) const \
240 { return m_ptr < i.m_ptr; } \
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 bool operator>=(const iterator_name& i) const \
246 { return m_ptr >= i.m_ptr; } \
247 \
248 private: \
249 /* for wxStringImpl use only */ \
250 operator pointer() const { return m_ptr; } \
251 \
252 friend class wxStringImpl; \
253 \
254 pointer m_ptr
255
256 // we need to declare const_iterator in wxStringImpl scope, the friend
257 // declaration inside iterator class itself is not enough, or at least not
258 // for g++ 3.4 (g++ 4 is ok)
259 class WXDLLIMPEXP_FWD_BASE const_iterator;
260
261 class WXDLLIMPEXP_BASE iterator
262 {
263 WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
264 wxStringCharType&,
265 wxStringCharType*);
266
267 friend class const_iterator;
268 };
269
270 class WXDLLIMPEXP_BASE const_iterator
271 {
272 public:
273 const_iterator(iterator i) : m_ptr(i.m_ptr) { }
274
275 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator,
276 const wxStringCharType&,
277 const wxStringCharType*);
278 };
279
280 #undef WX_DEFINE_STRINGIMPL_ITERATOR
281
282
283 // constructors and destructor
284 // ctor for an empty string
285 wxStringImpl() { Init(); }
286 // copy ctor
287 wxStringImpl(const wxStringImpl& stringSrc)
288 {
289 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
290 _T("did you forget to call UngetWriteBuf()?") );
291
292 if ( stringSrc.empty() ) {
293 // nothing to do for an empty string
294 Init();
295 }
296 else {
297 m_pchData = stringSrc.m_pchData; // share same data
298 GetStringData()->Lock(); // => one more copy
299 }
300 }
301 // string containing nRepeat copies of ch
302 wxStringImpl(size_type nRepeat, wxStringCharType ch);
303 // ctor takes first nLength characters from C string
304 // (default value of npos means take all the string)
305 wxStringImpl(const wxStringCharType *psz)
306 { InitWith(psz, 0, npos); }
307 wxStringImpl(const wxStringCharType *psz, size_t nLength)
308 { InitWith(psz, 0, nLength); }
309 // take nLen chars starting at nPos
310 wxStringImpl(const wxStringImpl& str, size_t nPos, size_t nLen)
311 {
312 wxASSERT_MSG( str.GetStringData()->IsValid(),
313 _T("did you forget to call UngetWriteBuf()?") );
314 Init();
315 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
316 InitWith(str.c_str(), nPos, nLen);
317 }
318 // take everything between start and end
319 wxStringImpl(const_iterator start, const_iterator end);
320
321
322 // ctor from and conversion to std::string
323 #if wxUSE_STD_STRING
324 wxStringImpl(const wxStdString& impl)
325 { InitWith(impl.c_str(), 0, impl.length()); }
326
327 operator wxStdString() const
328 { return wxStdString(c_str(), length()); }
329 #endif
330
331
332 // dtor is not virtual, this class must not be inherited from!
333 ~wxStringImpl()
334 {
335 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
336 //RN - according to the above VC++ does indeed inline this,
337 //even though it spits out two warnings
338 #pragma warning (disable:4714)
339 #endif
340
341 GetStringData()->Unlock();
342 }
343
344 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
345 //re-enable inlining warning
346 #pragma warning (default:4714)
347 #endif
348 // overloaded assignment
349 // from another wxString
350 wxStringImpl& operator=(const wxStringImpl& stringSrc);
351 // from a character
352 wxStringImpl& operator=(wxStringCharType ch);
353 // from a C string
354 wxStringImpl& operator=(const wxStringCharType *psz);
355
356 // return the length of the string
357 size_type length() const { return GetStringData()->nDataLength; }
358 // return the length of the string
359 size_type size() const { return length(); }
360 // return the maximum size of the string
361 size_type max_size() const { return npos; }
362 // resize the string, filling the space with c if c != 0
363 void resize(size_t nSize, wxStringCharType ch = '\0');
364 // delete the contents of the string
365 void clear() { erase(0, npos); }
366 // returns true if the string is empty
367 bool empty() const { return length() == 0; }
368 // inform string about planned change in size
369 void reserve(size_t sz) { Alloc(sz); }
370 size_type capacity() const { return GetStringData()->nAllocLength; }
371
372 // lib.string.access
373 // return the character at position n
374 value_type at(size_type n) const
375 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
376 // returns the writable character at position n
377 reference at(size_type n)
378 {
379 wxASSERT_VALID_INDEX( n );
380 CopyBeforeWrite();
381 return m_pchData[n];
382 } // FIXME-UTF8: not useful for us...?
383
384 // lib.string.modifiers
385 // append elements str[pos], ..., str[pos+n]
386 wxStringImpl& append(const wxStringImpl& str, size_t pos, size_t n)
387 {
388 wxASSERT(pos <= str.length());
389 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
390 return *this;
391 }
392 // append a string
393 wxStringImpl& append(const wxStringImpl& str)
394 { ConcatSelf(str.length(), str.c_str()); return *this; }
395 // append first n (or all if n == npos) characters of sz
396 wxStringImpl& append(const wxStringCharType *sz)
397 { ConcatSelf(wxStrlen(sz), sz); return *this; }
398 wxStringImpl& append(const wxStringCharType *sz, size_t n)
399 { ConcatSelf(n, sz); return *this; }
400 // append n copies of ch
401 wxStringImpl& append(size_t n, wxStringCharType ch);
402 // append from first to last
403 wxStringImpl& append(const_iterator first, const_iterator last)
404 { ConcatSelf(last - first, first); return *this; }
405
406 // same as `this_string = str'
407 wxStringImpl& assign(const wxStringImpl& str)
408 { return *this = str; }
409 // same as ` = str[pos..pos + n]
410 wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n)
411 { return replace(0, npos, str, pos, n); }
412 // same as `= first n (or all if n == npos) characters of sz'
413 wxStringImpl& assign(const wxStringCharType *sz)
414 { return replace(0, npos, sz, wxStrlen(sz)); }
415 wxStringImpl& assign(const wxStringCharType *sz, size_t n)
416 { return replace(0, npos, sz, n); }
417 // same as `= n copies of ch'
418 wxStringImpl& assign(size_t n, wxStringCharType ch)
419 { return replace(0, npos, n, ch); }
420 // assign from first to last
421 wxStringImpl& assign(const_iterator first, const_iterator last)
422 { return replace(begin(), end(), first, last); }
423
424 // first valid index position
425 const_iterator begin() const { return m_pchData; }
426 iterator begin();
427 // position one after the last valid one
428 const_iterator end() const { return m_pchData + length(); }
429 iterator end();
430
431 // insert another string
432 wxStringImpl& insert(size_t nPos, const wxStringImpl& str)
433 {
434 wxASSERT( str.GetStringData()->IsValid() );
435 return insert(nPos, str.c_str(), str.length());
436 }
437 // insert n chars of str starting at nStart (in str)
438 wxStringImpl& insert(size_t nPos, const wxStringImpl& str, size_t nStart, size_t n)
439 {
440 wxASSERT( str.GetStringData()->IsValid() );
441 wxASSERT( nStart < str.length() );
442 size_t strLen = str.length() - nStart;
443 n = strLen < n ? strLen : n;
444 return insert(nPos, str.c_str() + nStart, n);
445 }
446 // insert first n (or all if n == npos) characters of sz
447 wxStringImpl& insert(size_t nPos, const wxStringCharType *sz, size_t n = npos);
448 // insert n copies of ch
449 wxStringImpl& insert(size_t nPos, size_t n, wxStringCharType ch)
450 { return insert(nPos, wxStringImpl(n, ch)); }
451 iterator insert(iterator it, wxStringCharType ch)
452 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
453 void insert(iterator it, const_iterator first, const_iterator last)
454 { insert(it - begin(), first, last - first); }
455 void insert(iterator it, size_type n, wxStringCharType ch)
456 { insert(it - begin(), n, ch); }
457
458 // delete characters from nStart to nStart + nLen
459 wxStringImpl& erase(size_type pos = 0, size_type n = npos);
460 iterator erase(iterator first, iterator last)
461 {
462 size_t idx = first - begin();
463 erase(idx, last - first);
464 return begin() + idx;
465 }
466 iterator erase(iterator first);
467
468 // explicit conversion to C string (use this with printf()!)
469 const wxStringCharType* c_str() const { return m_pchData; }
470 const wxStringCharType* data() const { return m_pchData; }
471
472 // replaces the substring of length nLen starting at nStart
473 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz)
474 { return replace(nStart, nLen, sz, npos); }
475 // replaces the substring of length nLen starting at nStart
476 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str)
477 { return replace(nStart, nLen, str.c_str(), str.length()); }
478 // replaces the substring with nCount copies of ch
479 wxStringImpl& replace(size_t nStart, size_t nLen,
480 size_t nCount, wxStringCharType ch)
481 { return replace(nStart, nLen, wxStringImpl(nCount, ch)); }
482 // replaces a substring with another substring
483 wxStringImpl& replace(size_t nStart, size_t nLen,
484 const wxStringImpl& str, size_t nStart2, size_t nLen2)
485 { return replace(nStart, nLen, str.substr(nStart2, nLen2)); }
486 // replaces the substring with first nCount chars of sz
487 wxStringImpl& replace(size_t nStart, size_t nLen,
488 const wxStringCharType* sz, size_t nCount);
489
490 wxStringImpl& replace(iterator first, iterator last, const_pointer s)
491 { return replace(first - begin(), last - first, s); }
492 wxStringImpl& replace(iterator first, iterator last, const_pointer s,
493 size_type n)
494 { return replace(first - begin(), last - first, s, n); }
495 wxStringImpl& replace(iterator first, iterator last, const wxStringImpl& s)
496 { return replace(first - begin(), last - first, s); }
497 wxStringImpl& replace(iterator first, iterator last, size_type n, wxStringCharType c)
498 { return replace(first - begin(), last - first, n, c); }
499 wxStringImpl& replace(iterator first, iterator last,
500 const_iterator first1, const_iterator last1)
501 { return replace(first - begin(), last - first, first1, last1 - first1); }
502
503 // swap two strings
504 void swap(wxStringImpl& str);
505
506 // All find() functions take the nStart argument which specifies the
507 // position to start the search on, the default value is 0. All functions
508 // return npos if there were no match.
509
510 // find a substring
511 size_t find(const wxStringImpl& str, size_t nStart = 0) const;
512
513 // find first n characters of sz
514 size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
515
516 // find the first occurrence of character ch after nStart
517 size_t find(wxStringCharType ch, size_t nStart = 0) const;
518
519 // rfind() family is exactly like find() but works right to left
520
521 // as find, but from the end
522 size_t rfind(const wxStringImpl& str, size_t nStart = npos) const;
523
524 // as find, but from the end
525 size_t rfind(const wxStringCharType* sz, size_t nStart = npos,
526 size_t n = npos) const;
527 // as find, but from the end
528 size_t rfind(wxStringCharType ch, size_t nStart = npos) const;
529
530 size_type copy(wxStringCharType* s, size_type n, size_type pos = 0);
531
532 // substring extraction
533 wxStringImpl substr(size_t nStart = 0, size_t nLen = npos) const;
534
535 // string += string
536 wxStringImpl& operator+=(const wxStringImpl& s) { return append(s); }
537 // string += C string
538 wxStringImpl& operator+=(const wxStringCharType *psz) { return append(psz); }
539 // string += char
540 wxStringImpl& operator+=(wxStringCharType ch) { return append(1, ch); }
541
542 // helpers for wxStringBuffer and wxStringBufferLength
543 wxStringCharType *DoGetWriteBuf(size_t nLen);
544 void DoUngetWriteBuf();
545 void DoUngetWriteBuf(size_t nLen);
546
547 friend class WXDLLIMPEXP_FWD_BASE wxString;
548 };
549
550 #endif // !wxUSE_STL_BASED_WXSTRING
551
552 // don't pollute the library user's name space
553 #undef wxASSERT_VALID_INDEX
554
555 #endif // _WX_WXSTRINGIMPL_H__