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 #if wxUSE_UNICODE_WCHAR
65 #ifdef HAVE_STD_WSTRING
66 typedef std::wstring wxStdString
;
68 typedef std::basic_string
<wxStringCharType
> wxStdString
;
71 typedef std::string wxStdString
;
74 #endif // need <string>
76 #if wxUSE_STL_BASED_WXSTRING
78 // we always want ctor from std::string when using std::string internally
79 #undef wxUSE_STD_STRING
80 #define wxUSE_STD_STRING 1
82 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
83 (defined(_MSC_VER) && (_MSC_VER <= 1200))
84 #define wxSTRING_BASE_HASNT_CLEAR
87 typedef wxStdString wxStringImpl
;
88 #else // if !wxUSE_STL_BASED_WXSTRING
90 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
91 #undef HAVE_STD_STRING_COMPARE
93 // ---------------------------------------------------------------------------
94 // string data prepended with some housekeeping info (used by wxString class),
95 // is never used directly (but had to be put here to allow inlining)
96 // ---------------------------------------------------------------------------
98 struct WXDLLIMPEXP_BASE wxStringData
100 int nRefs
; // reference count
101 size_t nDataLength
, // actual string length
102 nAllocLength
; // allocated memory size
104 // mimics declaration 'wxStringCharType data[nAllocLength]'
105 wxStringCharType
* data() const { return (wxStringCharType
*)(this + 1); }
107 // empty string has a special ref count so it's never deleted
108 bool IsEmpty() const { return (nRefs
== -1); }
109 bool IsShared() const { return (nRefs
> 1); }
112 void Lock() { if ( !IsEmpty() ) nRefs
++; }
114 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
115 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
118 // VC++ free must take place in same DLL as allocation when using non dll
119 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
120 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
121 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
122 // we must not inline deallocation since allocation is not inlined
125 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
128 // if we had taken control over string memory (GetWriteBuf), it's
129 // intentionally put in invalid state
130 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
131 bool IsValid() const { return (nRefs
!= 0); }
134 class WXDLLIMPEXP_BASE wxStringImpl
137 // an 'invalid' value for string index, moved to this place due to a CW bug
138 static const size_t npos
;
141 // points to data preceded by wxStringData structure with ref count info
142 wxStringCharType
*m_pchData
;
144 // accessor to string data
145 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
147 // string (re)initialization functions
148 // initializes the string to the empty value (must be called only from
149 // ctors, use Reinit() otherwise)
150 #if wxUSE_UNICODE_UTF8
151 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyStringImpl
; } // FIXME-UTF8
153 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
155 // initializes the string with (a part of) C-string
156 void InitWith(const wxStringCharType
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
157 // as Init, but also frees old data
158 void Reinit() { GetStringData()->Unlock(); Init(); }
161 // allocates memory for string of length nLen
162 bool AllocBuffer(size_t nLen
);
163 // effectively copies data to string
164 bool AssignCopy(size_t, const wxStringCharType
*);
166 // append a (sub)string
167 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
, size_t nMaxLen
);
168 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
)
169 { return ConcatSelf(nLen
, src
, nLen
); }
171 // functions called before writing to the string: they copy it if there
172 // are other references to our data (should be the only owner when writing)
173 bool CopyBeforeWrite();
174 bool AllocBeforeWrite(size_t);
176 // compatibility with wxString
177 bool Alloc(size_t nLen
);
181 typedef wxStringCharType value_type
;
182 typedef wxStringCharType char_type
;
183 typedef size_t size_type
;
184 typedef value_type
& reference
;
185 typedef const value_type
& const_reference
;
186 typedef value_type
* pointer
;
187 typedef const value_type
* const_pointer
;
189 // macro to define the bulk of iterator and const_iterator classes
190 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
192 typedef wxStringCharType value_type; \
193 typedef ref_type reference; \
194 typedef ptr_type pointer; \
195 typedef int difference_type; \
197 iterator_name(pointer ptr) : m_ptr(ptr) { } \
199 reference operator*() const { return *m_ptr; } \
201 iterator_name& operator++() { m_ptr++; return *this; } \
202 iterator_name operator++(int) \
204 const iterator_name tmp(*this); \
209 iterator_name& operator--() { m_ptr--; return *this; } \
210 iterator_name operator--(int) \
212 const iterator_name tmp(*this); \
217 iterator_name operator+(int n) const \
218 { return iterator_name(m_ptr + n); } \
219 iterator_name operator+(size_t n) const \
220 { return iterator_name(m_ptr + n); } \
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) \
226 { m_ptr += n; return *this; } \
227 iterator_name& operator+=(size_t n) \
228 { m_ptr += n; return *this; } \
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; } \
234 difference_type operator-(const iterator_name& i) const \
235 { return m_ptr - i.m_ptr; } \
237 bool operator==(const iterator_name& i) const \
238 { return m_ptr == i.m_ptr; } \
239 bool 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; } \
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; } \
252 /* for wxStringImpl use only */ \
253 operator pointer() const { return m_ptr; } \
255 friend class WXDLLIMPEXP_BASE wxStringImpl; \
259 // we need to declare const_iterator in wxStringImpl scope, the friend
260 // declaration inside iterator class itself is not enough, or at least not
261 // for g++ 3.4 (g++ 4 is ok)
262 class const_iterator
;
266 WX_DEFINE_STRINGIMPL_ITERATOR(iterator
,
270 friend class const_iterator
;
276 const_iterator(iterator i
) : m_ptr(i
.m_ptr
) { }
278 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator
,
279 const wxStringCharType
&,
280 const wxStringCharType
*);
283 #undef WX_DEFINE_STRINGIMPL_ITERATOR
286 // constructors and destructor
287 // ctor for an empty string
288 wxStringImpl() { Init(); }
290 wxStringImpl(const wxStringImpl
& stringSrc
)
292 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
293 _T("did you forget to call UngetWriteBuf()?") );
295 if ( stringSrc
.empty() ) {
296 // nothing to do for an empty string
300 m_pchData
= stringSrc
.m_pchData
; // share same data
301 GetStringData()->Lock(); // => one more copy
304 // string containing nRepeat copies of ch
305 wxStringImpl(size_type nRepeat
, wxStringCharType ch
);
306 // ctor takes first nLength characters from C string
307 // (default value of npos means take all the string)
308 wxStringImpl(const wxStringCharType
*psz
)
309 { InitWith(psz
, 0, npos
); }
310 wxStringImpl(const wxStringCharType
*psz
, size_t nLength
)
311 { InitWith(psz
, 0, nLength
); }
312 // take nLen chars starting at nPos
313 wxStringImpl(const wxStringImpl
& str
, size_t nPos
, size_t nLen
)
315 wxASSERT_MSG( str
.GetStringData()->IsValid(),
316 _T("did you forget to call UngetWriteBuf()?") );
318 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
319 InitWith(str
.c_str(), nPos
, nLen
);
321 // take everything between start and end
322 wxStringImpl(const_iterator start
, const_iterator end
);
324 // dtor is not virtual, this class must not be inherited from!
327 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
328 //RN - according to the above VC++ does indeed inline this,
329 //even though it spits out two warnings
330 #pragma warning (disable:4714)
333 GetStringData()->Unlock();
336 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
337 //re-enable inlining warning
338 #pragma warning (default:4714)
340 // overloaded assignment
341 // from another wxString
342 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
344 wxStringImpl
& operator=(wxStringCharType ch
);
346 wxStringImpl
& operator=(const wxStringCharType
*psz
);
348 // return the length of the string
349 size_type
length() const { return GetStringData()->nDataLength
; }
350 // return the length of the string
351 size_type
size() const { return length(); }
352 // return the maximum size of the string
353 size_type
max_size() const { return npos
; }
354 // resize the string, filling the space with c if c != 0
355 void resize(size_t nSize
, wxStringCharType ch
= '\0');
356 // delete the contents of the string
357 void clear() { erase(0, npos
); }
358 // returns true if the string is empty
359 bool empty() const { return length() == 0; }
360 // inform string about planned change in size
361 void reserve(size_t sz
) { Alloc(sz
); }
362 size_type
capacity() const { return GetStringData()->nAllocLength
; }
365 // return the character at position n
366 value_type
at(size_type n
) const
367 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
368 // returns the writable character at position n
369 reference
at(size_type n
)
371 wxASSERT_VALID_INDEX( n
);
374 } // FIXME-UTF8: not useful for us...?
376 // lib.string.modifiers
377 // append elements str[pos], ..., str[pos+n]
378 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
380 wxASSERT(pos
<= str
.length());
381 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
385 wxStringImpl
& append(const wxStringImpl
& str
)
386 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
387 // append first n (or all if n == npos) characters of sz
388 wxStringImpl
& append(const wxStringCharType
*sz
)
389 { ConcatSelf(Strsize(sz
), sz
); return *this; }
390 wxStringImpl
& append(const wxStringCharType
*sz
, size_t n
)
391 { ConcatSelf(n
, sz
); return *this; }
392 // append n copies of ch
393 wxStringImpl
& append(size_t n
, wxStringCharType ch
);
394 // append from first to last
395 wxStringImpl
& append(const_iterator first
, const_iterator last
)
396 { ConcatSelf(last
- first
, first
); return *this; }
398 // same as `this_string = str'
399 wxStringImpl
& assign(const wxStringImpl
& str
)
400 { return *this = str
; }
401 // same as ` = str[pos..pos + n]
402 wxStringImpl
& assign(const wxStringImpl
& str
, size_t pos
, size_t n
)
403 { clear(); return append(str
, pos
, n
); }
404 // same as `= first n (or all if n == npos) characters of sz'
405 wxStringImpl
& assign(const wxStringCharType
*sz
)
406 { clear(); return append(sz
, Strsize(sz
)); }
407 wxStringImpl
& assign(const wxStringCharType
*sz
, size_t n
)
408 { clear(); return append(sz
, n
); }
409 // same as `= n copies of ch'
410 wxStringImpl
& assign(size_t n
, wxStringCharType ch
)
411 { clear(); return append(n
, ch
); }
412 // assign from first to last
413 wxStringImpl
& assign(const_iterator first
, const_iterator last
)
414 { clear(); return append(first
, last
); }
416 // first valid index position
417 const_iterator
begin() const { return m_pchData
; }
419 // position one after the last valid one
420 const_iterator
end() const { return m_pchData
+ length(); }
423 // insert another string
424 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
426 wxASSERT( str
.GetStringData()->IsValid() );
427 return insert(nPos
, str
.c_str(), str
.length());
429 // insert n chars of str starting at nStart (in str)
430 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
, size_t nStart
, size_t n
)
432 wxASSERT( str
.GetStringData()->IsValid() );
433 wxASSERT( nStart
< str
.length() );
434 size_t strLen
= str
.length() - nStart
;
435 n
= strLen
< n
? strLen
: n
;
436 return insert(nPos
, str
.c_str() + nStart
, n
);
438 // insert first n (or all if n == npos) characters of sz
439 wxStringImpl
& insert(size_t nPos
, const wxStringCharType
*sz
, size_t n
= npos
);
440 // insert n copies of ch
441 wxStringImpl
& insert(size_t nPos
, size_t n
, wxStringCharType ch
)
442 { return insert(nPos
, wxStringImpl(n
, ch
)); }
443 iterator
insert(iterator it
, wxStringCharType ch
)
444 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
445 void insert(iterator it
, const_iterator first
, const_iterator last
)
446 { insert(it
- begin(), first
, last
- first
); }
447 void insert(iterator it
, size_type n
, wxStringCharType ch
)
448 { insert(it
- begin(), n
, ch
); }
450 // delete characters from nStart to nStart + nLen
451 wxStringImpl
& erase(size_type pos
= 0, size_type n
= npos
);
452 iterator
erase(iterator first
, iterator last
)
454 size_t idx
= first
- begin();
455 erase(idx
, last
- first
);
456 return begin() + idx
;
458 iterator
erase(iterator first
);
460 // explicit conversion to C string (use this with printf()!)
461 const wxStringCharType
* c_str() const { return m_pchData
; }
462 const wxStringCharType
* data() const { return m_pchData
; }
464 // replaces the substring of length nLen starting at nStart
465 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringCharType
* sz
);
466 // replaces the substring of length nLen starting at nStart
467 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringImpl
& str
)
468 { return replace(nStart
, nLen
, str
.c_str()); }
469 // replaces the substring with nCount copies of ch
470 wxStringImpl
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxStringCharType ch
);
471 // replaces a substring with another substring
472 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
473 const wxStringImpl
& str
, size_t nStart2
, size_t nLen2
);
474 // replaces the substring with first nCount chars of sz
475 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
476 const wxStringCharType
* sz
, size_t nCount
);
477 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
)
478 { return replace(first
- begin(), last
- first
, s
); }
479 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
,
481 { return replace(first
- begin(), last
- first
, s
, n
); }
482 wxStringImpl
& replace(iterator first
, iterator last
, const wxStringImpl
& s
)
483 { return replace(first
- begin(), last
- first
, s
); }
484 wxStringImpl
& replace(iterator first
, iterator last
, size_type n
, wxStringCharType c
)
485 { return replace(first
- begin(), last
- first
, n
, c
); }
486 wxStringImpl
& replace(iterator first
, iterator last
,
487 const_iterator first1
, const_iterator last1
)
488 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
491 void swap(wxStringImpl
& str
);
493 // All find() functions take the nStart argument which specifies the
494 // position to start the search on, the default value is 0. All functions
495 // return npos if there were no match.
498 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
500 // find first n characters of sz
501 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
503 // find the first occurrence of character ch after nStart
504 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
506 // rfind() family is exactly like find() but works right to left
508 // as find, but from the end
509 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
511 // as find, but from the end
512 size_t rfind(const wxStringCharType
* sz
, size_t nStart
= npos
,
513 size_t n
= npos
) const;
514 // as find, but from the end
515 size_t rfind(wxStringCharType ch
, size_t nStart
= npos
) const;
517 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
519 // substring extraction
520 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
523 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
524 // string += C string
525 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
527 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
529 #if !wxUSE_UNICODE_UTF8
530 // helpers for wxStringBuffer and wxStringBufferLength
531 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
532 void DoUngetWriteBuf();
533 void DoUngetWriteBuf(size_t nLen
);
537 #if wxUSE_UNICODE_UTF8
538 static size_t Strsize(const wxStringCharType
*s
) { return strlen(s
); }
540 static size_t Strsize(const wxStringCharType
*s
) { return wxStrlen(s
); }
543 friend class WXDLLIMPEXP_BASE wxString
;
546 #endif // !wxUSE_STL_BASED_WXSTRING
548 // don't pollute the library user's name space
549 #undef wxASSERT_VALID_INDEX
551 #endif // _WX_WXSTRINGIMPL_H__