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 #define wxUSE_STL_BASED_WXSTRING wxUSE_STL
58 // in both cases we need to define wxStdString
59 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
61 #include "wx/beforestd.h"
63 #include "wx/afterstd.h"
65 #ifdef HAVE_STD_WSTRING
66 typedef std::wstring wxStdWideString
;
68 typedef std::basic_string
<wchar_t> wxStdWideString
;
71 #if wxUSE_UNICODE_WCHAR
72 typedef wxStdWideString wxStdString
;
74 typedef std::string wxStdString
;
77 #endif // wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
80 #if wxUSE_STL_BASED_WXSTRING
82 // we always want ctor from std::string when using std::string internally
83 #undef wxUSE_STD_STRING
84 #define wxUSE_STD_STRING 1
86 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
87 (defined(_MSC_VER) && (_MSC_VER <= 1200))
88 #define wxSTRING_BASE_HASNT_CLEAR
91 typedef wxStdString wxStringImpl
;
92 #else // if !wxUSE_STL_BASED_WXSTRING
94 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
95 #undef HAVE_STD_STRING_COMPARE
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 // ---------------------------------------------------------------------------
102 struct WXDLLIMPEXP_BASE wxStringData
104 int nRefs
; // reference count
105 size_t nDataLength
, // actual string length
106 nAllocLength
; // allocated memory size
108 // mimics declaration 'wxStringCharType data[nAllocLength]'
109 wxStringCharType
* data() const { return (wxStringCharType
*)(this + 1); }
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); }
116 void Lock() { if ( !IsEmpty() ) nRefs
++; }
118 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
119 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
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
129 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
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); }
138 class WXDLLIMPEXP_BASE wxStringImpl
141 // an 'invalid' value for string index, moved to this place due to a CW bug
142 static const size_t npos
;
145 // points to data preceded by wxStringData structure with ref count info
146 wxStringCharType
*m_pchData
;
148 // accessor to string data
149 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
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
157 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
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(); }
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
*);
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
); }
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);
180 // compatibility with wxString
181 bool Alloc(size_t nLen
);
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
;
193 // macro to define the bulk of iterator and const_iterator classes
194 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
196 typedef wxStringCharType value_type; \
197 typedef ref_type reference; \
198 typedef ptr_type pointer; \
199 typedef int difference_type; \
201 iterator_name(pointer ptr) : m_ptr(ptr) { } \
203 reference operator*() const { return *m_ptr; } \
205 iterator_name& operator++() { m_ptr++; return *this; } \
206 iterator_name operator++(int) \
208 const iterator_name tmp(*this); \
213 iterator_name& operator--() { m_ptr--; return *this; } \
214 iterator_name operator--(int) \
216 const iterator_name tmp(*this); \
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) const \
226 { return iterator_name(m_ptr - n); } \
227 iterator_name operator-(size_t n) const \
228 { return iterator_name(m_ptr - n); } \
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 iterator_name& operator-=(int n) \
234 { m_ptr -= n; return *this; } \
235 iterator_name& operator-=(size_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 operator pointer() const { return m_ptr; } \
259 friend class WXDLLIMPEXP_BASE 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 const_iterator
;
270 WX_DEFINE_STRINGIMPL_ITERATOR(iterator
,
274 friend class 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 _T("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 _T("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()); }
339 // dtor is not virtual, this class must not be inherited from!
342 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
343 //RN - according to the above VC++ does indeed inline this,
344 //even though it spits out two warnings
345 #pragma warning (disable:4714)
348 GetStringData()->Unlock();
351 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
352 //re-enable inlining warning
353 #pragma warning (default:4714)
355 // overloaded assignment
356 // from another wxString
357 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
359 wxStringImpl
& operator=(wxStringCharType ch
);
361 wxStringImpl
& operator=(const wxStringCharType
*psz
);
363 // return the length of the string
364 size_type
length() const { return GetStringData()->nDataLength
; }
365 // return the length of the string
366 size_type
size() const { return length(); }
367 // return the maximum size of the string
368 size_type
max_size() const { return npos
; }
369 // resize the string, filling the space with c if c != 0
370 void resize(size_t nSize
, wxStringCharType ch
= '\0');
371 // delete the contents of the string
372 void clear() { erase(0, npos
); }
373 // returns true if the string is empty
374 bool empty() const { return length() == 0; }
375 // inform string about planned change in size
376 void reserve(size_t sz
) { Alloc(sz
); }
377 size_type
capacity() const { return GetStringData()->nAllocLength
; }
380 // return the character at position n
381 value_type
at(size_type n
) const
382 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
383 // returns the writable character at position n
384 reference
at(size_type n
)
386 wxASSERT_VALID_INDEX( n
);
389 } // FIXME-UTF8: not useful for us...?
391 // lib.string.modifiers
392 // append elements str[pos], ..., str[pos+n]
393 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
395 wxASSERT(pos
<= str
.length());
396 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
400 wxStringImpl
& append(const wxStringImpl
& str
)
401 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
402 // append first n (or all if n == npos) characters of sz
403 wxStringImpl
& append(const wxStringCharType
*sz
)
404 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
405 wxStringImpl
& append(const wxStringCharType
*sz
, size_t n
)
406 { ConcatSelf(n
, sz
); return *this; }
407 // append n copies of ch
408 wxStringImpl
& append(size_t n
, wxStringCharType ch
);
409 // append from first to last
410 wxStringImpl
& append(const_iterator first
, const_iterator last
)
411 { ConcatSelf(last
- first
, first
); return *this; }
413 // same as `this_string = str'
414 wxStringImpl
& assign(const wxStringImpl
& str
)
415 { return *this = str
; }
416 // same as ` = str[pos..pos + n]
417 wxStringImpl
& assign(const wxStringImpl
& str
, size_t pos
, size_t n
)
418 { clear(); return append(str
, pos
, n
); }
419 // same as `= first n (or all if n == npos) characters of sz'
420 wxStringImpl
& assign(const wxStringCharType
*sz
)
421 { clear(); return append(sz
, wxStrlen(sz
)); }
422 wxStringImpl
& assign(const wxStringCharType
*sz
, size_t n
)
423 { clear(); return append(sz
, n
); }
424 // same as `= n copies of ch'
425 wxStringImpl
& assign(size_t n
, wxStringCharType ch
)
426 { clear(); return append(n
, ch
); }
427 // assign from first to last
428 wxStringImpl
& assign(const_iterator first
, const_iterator last
)
429 { clear(); return append(first
, last
); }
431 // first valid index position
432 const_iterator
begin() const { return m_pchData
; }
434 // position one after the last valid one
435 const_iterator
end() const { return m_pchData
+ length(); }
438 // insert another string
439 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
441 wxASSERT( str
.GetStringData()->IsValid() );
442 return insert(nPos
, str
.c_str(), str
.length());
444 // insert n chars of str starting at nStart (in str)
445 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
, size_t nStart
, size_t n
)
447 wxASSERT( str
.GetStringData()->IsValid() );
448 wxASSERT( nStart
< str
.length() );
449 size_t strLen
= str
.length() - nStart
;
450 n
= strLen
< n
? strLen
: n
;
451 return insert(nPos
, str
.c_str() + nStart
, n
);
453 // insert first n (or all if n == npos) characters of sz
454 wxStringImpl
& insert(size_t nPos
, const wxStringCharType
*sz
, size_t n
= npos
);
455 // insert n copies of ch
456 wxStringImpl
& insert(size_t nPos
, size_t n
, wxStringCharType ch
)
457 { return insert(nPos
, wxStringImpl(n
, ch
)); }
458 iterator
insert(iterator it
, wxStringCharType ch
)
459 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
460 void insert(iterator it
, const_iterator first
, const_iterator last
)
461 { insert(it
- begin(), first
, last
- first
); }
462 void insert(iterator it
, size_type n
, wxStringCharType ch
)
463 { insert(it
- begin(), n
, ch
); }
465 // delete characters from nStart to nStart + nLen
466 wxStringImpl
& erase(size_type pos
= 0, size_type n
= npos
);
467 iterator
erase(iterator first
, iterator last
)
469 size_t idx
= first
- begin();
470 erase(idx
, last
- first
);
471 return begin() + idx
;
473 iterator
erase(iterator first
);
475 // explicit conversion to C string (use this with printf()!)
476 const wxStringCharType
* c_str() const { return m_pchData
; }
477 const wxStringCharType
* data() const { return m_pchData
; }
479 // replaces the substring of length nLen starting at nStart
480 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringCharType
* sz
);
481 // replaces the substring of length nLen starting at nStart
482 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringImpl
& str
)
483 { return replace(nStart
, nLen
, str
.c_str()); }
484 // replaces the substring with nCount copies of ch
485 wxStringImpl
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxStringCharType ch
);
486 // replaces a substring with another substring
487 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
488 const wxStringImpl
& str
, size_t nStart2
, size_t nLen2
);
489 // replaces the substring with first nCount chars of sz
490 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
491 const wxStringCharType
* sz
, size_t nCount
);
492 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
)
493 { return replace(first
- begin(), last
- first
, s
); }
494 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
,
496 { return replace(first
- begin(), last
- first
, s
, n
); }
497 wxStringImpl
& replace(iterator first
, iterator last
, const wxStringImpl
& s
)
498 { return replace(first
- begin(), last
- first
, s
); }
499 wxStringImpl
& replace(iterator first
, iterator last
, size_type n
, wxStringCharType c
)
500 { return replace(first
- begin(), last
- first
, n
, c
); }
501 wxStringImpl
& replace(iterator first
, iterator last
,
502 const_iterator first1
, const_iterator last1
)
503 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
506 void swap(wxStringImpl
& str
);
508 // All find() functions take the nStart argument which specifies the
509 // position to start the search on, the default value is 0. All functions
510 // return npos if there were no match.
513 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
515 // find first n characters of sz
516 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
518 // find the first occurrence of character ch after nStart
519 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
521 // rfind() family is exactly like find() but works right to left
523 // as find, but from the end
524 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
526 // as find, but from the end
527 size_t rfind(const wxStringCharType
* sz
, size_t nStart
= npos
,
528 size_t n
= npos
) const;
529 // as find, but from the end
530 size_t rfind(wxStringCharType ch
, size_t nStart
= npos
) const;
532 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
534 // substring extraction
535 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
538 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
539 // string += C string
540 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
542 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
544 // helpers for wxStringBuffer and wxStringBufferLength
545 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
546 void DoUngetWriteBuf();
547 void DoUngetWriteBuf(size_t nLen
);
549 friend class WXDLLIMPEXP_BASE wxString
;
552 #endif // !wxUSE_STL_BASED_WXSTRING
554 // don't pollute the library user's name space
555 #undef wxASSERT_VALID_INDEX
557 #endif // _WX_WXSTRINGIMPL_H__