]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stringimpl.h
4173d2c69b5b778d72a78a796a802ae5d47fd51e
[wxWidgets.git] / include / wx / stringimpl.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stringimpl.h
3 // Purpose: wxStringImpl class, implementation of wxString
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 /*
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.
16 */
17
18 #ifndef _WX_WXSTRINGIMPL_H__
19 #define _WX_WXSTRINGIMPL_H__
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 #include "wx/defs.h" // everybody should include this
26 #include "wx/wxchar.h" // for wxChar, wxStrlen() etc.
27
28 #include <stdlib.h>
29
30 // ---------------------------------------------------------------------------
31 // macros
32 // ---------------------------------------------------------------------------
33
34 // implementation only
35 #define wxASSERT_VALID_INDEX(i) \
36 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
37
38
39 // ----------------------------------------------------------------------------
40 // global data
41 // ----------------------------------------------------------------------------
42
43 // global pointer to empty string
44 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
45
46
47 // ----------------------------------------------------------------------------
48 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
49 // ----------------------------------------------------------------------------
50
51 #define wxUSE_STL_BASED_WXSTRING wxUSE_STL
52
53 // in both cases we need to define wxStdString
54 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
55
56 #include "wx/beforestd.h"
57 #include <string>
58 #include "wx/afterstd.h"
59
60 #if wxUSE_UNICODE_WCHAR
61 #ifdef HAVE_STD_WSTRING
62 typedef std::wstring wxStdString;
63 #else
64 typedef std::basic_string<wxChar> wxStdString;
65 #endif
66 #else
67 typedef std::string wxStdString;
68 #endif
69
70 #endif // need <string>
71
72 #if wxUSE_STL_BASED_WXSTRING
73
74 // we always want ctor from std::string when using std::string internally
75 #undef wxUSE_STD_STRING
76 #define wxUSE_STD_STRING 1
77
78 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
79 (defined(_MSC_VER) && (_MSC_VER <= 1200))
80 #define wxSTRING_BASE_HASNT_CLEAR
81 #endif
82
83 typedef wxStdString wxStringImpl;
84 #else // if !wxUSE_STL_BASED_WXSTRING
85
86 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
87 #undef HAVE_STD_STRING_COMPARE
88
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 // ---------------------------------------------------------------------------
93
94 struct WXDLLIMPEXP_BASE wxStringData
95 {
96 int nRefs; // reference count
97 size_t nDataLength, // actual string length
98 nAllocLength; // allocated memory size
99
100 // mimics declaration 'wxChar data[nAllocLength]'
101 wxChar* data() const { return (wxChar*)(this + 1); }
102
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); }
106
107 // lock/unlock
108 void Lock() { if ( !IsEmpty() ) nRefs++; }
109
110 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
111 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
112 __forceinline
113 #endif
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
119 void Free();
120 #else
121 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
122 #endif
123
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); }
128 };
129
130 class WXDLLIMPEXP_BASE wxStringImpl
131 {
132 public:
133 // an 'invalid' value for string index, moved to this place due to a CW bug
134 static const size_t npos;
135
136 protected:
137 // points to data preceded by wxStringData structure with ref count info
138 wxStringCharType *m_pchData;
139
140 // accessor to string data
141 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
142
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(); }
151
152 // memory allocation
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 *);
157
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); }
162
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);
167
168 // compatibility with wxString
169 bool Alloc(size_t nLen);
170
171 public:
172 // standard types
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;
180 typedef value_type *iterator;
181 typedef const value_type *const_iterator;
182
183 // constructors and destructor
184 // ctor for an empty string
185 wxStringImpl() { Init(); }
186 // copy ctor
187 wxStringImpl(const wxStringImpl& stringSrc)
188 {
189 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
190 _T("did you forget to call UngetWriteBuf()?") );
191
192 if ( stringSrc.empty() ) {
193 // nothing to do for an empty string
194 Init();
195 }
196 else {
197 m_pchData = stringSrc.m_pchData; // share same data
198 GetStringData()->Lock(); // => one more copy
199 }
200 }
201 // string containing nRepeat copies of ch
202 wxStringImpl(size_type nRepeat, wxStringCharType ch);
203 // ctor takes first nLength characters from C string
204 // (default value of npos means take all the string)
205 wxStringImpl(const wxStringCharType *psz)
206 { InitWith(psz, 0, npos); }
207 wxStringImpl(const wxStringCharType *psz, size_t nLength)
208 { InitWith(psz, 0, nLength); }
209 // take nLen chars starting at nPos
210 wxStringImpl(const wxStringImpl& str, size_t nPos, size_t nLen)
211 {
212 wxASSERT_MSG( str.GetStringData()->IsValid(),
213 _T("did you forget to call UngetWriteBuf()?") );
214 Init();
215 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
216 InitWith(str.c_str(), nPos, nLen);
217 }
218 // take all characters from pStart to pEnd
219 wxStringImpl(const void *pStart, const void *pEnd);
220
221 // dtor is not virtual, this class must not be inherited from!
222 ~wxStringImpl()
223 {
224 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
225 //RN - according to the above VC++ does indeed inline this,
226 //even though it spits out two warnings
227 #pragma warning (disable:4714)
228 #endif
229
230 GetStringData()->Unlock();
231 }
232
233 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
234 //re-enable inlining warning
235 #pragma warning (default:4714)
236 #endif
237 // overloaded assignment
238 // from another wxString
239 wxStringImpl& operator=(const wxStringImpl& stringSrc);
240 // from a character
241 wxStringImpl& operator=(wxStringCharType ch);
242 // from a C string
243 wxStringImpl& operator=(const wxStringCharType *psz);
244
245 // return the length of the string
246 size_type length() const { return GetStringData()->nDataLength; }
247 // return the length of the string
248 size_type size() const { return length(); }
249 // return the maximum size of the string
250 size_type max_size() const { return npos; }
251 // resize the string, filling the space with c if c != 0
252 void resize(size_t nSize, wxStringCharType ch = '\0');
253 // delete the contents of the string
254 void clear() { erase(0, npos); }
255 // returns true if the string is empty
256 bool empty() const { return length() == 0; }
257 // inform string about planned change in size
258 void reserve(size_t sz) { Alloc(sz); }
259 size_type capacity() const { return GetStringData()->nAllocLength; }
260
261 // lib.string.access
262 // return the character at position n
263 value_type at(size_type n) const
264 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
265 // returns the writable character at position n
266 reference at(size_type n)
267 {
268 wxASSERT_VALID_INDEX( n );
269 CopyBeforeWrite();
270 return m_pchData[n];
271 } // FIXME-UTF8: not useful for us...?
272
273 // lib.string.modifiers
274 // append elements str[pos], ..., str[pos+n]
275 wxStringImpl& append(const wxStringImpl& str, size_t pos, size_t n)
276 {
277 wxASSERT(pos <= str.length());
278 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
279 return *this;
280 }
281 // append a string
282 wxStringImpl& append(const wxStringImpl& str)
283 { ConcatSelf(str.length(), str.c_str()); return *this; }
284 // append first n (or all if n == npos) characters of sz
285 wxStringImpl& append(const wxStringCharType *sz)
286 { ConcatSelf(wxStrlen(sz), sz); return *this; }
287 wxStringImpl& append(const wxStringCharType *sz, size_t n)
288 { ConcatSelf(n, sz); return *this; }
289 // append n copies of ch
290 wxStringImpl& append(size_t n, wxStringCharType ch);
291 // append from first to last
292 wxStringImpl& append(const_iterator first, const_iterator last)
293 { ConcatSelf(last - first, first); return *this; }
294
295 // same as `this_string = str'
296 wxStringImpl& assign(const wxStringImpl& str)
297 { return *this = str; }
298 // same as ` = str[pos..pos + n]
299 wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n)
300 { clear(); return append(str, pos, n); }
301 // same as `= first n (or all if n == npos) characters of sz'
302 wxStringImpl& assign(const wxStringCharType *sz)
303 { clear(); return append(sz, wxStrlen(sz)); }
304 wxStringImpl& assign(const wxStringCharType *sz, size_t n)
305 { clear(); return append(sz, n); }
306 // same as `= n copies of ch'
307 wxStringImpl& assign(size_t n, wxStringCharType ch)
308 { clear(); return append(n, ch); }
309 // assign from first to last
310 wxStringImpl& assign(const_iterator first, const_iterator last)
311 { clear(); return append(first, last); }
312
313 // first valid index position
314 const_iterator begin() const { return m_pchData; }
315 iterator begin();
316 // position one after the last valid one
317 const_iterator end() const { return m_pchData + length(); }
318 iterator end();
319
320 // insert another string
321 wxStringImpl& insert(size_t nPos, const wxStringImpl& str)
322 {
323 wxASSERT( str.GetStringData()->IsValid() );
324 return insert(nPos, str.c_str(), str.length());
325 }
326 // insert n chars of str starting at nStart (in str)
327 wxStringImpl& insert(size_t nPos, const wxStringImpl& str, size_t nStart, size_t n)
328 {
329 wxASSERT( str.GetStringData()->IsValid() );
330 wxASSERT( nStart < str.length() );
331 size_t strLen = str.length() - nStart;
332 n = strLen < n ? strLen : n;
333 return insert(nPos, str.c_str() + nStart, n);
334 }
335 // insert first n (or all if n == npos) characters of sz
336 wxStringImpl& insert(size_t nPos, const wxStringCharType *sz, size_t n = npos);
337 // insert n copies of ch
338 wxStringImpl& insert(size_t nPos, size_t n, wxStringCharType ch)// FIXME-UTF8: tricky
339 { return insert(nPos, wxStringImpl(n, ch)); }
340 iterator insert(iterator it, wxStringCharType ch) // FIXME-UTF8: tricky
341 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
342 void insert(iterator it, const_iterator first, const_iterator last)
343 { insert(it - begin(), first, last - first); }
344 void insert(iterator it, size_type n, wxStringCharType ch)
345 { insert(it - begin(), n, ch); }
346
347 // delete characters from nStart to nStart + nLen
348 wxStringImpl& erase(size_type pos = 0, size_type n = npos);
349 iterator erase(iterator first, iterator last)
350 {
351 size_t idx = first - begin();
352 erase(idx, last - first);
353 return begin() + idx;
354 }
355 iterator erase(iterator first);
356
357 // explicit conversion to C string (use this with printf()!)
358 const wxStringCharType* c_str() const { return m_pchData; }
359 const wxStringCharType* data() const { return m_pchData; }
360
361 // replaces the substring of length nLen starting at nStart
362 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz);
363 // replaces the substring of length nLen starting at nStart
364 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str)
365 { return replace(nStart, nLen, str.c_str()); }
366 // replaces the substring with nCount copies of ch
367 wxStringImpl& replace(size_t nStart, size_t nLen, size_t nCount, wxStringCharType ch);
368 // replaces a substring with another substring
369 wxStringImpl& replace(size_t nStart, size_t nLen,
370 const wxStringImpl& str, size_t nStart2, size_t nLen2);
371 // replaces the substring with first nCount chars of sz
372 wxStringImpl& replace(size_t nStart, size_t nLen,
373 const wxStringCharType* sz, size_t nCount);
374 wxStringImpl& replace(iterator first, iterator last, const_pointer s)
375 { return replace(first - begin(), last - first, s); }
376 wxStringImpl& replace(iterator first, iterator last, const_pointer s,
377 size_type n)
378 { return replace(first - begin(), last - first, s, n); }
379 wxStringImpl& replace(iterator first, iterator last, const wxStringImpl& s)
380 { return replace(first - begin(), last - first, s); }
381 wxStringImpl& replace(iterator first, iterator last, size_type n, wxStringCharType c)
382 { return replace(first - begin(), last - first, n, c); }
383 wxStringImpl& replace(iterator first, iterator last,
384 const_iterator first1, const_iterator last1)
385 { return replace(first - begin(), last - first, first1, last1 - first1); }
386
387 // swap two strings
388 void swap(wxStringImpl& str);
389
390 // All find() functions take the nStart argument which specifies the
391 // position to start the search on, the default value is 0. All functions
392 // return npos if there were no match.
393
394 // find a substring
395 size_t find(const wxStringImpl& str, size_t nStart = 0) const;
396
397 // find first n characters of sz
398 size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
399
400 // find the first occurence of character ch after nStart
401 size_t find(wxStringCharType ch, size_t nStart = 0) const;
402
403 // rfind() family is exactly like find() but works right to left
404
405 // as find, but from the end
406 size_t rfind(const wxStringImpl& str, size_t nStart = npos) const;
407
408 // as find, but from the end
409 size_t rfind(const wxStringCharType* sz, size_t nStart = npos,
410 size_t n = npos) const;
411 // as find, but from the end
412 size_t rfind(wxStringCharType ch, size_t nStart = npos) const;
413
414 size_type copy(wxStringCharType* s, size_type n, size_type pos = 0);
415
416 // substring extraction
417 wxStringImpl substr(size_t nStart = 0, size_t nLen = npos) const;
418
419 // string += string
420 wxStringImpl& operator+=(const wxStringImpl& s) { return append(s); }
421 // string += C string
422 wxStringImpl& operator+=(const wxStringCharType *psz) { return append(psz); }
423 // string += char
424 wxStringImpl& operator+=(wxStringCharType ch) { return append(1, ch); }
425
426 #if !wxUSE_UNICODE_UTF8
427 // helpers for wxStringBuffer and wxStringBufferLength
428 wxStringCharType *DoGetWriteBuf(size_t nLen);
429 void DoUngetWriteBuf();
430 void DoUngetWriteBuf(size_t nLen);
431 #endif
432
433 friend class WXDLLIMPEXP_BASE wxString;
434 };
435
436 #endif // !wxUSE_STL_BASED_WXSTRING
437
438 // don't pollute the library user's name space
439 #undef wxASSERT_VALID_INDEX
440
441 #endif // _WX_WXSTRINGIMPL_H__