1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stringimpl.h
3 // Purpose: wxStringImpl class, implementation of wxString
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
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.
18 #ifndef _WX_WXSTRINGIMPL_H__
19 #define _WX_WXSTRINGIMPL_H__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
26 #include "wx/chartype.h" // for wxChar
27 #include "wx/wxcrtbase.h" // for wxStrlen() etc.
31 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 // implementation only
36 #define wxASSERT_VALID_INDEX(i) \
37 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
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
;
52 // ----------------------------------------------------------------------------
53 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
54 // ----------------------------------------------------------------------------
56 // using STL implies using std::string
58 #undef wxUSE_STD_STRING
59 #define wxUSE_STD_STRING 1
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
69 // in both cases we need to define wxStdString
70 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
72 #include "wx/beforestd.h"
74 #include "wx/afterstd.h"
76 #ifdef HAVE_STD_WSTRING
77 typedef std::wstring wxStdWideString
;
79 typedef std::basic_string
<wchar_t> wxStdWideString
;
82 #if wxUSE_UNICODE_WCHAR
83 typedef wxStdWideString wxStdString
;
85 typedef std::string wxStdString
;
88 #endif // wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
91 #if wxUSE_STL_BASED_WXSTRING
93 // we always want ctor from std::string when using std::string internally
94 #undef wxUSE_STD_STRING
95 #define wxUSE_STD_STRING 1
97 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
98 (defined(_MSC_VER) && (_MSC_VER <= 1200))
99 #define wxSTRING_BASE_HASNT_CLEAR
102 typedef wxStdString wxStringImpl
;
103 #else // if !wxUSE_STL_BASED_WXSTRING
105 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
106 #undef HAVE_STD_STRING_COMPARE
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 // ---------------------------------------------------------------------------
113 struct WXDLLIMPEXP_BASE wxStringData
115 int nRefs
; // reference count
116 size_t nDataLength
, // actual string length
117 nAllocLength
; // allocated memory size
119 // mimics declaration 'wxStringCharType data[nAllocLength]'
120 wxStringCharType
* data() const { return (wxStringCharType
*)(this + 1); }
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); }
127 void Lock() { if ( !IsEmpty() ) nRefs
++; }
129 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
130 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
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
140 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
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); }
149 class WXDLLIMPEXP_BASE wxStringImpl
152 // an 'invalid' value for string index, moved to this place due to a CW bug
153 static const size_t npos
;
156 // points to data preceded by wxStringData structure with ref count info
157 wxStringCharType
*m_pchData
;
159 // accessor to string data
160 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
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
168 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
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(); }
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
*);
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
); }
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);
191 // compatibility with wxString
192 bool Alloc(size_t nLen
);
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
;
204 // macro to define the bulk of iterator and const_iterator classes
205 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
207 typedef wxStringCharType value_type; \
208 typedef ref_type reference; \
209 typedef ptr_type pointer; \
210 typedef int difference_type; \
212 iterator_name() : m_ptr(NULL) { } \
213 iterator_name(pointer ptr) : m_ptr(ptr) { } \
215 reference operator*() const { return *m_ptr; } \
217 iterator_name& operator++() { m_ptr++; return *this; } \
218 iterator_name operator++(int) \
220 const iterator_name tmp(*this); \
225 iterator_name& operator--() { m_ptr--; return *this; } \
226 iterator_name operator--(int) \
228 const iterator_name tmp(*this); \
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; } \
242 difference_type operator-(const iterator_name& i) const \
243 { return m_ptr - i.m_ptr; } \
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; } \
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; } \
260 /* for wxStringImpl use only */ \
261 pointer GetPtr() const { return m_ptr; } \
263 friend class wxStringImpl; \
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
;
272 class WXDLLIMPEXP_BASE iterator
274 WX_DEFINE_STRINGIMPL_ITERATOR(iterator
,
278 friend class const_iterator
;
281 class WXDLLIMPEXP_BASE const_iterator
284 const_iterator(iterator i
) : m_ptr(i
.m_ptr
) { }
286 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator
,
287 const wxStringCharType
&,
288 const wxStringCharType
*);
291 #undef WX_DEFINE_STRINGIMPL_ITERATOR
294 // constructors and destructor
295 // ctor for an empty string
296 wxStringImpl() { Init(); }
298 wxStringImpl(const wxStringImpl
& stringSrc
)
300 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
301 _T("did you forget to call UngetWriteBuf()?") );
303 if ( stringSrc
.empty() ) {
304 // nothing to do for an empty string
308 m_pchData
= stringSrc
.m_pchData
; // share same data
309 GetStringData()->Lock(); // => one more copy
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
)
323 wxASSERT_MSG( str
.GetStringData()->IsValid(),
324 _T("did you forget to call UngetWriteBuf()?") );
326 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
327 InitWith(str
.c_str(), nPos
, nLen
);
329 // take everything between start and end
330 wxStringImpl(const_iterator start
, const_iterator end
);
333 // ctor from and conversion to std::string
335 wxStringImpl(const wxStdString
& impl
)
336 { InitWith(impl
.c_str(), 0, impl
.length()); }
338 operator wxStdString() const
339 { return wxStdString(c_str(), length()); }
343 // dtor is not virtual, this class must not be inherited from!
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)
352 GetStringData()->Unlock();
355 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
356 //re-enable inlining warning
357 #pragma warning (default:4714)
359 // overloaded assignment
360 // from another wxString
361 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
363 wxStringImpl
& operator=(wxStringCharType ch
);
365 wxStringImpl
& operator=(const wxStringCharType
*psz
);
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
; }
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
)
390 wxASSERT_VALID_INDEX( n
);
393 } // FIXME-UTF8: not useful for us...?
395 // lib.string.modifiers
396 // append elements str[pos], ..., str[pos+n]
397 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
399 wxASSERT(pos
<= str
.length());
400 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
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; }
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
); }
435 // first valid index position
436 const_iterator
begin() const { return m_pchData
; }
438 // position one after the last valid one
439 const_iterator
end() const { return m_pchData
+ length(); }
442 // insert another string
443 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
445 wxASSERT( str
.GetStringData()->IsValid() );
446 return insert(nPos
, str
.c_str(), str
.length());
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
)
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
);
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
); }
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
)
473 size_t idx
= first
- begin();
474 erase(idx
, last
- first
);
475 return begin() + idx
;
477 iterator
erase(iterator first
);
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
; }
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
);
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
,
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
); }
515 void swap(wxStringImpl
& str
);
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.
522 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
524 // find first n characters of sz
525 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
527 // find the first occurrence of character ch after nStart
528 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
530 // rfind() family is exactly like find() but works right to left
532 // as find, but from the end
533 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
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;
541 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
543 // substring extraction
544 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
547 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
548 // string += C string
549 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
551 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
553 // helpers for wxStringBuffer and wxStringBufferLength
554 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
555 void DoUngetWriteBuf();
556 void DoUngetWriteBuf(size_t nLen
);
558 friend class WXDLLIMPEXP_FWD_BASE wxString
;
561 #endif // !wxUSE_STL_BASED_WXSTRING
563 // don't pollute the library user's name space
564 #undef wxASSERT_VALID_INDEX
566 #endif // _WX_WXSTRINGIMPL_H__