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
;
47 // ----------------------------------------------------------------------------
48 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
49 // ----------------------------------------------------------------------------
51 #define wxUSE_STL_BASED_WXSTRING wxUSE_STL
53 // in both cases we need to define wxStdString
54 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
56 #include "wx/beforestd.h"
58 #include "wx/afterstd.h"
60 #if wxUSE_UNICODE_WCHAR
61 #ifdef HAVE_STD_WSTRING
62 typedef std::wstring wxStdString
;
64 typedef std::basic_string
<wxChar
> wxStdString
;
67 typedef std::string wxStdString
;
70 #endif // need <string>
72 #if wxUSE_STL_BASED_WXSTRING
74 // we always want ctor from std::string when using std::string internally
75 #undef wxUSE_STD_STRING
76 #define wxUSE_STD_STRING 1
78 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
79 (defined(_MSC_VER) && (_MSC_VER <= 1200))
80 #define wxSTRING_BASE_HASNT_CLEAR
83 typedef wxStdString wxStringImpl
;
84 #else // if !wxUSE_STL_BASED_WXSTRING
86 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
87 #undef HAVE_STD_STRING_COMPARE
89 // ---------------------------------------------------------------------------
90 // string data prepended with some housekeeping info (used by wxString class),
91 // is never used directly (but had to be put here to allow inlining)
92 // ---------------------------------------------------------------------------
94 struct WXDLLIMPEXP_BASE wxStringData
96 int nRefs
; // reference count
97 size_t nDataLength
, // actual string length
98 nAllocLength
; // allocated memory size
100 // mimics declaration 'wxChar data[nAllocLength]'
101 wxChar
* data() const { return (wxChar
*)(this + 1); }
103 // empty string has a special ref count so it's never deleted
104 bool IsEmpty() const { return (nRefs
== -1); }
105 bool IsShared() const { return (nRefs
> 1); }
108 void Lock() { if ( !IsEmpty() ) nRefs
++; }
110 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
111 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
114 // VC++ free must take place in same DLL as allocation when using non dll
115 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
116 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
117 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
118 // we must not inline deallocation since allocation is not inlined
121 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
124 // if we had taken control over string memory (GetWriteBuf), it's
125 // intentionally put in invalid state
126 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
127 bool IsValid() const { return (nRefs
!= 0); }
130 class WXDLLIMPEXP_BASE wxStringImpl
133 // an 'invalid' value for string index, moved to this place due to a CW bug
134 static const size_t npos
;
137 // points to data preceded by wxStringData structure with ref count info
138 wxStringCharType
*m_pchData
;
140 // accessor to string data
141 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
143 // string (re)initialization functions
144 // initializes the string to the empty value (must be called only from
145 // ctors, use Reinit() otherwise)
146 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
147 // initializes the string with (a part of) C-string
148 void InitWith(const wxStringCharType
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
149 // as Init, but also frees old data
150 void Reinit() { GetStringData()->Unlock(); Init(); }
153 // allocates memory for string of length nLen
154 bool AllocBuffer(size_t nLen
);
155 // effectively copies data to string
156 bool AssignCopy(size_t, const wxStringCharType
*);
158 // append a (sub)string
159 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
, size_t nMaxLen
);
160 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
)
161 { return ConcatSelf(nLen
, src
, nLen
); }
163 // functions called before writing to the string: they copy it if there
164 // are other references to our data (should be the only owner when writing)
165 bool CopyBeforeWrite();
166 bool AllocBeforeWrite(size_t);
168 // compatibility with wxString
169 bool Alloc(size_t nLen
);
173 typedef wxStringCharType value_type
;
174 typedef wxStringCharType char_type
;
175 typedef size_t size_type
;
176 typedef value_type
& reference
;
177 typedef const value_type
& const_reference
;
178 typedef value_type
* pointer
;
179 typedef const value_type
* const_pointer
;
181 // macro to define the bulk of iterator and const_iterator classes
182 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
184 typedef wxStringCharType value_type; \
185 typedef ref_type reference; \
186 typedef ptr_type pointer; \
187 typedef int difference_type; \
189 iterator_name(pointer ptr) : m_ptr(ptr) { } \
191 reference operator*() const { return *m_ptr; } \
193 iterator_name& operator++() { m_ptr++; return *this; } \
194 iterator_name operator++(int) \
196 const iterator_name tmp(*this); \
201 iterator_name& operator--() { m_ptr--; return *this; } \
202 iterator_name operator--(int) \
204 const iterator_name tmp(*this); \
209 iterator_name operator+(int n) const \
210 { return iterator_name(m_ptr + n); } \
211 iterator_name operator+(size_t n) const \
212 { return iterator_name(m_ptr + n); } \
213 iterator_name operator-(int n) const \
214 { return iterator_name(m_ptr - n); } \
215 iterator_name operator-(size_t n) const \
216 { return iterator_name(m_ptr - n); } \
217 iterator_name& operator+=(int n) \
218 { m_ptr += n; return *this; } \
219 iterator_name& operator+=(size_t n) \
220 { m_ptr += n; return *this; } \
221 iterator_name& operator-=(int n) \
222 { m_ptr -= n; return *this; } \
223 iterator_name& operator-=(size_t n) \
224 { m_ptr -= n; return *this; } \
226 difference_type operator-(const iterator_name& i) const \
227 { return m_ptr - i.m_ptr; } \
229 bool operator==(const iterator_name& i) const \
230 { return m_ptr == i.m_ptr; } \
231 bool operator!=(const iterator_name& i) const \
232 { return m_ptr != i.m_ptr; } \
234 bool operator<(const iterator_name& i) const \
235 { return m_ptr < i.m_ptr; } \
236 bool operator>(const iterator_name& i) const \
237 { return m_ptr > i.m_ptr; } \
238 bool operator<=(const iterator_name& i) const \
239 { return m_ptr <= i.m_ptr; } \
240 bool operator>=(const iterator_name& i) const \
241 { return m_ptr >= i.m_ptr; } \
244 /* for wxStringImpl use only */ \
245 operator pointer() const { return m_ptr; } \
247 friend class WXDLLIMPEXP_BASE wxStringImpl; \
251 // we need to declare const_iterator in wxStringImpl scope, the friend
252 // declaration inside iterator class itself is not enough, or at least not
253 // for g++ 3.4 (g++ 4 is ok)
254 class const_iterator
;
258 WX_DEFINE_STRINGIMPL_ITERATOR(iterator
,
262 friend class const_iterator
;
268 const_iterator(iterator i
) : m_ptr(i
.m_ptr
) { }
270 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator
,
271 const wxStringCharType
&,
272 const wxStringCharType
*);
275 #undef WX_DEFINE_STRINGIMPL_ITERATOR
278 // constructors and destructor
279 // ctor for an empty string
280 wxStringImpl() { Init(); }
282 wxStringImpl(const wxStringImpl
& stringSrc
)
284 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
285 _T("did you forget to call UngetWriteBuf()?") );
287 if ( stringSrc
.empty() ) {
288 // nothing to do for an empty string
292 m_pchData
= stringSrc
.m_pchData
; // share same data
293 GetStringData()->Lock(); // => one more copy
296 // string containing nRepeat copies of ch
297 wxStringImpl(size_type nRepeat
, wxStringCharType ch
);
298 // ctor takes first nLength characters from C string
299 // (default value of npos means take all the string)
300 wxStringImpl(const wxStringCharType
*psz
)
301 { InitWith(psz
, 0, npos
); }
302 wxStringImpl(const wxStringCharType
*psz
, size_t nLength
)
303 { InitWith(psz
, 0, nLength
); }
304 // take nLen chars starting at nPos
305 wxStringImpl(const wxStringImpl
& str
, size_t nPos
, size_t nLen
)
307 wxASSERT_MSG( str
.GetStringData()->IsValid(),
308 _T("did you forget to call UngetWriteBuf()?") );
310 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
311 InitWith(str
.c_str(), nPos
, nLen
);
313 // take everything between start and end
314 wxStringImpl(const_iterator start
, const_iterator end
);
316 // dtor is not virtual, this class must not be inherited from!
319 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
320 //RN - according to the above VC++ does indeed inline this,
321 //even though it spits out two warnings
322 #pragma warning (disable:4714)
325 GetStringData()->Unlock();
328 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
329 //re-enable inlining warning
330 #pragma warning (default:4714)
332 // overloaded assignment
333 // from another wxString
334 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
336 wxStringImpl
& operator=(wxStringCharType ch
);
338 wxStringImpl
& operator=(const wxStringCharType
*psz
);
340 // return the length of the string
341 size_type
length() const { return GetStringData()->nDataLength
; }
342 // return the length of the string
343 size_type
size() const { return length(); }
344 // return the maximum size of the string
345 size_type
max_size() const { return npos
; }
346 // resize the string, filling the space with c if c != 0
347 void resize(size_t nSize
, wxStringCharType ch
= '\0');
348 // delete the contents of the string
349 void clear() { erase(0, npos
); }
350 // returns true if the string is empty
351 bool empty() const { return length() == 0; }
352 // inform string about planned change in size
353 void reserve(size_t sz
) { Alloc(sz
); }
354 size_type
capacity() const { return GetStringData()->nAllocLength
; }
357 // return the character at position n
358 value_type
at(size_type n
) const
359 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
360 // returns the writable character at position n
361 reference
at(size_type n
)
363 wxASSERT_VALID_INDEX( n
);
366 } // FIXME-UTF8: not useful for us...?
368 // lib.string.modifiers
369 // append elements str[pos], ..., str[pos+n]
370 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
372 wxASSERT(pos
<= str
.length());
373 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
377 wxStringImpl
& append(const wxStringImpl
& str
)
378 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
379 // append first n (or all if n == npos) characters of sz
380 wxStringImpl
& append(const wxStringCharType
*sz
)
381 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
382 wxStringImpl
& append(const wxStringCharType
*sz
, size_t n
)
383 { ConcatSelf(n
, sz
); return *this; }
384 // append n copies of ch
385 wxStringImpl
& append(size_t n
, wxStringCharType ch
);
386 // append from first to last
387 wxStringImpl
& append(const_iterator first
, const_iterator last
)
388 { ConcatSelf(last
- first
, first
); return *this; }
390 // same as `this_string = str'
391 wxStringImpl
& assign(const wxStringImpl
& str
)
392 { return *this = str
; }
393 // same as ` = str[pos..pos + n]
394 wxStringImpl
& assign(const wxStringImpl
& str
, size_t pos
, size_t n
)
395 { clear(); return append(str
, pos
, n
); }
396 // same as `= first n (or all if n == npos) characters of sz'
397 wxStringImpl
& assign(const wxStringCharType
*sz
)
398 { clear(); return append(sz
, wxStrlen(sz
)); }
399 wxStringImpl
& assign(const wxStringCharType
*sz
, size_t n
)
400 { clear(); return append(sz
, n
); }
401 // same as `= n copies of ch'
402 wxStringImpl
& assign(size_t n
, wxStringCharType ch
)
403 { clear(); return append(n
, ch
); }
404 // assign from first to last
405 wxStringImpl
& assign(const_iterator first
, const_iterator last
)
406 { clear(); return append(first
, last
); }
408 // first valid index position
409 const_iterator
begin() const { return m_pchData
; }
411 // position one after the last valid one
412 const_iterator
end() const { return m_pchData
+ length(); }
415 // insert another string
416 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
418 wxASSERT( str
.GetStringData()->IsValid() );
419 return insert(nPos
, str
.c_str(), str
.length());
421 // insert n chars of str starting at nStart (in str)
422 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
, size_t nStart
, size_t n
)
424 wxASSERT( str
.GetStringData()->IsValid() );
425 wxASSERT( nStart
< str
.length() );
426 size_t strLen
= str
.length() - nStart
;
427 n
= strLen
< n
? strLen
: n
;
428 return insert(nPos
, str
.c_str() + nStart
, n
);
430 // insert first n (or all if n == npos) characters of sz
431 wxStringImpl
& insert(size_t nPos
, const wxStringCharType
*sz
, size_t n
= npos
);
432 // insert n copies of ch
433 wxStringImpl
& insert(size_t nPos
, size_t n
, wxStringCharType ch
)// FIXME-UTF8: tricky
434 { return insert(nPos
, wxStringImpl(n
, ch
)); }
435 iterator
insert(iterator it
, wxStringCharType ch
) // FIXME-UTF8: tricky
436 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
437 void insert(iterator it
, const_iterator first
, const_iterator last
)
438 { insert(it
- begin(), first
, last
- first
); }
439 void insert(iterator it
, size_type n
, wxStringCharType ch
)
440 { insert(it
- begin(), n
, ch
); }
442 // delete characters from nStart to nStart + nLen
443 wxStringImpl
& erase(size_type pos
= 0, size_type n
= npos
);
444 iterator
erase(iterator first
, iterator last
)
446 size_t idx
= first
- begin();
447 erase(idx
, last
- first
);
448 return begin() + idx
;
450 iterator
erase(iterator first
);
452 // explicit conversion to C string (use this with printf()!)
453 const wxStringCharType
* c_str() const { return m_pchData
; }
454 const wxStringCharType
* data() const { return m_pchData
; }
456 // replaces the substring of length nLen starting at nStart
457 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringCharType
* sz
);
458 // replaces the substring of length nLen starting at nStart
459 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringImpl
& str
)
460 { return replace(nStart
, nLen
, str
.c_str()); }
461 // replaces the substring with nCount copies of ch
462 wxStringImpl
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxStringCharType ch
);
463 // replaces a substring with another substring
464 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
465 const wxStringImpl
& str
, size_t nStart2
, size_t nLen2
);
466 // replaces the substring with first nCount chars of sz
467 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
468 const wxStringCharType
* sz
, size_t nCount
);
469 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
)
470 { return replace(first
- begin(), last
- first
, s
); }
471 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
,
473 { return replace(first
- begin(), last
- first
, s
, n
); }
474 wxStringImpl
& replace(iterator first
, iterator last
, const wxStringImpl
& s
)
475 { return replace(first
- begin(), last
- first
, s
); }
476 wxStringImpl
& replace(iterator first
, iterator last
, size_type n
, wxStringCharType c
)
477 { return replace(first
- begin(), last
- first
, n
, c
); }
478 wxStringImpl
& replace(iterator first
, iterator last
,
479 const_iterator first1
, const_iterator last1
)
480 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
483 void swap(wxStringImpl
& str
);
485 // All find() functions take the nStart argument which specifies the
486 // position to start the search on, the default value is 0. All functions
487 // return npos if there were no match.
490 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
492 // find first n characters of sz
493 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
495 // find the first occurrence of character ch after nStart
496 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
498 // rfind() family is exactly like find() but works right to left
500 // as find, but from the end
501 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
503 // as find, but from the end
504 size_t rfind(const wxStringCharType
* sz
, size_t nStart
= npos
,
505 size_t n
= npos
) const;
506 // as find, but from the end
507 size_t rfind(wxStringCharType ch
, size_t nStart
= npos
) const;
509 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
511 // substring extraction
512 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
515 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
516 // string += C string
517 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
519 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
521 #if !wxUSE_UNICODE_UTF8
522 // helpers for wxStringBuffer and wxStringBufferLength
523 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
524 void DoUngetWriteBuf();
525 void DoUngetWriteBuf(size_t nLen
);
528 friend class WXDLLIMPEXP_BASE wxString
;
531 #endif // !wxUSE_STL_BASED_WXSTRING
533 // don't pollute the library user's name space
534 #undef wxASSERT_VALID_INDEX
536 #endif // _WX_WXSTRINGIMPL_H__