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.
28 // ---------------------------------------------------------------------------
30 // ---------------------------------------------------------------------------
32 // implementation only
33 #define wxASSERT_VALID_INDEX(i) \
34 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // global pointer to empty string
42 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
45 // ----------------------------------------------------------------------------
46 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
47 // ----------------------------------------------------------------------------
49 #define wxUSE_STL_BASED_WXSTRING wxUSE_STL
51 // in both cases we need to define wxStdString
52 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
54 #include "wx/beforestd.h"
56 #include "wx/afterstd.h"
58 #if wxUSE_UNICODE_WCHAR
59 #ifdef HAVE_STD_WSTRING
60 typedef std::wstring wxStdString
;
62 typedef std::basic_string
<wxChar
> wxStdString
;
65 typedef std::string wxStdString
;
68 #endif // need <string>
70 #if wxUSE_STL_BASED_WXSTRING
72 // we always want ctor from std::string when using std::string internally
73 #undef wxUSE_STD_STRING
74 #define wxUSE_STD_STRING 1
76 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
77 (defined(_MSC_VER) && (_MSC_VER <= 1200))
78 #define wxSTRING_BASE_HASNT_CLEAR
81 typedef wxStdString wxStringImpl
;
82 #else // if !wxUSE_STL_BASED_WXSTRING
84 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
85 #undef HAVE_STD_STRING_COMPARE
87 // ---------------------------------------------------------------------------
88 // string data prepended with some housekeeping info (used by wxString class),
89 // is never used directly (but had to be put here to allow inlining)
90 // ---------------------------------------------------------------------------
92 struct WXDLLIMPEXP_BASE wxStringData
94 int nRefs
; // reference count
95 size_t nDataLength
, // actual string length
96 nAllocLength
; // allocated memory size
98 // mimics declaration 'wxChar data[nAllocLength]'
99 wxChar
* data() const { return (wxChar
*)(this + 1); }
101 // empty string has a special ref count so it's never deleted
102 bool IsEmpty() const { return (nRefs
== -1); }
103 bool IsShared() const { return (nRefs
> 1); }
106 void Lock() { if ( !IsEmpty() ) nRefs
++; }
108 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
109 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
112 // VC++ free must take place in same DLL as allocation when using non dll
113 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
114 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
115 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
116 // we must not inline deallocation since allocation is not inlined
119 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
122 // if we had taken control over string memory (GetWriteBuf), it's
123 // intentionally put in invalid state
124 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
125 bool IsValid() const { return (nRefs
!= 0); }
128 class WXDLLIMPEXP_BASE wxStringImpl
131 // an 'invalid' value for string index, moved to this place due to a CW bug
132 static const size_t npos
;
135 // points to data preceded by wxStringData structure with ref count info
136 wxStringCharType
*m_pchData
;
138 // accessor to string data
139 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
141 // string (re)initialization functions
142 // initializes the string to the empty value (must be called only from
143 // ctors, use Reinit() otherwise)
144 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
145 // initializes the string with (a part of) C-string
146 void InitWith(const wxStringCharType
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
147 // as Init, but also frees old data
148 void Reinit() { GetStringData()->Unlock(); Init(); }
151 // allocates memory for string of length nLen
152 bool AllocBuffer(size_t nLen
);
153 // effectively copies data to string
154 bool AssignCopy(size_t, const wxStringCharType
*);
156 // append a (sub)string
157 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
, size_t nMaxLen
);
158 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
)
159 { return ConcatSelf(nLen
, src
, nLen
); }
161 // functions called before writing to the string: they copy it if there
162 // are other references to our data (should be the only owner when writing)
163 bool CopyBeforeWrite();
164 bool AllocBeforeWrite(size_t);
166 // compatibility with wxString
167 bool Alloc(size_t nLen
);
171 typedef wxStringCharType value_type
;
172 typedef wxStringCharType char_type
;
173 typedef size_t size_type
;
174 typedef value_type
& reference
;
175 typedef const value_type
& const_reference
;
176 typedef value_type
* pointer
;
177 typedef const value_type
* const_pointer
;
178 typedef value_type
*iterator
;
179 typedef const value_type
*const_iterator
;
181 // constructors and destructor
182 // ctor for an empty string
183 wxStringImpl() { Init(); }
185 wxStringImpl(const wxStringImpl
& stringSrc
)
187 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
188 _T("did you forget to call UngetWriteBuf()?") );
190 if ( stringSrc
.empty() ) {
191 // nothing to do for an empty string
195 m_pchData
= stringSrc
.m_pchData
; // share same data
196 GetStringData()->Lock(); // => one more copy
199 // string containing nRepeat copies of ch
200 wxStringImpl(size_type nRepeat
, wxStringCharType ch
);
201 // ctor takes first nLength characters from C string
202 // (default value of npos means take all the string)
203 wxStringImpl(const wxStringCharType
*psz
)
204 { InitWith(psz
, 0, npos
); }
205 wxStringImpl(const wxStringCharType
*psz
, size_t nLength
)
206 { InitWith(psz
, 0, nLength
); }
207 // take nLen chars starting at nPos
208 wxStringImpl(const wxStringImpl
& str
, size_t nPos
, size_t nLen
)
210 wxASSERT_MSG( str
.GetStringData()->IsValid(),
211 _T("did you forget to call UngetWriteBuf()?") );
213 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
214 InitWith(str
.c_str(), nPos
, nLen
);
216 // take all characters from pStart to pEnd
217 wxStringImpl(const void *pStart
, const void *pEnd
);
219 // dtor is not virtual, this class must not be inherited from!
222 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
223 //RN - according to the above VC++ does indeed inline this,
224 //even though it spits out two warnings
225 #pragma warning (disable:4714)
228 GetStringData()->Unlock();
231 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
232 //re-enable inlining warning
233 #pragma warning (default:4714)
235 // overloaded assignment
236 // from another wxString
237 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
239 wxStringImpl
& operator=(wxStringCharType ch
);
241 wxStringImpl
& operator=(const wxStringCharType
*psz
);
243 // return the length of the string
244 size_type
length() const { return GetStringData()->nDataLength
; }
245 // return the length of the string
246 size_type
size() const { return length(); }
247 // return the maximum size of the string
248 size_type
max_size() const { return npos
; }
249 // resize the string, filling the space with c if c != 0
250 void resize(size_t nSize
, wxStringCharType ch
= '\0');
251 // delete the contents of the string
252 void clear() { erase(0, npos
); }
253 // returns true if the string is empty
254 bool empty() const { return length() == 0; }
255 // inform string about planned change in size
256 void reserve(size_t sz
) { Alloc(sz
); }
257 size_type
capacity() const { return GetStringData()->nAllocLength
; }
260 // return the character at position n
261 value_type
at(size_type n
) const
262 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
263 // returns the writable character at position n
264 reference
at(size_type n
)
266 wxASSERT_VALID_INDEX( n
);
269 } // FIXME-UTF8: not useful for us...?
271 // lib.string.modifiers
272 // append elements str[pos], ..., str[pos+n]
273 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
275 wxASSERT(pos
<= str
.length());
276 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
280 wxStringImpl
& append(const wxStringImpl
& str
)
281 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
282 // append first n (or all if n == npos) characters of sz
283 wxStringImpl
& append(const wxStringCharType
*sz
)
284 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
285 wxStringImpl
& append(const wxStringCharType
*sz
, size_t n
)
286 { ConcatSelf(n
, sz
); return *this; }
287 // append n copies of ch
288 wxStringImpl
& append(size_t n
, wxStringCharType ch
);
289 // append from first to last
290 wxStringImpl
& append(const_iterator first
, const_iterator last
)
291 { ConcatSelf(last
- first
, first
); return *this; }
293 // same as `this_string = str'
294 wxStringImpl
& assign(const wxStringImpl
& str
)
295 { return *this = str
; }
296 // same as ` = str[pos..pos + n]
297 wxStringImpl
& assign(const wxStringImpl
& str
, size_t pos
, size_t n
)
298 { clear(); return append(str
, pos
, n
); }
299 // same as `= first n (or all if n == npos) characters of sz'
300 wxStringImpl
& assign(const wxStringCharType
*sz
)
301 { clear(); return append(sz
, wxStrlen(sz
)); }
302 wxStringImpl
& assign(const wxStringCharType
*sz
, size_t n
)
303 { clear(); return append(sz
, n
); }
304 // same as `= n copies of ch'
305 wxStringImpl
& assign(size_t n
, wxStringCharType ch
)
306 { clear(); return append(n
, ch
); }
307 // assign from first to last
308 wxStringImpl
& assign(const_iterator first
, const_iterator last
)
309 { clear(); return append(first
, last
); }
311 // first valid index position
312 const_iterator
begin() const { return m_pchData
; }
314 // position one after the last valid one
315 const_iterator
end() const { return m_pchData
+ length(); }
318 // insert another string
319 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
321 wxASSERT( str
.GetStringData()->IsValid() );
322 return insert(nPos
, str
.c_str(), str
.length());
324 // insert n chars of str starting at nStart (in str)
325 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
, size_t nStart
, size_t n
)
327 wxASSERT( str
.GetStringData()->IsValid() );
328 wxASSERT( nStart
< str
.length() );
329 size_t strLen
= str
.length() - nStart
;
330 n
= strLen
< n
? strLen
: n
;
331 return insert(nPos
, str
.c_str() + nStart
, n
);
333 // insert first n (or all if n == npos) characters of sz
334 wxStringImpl
& insert(size_t nPos
, const wxStringCharType
*sz
, size_t n
= npos
);
335 // insert n copies of ch
336 wxStringImpl
& insert(size_t nPos
, size_t n
, wxStringCharType ch
)// FIXME-UTF8: tricky
337 { return insert(nPos
, wxStringImpl(n
, ch
)); }
338 iterator
insert(iterator it
, wxStringCharType ch
) // FIXME-UTF8: tricky
339 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
340 void insert(iterator it
, const_iterator first
, const_iterator last
)
341 { insert(it
- begin(), first
, last
- first
); }
342 void insert(iterator it
, size_type n
, wxStringCharType ch
)
343 { insert(it
- begin(), n
, ch
); }
345 // delete characters from nStart to nStart + nLen
346 wxStringImpl
& erase(size_type pos
= 0, size_type n
= npos
);
347 iterator
erase(iterator first
, iterator last
)
349 size_t idx
= first
- begin();
350 erase(idx
, last
- first
);
351 return begin() + idx
;
353 iterator
erase(iterator first
);
355 // explicit conversion to C string (use this with printf()!)
356 const wxStringCharType
* c_str() const { return m_pchData
; }
357 const wxStringCharType
* data() const { return m_pchData
; }
359 // replaces the substring of length nLen starting at nStart
360 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringCharType
* sz
);
361 // replaces the substring of length nLen starting at nStart
362 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringImpl
& str
)
363 { return replace(nStart
, nLen
, str
.c_str()); }
364 // replaces the substring with nCount copies of ch
365 wxStringImpl
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxStringCharType ch
);
366 // replaces a substring with another substring
367 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
368 const wxStringImpl
& str
, size_t nStart2
, size_t nLen2
);
369 // replaces the substring with first nCount chars of sz
370 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
371 const wxStringCharType
* sz
, size_t nCount
);
372 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
)
373 { return replace(first
- begin(), last
- first
, s
); }
374 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
,
376 { return replace(first
- begin(), last
- first
, s
, n
); }
377 wxStringImpl
& replace(iterator first
, iterator last
, const wxStringImpl
& s
)
378 { return replace(first
- begin(), last
- first
, s
); }
379 wxStringImpl
& replace(iterator first
, iterator last
, size_type n
, wxStringCharType c
)
380 { return replace(first
- begin(), last
- first
, n
, c
); }
381 wxStringImpl
& replace(iterator first
, iterator last
,
382 const_iterator first1
, const_iterator last1
)
383 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
386 void swap(wxStringImpl
& str
);
388 // All find() functions take the nStart argument which specifies the
389 // position to start the search on, the default value is 0. All functions
390 // return npos if there were no match.
393 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
395 // find first n characters of sz
396 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
398 // find the first occurence of character ch after nStart
399 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
401 // rfind() family is exactly like find() but works right to left
403 // as find, but from the end
404 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
406 // as find, but from the end
407 size_t rfind(const wxStringCharType
* sz
, size_t nStart
= npos
,
408 size_t n
= npos
) const;
409 // as find, but from the end
410 size_t rfind(wxStringCharType ch
, size_t nStart
= npos
) const;
412 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
414 // substring extraction
415 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
418 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
419 // string += C string
420 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
422 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
424 #if !wxUSE_UNICODE_UTF8
425 // helpers for wxStringBuffer and wxStringBufferLength
426 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
427 void DoUngetWriteBuf();
428 void DoUngetWriteBuf(size_t nLen
);
431 friend class WXDLLIMPEXP_BASE wxString
;
434 #endif // !wxUSE_STL_BASED_WXSTRING
436 // don't pollute the library user's name space
437 #undef wxASSERT_VALID_INDEX
439 #endif // _WX_WXSTRINGIMPL_H__