]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stringimpl.h
provide overloads for all kinds of strings for wxDataFormat ctor to allow passing...
[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
2523e9b7
VS
26#include "wx/chartype.h" // for wxChar
27#include "wx/wxcrt.h" // for wxStrlen() etc.
a7ea63e2 28
745817ff
VS
29#include <stdlib.h>
30
a7ea63e2
VS
31// ---------------------------------------------------------------------------
32// macros
33// ---------------------------------------------------------------------------
34
35// implementation only
36#define wxASSERT_VALID_INDEX(i) \
37 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
38
39
40// ----------------------------------------------------------------------------
41// global data
42// ----------------------------------------------------------------------------
43
44// global pointer to empty string
45extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
81727065
VS
46#if wxUSE_UNICODE_UTF8
47// FIXME-UTF8: we should have only one wxEmptyString
48extern WXDLLIMPEXP_DATA_BASE(const wxStringCharType*) wxEmptyStringImpl;
49#endif
a7ea63e2
VS
50
51
52// ----------------------------------------------------------------------------
53// deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
54// ----------------------------------------------------------------------------
55
56#define wxUSE_STL_BASED_WXSTRING wxUSE_STL
57
58// in both cases we need to define wxStdString
59#if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
60
61#include "wx/beforestd.h"
62#include <string>
63#include "wx/afterstd.h"
64
59953bf4
VS
65#ifdef HAVE_STD_WSTRING
66 typedef std::wstring wxStdWideString;
67#else
68 typedef std::basic_string<wchar_t> wxStdWideString;
69#endif
70
a7ea63e2 71#if wxUSE_UNICODE_WCHAR
59953bf4 72 typedef wxStdWideString wxStdString;
a7ea63e2
VS
73#else
74 typedef std::string wxStdString;
75#endif
76
59953bf4
VS
77#endif // wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
78
a7ea63e2
VS
79
80#if wxUSE_STL_BASED_WXSTRING
81
82 // we always want ctor from std::string when using std::string internally
83 #undef wxUSE_STD_STRING
84 #define wxUSE_STD_STRING 1
85
86 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
87 (defined(_MSC_VER) && (_MSC_VER <= 1200))
88 #define wxSTRING_BASE_HASNT_CLEAR
89 #endif
90
91 typedef wxStdString wxStringImpl;
92#else // if !wxUSE_STL_BASED_WXSTRING
93
94// in non-STL mode, compare() is implemented in wxString and not wxStringImpl
95#undef HAVE_STD_STRING_COMPARE
96
97// ---------------------------------------------------------------------------
98// string data prepended with some housekeeping info (used by wxString class),
99// is never used directly (but had to be put here to allow inlining)
100// ---------------------------------------------------------------------------
101
102struct WXDLLIMPEXP_BASE wxStringData
103{
104 int nRefs; // reference count
105 size_t nDataLength, // actual string length
106 nAllocLength; // allocated memory size
107
81727065
VS
108 // mimics declaration 'wxStringCharType data[nAllocLength]'
109 wxStringCharType* data() const { return (wxStringCharType*)(this + 1); }
a7ea63e2
VS
110
111 // empty string has a special ref count so it's never deleted
112 bool IsEmpty() const { return (nRefs == -1); }
113 bool IsShared() const { return (nRefs > 1); }
114
115 // lock/unlock
116 void Lock() { if ( !IsEmpty() ) nRefs++; }
117
118 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
119#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
120 __forceinline
121#endif
122 // VC++ free must take place in same DLL as allocation when using non dll
123 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
124#if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
125 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
126 // we must not inline deallocation since allocation is not inlined
127 void Free();
128#else
129 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
130#endif
131
132 // if we had taken control over string memory (GetWriteBuf), it's
133 // intentionally put in invalid state
134 void Validate(bool b) { nRefs = (b ? 1 : 0); }
135 bool IsValid() const { return (nRefs != 0); }
136};
137
138class WXDLLIMPEXP_BASE wxStringImpl
139{
140public:
141 // an 'invalid' value for string index, moved to this place due to a CW bug
142 static const size_t npos;
143
144protected:
145 // points to data preceded by wxStringData structure with ref count info
146 wxStringCharType *m_pchData;
147
148 // accessor to string data
149 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
150
151 // string (re)initialization functions
152 // initializes the string to the empty value (must be called only from
153 // ctors, use Reinit() otherwise)
81727065
VS
154#if wxUSE_UNICODE_UTF8
155 void Init() { m_pchData = (wxStringCharType *)wxEmptyStringImpl; } // FIXME-UTF8
156#else
a7ea63e2 157 void Init() { m_pchData = (wxStringCharType *)wxEmptyString; }
81727065 158#endif
a7ea63e2
VS
159 // initializes the string with (a part of) C-string
160 void InitWith(const wxStringCharType *psz, size_t nPos = 0, size_t nLen = npos);
161 // as Init, but also frees old data
162 void Reinit() { GetStringData()->Unlock(); Init(); }
163
164 // memory allocation
165 // allocates memory for string of length nLen
166 bool AllocBuffer(size_t nLen);
167 // effectively copies data to string
168 bool AssignCopy(size_t, const wxStringCharType *);
169
170 // append a (sub)string
171 bool ConcatSelf(size_t nLen, const wxStringCharType *src, size_t nMaxLen);
172 bool ConcatSelf(size_t nLen, const wxStringCharType *src)
173 { return ConcatSelf(nLen, src, nLen); }
174
175 // functions called before writing to the string: they copy it if there
176 // are other references to our data (should be the only owner when writing)
177 bool CopyBeforeWrite();
178 bool AllocBeforeWrite(size_t);
179
180 // compatibility with wxString
181 bool Alloc(size_t nLen);
182
183public:
184 // standard types
185 typedef wxStringCharType value_type;
186 typedef wxStringCharType char_type;
187 typedef size_t size_type;
188 typedef value_type& reference;
189 typedef const value_type& const_reference;
190 typedef value_type* pointer;
191 typedef const value_type* const_pointer;
f2a1b1bd
VZ
192
193 // macro to define the bulk of iterator and const_iterator classes
194 #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
195 public: \
196 typedef wxStringCharType value_type; \
197 typedef ref_type reference; \
198 typedef ptr_type pointer; \
89360a8c 199 typedef int difference_type; \
f2a1b1bd
VZ
200 \
201 iterator_name(pointer ptr) : m_ptr(ptr) { } \
202 \
203 reference operator*() const { return *m_ptr; } \
204 \
205 iterator_name& operator++() { m_ptr++; return *this; } \
206 iterator_name operator++(int) \
207 { \
208 const iterator_name tmp(*this); \
209 m_ptr++; \
210 return tmp; \
211 } \
212 \
213 iterator_name& operator--() { m_ptr--; return *this; } \
214 iterator_name operator--(int) \
215 { \
216 const iterator_name tmp(*this); \
217 m_ptr--; \
218 return tmp; \
219 } \
220 \
221 iterator_name operator+(int n) const \
222 { return iterator_name(m_ptr + n); } \
15c12109
VZ
223 iterator_name operator+(size_t n) const \
224 { return iterator_name(m_ptr + n); } \
f2a1b1bd
VZ
225 iterator_name operator-(int n) const \
226 { return iterator_name(m_ptr - n); } \
15c12109
VZ
227 iterator_name operator-(size_t n) const \
228 { return iterator_name(m_ptr - n); } \
f2a1b1bd
VZ
229 iterator_name& operator+=(int n) \
230 { m_ptr += n; return *this; } \
15c12109
VZ
231 iterator_name& operator+=(size_t n) \
232 { m_ptr += n; return *this; } \
f2a1b1bd 233 iterator_name& operator-=(int n) \
15c12109
VZ
234 { m_ptr -= n; return *this; } \
235 iterator_name& operator-=(size_t n) \
f2a1b1bd
VZ
236 { m_ptr -= n; return *this; } \
237 \
89360a8c 238 difference_type operator-(const iterator_name& i) const \
f2a1b1bd
VZ
239 { return m_ptr - i.m_ptr; } \
240 \
241 bool operator==(const iterator_name& i) const \
242 { return m_ptr == i.m_ptr; } \
243 bool operator!=(const iterator_name& i) const \
244 { return m_ptr != i.m_ptr; } \
245 \
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; } \
250 bool operator<=(const iterator_name& i) const \
251 { return m_ptr <= i.m_ptr; } \
252 bool operator>=(const iterator_name& i) const \
253 { return m_ptr >= i.m_ptr; } \
254 \
255 private: \
256 /* for wxStringImpl use only */ \
257 operator pointer() const { return m_ptr; } \
258 \
259 friend class WXDLLIMPEXP_BASE wxStringImpl; \
260 \
261 pointer m_ptr
262
7c2b62db
VZ
263 // we need to declare const_iterator in wxStringImpl scope, the friend
264 // declaration inside iterator class itself is not enough, or at least not
265 // for g++ 3.4 (g++ 4 is ok)
266 class const_iterator;
267
f2a1b1bd
VZ
268 class iterator
269 {
270 WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
271 wxStringCharType&,
272 wxStringCharType*);
273
274 friend class const_iterator;
275 };
276
277 class const_iterator
278 {
279 public:
280 const_iterator(iterator i) : m_ptr(i.m_ptr) { }
281
282 WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator,
283 const wxStringCharType&,
284 const wxStringCharType*);
285 };
286
89360a8c
VS
287 #undef WX_DEFINE_STRINGIMPL_ITERATOR
288
a7ea63e2
VS
289
290 // constructors and destructor
291 // ctor for an empty string
292 wxStringImpl() { Init(); }
293 // copy ctor
294 wxStringImpl(const wxStringImpl& stringSrc)
295 {
296 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
297 _T("did you forget to call UngetWriteBuf()?") );
298
299 if ( stringSrc.empty() ) {
300 // nothing to do for an empty string
301 Init();
302 }
303 else {
304 m_pchData = stringSrc.m_pchData; // share same data
305 GetStringData()->Lock(); // => one more copy
306 }
307 }
308 // string containing nRepeat copies of ch
309 wxStringImpl(size_type nRepeat, wxStringCharType ch);
310 // ctor takes first nLength characters from C string
311 // (default value of npos means take all the string)
312 wxStringImpl(const wxStringCharType *psz)
313 { InitWith(psz, 0, npos); }
314 wxStringImpl(const wxStringCharType *psz, size_t nLength)
315 { InitWith(psz, 0, nLength); }
316 // take nLen chars starting at nPos
317 wxStringImpl(const wxStringImpl& str, size_t nPos, size_t nLen)
318 {
319 wxASSERT_MSG( str.GetStringData()->IsValid(),
320 _T("did you forget to call UngetWriteBuf()?") );
321 Init();
322 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
323 InitWith(str.c_str(), nPos, nLen);
324 }
f2a1b1bd
VZ
325 // take everything between start and end
326 wxStringImpl(const_iterator start, const_iterator end);
a7ea63e2 327
59953bf4
VS
328
329 // ctor from and conversion to std::string
330#if wxUSE_STD_STRING
331 wxStringImpl(const wxStdString& impl)
332 { InitWith(impl.c_str(), 0, impl.length()); }
333
334 operator wxStdString() const
335 { return wxStdString(c_str(), length()); }
336#endif
337
338
a7ea63e2
VS
339 // dtor is not virtual, this class must not be inherited from!
340 ~wxStringImpl()
341 {
342#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
343 //RN - according to the above VC++ does indeed inline this,
344 //even though it spits out two warnings
345 #pragma warning (disable:4714)
346#endif
347
348 GetStringData()->Unlock();
349 }
350
351#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
352 //re-enable inlining warning
353 #pragma warning (default:4714)
354#endif
355 // overloaded assignment
356 // from another wxString
357 wxStringImpl& operator=(const wxStringImpl& stringSrc);
358 // from a character
359 wxStringImpl& operator=(wxStringCharType ch);
360 // from a C string
361 wxStringImpl& operator=(const wxStringCharType *psz);
362
363 // return the length of the string
364 size_type length() const { return GetStringData()->nDataLength; }
365 // return the length of the string
366 size_type size() const { return length(); }
367 // return the maximum size of the string
368 size_type max_size() const { return npos; }
369 // resize the string, filling the space with c if c != 0
370 void resize(size_t nSize, wxStringCharType ch = '\0');
371 // delete the contents of the string
372 void clear() { erase(0, npos); }
373 // returns true if the string is empty
374 bool empty() const { return length() == 0; }
375 // inform string about planned change in size
376 void reserve(size_t sz) { Alloc(sz); }
377 size_type capacity() const { return GetStringData()->nAllocLength; }
378
379 // lib.string.access
380 // return the character at position n
381 value_type at(size_type n) const
382 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
383 // returns the writable character at position n
384 reference at(size_type n)
385 {
386 wxASSERT_VALID_INDEX( n );
387 CopyBeforeWrite();
388 return m_pchData[n];
389 } // FIXME-UTF8: not useful for us...?
390
391 // lib.string.modifiers
392 // append elements str[pos], ..., str[pos+n]
393 wxStringImpl& append(const wxStringImpl& str, size_t pos, size_t n)
394 {
395 wxASSERT(pos <= str.length());
396 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
397 return *this;
398 }
399 // append a string
400 wxStringImpl& append(const wxStringImpl& str)
401 { ConcatSelf(str.length(), str.c_str()); return *this; }
402 // append first n (or all if n == npos) characters of sz
403 wxStringImpl& append(const wxStringCharType *sz)
81727065 404 { ConcatSelf(Strsize(sz), sz); return *this; }
a7ea63e2
VS
405 wxStringImpl& append(const wxStringCharType *sz, size_t n)
406 { ConcatSelf(n, sz); return *this; }
407 // append n copies of ch
408 wxStringImpl& append(size_t n, wxStringCharType ch);
409 // append from first to last
410 wxStringImpl& append(const_iterator first, const_iterator last)
411 { ConcatSelf(last - first, first); return *this; }
412
413 // same as `this_string = str'
414 wxStringImpl& assign(const wxStringImpl& str)
415 { return *this = str; }
416 // same as ` = str[pos..pos + n]
417 wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n)
418 { clear(); return append(str, pos, n); }
419 // same as `= first n (or all if n == npos) characters of sz'
420 wxStringImpl& assign(const wxStringCharType *sz)
81727065 421 { clear(); return append(sz, Strsize(sz)); }
a7ea63e2
VS
422 wxStringImpl& assign(const wxStringCharType *sz, size_t n)
423 { clear(); return append(sz, n); }
424 // same as `= n copies of ch'
425 wxStringImpl& assign(size_t n, wxStringCharType ch)
426 { clear(); return append(n, ch); }
427 // assign from first to last
428 wxStringImpl& assign(const_iterator first, const_iterator last)
429 { clear(); return append(first, last); }
430
431 // first valid index position
432 const_iterator begin() const { return m_pchData; }
433 iterator begin();
434 // position one after the last valid one
435 const_iterator end() const { return m_pchData + length(); }
436 iterator end();
437
438 // insert another string
439 wxStringImpl& insert(size_t nPos, const wxStringImpl& str)
440 {
441 wxASSERT( str.GetStringData()->IsValid() );
442 return insert(nPos, str.c_str(), str.length());
443 }
444 // insert n chars of str starting at nStart (in str)
445 wxStringImpl& insert(size_t nPos, const wxStringImpl& str, size_t nStart, size_t n)
446 {
447 wxASSERT( str.GetStringData()->IsValid() );
448 wxASSERT( nStart < str.length() );
449 size_t strLen = str.length() - nStart;
450 n = strLen < n ? strLen : n;
451 return insert(nPos, str.c_str() + nStart, n);
452 }
453 // insert first n (or all if n == npos) characters of sz
454 wxStringImpl& insert(size_t nPos, const wxStringCharType *sz, size_t n = npos);
455 // insert n copies of ch
81727065 456 wxStringImpl& insert(size_t nPos, size_t n, wxStringCharType ch)
a7ea63e2 457 { return insert(nPos, wxStringImpl(n, ch)); }
81727065 458 iterator insert(iterator it, wxStringCharType ch)
a7ea63e2
VS
459 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
460 void insert(iterator it, const_iterator first, const_iterator last)
461 { insert(it - begin(), first, last - first); }
462 void insert(iterator it, size_type n, wxStringCharType ch)
463 { insert(it - begin(), n, ch); }
464
465 // delete characters from nStart to nStart + nLen
466 wxStringImpl& erase(size_type pos = 0, size_type n = npos);
467 iterator erase(iterator first, iterator last)
468 {
469 size_t idx = first - begin();
470 erase(idx, last - first);
471 return begin() + idx;
472 }
473 iterator erase(iterator first);
474
475 // explicit conversion to C string (use this with printf()!)
476 const wxStringCharType* c_str() const { return m_pchData; }
477 const wxStringCharType* data() const { return m_pchData; }
478
479 // replaces the substring of length nLen starting at nStart
480 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz);
481 // replaces the substring of length nLen starting at nStart
482 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str)
483 { return replace(nStart, nLen, str.c_str()); }
484 // replaces the substring with nCount copies of ch
485 wxStringImpl& replace(size_t nStart, size_t nLen, size_t nCount, wxStringCharType ch);
486 // replaces a substring with another substring
487 wxStringImpl& replace(size_t nStart, size_t nLen,
488 const wxStringImpl& str, size_t nStart2, size_t nLen2);
489 // replaces the substring with first nCount chars of sz
490 wxStringImpl& replace(size_t nStart, size_t nLen,
491 const wxStringCharType* sz, size_t nCount);
492 wxStringImpl& replace(iterator first, iterator last, const_pointer s)
493 { return replace(first - begin(), last - first, s); }
494 wxStringImpl& replace(iterator first, iterator last, const_pointer s,
495 size_type n)
496 { return replace(first - begin(), last - first, s, n); }
497 wxStringImpl& replace(iterator first, iterator last, const wxStringImpl& s)
498 { return replace(first - begin(), last - first, s); }
499 wxStringImpl& replace(iterator first, iterator last, size_type n, wxStringCharType c)
500 { return replace(first - begin(), last - first, n, c); }
501 wxStringImpl& replace(iterator first, iterator last,
502 const_iterator first1, const_iterator last1)
503 { return replace(first - begin(), last - first, first1, last1 - first1); }
504
505 // swap two strings
506 void swap(wxStringImpl& str);
507
508 // All find() functions take the nStart argument which specifies the
509 // position to start the search on, the default value is 0. All functions
510 // return npos if there were no match.
511
512 // find a substring
513 size_t find(const wxStringImpl& str, size_t nStart = 0) const;
514
515 // find first n characters of sz
516 size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
517
f2a1b1bd 518 // find the first occurrence of character ch after nStart
a7ea63e2
VS
519 size_t find(wxStringCharType ch, size_t nStart = 0) const;
520
521 // rfind() family is exactly like find() but works right to left
522
523 // as find, but from the end
524 size_t rfind(const wxStringImpl& str, size_t nStart = npos) const;
525
526 // as find, but from the end
527 size_t rfind(const wxStringCharType* sz, size_t nStart = npos,
528 size_t n = npos) const;
529 // as find, but from the end
530 size_t rfind(wxStringCharType ch, size_t nStart = npos) const;
531
532 size_type copy(wxStringCharType* s, size_type n, size_type pos = 0);
533
534 // substring extraction
535 wxStringImpl substr(size_t nStart = 0, size_t nLen = npos) const;
536
537 // string += string
538 wxStringImpl& operator+=(const wxStringImpl& s) { return append(s); }
539 // string += C string
540 wxStringImpl& operator+=(const wxStringCharType *psz) { return append(psz); }
541 // string += char
542 wxStringImpl& operator+=(wxStringCharType ch) { return append(1, ch); }
543
a7ea63e2
VS
544 // helpers for wxStringBuffer and wxStringBufferLength
545 wxStringCharType *DoGetWriteBuf(size_t nLen);
546 void DoUngetWriteBuf();
547 void DoUngetWriteBuf(size_t nLen);
a7ea63e2 548
81727065
VS
549private:
550#if wxUSE_UNICODE_UTF8
551 static size_t Strsize(const wxStringCharType *s) { return strlen(s); }
552#else
553 static size_t Strsize(const wxStringCharType *s) { return wxStrlen(s); }
554#endif
555
a7ea63e2
VS
556 friend class WXDLLIMPEXP_BASE wxString;
557};
558
559#endif // !wxUSE_STL_BASED_WXSTRING
560
561// don't pollute the library user's name space
562#undef wxASSERT_VALID_INDEX
563
564#endif // _WX_WXSTRINGIMPL_H__