1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/arrstr.h
3 // Purpose: wxArrayString class
4 // Author: Mattia Barbon and Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
16 #include "wx/string.h"
18 // these functions are only used in STL build now but we define them in any
19 // case for compatibility with the existing code outside of the library which
20 // could be using them
21 inline int wxCMPFUNC_CONV
wxStringSortAscending(wxString
* s1
, wxString
* s2
)
26 inline int wxCMPFUNC_CONV
wxStringSortDescending(wxString
* s1
, wxString
* s2
)
28 return wxStringSortAscending(s2
, s1
);
33 #include "wx/dynarray.h"
35 typedef int (wxCMPFUNC_CONV
*CMPFUNCwxString
)(wxString
*, wxString
*);
36 typedef wxString _wxArraywxBaseArrayStringBase
;
37 _WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase
, wxBaseArrayStringBase
,
38 wxArray_SortFunction
<wxString
>,
39 class WXDLLIMPEXP_BASE
);
40 WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString
, wxArrayStringBase
,
41 wxBaseArrayStringBase
, WXDLLIMPEXP_BASE
);
42 _WX_DEFINE_SORTED_TYPEARRAY_2(wxString
, wxSortedArrayStringBase
,
43 wxBaseArrayStringBase
, = wxStringSortAscending
,
44 class WXDLLIMPEXP_BASE
, CMPFUNCwxString
);
46 class WXDLLIMPEXP_BASE wxArrayString
: public wxArrayStringBase
49 // type of function used by wxArrayString::Sort()
50 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
51 const wxString
& second
);
54 wxArrayString(const wxArrayString
& a
) : wxArrayStringBase(a
) { }
55 wxArrayString(size_t sz
, const char** a
);
56 wxArrayString(size_t sz
, const wchar_t** a
);
57 wxArrayString(size_t sz
, const wxString
* a
);
59 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
61 void Sort(bool reverseOrder
= false);
62 void Sort(CompareFunction function
);
63 void Sort(CMPFUNCwxString function
) { wxArrayStringBase::Sort(function
); }
65 size_t Add(const wxString
& string
, size_t copies
= 1)
67 wxArrayStringBase::Add(string
, copies
);
68 return size() - copies
;
72 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
75 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
77 wxSortedArrayString(const wxSortedArrayString
& array
)
78 : wxSortedArrayStringBase(array
)
80 wxSortedArrayString(const wxArrayString
& src
)
81 : wxSortedArrayStringBase(wxStringSortAscending
)
85 for ( size_t n
= 0; n
< src
.size(); n
++ )
89 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
92 #else // if !wxUSE_STL
94 // this shouldn't be defined for compilers not supporting template methods or
95 // without std::distance() -- and if all of the currently supported compilers
96 // do have it, then it can just be removed and wxHAS_VECTOR_TEMPLATE_ASSIGN
98 #define wxHAS_VECTOR_TEMPLATE_ASSIGN
100 #ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
101 #include "wx/beforestd.h"
103 #include "wx/afterstd.h"
104 #endif // wxHAS_VECTOR_TEMPLATE_ASSIGN
106 class WXDLLIMPEXP_BASE wxArrayString
109 // type of function used by wxArrayString::Sort()
110 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
111 const wxString
& second
);
112 // type of function used by wxArrayString::Sort(), for compatibility with
114 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
117 // constructors and destructor
119 wxArrayString() { Init(false); }
120 // if autoSort is true, the array is always sorted (in alphabetical order)
122 // NB: the reason for using int and not bool is that like this we can avoid
123 // using this ctor for implicit conversions from "const char *" (which
124 // we'd like to be implicitly converted to wxString instead!)
126 // of course, using explicit would be even better - if all compilers
128 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
129 // C string array ctor
130 wxArrayString(size_t sz
, const char** a
);
131 wxArrayString(size_t sz
, const wchar_t** a
);
132 // wxString string array ctor
133 wxArrayString(size_t sz
, const wxString
* a
);
135 wxArrayString(const wxArrayString
& array
);
136 // assignment operator
137 wxArrayString
& operator=(const wxArrayString
& src
);
138 // not virtual, this class should not be derived from
142 // empties the list, but doesn't release memory
144 // empties the list and releases memory
146 // preallocates memory for given number of items
147 void Alloc(size_t nCount
);
148 // minimzes the memory usage (by freeing all extra memory)
152 // number of elements in the array
153 size_t GetCount() const { return m_nCount
; }
155 bool IsEmpty() const { return m_nCount
== 0; }
156 // number of elements in the array (GetCount is preferred API)
157 size_t Count() const { return m_nCount
; }
159 // items access (range checking is done in debug version)
160 // get item at position uiIndex
161 wxString
& Item(size_t nIndex
) const
163 wxASSERT_MSG( nIndex
< m_nCount
,
164 _T("wxArrayString: index out of bounds") );
166 return m_pItems
[nIndex
];
170 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
172 wxString
& Last() const
174 wxASSERT_MSG( !IsEmpty(),
175 _T("wxArrayString: index out of bounds") );
176 return Item(GetCount() - 1);
181 // Search the element in the array, starting from the beginning if
182 // bFromEnd is false or from end otherwise. If bCase, comparison is case
183 // sensitive (default). Returns index of the first item matched or
185 int Index (const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
186 // add new element at the end (if the array is not sorted), return its
188 size_t Add(const wxString
& str
, size_t nInsert
= 1);
189 // add new element at given position
190 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
191 // expand the array to have count elements
192 void SetCount(size_t count
);
193 // remove first item matching this value
194 void Remove(const wxString
& sz
);
195 // remove item by index
196 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
199 // sort array elements in alphabetical order (or reversed alphabetical
200 // order if reverseOrder parameter is true)
201 void Sort(bool reverseOrder
= false);
202 // sort array elements using specified comparison function
203 void Sort(CompareFunction compareFunction
);
204 void Sort(CompareFunction2 compareFunction
);
207 // compare two arrays case sensitively
208 bool operator==(const wxArrayString
& a
) const;
209 // compare two arrays case sensitively
210 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
212 // STL-like interface
213 typedef wxString value_type
;
214 typedef value_type
* pointer
;
215 typedef const value_type
* const_pointer
;
216 typedef value_type
* iterator
;
217 typedef const value_type
* const_iterator
;
218 typedef value_type
& reference
;
219 typedef const value_type
& const_reference
;
220 typedef int difference_type
;
221 typedef size_t size_type
;
223 // TODO: this code duplicates the one in dynarray.h
224 class reverse_iterator
226 typedef wxString value_type
;
227 typedef value_type
* pointer
;
228 typedef value_type
& reference
;
229 typedef reverse_iterator itor
;
230 friend itor
operator+(int o
, const itor
& it
);
231 friend itor
operator+(const itor
& it
, int o
);
232 friend itor
operator-(const itor
& it
, int o
);
233 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
236 reverse_iterator() : m_ptr(NULL
) { }
237 reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
238 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
239 reference
operator*() const { return *m_ptr
; }
240 pointer
operator->() const { return m_ptr
; }
241 itor
& operator++() { --m_ptr
; return *this; }
242 const itor
operator++(int)
243 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
244 itor
& operator--() { ++m_ptr
; return *this; }
245 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
246 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
247 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
250 class const_reverse_iterator
252 typedef wxString value_type
;
253 typedef const value_type
* pointer
;
254 typedef const value_type
& reference
;
255 typedef const_reverse_iterator itor
;
256 friend itor
operator+(int o
, const itor
& it
);
257 friend itor
operator+(const itor
& it
, int o
);
258 friend itor
operator-(const itor
& it
, int o
);
259 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
262 const_reverse_iterator() : m_ptr(NULL
) { }
263 const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
264 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
265 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
266 reference
operator*() const { return *m_ptr
; }
267 pointer
operator->() const { return m_ptr
; }
268 itor
& operator++() { --m_ptr
; return *this; }
269 const itor
operator++(int)
270 { itor tmp
= *this; --m_ptr
; return tmp
; }
271 itor
& operator--() { ++m_ptr
; return *this; }
272 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
273 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
274 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
277 wxArrayString(const_iterator first
, const_iterator last
)
278 { Init(false); assign(first
, last
); }
279 wxArrayString(size_type n
, const_reference v
) { Init(false); assign(n
, v
); }
281 #ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
282 template <class Iterator
>
283 void assign(Iterator first
, Iterator last
)
286 reserve(std::distance(first
, last
));
287 for(; first
!= last
; ++first
)
290 #else // !wxHAS_VECTOR_TEMPLATE_ASSIGN
291 void assign(const_iterator first
, const_iterator last
)
294 reserve(last
- first
);
295 for(; first
!= last
; ++first
)
298 #endif // wxHAS_VECTOR_TEMPLATE_ASSIGN/!wxHAS_VECTOR_TEMPLATE_ASSIGN
300 void assign(size_type n
, const_reference v
)
301 { clear(); Add(v
, n
); }
302 reference
back() { return *(end() - 1); }
303 const_reference
back() const { return *(end() - 1); }
304 iterator
begin() { return m_pItems
; }
305 const_iterator
begin() const { return m_pItems
; }
306 size_type
capacity() const { return m_nSize
; }
307 void clear() { Clear(); }
308 bool empty() const { return IsEmpty(); }
309 iterator
end() { return begin() + GetCount(); }
310 const_iterator
end() const { return begin() + GetCount(); }
311 iterator
erase(iterator first
, iterator last
)
313 size_t idx
= first
- begin();
314 RemoveAt(idx
, last
- first
);
315 return begin() + idx
;
317 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
318 reference
front() { return *begin(); }
319 const_reference
front() const { return *begin(); }
320 void insert(iterator it
, size_type n
, const_reference v
)
321 { Insert(v
, it
- begin(), n
); }
322 iterator
insert(iterator it
, const_reference v
= value_type())
323 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
324 void insert(iterator it
, const_iterator first
, const_iterator last
);
325 size_type
max_size() const { return INT_MAX
; }
326 void pop_back() { RemoveAt(GetCount() - 1); }
327 void push_back(const_reference v
) { Add(v
); }
328 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
329 const_reverse_iterator
rbegin() const
330 { return const_reverse_iterator(end() - 1); }
331 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
332 const_reverse_iterator
rend() const
333 { return const_reverse_iterator(begin() - 1); }
334 void reserve(size_type n
) /* base::reserve*/;
335 void resize(size_type n
, value_type v
= value_type());
336 size_type
size() const { return GetCount(); }
337 void swap(wxArrayString
& other
)
339 wxSwap(m_nSize
, other
.m_nSize
);
340 wxSwap(m_nCount
, other
.m_nCount
);
341 wxSwap(m_pItems
, other
.m_pItems
);
342 wxSwap(m_autoSort
, other
.m_autoSort
);
346 void Init(bool autoSort
); // common part of all ctors
347 void Copy(const wxArrayString
& src
); // copies the contents of another array
350 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
352 size_t m_nSize
, // current size of the array
353 m_nCount
; // current number of elements
355 wxString
*m_pItems
; // pointer to data
357 bool m_autoSort
; // if true, keep the array always sorted
360 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
363 wxSortedArrayString() : wxArrayString(true)
365 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
371 // this class provides a temporary wxString* from a
373 class WXDLLIMPEXP_BASE wxCArrayString
376 wxCArrayString( const wxArrayString
& array
)
377 : m_array( array
), m_strings( NULL
)
379 ~wxCArrayString() { delete[] m_strings
; }
381 size_t GetCount() const { return m_array
.GetCount(); }
382 wxString
* GetStrings()
384 if( m_strings
) return m_strings
;
385 size_t count
= m_array
.GetCount();
386 m_strings
= new wxString
[count
];
387 for( size_t i
= 0; i
< count
; ++i
)
388 m_strings
[i
] = m_array
[i
];
394 wxString
*r
= GetStrings();
400 const wxArrayString
& m_array
;
405 // ----------------------------------------------------------------------------
406 // helper functions for working with arrays
407 // ----------------------------------------------------------------------------
409 // by default, these functions use the escape character to escape the
410 // separators occuring inside the string to be joined, this can be disabled by
411 // passing '\0' as escape
413 WXDLLIMPEXP_BASE wxString
wxJoin(const wxArrayString
& arr
,
415 const wxChar escape
= wxT('\\'));
417 WXDLLIMPEXP_BASE wxArrayString
wxSplit(const wxString
& str
,
419 const wxChar escape
= wxT('\\'));
422 // ----------------------------------------------------------------------------
423 // This helper class allows to pass both C array of wxStrings or wxArrayString
424 // using the same interface.
426 // Use it when you have two methods taking wxArrayString or (int, wxString[]),
427 // that do the same thing. This class lets you iterate over input data in the
428 // same way whether it is a raw array of strings or wxArrayString.
430 // The object does not take ownership of the data -- internally it keeps
431 // pointers to the data, therefore the data must be disposed of by user
432 // and only after this object is destroyed. Usually it is not a problem as
433 // only temporary objects of this class are used.
434 // ----------------------------------------------------------------------------
436 class wxArrayStringsAdapter
439 // construct an adapter from a wxArrayString
440 wxArrayStringsAdapter(const wxArrayString
& strings
)
441 : m_type(wxSTRING_ARRAY
), m_size(strings
.size())
443 m_data
.array
= &strings
;
446 // construct an adapter from a wxString[]
447 wxArrayStringsAdapter(unsigned int n
, const wxString
*strings
)
448 : m_type(wxSTRING_POINTER
), m_size(n
)
450 m_data
.ptr
= strings
;
453 // construct an adapter from a single wxString
454 wxArrayStringsAdapter(const wxString
& s
)
455 : m_type(wxSTRING_POINTER
), m_size(1)
460 // default copy constructor is ok
462 // iteration interface
463 size_t GetCount() const { return m_size
; }
464 bool IsEmpty() const { return GetCount() == 0; }
465 const wxString
& operator[] (unsigned int i
) const
467 wxASSERT_MSG( i
< GetCount(), wxT("index out of bounds") );
468 if(m_type
== wxSTRING_POINTER
)
469 return m_data
.ptr
[i
];
470 return m_data
.array
->Item(i
);
472 wxArrayString
AsArrayString() const
474 if(m_type
== wxSTRING_ARRAY
)
475 return *m_data
.array
;
476 return wxArrayString(GetCount(), m_data
.ptr
);
480 // type of the data being held
481 enum wxStringContainerType
483 wxSTRING_ARRAY
, // wxArrayString
484 wxSTRING_POINTER
// wxString[]
487 wxStringContainerType m_type
;
491 const wxString
* ptr
;
492 const wxArrayString
* array
;
495 wxDECLARE_NO_ASSIGN_CLASS(wxArrayStringsAdapter
);
498 #endif // _WX_ARRSTR_H