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/wxchar.h" // for wxChar, wxStrlen() etc.
30 // ---------------------------------------------------------------------------
32 // ---------------------------------------------------------------------------
34 // implementation only
35 #define wxASSERT_VALID_INDEX(i) \
36 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // global pointer to empty string
44 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
45 #if wxUSE_UNICODE_UTF8
46 // FIXME-UTF8: we should have only one wxEmptyString
47 extern WXDLLIMPEXP_DATA_BASE(const wxStringCharType
*) wxEmptyStringImpl
;
51 // ----------------------------------------------------------------------------
52 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
53 // ----------------------------------------------------------------------------
55 #define wxUSE_STL_BASED_WXSTRING wxUSE_STL
57 // in both cases we need to define wxStdString
58 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
60 #include "wx/beforestd.h"
62 #include "wx/afterstd.h"
64 #ifdef HAVE_STD_WSTRING
65 typedef std::wstring wxStdWideString
;
67 typedef std::basic_string
<wchar_t> wxStdWideString
;
70 #if wxUSE_UNICODE_WCHAR
71 typedef wxStdWideString wxStdString
;
73 typedef std::string wxStdString
;
76 #endif // wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
79 #if wxUSE_STL_BASED_WXSTRING
81 // we always want ctor from std::string when using std::string internally
82 #undef wxUSE_STD_STRING
83 #define wxUSE_STD_STRING 1
85 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
86 (defined(_MSC_VER) && (_MSC_VER <= 1200))
87 #define wxSTRING_BASE_HASNT_CLEAR
90 typedef wxStdString wxStringImpl
;
91 #else // if !wxUSE_STL_BASED_WXSTRING
93 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
94 #undef HAVE_STD_STRING_COMPARE
96 // ---------------------------------------------------------------------------
97 // string data prepended with some housekeeping info (used by wxString class),
98 // is never used directly (but had to be put here to allow inlining)
99 // ---------------------------------------------------------------------------
101 struct WXDLLIMPEXP_BASE wxStringData
103 int nRefs
; // reference count
104 size_t nDataLength
, // actual string length
105 nAllocLength
; // allocated memory size
107 // mimics declaration 'wxStringCharType data[nAllocLength]'
108 wxStringCharType
* data() const { return (wxStringCharType
*)(this + 1); }
110 // empty string has a special ref count so it's never deleted
111 bool IsEmpty() const { return (nRefs
== -1); }
112 bool IsShared() const { return (nRefs
> 1); }
115 void Lock() { if ( !IsEmpty() ) nRefs
++; }
117 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
118 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
121 // VC++ free must take place in same DLL as allocation when using non dll
122 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
123 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
124 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
125 // we must not inline deallocation since allocation is not inlined
128 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
131 // if we had taken control over string memory (GetWriteBuf), it's
132 // intentionally put in invalid state
133 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
134 bool IsValid() const { return (nRefs
!= 0); }
137 class WXDLLIMPEXP_BASE wxStringImpl
140 // an 'invalid' value for string index, moved to this place due to a CW bug
141 static const size_t npos
;
144 // points to data preceded by wxStringData structure with ref count info
145 wxStringCharType
*m_pchData
;
147 // accessor to string data
148 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
150 // string (re)initialization functions
151 // initializes the string to the empty value (must be called only from
152 // ctors, use Reinit() otherwise)
153 #if wxUSE_UNICODE_UTF8
154 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyStringImpl
; } // FIXME-UTF8
156 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
158 // initializes the string with (a part of) C-string
159 void InitWith(const wxStringCharType
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
160 // as Init, but also frees old data
161 void Reinit() { GetStringData()->Unlock(); Init(); }
164 // allocates memory for string of length nLen
165 bool AllocBuffer(size_t nLen
);
166 // effectively copies data to string
167 bool AssignCopy(size_t, const wxStringCharType
*);
169 // append a (sub)string
170 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
, size_t nMaxLen
);
171 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
)
172 { return ConcatSelf(nLen
, src
, nLen
); }
174 // functions called before writing to the string: they copy it if there
175 // are other references to our data (should be the only owner when writing)
176 bool CopyBeforeWrite();
177 bool AllocBeforeWrite(size_t);
179 // compatibility with wxString
180 bool Alloc(size_t nLen
);
184 typedef wxStringCharType value_type
;
185 typedef wxStringCharType char_type
;
186 typedef size_t size_type
;
187 typedef value_type
& reference
;
188 typedef const value_type
& const_reference
;
189 typedef value_type
* pointer
;
190 typedef const value_type
* const_pointer
;
192 // macro to define the bulk of iterator and const_iterator classes
193 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
195 typedef wxStringCharType value_type; \
196 typedef ref_type reference; \
197 typedef ptr_type pointer; \
198 typedef int difference_type; \
200 iterator_name(pointer ptr) : m_ptr(ptr) { } \
202 reference operator*() const { return *m_ptr; } \
204 iterator_name& operator++() { m_ptr++; return *this; } \
205 iterator_name operator++(int) \
207 const iterator_name tmp(*this); \
212 iterator_name& operator--() { m_ptr--; return *this; } \
213 iterator_name operator--(int) \
215 const iterator_name tmp(*this); \
220 iterator_name operator+(int n) const \
221 { return iterator_name(m_ptr + n); } \
222 iterator_name operator+(size_t n) const \
223 { return iterator_name(m_ptr + n); } \
224 iterator_name operator-(int n) const \
225 { return iterator_name(m_ptr - n); } \
226 iterator_name operator-(size_t n) const \
227 { return iterator_name(m_ptr - n); } \
228 iterator_name& operator+=(int n) \
229 { m_ptr += n; return *this; } \
230 iterator_name& operator+=(size_t n) \
231 { m_ptr += n; return *this; } \
232 iterator_name& operator-=(int n) \
233 { m_ptr -= n; return *this; } \
234 iterator_name& operator-=(size_t n) \
235 { m_ptr -= n; return *this; } \
237 difference_type operator-(const iterator_name& i) const \
238 { return m_ptr - i.m_ptr; } \
240 bool operator==(const iterator_name& i) const \
241 { return m_ptr == i.m_ptr; } \
242 bool 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; } \
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; } \
255 /* for wxStringImpl use only */ \
256 operator pointer() const { return m_ptr; } \
258 friend class WXDLLIMPEXP_BASE wxStringImpl; \
262 // we need to declare const_iterator in wxStringImpl scope, the friend
263 // declaration inside iterator class itself is not enough, or at least not
264 // for g++ 3.4 (g++ 4 is ok)
265 class const_iterator
;
269 WX_DEFINE_STRINGIMPL_ITERATOR(iterator
,
273 friend class const_iterator
;
279 const_iterator(iterator i
) : m_ptr(i
.m_ptr
) { }
281 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator
,
282 const wxStringCharType
&,
283 const wxStringCharType
*);
286 #undef WX_DEFINE_STRINGIMPL_ITERATOR
289 // constructors and destructor
290 // ctor for an empty string
291 wxStringImpl() { Init(); }
293 wxStringImpl(const wxStringImpl
& stringSrc
)
295 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
296 _T("did you forget to call UngetWriteBuf()?") );
298 if ( stringSrc
.empty() ) {
299 // nothing to do for an empty string
303 m_pchData
= stringSrc
.m_pchData
; // share same data
304 GetStringData()->Lock(); // => one more copy
307 // string containing nRepeat copies of ch
308 wxStringImpl(size_type nRepeat
, wxStringCharType ch
);
309 // ctor takes first nLength characters from C string
310 // (default value of npos means take all the string)
311 wxStringImpl(const wxStringCharType
*psz
)
312 { InitWith(psz
, 0, npos
); }
313 wxStringImpl(const wxStringCharType
*psz
, size_t nLength
)
314 { InitWith(psz
, 0, nLength
); }
315 // take nLen chars starting at nPos
316 wxStringImpl(const wxStringImpl
& str
, size_t nPos
, size_t nLen
)
318 wxASSERT_MSG( str
.GetStringData()->IsValid(),
319 _T("did you forget to call UngetWriteBuf()?") );
321 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
322 InitWith(str
.c_str(), nPos
, nLen
);
324 // take everything between start and end
325 wxStringImpl(const_iterator start
, const_iterator end
);
328 // ctor from and conversion to std::string
330 wxStringImpl(const wxStdString
& impl
)
331 { InitWith(impl
.c_str(), 0, impl
.length()); }
333 operator wxStdString() const
334 { return wxStdString(c_str(), length()); }
338 // dtor is not virtual, this class must not be inherited from!
341 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
342 //RN - according to the above VC++ does indeed inline this,
343 //even though it spits out two warnings
344 #pragma warning (disable:4714)
347 GetStringData()->Unlock();
350 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
351 //re-enable inlining warning
352 #pragma warning (default:4714)
354 // overloaded assignment
355 // from another wxString
356 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
358 wxStringImpl
& operator=(wxStringCharType ch
);
360 wxStringImpl
& operator=(const wxStringCharType
*psz
);
362 // return the length of the string
363 size_type
length() const { return GetStringData()->nDataLength
; }
364 // return the length of the string
365 size_type
size() const { return length(); }
366 // return the maximum size of the string
367 size_type
max_size() const { return npos
; }
368 // resize the string, filling the space with c if c != 0
369 void resize(size_t nSize
, wxStringCharType ch
= '\0');
370 // delete the contents of the string
371 void clear() { erase(0, npos
); }
372 // returns true if the string is empty
373 bool empty() const { return length() == 0; }
374 // inform string about planned change in size
375 void reserve(size_t sz
) { Alloc(sz
); }
376 size_type
capacity() const { return GetStringData()->nAllocLength
; }
379 // return the character at position n
380 value_type
at(size_type n
) const
381 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
382 // returns the writable character at position n
383 reference
at(size_type n
)
385 wxASSERT_VALID_INDEX( n
);
388 } // FIXME-UTF8: not useful for us...?
390 // lib.string.modifiers
391 // append elements str[pos], ..., str[pos+n]
392 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
394 wxASSERT(pos
<= str
.length());
395 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
399 wxStringImpl
& append(const wxStringImpl
& str
)
400 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
401 // append first n (or all if n == npos) characters of sz
402 wxStringImpl
& append(const wxStringCharType
*sz
)
403 { ConcatSelf(Strsize(sz
), sz
); return *this; }
404 wxStringImpl
& append(const wxStringCharType
*sz
, size_t n
)
405 { ConcatSelf(n
, sz
); return *this; }
406 // append n copies of ch
407 wxStringImpl
& append(size_t n
, wxStringCharType ch
);
408 // append from first to last
409 wxStringImpl
& append(const_iterator first
, const_iterator last
)
410 { ConcatSelf(last
- first
, first
); return *this; }
412 // same as `this_string = str'
413 wxStringImpl
& assign(const wxStringImpl
& str
)
414 { return *this = str
; }
415 // same as ` = str[pos..pos + n]
416 wxStringImpl
& assign(const wxStringImpl
& str
, size_t pos
, size_t n
)
417 { clear(); return append(str
, pos
, n
); }
418 // same as `= first n (or all if n == npos) characters of sz'
419 wxStringImpl
& assign(const wxStringCharType
*sz
)
420 { clear(); return append(sz
, Strsize(sz
)); }
421 wxStringImpl
& assign(const wxStringCharType
*sz
, size_t n
)
422 { clear(); return append(sz
, n
); }
423 // same as `= n copies of ch'
424 wxStringImpl
& assign(size_t n
, wxStringCharType ch
)
425 { clear(); return append(n
, ch
); }
426 // assign from first to last
427 wxStringImpl
& assign(const_iterator first
, const_iterator last
)
428 { clear(); return append(first
, last
); }
430 // first valid index position
431 const_iterator
begin() const { return m_pchData
; }
433 // position one after the last valid one
434 const_iterator
end() const { return m_pchData
+ length(); }
437 // insert another string
438 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
440 wxASSERT( str
.GetStringData()->IsValid() );
441 return insert(nPos
, str
.c_str(), str
.length());
443 // insert n chars of str starting at nStart (in str)
444 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
, size_t nStart
, size_t n
)
446 wxASSERT( str
.GetStringData()->IsValid() );
447 wxASSERT( nStart
< str
.length() );
448 size_t strLen
= str
.length() - nStart
;
449 n
= strLen
< n
? strLen
: n
;
450 return insert(nPos
, str
.c_str() + nStart
, n
);
452 // insert first n (or all if n == npos) characters of sz
453 wxStringImpl
& insert(size_t nPos
, const wxStringCharType
*sz
, size_t n
= npos
);
454 // insert n copies of ch
455 wxStringImpl
& insert(size_t nPos
, size_t n
, wxStringCharType ch
)
456 { return insert(nPos
, wxStringImpl(n
, ch
)); }
457 iterator
insert(iterator it
, wxStringCharType ch
)
458 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
459 void insert(iterator it
, const_iterator first
, const_iterator last
)
460 { insert(it
- begin(), first
, last
- first
); }
461 void insert(iterator it
, size_type n
, wxStringCharType ch
)
462 { insert(it
- begin(), n
, ch
); }
464 // delete characters from nStart to nStart + nLen
465 wxStringImpl
& erase(size_type pos
= 0, size_type n
= npos
);
466 iterator
erase(iterator first
, iterator last
)
468 size_t idx
= first
- begin();
469 erase(idx
, last
- first
);
470 return begin() + idx
;
472 iterator
erase(iterator first
);
474 // explicit conversion to C string (use this with printf()!)
475 const wxStringCharType
* c_str() const { return m_pchData
; }
476 const wxStringCharType
* data() const { return m_pchData
; }
478 // replaces the substring of length nLen starting at nStart
479 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringCharType
* sz
);
480 // replaces the substring of length nLen starting at nStart
481 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringImpl
& str
)
482 { return replace(nStart
, nLen
, str
.c_str()); }
483 // replaces the substring with nCount copies of ch
484 wxStringImpl
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxStringCharType ch
);
485 // replaces a substring with another substring
486 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
487 const wxStringImpl
& str
, size_t nStart2
, size_t nLen2
);
488 // replaces the substring with first nCount chars of sz
489 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
490 const wxStringCharType
* sz
, size_t nCount
);
491 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
)
492 { return replace(first
- begin(), last
- first
, s
); }
493 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
,
495 { return replace(first
- begin(), last
- first
, s
, n
); }
496 wxStringImpl
& replace(iterator first
, iterator last
, const wxStringImpl
& s
)
497 { return replace(first
- begin(), last
- first
, s
); }
498 wxStringImpl
& replace(iterator first
, iterator last
, size_type n
, wxStringCharType c
)
499 { return replace(first
- begin(), last
- first
, n
, c
); }
500 wxStringImpl
& replace(iterator first
, iterator last
,
501 const_iterator first1
, const_iterator last1
)
502 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
505 void swap(wxStringImpl
& str
);
507 // All find() functions take the nStart argument which specifies the
508 // position to start the search on, the default value is 0. All functions
509 // return npos if there were no match.
512 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
514 // find first n characters of sz
515 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
517 // find the first occurrence of character ch after nStart
518 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
520 // rfind() family is exactly like find() but works right to left
522 // as find, but from the end
523 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
525 // as find, but from the end
526 size_t rfind(const wxStringCharType
* sz
, size_t nStart
= npos
,
527 size_t n
= npos
) const;
528 // as find, but from the end
529 size_t rfind(wxStringCharType ch
, size_t nStart
= npos
) const;
531 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
533 // substring extraction
534 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
537 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
538 // string += C string
539 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
541 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
543 #if !wxUSE_UNICODE_UTF8
544 // helpers for wxStringBuffer and wxStringBufferLength
545 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
546 void DoUngetWriteBuf();
547 void DoUngetWriteBuf(size_t nLen
);
551 #if wxUSE_UNICODE_UTF8
552 static size_t Strsize(const wxStringCharType
*s
) { return strlen(s
); }
554 static size_t Strsize(const wxStringCharType
*s
) { return wxStrlen(s
); }
557 friend class WXDLLIMPEXP_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__