]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stringimpl.h
replace 5 copies of size constraining code with one function
[wxWidgets.git] / include / wx / stringimpl.h
CommitLineData
a7ea63e2
VS
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
745817ff
VS
28#include <stdlib.h>
29
a7ea63e2
VS
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
44extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
81727065
VS
45#if wxUSE_UNICODE_UTF8
46// FIXME-UTF8: we should have only one wxEmptyString
47extern WXDLLIMPEXP_DATA_BASE(const wxStringCharType*) wxEmptyStringImpl;
48#endif
a7ea63e2
VS
49
50
51// ----------------------------------------------------------------------------
52// deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
53// ----------------------------------------------------------------------------
54
55#define wxUSE_STL_BASED_WXSTRING wxUSE_STL
56
57// in both cases we need to define wxStdString
58#if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
59
60#include "wx/beforestd.h"
61#include <string>
62#include "wx/afterstd.h"
63
59953bf4
VS
64#ifdef HAVE_STD_WSTRING
65 typedef std::wstring wxStdWideString;
66#else
67 typedef std::basic_string<wchar_t> wxStdWideString;
68#endif
69
a7ea63e2 70#if wxUSE_UNICODE_WCHAR
59953bf4 71 typedef wxStdWideString wxStdString;
a7ea63e2
VS
72#else
73 typedef std::string wxStdString;
74#endif
75
59953bf4
VS
76#endif // wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
77
a7ea63e2
VS
78
79#if wxUSE_STL_BASED_WXSTRING
80
81 // we always want ctor from std::string when using std::string internally
82 #undef wxUSE_STD_STRING
83 #define wxUSE_STD_STRING 1
84
85 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
86 (defined(_MSC_VER) && (_MSC_VER <= 1200))
87 #define wxSTRING_BASE_HASNT_CLEAR
88 #endif
89
90 typedef wxStdString wxStringImpl;
91#else // if !wxUSE_STL_BASED_WXSTRING
92
93// in non-STL mode, compare() is implemented in wxString and not wxStringImpl
94#undef HAVE_STD_STRING_COMPARE
95
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// ---------------------------------------------------------------------------
100
101struct WXDLLIMPEXP_BASE wxStringData
102{
103 int nRefs; // reference count
104 size_t nDataLength, // actual string length
105 nAllocLength; // allocated memory size
106
81727065
VS
107 // mimics declaration 'wxStringCharType data[nAllocLength]'
108 wxStringCharType* data() const { return (wxStringCharType*)(this + 1); }
a7ea63e2
VS
109
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); }
113
114 // lock/unlock
115 void Lock() { if ( !IsEmpty() ) nRefs++; }
116
117 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
118#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
119 __forceinline
120#endif
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
126 void Free();
127#else
128 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
129#endif
130
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); }
135};
136
137class WXDLLIMPEXP_BASE wxStringImpl
138{
139public:
140 // an 'invalid' value for string index, moved to this place due to a CW bug
141 static const size_t npos;
142
143protected:
144 // points to data preceded by wxStringData structure with ref count info
145 wxStringCharType *m_pchData;
146
147 // accessor to string data
148 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
149
150 // string (re)initialization functions
151 // initializes the string to the empty value (must be called only from
152 // ctors, use Reinit() otherwise)
81727065
VS
153#if wxUSE_UNICODE_UTF8
154 void Init() { m_pchData = (wxStringCharType *)wxEmptyStringImpl; } // FIXME-UTF8
155#else
a7ea63e2 156 void Init() { m_pchData = (wxStringCharType *)wxEmptyString; }
81727065 157#endif
a7ea63e2
VS
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(); }
162
163 // memory allocation
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 *);
168
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); }
173
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);
178
179 // compatibility with wxString
180 bool Alloc(size_t nLen);
181
182public:
183 // standard types
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;
f2a1b1bd
VZ
191
192 // macro to define the bulk of iterator and const_iterator classes
193 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
194 public: \
195 typedef wxStringCharType value_type; \
196 typedef ref_type reference; \
197 typedef ptr_type pointer; \
89360a8c 198 typedef int difference_type; \
f2a1b1bd
VZ
199 \
200 iterator_name(pointer ptr) : m_ptr(ptr) { } \
201 \
202 reference operator*() const { return *m_ptr; } \
203 \
204 iterator_name& operator++() { m_ptr++; return *this; } \
205 iterator_name operator++(int) \
206 { \
207 const iterator_name tmp(*this); \
208 m_ptr++; \
209 return tmp; \
210 } \
211 \
212 iterator_name& operator--() { m_ptr--; return *this; } \
213 iterator_name operator--(int) \
214 { \
215 const iterator_name tmp(*this); \
216 m_ptr--; \
217 return tmp; \
218 } \
219 \
220 iterator_name operator+(int n) const \
221 { return iterator_name(m_ptr + n); } \
15c12109
VZ
222 iterator_name operator+(size_t n) const \
223 { return iterator_name(m_ptr + n); } \
f2a1b1bd
VZ
224 iterator_name operator-(int n) const \
225 { return iterator_name(m_ptr - n); } \
15c12109
VZ
226 iterator_name operator-(size_t n) const \
227 { return iterator_name(m_ptr - n); } \
f2a1b1bd
VZ
228 iterator_name& operator+=(int n) \
229 { m_ptr += n; return *this; } \
15c12109
VZ
230 iterator_name& operator+=(size_t n) \
231 { m_ptr += n; return *this; } \
f2a1b1bd 232 iterator_name& operator-=(int n) \
15c12109
VZ
233 { m_ptr -= n; return *this; } \
234 iterator_name& operator-=(size_t n) \
f2a1b1bd
VZ
235 { m_ptr -= n; return *this; } \
236 \
89360a8c 237 difference_type operator-(const iterator_name& i) const \
f2a1b1bd
VZ
238 { return m_ptr - i.m_ptr; } \
239 \
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; } \
244 \
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; } \
253 \
254 private: \
255 /* for wxStringImpl use only */ \
256 operator pointer() const { return m_ptr; } \
257 \
258 friend class WXDLLIMPEXP_BASE wxStringImpl; \
259 \
260 pointer m_ptr
261
7c2b62db
VZ
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;
266
f2a1b1bd
VZ
267 class iterator
268 {
269 WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
270 wxStringCharType&,
271 wxStringCharType*);
272
273 friend class const_iterator;
274 };
275
276 class const_iterator
277 {
278 public:
279 const_iterator(iterator i) : m_ptr(i.m_ptr) { }
280
281 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator,
282 const wxStringCharType&,
283 const wxStringCharType*);
284 };
285
89360a8c
VS
286 #undef WX_DEFINE_STRINGIMPL_ITERATOR
287
a7ea63e2
VS
288
289 // constructors and destructor
290 // ctor for an empty string
291 wxStringImpl() { Init(); }
292 // copy ctor
293 wxStringImpl(const wxStringImpl& stringSrc)
294 {
295 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
296 _T("did you forget to call UngetWriteBuf()?") );
297
298 if ( stringSrc.empty() ) {
299 // nothing to do for an empty string
300 Init();
301 }
302 else {
303 m_pchData = stringSrc.m_pchData; // share same data
304 GetStringData()->Lock(); // => one more copy
305 }
306 }
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)
317 {
318 wxASSERT_MSG( str.GetStringData()->IsValid(),
319 _T("did you forget to call UngetWriteBuf()?") );
320 Init();
321 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
322 InitWith(str.c_str(), nPos, nLen);
323 }
f2a1b1bd
VZ
324 // take everything between start and end
325 wxStringImpl(const_iterator start, const_iterator end);
a7ea63e2 326
59953bf4
VS
327
328 // ctor from and conversion to std::string
329#if wxUSE_STD_STRING
330 wxStringImpl(const wxStdString& impl)
331 { InitWith(impl.c_str(), 0, impl.length()); }
332
333 operator wxStdString() const
334 { return wxStdString(c_str(), length()); }
335#endif
336
337
a7ea63e2
VS
338 // dtor is not virtual, this class must not be inherited from!
339 ~wxStringImpl()
340 {
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)
345#endif
346
347 GetStringData()->Unlock();
348 }
349
350#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
351 //re-enable inlining warning
352 #pragma warning (default:4714)
353#endif
354 // overloaded assignment
355 // from another wxString
356 wxStringImpl& operator=(const wxStringImpl& stringSrc);
357 // from a character
358 wxStringImpl& operator=(wxStringCharType ch);
359 // from a C string
360 wxStringImpl& operator=(const wxStringCharType *psz);
361
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; }
377
378 // lib.string.access
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)
384 {
385 wxASSERT_VALID_INDEX( n );
386 CopyBeforeWrite();
387 return m_pchData[n];
388 } // FIXME-UTF8: not useful for us...?
389
390 // lib.string.modifiers
391 // append elements str[pos], ..., str[pos+n]
392 wxStringImpl& append(const wxStringImpl& str, size_t pos, size_t n)
393 {
394 wxASSERT(pos <= str.length());
395 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
396 return *this;
397 }
398 // append a string
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)
81727065 403 { ConcatSelf(Strsize(sz), sz); return *this; }
a7ea63e2
VS
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; }
411
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)
81727065 420 { clear(); return append(sz, Strsize(sz)); }
a7ea63e2
VS
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); }
429
430 // first valid index position
431 const_iterator begin() const { return m_pchData; }
432 iterator begin();
433 // position one after the last valid one
434 const_iterator end() const { return m_pchData + length(); }
435 iterator end();
436
437 // insert another string
438 wxStringImpl& insert(size_t nPos, const wxStringImpl& str)
439 {
440 wxASSERT( str.GetStringData()->IsValid() );
441 return insert(nPos, str.c_str(), str.length());
442 }
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)
445 {
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);
451 }
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
81727065 455 wxStringImpl& insert(size_t nPos, size_t n, wxStringCharType ch)
a7ea63e2 456 { return insert(nPos, wxStringImpl(n, ch)); }
81727065 457 iterator insert(iterator it, wxStringCharType ch)
a7ea63e2
VS
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); }
463
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)
467 {
468 size_t idx = first - begin();
469 erase(idx, last - first);
470 return begin() + idx;
471 }
472 iterator erase(iterator first);
473
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; }
477
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,
494 size_type n)
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); }
503
504 // swap two strings
505 void swap(wxStringImpl& str);
506
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.
510
511 // find a substring
512 size_t find(const wxStringImpl& str, size_t nStart = 0) const;
513
514 // find first n characters of sz
515 size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
516
f2a1b1bd 517 // find the first occurrence of character ch after nStart
a7ea63e2
VS
518 size_t find(wxStringCharType ch, size_t nStart = 0) const;
519
520 // rfind() family is exactly like find() but works right to left
521
522 // as find, but from the end
523 size_t rfind(const wxStringImpl& str, size_t nStart = npos) const;
524
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;
530
531 size_type copy(wxStringCharType* s, size_type n, size_type pos = 0);
532
533 // substring extraction
534 wxStringImpl substr(size_t nStart = 0, size_t nLen = npos) const;
535
536 // string += string
537 wxStringImpl& operator+=(const wxStringImpl& s) { return append(s); }
538 // string += C string
539 wxStringImpl& operator+=(const wxStringCharType *psz) { return append(psz); }
540 // string += char
541 wxStringImpl& operator+=(wxStringCharType ch) { return append(1, ch); }
542
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);
548#endif
549
81727065
VS
550private:
551#if wxUSE_UNICODE_UTF8
552 static size_t Strsize(const wxStringCharType *s) { return strlen(s); }
553#else
554 static size_t Strsize(const wxStringCharType *s) { return wxStrlen(s); }
555#endif
556
a7ea63e2
VS
557 friend class WXDLLIMPEXP_BASE wxString;
558};
559
560#endif // !wxUSE_STL_BASED_WXSTRING
561
562// don't pollute the library user's name space
563#undef wxASSERT_VALID_INDEX
564
565#endif // _WX_WXSTRINGIMPL_H__