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_STD_STRING=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(), wxT("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 various build options
54 // ----------------------------------------------------------------------------
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
63 // in both cases we need to define wxStdString
64 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
66 #include "wx/beforestd.h"
68 #include "wx/afterstd.h"
70 #ifdef HAVE_STD_WSTRING
71 typedef std::wstring wxStdWideString
;
73 typedef std::basic_string
<wchar_t> wxStdWideString
;
76 #if wxUSE_UNICODE_WCHAR
77 typedef wxStdWideString wxStdString
;
79 typedef std::string wxStdString
;
82 #endif // wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
85 #if wxUSE_STL_BASED_WXSTRING
87 // we always want ctor from std::string when using std::string internally
88 #undef wxUSE_STD_STRING
89 #define wxUSE_STD_STRING 1
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
98 typedef wxStdString wxStringImpl
;
99 #else // if !wxUSE_STL_BASED_WXSTRING
101 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
102 #undef HAVE_STD_STRING_COMPARE
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 // ---------------------------------------------------------------------------
109 struct WXDLLIMPEXP_BASE wxStringData
111 int nRefs
; // reference count
112 size_t nDataLength
, // actual string length
113 nAllocLength
; // allocated memory size
115 // mimics declaration 'wxStringCharType data[nAllocLength]'
116 wxStringCharType
* data() const { return (wxStringCharType
*)(this + 1); }
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); }
123 void Lock() { if ( !IsEmpty() ) nRefs
++; }
125 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
126 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
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
136 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
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); }
145 class WXDLLIMPEXP_BASE wxStringImpl
148 // an 'invalid' value for string index, moved to this place due to a CW bug
149 static const size_t npos
;
152 // points to data preceded by wxStringData structure with ref count info
153 wxStringCharType
*m_pchData
;
155 // accessor to string data
156 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
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
164 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
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(); }
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
*);
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
); }
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);
187 // compatibility with wxString
188 bool Alloc(size_t nLen
);
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
;
200 // macro to define the bulk of iterator and const_iterator classes
201 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
203 typedef wxStringCharType value_type; \
204 typedef ref_type reference; \
205 typedef ptr_type pointer; \
206 typedef int difference_type; \
208 iterator_name() : m_ptr(NULL) { } \
209 iterator_name(pointer ptr) : m_ptr(ptr) { } \
211 reference operator*() const { return *m_ptr; } \
213 iterator_name& operator++() { m_ptr++; return *this; } \
214 iterator_name operator++(int) \
216 const iterator_name tmp(*this); \
221 iterator_name& operator--() { m_ptr--; return *this; } \
222 iterator_name operator--(int) \
224 const iterator_name tmp(*this); \
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; } \
238 difference_type operator-(const iterator_name& i) const \
239 { 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; } \
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; } \
256 /* for wxStringImpl use only */ \
257 pointer GetPtr() const { return m_ptr; } \
259 friend class wxStringImpl; \
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
;
268 class WXDLLIMPEXP_BASE iterator
270 WX_DEFINE_STRINGIMPL_ITERATOR(iterator
,
274 friend class const_iterator
;
277 class WXDLLIMPEXP_BASE const_iterator
280 const_iterator(iterator i
) : m_ptr(i
.m_ptr
) { }
282 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator
,
283 const wxStringCharType
&,
284 const wxStringCharType
*);
287 #undef WX_DEFINE_STRINGIMPL_ITERATOR
290 // constructors and destructor
291 // ctor for an empty string
292 wxStringImpl() { Init(); }
294 wxStringImpl(const wxStringImpl
& stringSrc
)
296 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
297 wxT("did you forget to call UngetWriteBuf()?") );
299 if ( stringSrc
.empty() ) {
300 // nothing to do for an empty string
304 m_pchData
= stringSrc
.m_pchData
; // share same data
305 GetStringData()->Lock(); // => one more copy
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
)
319 wxASSERT_MSG( str
.GetStringData()->IsValid(),
320 wxT("did you forget to call UngetWriteBuf()?") );
322 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
323 InitWith(str
.c_str(), nPos
, nLen
);
325 // take everything between start and end
326 wxStringImpl(const_iterator start
, const_iterator end
);
329 // ctor from and conversion to std::string
331 wxStringImpl(const wxStdString
& impl
)
332 { InitWith(impl
.c_str(), 0, impl
.length()); }
334 operator wxStdString() const
335 { return wxStdString(c_str(), length()); }
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)
346 // dtor is not virtual, this class must not be inherited from!
349 GetStringData()->Unlock();
352 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
356 // overloaded assignment
357 // from another wxString
358 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
360 wxStringImpl
& operator=(wxStringCharType ch
);
362 wxStringImpl
& operator=(const wxStringCharType
*psz
);
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
; }
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
)
389 wxASSERT_VALID_INDEX( n
);
392 } // FIXME-UTF8: not useful for us...?
394 // lib.string.modifiers
395 // append elements str[pos], ..., str[pos+n]
396 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
398 wxASSERT(pos
<= str
.length());
399 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
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; }
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
); }
434 // first valid index position
435 const_iterator
begin() const { return m_pchData
; }
437 // position one after the last valid one
438 const_iterator
end() const { return m_pchData
+ length(); }
441 // insert another string
442 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
444 wxASSERT( str
.GetStringData()->IsValid() );
445 return insert(nPos
, str
.c_str(), str
.length());
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
)
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
);
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
); }
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
)
472 size_t idx
= first
- begin();
473 erase(idx
, last
- first
);
474 return begin() + idx
;
476 iterator
erase(iterator first
);
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
; }
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
);
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
,
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
); }
514 void swap(wxStringImpl
& str
);
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.
521 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
523 // find first n characters of sz
524 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
526 // find the first occurrence of character ch after nStart
527 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
529 // rfind() family is exactly like find() but works right to left
531 // as find, but from the end
532 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
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;
540 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
542 // substring extraction
543 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
546 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
547 // string += C string
548 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
550 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
552 // helpers for wxStringBuffer and wxStringBufferLength
553 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
554 void DoUngetWriteBuf();
555 void DoUngetWriteBuf(size_t nLen
);
557 friend class WXDLLIMPEXP_FWD_BASE wxString
;
560 #endif // !wxUSE_STL_BASED_WXSTRING
562 // don't pollute the library user's name space
563 #undef wxASSERT_VALID_INDEX
565 #endif // _WX_WXSTRINGIMPL_H__