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 class WXDLLIMPEXP_BASE wxArrayString
97 // type of function used by wxArrayString::Sort()
98 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
99 const wxString
& second
);
100 // type of function used by wxArrayString::Sort(), for compatibility with
102 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
105 // constructors and destructor
107 wxArrayString() { Init(false); }
108 // if autoSort is true, the array is always sorted (in alphabetical order)
110 // NB: the reason for using int and not bool is that like this we can avoid
111 // using this ctor for implicit conversions from "const char *" (which
112 // we'd like to be implicitly converted to wxString instead!)
114 // of course, using explicit would be even better - if all compilers
116 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
117 // C string array ctor
118 wxArrayString(size_t sz
, const char** a
);
119 wxArrayString(size_t sz
, const wchar_t** a
);
120 // wxString string array ctor
121 wxArrayString(size_t sz
, const wxString
* a
);
123 wxArrayString(const wxArrayString
& array
);
124 // assignment operator
125 wxArrayString
& operator=(const wxArrayString
& src
);
126 // not virtual, this class should not be derived from
130 // empties the list, but doesn't release memory
132 // empties the list and releases memory
134 // preallocates memory for given number of items
135 void Alloc(size_t nCount
);
136 // minimzes the memory usage (by freeing all extra memory)
140 // number of elements in the array
141 size_t GetCount() const { return m_nCount
; }
143 bool IsEmpty() const { return m_nCount
== 0; }
144 // number of elements in the array (GetCount is preferred API)
145 size_t Count() const { return m_nCount
; }
147 // items access (range checking is done in debug version)
148 // get item at position uiIndex
149 wxString
& Item(size_t nIndex
) const
151 wxASSERT_MSG( nIndex
< m_nCount
,
152 _T("wxArrayString: index out of bounds") );
154 return m_pItems
[nIndex
];
158 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
160 wxString
& Last() const
162 wxASSERT_MSG( !IsEmpty(),
163 _T("wxArrayString: index out of bounds") );
164 return Item(GetCount() - 1);
169 // Search the element in the array, starting from the beginning if
170 // bFromEnd is false or from end otherwise. If bCase, comparison is case
171 // sensitive (default). Returns index of the first item matched or
173 int Index (const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
174 // add new element at the end (if the array is not sorted), return its
176 size_t Add(const wxString
& str
, size_t nInsert
= 1);
177 // add new element at given position
178 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
179 // expand the array to have count elements
180 void SetCount(size_t count
);
181 // remove first item matching this value
182 void Remove(const wxString
& sz
);
183 // remove item by index
184 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
187 // sort array elements in alphabetical order (or reversed alphabetical
188 // order if reverseOrder parameter is true)
189 void Sort(bool reverseOrder
= false);
190 // sort array elements using specified comparison function
191 void Sort(CompareFunction compareFunction
);
192 void Sort(CompareFunction2 compareFunction
);
195 // compare two arrays case sensitively
196 bool operator==(const wxArrayString
& a
) const;
197 // compare two arrays case sensitively
198 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
200 // STL-like interface
201 typedef wxString value_type
;
202 typedef value_type
* pointer
;
203 typedef const value_type
* const_pointer
;
204 typedef value_type
* iterator
;
205 typedef const value_type
* const_iterator
;
206 typedef value_type
& reference
;
207 typedef const value_type
& const_reference
;
208 typedef int difference_type
;
209 typedef size_t size_type
;
211 // TODO: this code duplicates the one in dynarray.h
212 class reverse_iterator
214 typedef wxString value_type
;
215 typedef value_type
* pointer
;
216 typedef value_type
& reference
;
217 typedef reverse_iterator itor
;
218 friend itor
operator+(int o
, const itor
& it
);
219 friend itor
operator+(const itor
& it
, int o
);
220 friend itor
operator-(const itor
& it
, int o
);
221 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
224 reverse_iterator() : m_ptr(NULL
) { }
225 reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
226 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
227 reference
operator*() const { return *m_ptr
; }
228 pointer
operator->() const { return m_ptr
; }
229 itor
& operator++() { --m_ptr
; return *this; }
230 const itor
operator++(int)
231 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
232 itor
& operator--() { ++m_ptr
; return *this; }
233 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
234 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
235 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
238 class const_reverse_iterator
240 typedef wxString value_type
;
241 typedef const value_type
* pointer
;
242 typedef const value_type
& reference
;
243 typedef const_reverse_iterator itor
;
244 friend itor
operator+(int o
, const itor
& it
);
245 friend itor
operator+(const itor
& it
, int o
);
246 friend itor
operator-(const itor
& it
, int o
);
247 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
250 const_reverse_iterator() : m_ptr(NULL
) { }
251 const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
252 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
253 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
254 reference
operator*() const { return *m_ptr
; }
255 pointer
operator->() const { return m_ptr
; }
256 itor
& operator++() { --m_ptr
; return *this; }
257 const itor
operator++(int)
258 { itor tmp
= *this; --m_ptr
; return tmp
; }
259 itor
& operator--() { ++m_ptr
; return *this; }
260 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
261 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
262 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
265 wxArrayString(const_iterator first
, const_iterator last
)
266 { Init(false); assign(first
, last
); }
267 wxArrayString(size_type n
, const_reference v
) { Init(false); assign(n
, v
); }
268 void assign(const_iterator first
, const_iterator last
);
269 void assign(size_type n
, const_reference v
)
270 { clear(); Add(v
, n
); }
271 reference
back() { return *(end() - 1); }
272 const_reference
back() const { return *(end() - 1); }
273 iterator
begin() { return m_pItems
; }
274 const_iterator
begin() const { return m_pItems
; }
275 size_type
capacity() const { return m_nSize
; }
276 void clear() { Clear(); }
277 bool empty() const { return IsEmpty(); }
278 iterator
end() { return begin() + GetCount(); }
279 const_iterator
end() const { return begin() + GetCount(); }
280 iterator
erase(iterator first
, iterator last
)
282 size_t idx
= first
- begin();
283 RemoveAt(idx
, last
- first
);
284 return begin() + idx
;
286 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
287 reference
front() { return *begin(); }
288 const_reference
front() const { return *begin(); }
289 void insert(iterator it
, size_type n
, const_reference v
)
290 { Insert(v
, it
- begin(), n
); }
291 iterator
insert(iterator it
, const_reference v
= value_type())
292 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
293 void insert(iterator it
, const_iterator first
, const_iterator last
);
294 size_type
max_size() const { return INT_MAX
; }
295 void pop_back() { RemoveAt(GetCount() - 1); }
296 void push_back(const_reference v
) { Add(v
); }
297 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
298 const_reverse_iterator
rbegin() const
299 { return const_reverse_iterator(end() - 1); }
300 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
301 const_reverse_iterator
rend() const
302 { return const_reverse_iterator(begin() - 1); }
303 void reserve(size_type n
) /* base::reserve*/;
304 void resize(size_type n
, value_type v
= value_type());
305 size_type
size() const { return GetCount(); }
306 void swap(wxArrayString
& other
)
308 wxSwap(m_nSize
, other
.m_nSize
);
309 wxSwap(m_nCount
, other
.m_nCount
);
310 wxSwap(m_pItems
, other
.m_pItems
);
311 wxSwap(m_autoSort
, other
.m_autoSort
);
315 void Init(bool autoSort
); // common part of all ctors
316 void Copy(const wxArrayString
& src
); // copies the contents of another array
319 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
321 size_t m_nSize
, // current size of the array
322 m_nCount
; // current number of elements
324 wxString
*m_pItems
; // pointer to data
326 bool m_autoSort
; // if true, keep the array always sorted
329 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
332 wxSortedArrayString() : wxArrayString(true)
334 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
340 // this class provides a temporary wxString* from a
342 class WXDLLIMPEXP_BASE wxCArrayString
345 wxCArrayString( const wxArrayString
& array
)
346 : m_array( array
), m_strings( NULL
)
348 ~wxCArrayString() { delete[] m_strings
; }
350 size_t GetCount() const { return m_array
.GetCount(); }
351 wxString
* GetStrings()
353 if( m_strings
) return m_strings
;
354 size_t count
= m_array
.GetCount();
355 m_strings
= new wxString
[count
];
356 for( size_t i
= 0; i
< count
; ++i
)
357 m_strings
[i
] = m_array
[i
];
363 wxString
*r
= GetStrings();
369 const wxArrayString
& m_array
;
374 // ----------------------------------------------------------------------------
375 // helper functions for working with arrays
376 // ----------------------------------------------------------------------------
378 // by default, these functions use the escape character to escape the
379 // separators occuring inside the string to be joined, this can be disabled by
380 // passing '\0' as escape
382 WXDLLIMPEXP_BASE wxString
wxJoin(const wxArrayString
& arr
,
384 const wxChar escape
= wxT('\\'));
386 WXDLLIMPEXP_BASE wxArrayString
wxSplit(const wxString
& str
,
388 const wxChar escape
= wxT('\\'));
391 // ----------------------------------------------------------------------------
392 // This helper class allows to pass both C array of wxStrings or wxArrayString
393 // using the same interface.
395 // Use it when you have two methods taking wxArrayString or (int, wxString[]),
396 // that do the same thing. This class lets you iterate over input data in the
397 // same way whether it is a raw array of strings or wxArrayString.
399 // The object does not take ownership of the data -- internally it keeps
400 // pointers to the data, therefore the data must be disposed of by user
401 // and only after this object is destroyed. Usually it is not a problem as
402 // only temporary objects of this class are used.
403 // ----------------------------------------------------------------------------
405 class wxArrayStringsAdapter
408 // construct an adapter from a wxArrayString
409 wxArrayStringsAdapter(const wxArrayString
& strings
)
410 : m_type(wxSTRING_ARRAY
), m_size(strings
.size())
412 m_data
.array
= &strings
;
415 // construct an adapter from a wxString[]
416 wxArrayStringsAdapter(unsigned int n
, const wxString
*strings
)
417 : m_type(wxSTRING_POINTER
), m_size(n
)
419 m_data
.ptr
= strings
;
422 // construct an adapter from a single wxString
423 wxArrayStringsAdapter(const wxString
& s
)
424 : m_type(wxSTRING_POINTER
), m_size(1)
429 // default copy constructor is ok
431 // iteration interface
432 size_t GetCount() const { return m_size
; }
433 bool IsEmpty() const { return GetCount() == 0; }
434 const wxString
& operator[] (unsigned int i
) const
436 wxASSERT_MSG( i
< GetCount(), wxT("index out of bounds") );
437 if(m_type
== wxSTRING_POINTER
)
438 return m_data
.ptr
[i
];
439 return m_data
.array
->Item(i
);
441 wxArrayString
AsArrayString() const
443 if(m_type
== wxSTRING_ARRAY
)
444 return *m_data
.array
;
445 return wxArrayString(GetCount(), m_data
.ptr
);
449 // type of the data being held
450 enum wxStringContainerType
452 wxSTRING_ARRAY
, // wxArrayString
453 wxSTRING_POINTER
// wxString[]
456 wxStringContainerType m_type
;
460 const wxString
* ptr
;
461 const wxArrayString
* array
;
464 wxDECLARE_NO_ASSIGN_CLASS(wxArrayStringsAdapter
);
467 #endif // _WX_ARRSTR_H