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() : m_ptr(NULL) { } \
202 iterator_name(pointer ptr) : m_ptr(ptr) { } \
204 reference operator*() const { return *m_ptr; } \
206 iterator_name& operator++() { m_ptr++; return *this; } \
207 iterator_name operator++(int) \
209 const iterator_name tmp(*this); \
214 iterator_name& operator--() { m_ptr--; return *this; } \
215 iterator_name operator--(int) \
217 const iterator_name tmp(*this); \
222 iterator_name operator+(int n) const \
223 { return iterator_name(m_ptr + n); } \
224 iterator_name operator+(size_t n) const \
225 { return iterator_name(m_ptr + n); } \
226 iterator_name operator-(int n) const \
227 { return iterator_name(m_ptr - n); } \
228 iterator_name operator-(size_t n) const \
229 { return iterator_name(m_ptr - n); } \
230 iterator_name& operator+=(int n) \
231 { m_ptr += n; return *this; } \
232 iterator_name& operator+=(size_t n) \
233 { m_ptr += n; return *this; } \
234 iterator_name& operator-=(int n) \
235 { m_ptr -= n; return *this; } \
236 iterator_name& operator-=(size_t n) \
237 { m_ptr -= n; return *this; } \
239 difference_type operator-(const iterator_name& i) const \
240 { return m_ptr - i.m_ptr; } \
242 bool operator==(const iterator_name& i) const \
243 { return m_ptr == i.m_ptr; } \
244 bool operator!=(const iterator_name& i) const \
245 { return m_ptr != i.m_ptr; } \
247 bool operator<(const iterator_name& i) const \
248 { return m_ptr < i.m_ptr; } \
249 bool operator>(const iterator_name& i) const \
250 { return m_ptr > i.m_ptr; } \
251 bool operator<=(const iterator_name& i) const \
252 { return m_ptr <= i.m_ptr; } \
253 bool operator>=(const iterator_name& i) const \
254 { return m_ptr >= i.m_ptr; } \
257 /* for wxStringImpl use only */ \
258 operator pointer() const { return m_ptr; } \
260 friend class WXDLLIMPEXP_BASE wxStringImpl; \
264 // we need to declare const_iterator in wxStringImpl scope, the friend
265 // declaration inside iterator class itself is not enough, or at least not
266 // for g++ 3.4 (g++ 4 is ok)
267 class WXDLLIMPEXP_BASE const_iterator
;
269 class WXDLLIMPEXP_BASE iterator
271 WX_DEFINE_STRINGIMPL_ITERATOR(iterator
,
275 friend class const_iterator
;
278 class WXDLLIMPEXP_BASE const_iterator
281 const_iterator(iterator i
) : m_ptr(i
.m_ptr
) { }
283 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator
,
284 const wxStringCharType
&,
285 const wxStringCharType
*);
288 #undef WX_DEFINE_STRINGIMPL_ITERATOR
291 // constructors and destructor
292 // ctor for an empty string
293 wxStringImpl() { Init(); }
295 wxStringImpl(const wxStringImpl
& stringSrc
)
297 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
298 _T("did you forget to call UngetWriteBuf()?") );
300 if ( stringSrc
.empty() ) {
301 // nothing to do for an empty string
305 m_pchData
= stringSrc
.m_pchData
; // share same data
306 GetStringData()->Lock(); // => one more copy
309 // string containing nRepeat copies of ch
310 wxStringImpl(size_type nRepeat
, wxStringCharType ch
);
311 // ctor takes first nLength characters from C string
312 // (default value of npos means take all the string)
313 wxStringImpl(const wxStringCharType
*psz
)
314 { InitWith(psz
, 0, npos
); }
315 wxStringImpl(const wxStringCharType
*psz
, size_t nLength
)
316 { InitWith(psz
, 0, nLength
); }
317 // take nLen chars starting at nPos
318 wxStringImpl(const wxStringImpl
& str
, size_t nPos
, size_t nLen
)
320 wxASSERT_MSG( str
.GetStringData()->IsValid(),
321 _T("did you forget to call UngetWriteBuf()?") );
323 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
324 InitWith(str
.c_str(), nPos
, nLen
);
326 // take everything between start and end
327 wxStringImpl(const_iterator start
, const_iterator end
);
330 // ctor from and conversion to std::string
332 wxStringImpl(const wxStdString
& impl
)
333 { InitWith(impl
.c_str(), 0, impl
.length()); }
335 operator wxStdString() const
336 { return wxStdString(c_str(), length()); }
340 // dtor is not virtual, this class must not be inherited from!
343 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
344 //RN - according to the above VC++ does indeed inline this,
345 //even though it spits out two warnings
346 #pragma warning (disable:4714)
349 GetStringData()->Unlock();
352 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
353 //re-enable inlining warning
354 #pragma warning (default:4714)
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
at(size_type n
) const
383 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
384 // returns the writable character at position n
385 reference
at(size_type n
)
387 wxASSERT_VALID_INDEX( n
);
390 } // FIXME-UTF8: not useful for us...?
392 // lib.string.modifiers
393 // append elements str[pos], ..., str[pos+n]
394 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
396 wxASSERT(pos
<= str
.length());
397 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
401 wxStringImpl
& append(const wxStringImpl
& str
)
402 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
403 // append first n (or all if n == npos) characters of sz
404 wxStringImpl
& append(const wxStringCharType
*sz
)
405 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
406 wxStringImpl
& append(const wxStringCharType
*sz
, size_t n
)
407 { ConcatSelf(n
, sz
); return *this; }
408 // append n copies of ch
409 wxStringImpl
& append(size_t n
, wxStringCharType ch
);
410 // append from first to last
411 wxStringImpl
& append(const_iterator first
, const_iterator last
)
412 { ConcatSelf(last
- first
, first
); return *this; }
414 // same as `this_string = str'
415 wxStringImpl
& assign(const wxStringImpl
& str
)
416 { return *this = str
; }
417 // same as ` = str[pos..pos + n]
418 wxStringImpl
& assign(const wxStringImpl
& str
, size_t pos
, size_t n
)
419 { clear(); return append(str
, pos
, n
); }
420 // same as `= first n (or all if n == npos) characters of sz'
421 wxStringImpl
& assign(const wxStringCharType
*sz
)
422 { clear(); return append(sz
, wxStrlen(sz
)); }
423 wxStringImpl
& assign(const wxStringCharType
*sz
, size_t n
)
424 { clear(); return append(sz
, n
); }
425 // same as `= n copies of ch'
426 wxStringImpl
& assign(size_t n
, wxStringCharType ch
)
427 { clear(); return append(n
, ch
); }
428 // assign from first to last
429 wxStringImpl
& assign(const_iterator first
, const_iterator last
)
430 { clear(); return append(first
, last
); }
432 // first valid index position
433 const_iterator
begin() const { return m_pchData
; }
435 // position one after the last valid one
436 const_iterator
end() const { return m_pchData
+ length(); }
439 // insert another string
440 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
442 wxASSERT( str
.GetStringData()->IsValid() );
443 return insert(nPos
, str
.c_str(), str
.length());
445 // insert n chars of str starting at nStart (in str)
446 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
, size_t nStart
, size_t n
)
448 wxASSERT( str
.GetStringData()->IsValid() );
449 wxASSERT( nStart
< str
.length() );
450 size_t strLen
= str
.length() - nStart
;
451 n
= strLen
< n
? strLen
: n
;
452 return insert(nPos
, str
.c_str() + nStart
, n
);
454 // insert first n (or all if n == npos) characters of sz
455 wxStringImpl
& insert(size_t nPos
, const wxStringCharType
*sz
, size_t n
= npos
);
456 // insert n copies of ch
457 wxStringImpl
& insert(size_t nPos
, size_t n
, wxStringCharType ch
)
458 { return insert(nPos
, wxStringImpl(n
, ch
)); }
459 iterator
insert(iterator it
, wxStringCharType ch
)
460 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
461 void insert(iterator it
, const_iterator first
, const_iterator last
)
462 { insert(it
- begin(), first
, last
- first
); }
463 void insert(iterator it
, size_type n
, wxStringCharType ch
)
464 { insert(it
- begin(), n
, ch
); }
466 // delete characters from nStart to nStart + nLen
467 wxStringImpl
& erase(size_type pos
= 0, size_type n
= npos
);
468 iterator
erase(iterator first
, iterator last
)
470 size_t idx
= first
- begin();
471 erase(idx
, last
- first
);
472 return begin() + idx
;
474 iterator
erase(iterator first
);
476 // explicit conversion to C string (use this with printf()!)
477 const wxStringCharType
* c_str() const { return m_pchData
; }
478 const wxStringCharType
* data() const { return m_pchData
; }
480 // replaces the substring of length nLen starting at nStart
481 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringCharType
* sz
);
482 // replaces the substring of length nLen starting at nStart
483 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringImpl
& str
)
484 { return replace(nStart
, nLen
, str
.c_str()); }
485 // replaces the substring with nCount copies of ch
486 wxStringImpl
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxStringCharType ch
);
487 // replaces a substring with another substring
488 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
489 const wxStringImpl
& str
, size_t nStart2
, size_t nLen2
);
490 // replaces the substring with first nCount chars of sz
491 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
492 const wxStringCharType
* sz
, size_t nCount
);
493 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
)
494 { return replace(first
- begin(), last
- first
, s
); }
495 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
,
497 { return replace(first
- begin(), last
- first
, s
, n
); }
498 wxStringImpl
& replace(iterator first
, iterator last
, const wxStringImpl
& s
)
499 { return replace(first
- begin(), last
- first
, s
); }
500 wxStringImpl
& replace(iterator first
, iterator last
, size_type n
, wxStringCharType c
)
501 { return replace(first
- begin(), last
- first
, n
, c
); }
502 wxStringImpl
& replace(iterator first
, iterator last
,
503 const_iterator first1
, const_iterator last1
)
504 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
507 void swap(wxStringImpl
& str
);
509 // All find() functions take the nStart argument which specifies the
510 // position to start the search on, the default value is 0. All functions
511 // return npos if there were no match.
514 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
516 // find first n characters of sz
517 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
519 // find the first occurrence of character ch after nStart
520 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
522 // rfind() family is exactly like find() but works right to left
524 // as find, but from the end
525 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
527 // as find, but from the end
528 size_t rfind(const wxStringCharType
* sz
, size_t nStart
= npos
,
529 size_t n
= npos
) const;
530 // as find, but from the end
531 size_t rfind(wxStringCharType ch
, size_t nStart
= npos
) const;
533 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
535 // substring extraction
536 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
539 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
540 // string += C string
541 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
543 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
545 // helpers for wxStringBuffer and wxStringBufferLength
546 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
547 void DoUngetWriteBuf();
548 void DoUngetWriteBuf(size_t nLen
);
550 friend class WXDLLIMPEXP_BASE wxString
;
553 #endif // !wxUSE_STL_BASED_WXSTRING
555 // don't pollute the library user's name space
556 #undef wxASSERT_VALID_INDEX
558 #endif // _WX_WXSTRINGIMPL_H__