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 WXDLLIMPEXP_BASE
int wxCMPFUNC_CONV
wxStringSortAscending(wxString
*, wxString
*);
19 WXDLLIMPEXP_BASE
int wxCMPFUNC_CONV
wxStringSortDescending(wxString
*, wxString
*);
23 #include "wx/dynarray.h"
25 typedef int (wxCMPFUNC_CONV
*CMPFUNCwxString
)(wxString
*, wxString
*);
26 typedef wxString _wxArraywxBaseArrayStringBase
;
27 _WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase
, wxBaseArrayStringBase
,
28 wxArray_SortFunction
<wxString
>,
29 class WXDLLIMPEXP_BASE
);
30 WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString
, wxArrayStringBase
,
31 wxBaseArrayStringBase
, WXDLLIMPEXP_BASE
);
32 _WX_DEFINE_SORTED_TYPEARRAY_2(wxString
, wxSortedArrayStringBase
,
33 wxBaseArrayStringBase
, = wxStringSortAscending
,
34 class WXDLLIMPEXP_BASE
, CMPFUNCwxString
);
36 class WXDLLIMPEXP_BASE wxArrayString
: public wxArrayStringBase
39 // type of function used by wxArrayString::Sort()
40 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
41 const wxString
& second
);
44 wxArrayString(const wxArrayString
& a
) : wxArrayStringBase(a
) { }
45 wxArrayString(size_t sz
, const wxChar
** a
);
46 wxArrayString(size_t sz
, const wxString
* a
);
48 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
50 void Sort(bool reverseOrder
= false);
51 void Sort(CompareFunction function
);
52 void Sort(CMPFUNCwxString function
) { wxArrayStringBase::Sort(function
); }
54 size_t Add(const wxString
& string
, size_t copies
= 1)
56 wxArrayStringBase::Add(string
, copies
);
57 return size() - copies
;
61 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
64 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
66 wxSortedArrayString(const wxSortedArrayString
& array
)
67 : wxSortedArrayStringBase(array
)
69 wxSortedArrayString(const wxArrayString
& src
)
70 : wxSortedArrayStringBase(wxStringSortAscending
)
74 for ( size_t n
= 0; n
< src
.size(); n
++ )
78 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
81 #else // if !wxUSE_STL
83 class WXDLLIMPEXP_BASE wxArrayString
86 // type of function used by wxArrayString::Sort()
87 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
88 const wxString
& second
);
89 // type of function used by wxArrayString::Sort(), for compatibility with
91 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
94 // constructors and destructor
96 wxArrayString() { Init(false); }
97 // if autoSort is true, the array is always sorted (in alphabetical order)
99 // NB: the reason for using int and not bool is that like this we can avoid
100 // using this ctor for implicit conversions from "const char *" (which
101 // we'd like to be implicitly converted to wxString instead!)
103 // of course, using explicit would be even better - if all compilers
105 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
106 // C string array ctor
107 wxArrayString(size_t sz
, const char** a
);
108 wxArrayString(size_t sz
, const wchar_t** a
);
109 // wxString string array ctor
110 wxArrayString(size_t sz
, const wxString
* a
);
112 wxArrayString(const wxArrayString
& array
);
113 // assignment operator
114 wxArrayString
& operator=(const wxArrayString
& src
);
115 // not virtual, this class should not be derived from
119 // empties the list, but doesn't release memory
121 // empties the list and releases memory
123 // preallocates memory for given number of items
124 void Alloc(size_t nCount
);
125 // minimzes the memory usage (by freeing all extra memory)
129 // number of elements in the array
130 size_t GetCount() const { return m_nCount
; }
132 bool IsEmpty() const { return m_nCount
== 0; }
133 // number of elements in the array (GetCount is preferred API)
134 size_t Count() const { return m_nCount
; }
136 // items access (range checking is done in debug version)
137 // get item at position uiIndex
138 wxString
& Item(size_t nIndex
) const
140 wxASSERT_MSG( nIndex
< m_nCount
,
141 _T("wxArrayString: index out of bounds") );
143 return m_pItems
[nIndex
];
147 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
149 wxString
& Last() const
151 wxASSERT_MSG( !IsEmpty(),
152 _T("wxArrayString: index out of bounds") );
153 return Item(GetCount() - 1);
158 // Search the element in the array, starting from the beginning if
159 // bFromEnd is false or from end otherwise. If bCase, comparison is case
160 // sensitive (default). Returns index of the first item matched or
162 int Index (const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
163 // add new element at the end (if the array is not sorted), return its
165 size_t Add(const wxString
& str
, size_t nInsert
= 1);
166 // add new element at given position
167 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
168 // expand the array to have count elements
169 void SetCount(size_t count
);
170 // remove first item matching this value
171 void Remove(const wxString
& sz
);
172 // remove item by index
173 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
176 // sort array elements in alphabetical order (or reversed alphabetical
177 // order if reverseOrder parameter is true)
178 void Sort(bool reverseOrder
= false);
179 // sort array elements using specified comparaison function
180 void Sort(CompareFunction compareFunction
);
181 void Sort(CompareFunction2 compareFunction
);
184 // compare two arrays case sensitively
185 bool operator==(const wxArrayString
& a
) const;
186 // compare two arrays case sensitively
187 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
189 // STL-like interface
190 typedef wxString value_type
;
191 typedef value_type
* pointer
;
192 typedef const value_type
* const_pointer
;
193 typedef value_type
* iterator
;
194 typedef const value_type
* const_iterator
;
195 typedef value_type
& reference
;
196 typedef const value_type
& const_reference
;
197 typedef int difference_type
;
198 typedef size_t size_type
;
200 // TODO: this code duplicates the one in dynarray.h
201 class reverse_iterator
203 typedef wxString value_type
;
204 typedef value_type
* pointer
;
205 typedef value_type
& reference
;
206 typedef reverse_iterator itor
;
207 friend itor
operator+(int o
, const itor
& it
);
208 friend itor
operator+(const itor
& it
, int o
);
209 friend itor
operator-(const itor
& it
, int o
);
210 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
213 reverse_iterator() : m_ptr(NULL
) { }
214 reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
215 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
216 reference
operator*() const { return *m_ptr
; }
217 pointer
operator->() const { return m_ptr
; }
218 itor
& operator++() { --m_ptr
; return *this; }
219 const itor
operator++(int)
220 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
221 itor
& operator--() { ++m_ptr
; return *this; }
222 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
223 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
224 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
227 class const_reverse_iterator
229 typedef wxString value_type
;
230 typedef const value_type
* pointer
;
231 typedef const value_type
& reference
;
232 typedef const_reverse_iterator itor
;
233 friend itor
operator+(int o
, const itor
& it
);
234 friend itor
operator+(const itor
& it
, int o
);
235 friend itor
operator-(const itor
& it
, int o
);
236 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
239 const_reverse_iterator() : m_ptr(NULL
) { }
240 const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
241 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
242 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
243 reference
operator*() const { return *m_ptr
; }
244 pointer
operator->() const { return m_ptr
; }
245 itor
& operator++() { --m_ptr
; return *this; }
246 const itor
operator++(int)
247 { itor tmp
= *this; --m_ptr
; return tmp
; }
248 itor
& operator--() { ++m_ptr
; return *this; }
249 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
250 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
251 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
254 wxArrayString(const_iterator first
, const_iterator last
)
255 { Init(false); assign(first
, last
); }
256 wxArrayString(size_type n
, const_reference v
) { Init(false); assign(n
, v
); }
257 void assign(const_iterator first
, const_iterator last
);
258 void assign(size_type n
, const_reference v
)
259 { clear(); Add(v
, n
); }
260 reference
back() { return *(end() - 1); }
261 const_reference
back() const { return *(end() - 1); }
262 iterator
begin() { return m_pItems
; }
263 const_iterator
begin() const { return m_pItems
; }
264 size_type
capacity() const { return m_nSize
; }
265 void clear() { Clear(); }
266 bool empty() const { return IsEmpty(); }
267 iterator
end() { return begin() + GetCount(); }
268 const_iterator
end() const { return begin() + GetCount(); }
269 iterator
erase(iterator first
, iterator last
)
271 size_t idx
= first
- begin();
272 RemoveAt(idx
, last
- first
);
273 return begin() + idx
;
275 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
276 reference
front() { return *begin(); }
277 const_reference
front() const { return *begin(); }
278 void insert(iterator it
, size_type n
, const_reference v
)
279 { Insert(v
, it
- begin(), n
); }
280 iterator
insert(iterator it
, const_reference v
= value_type())
281 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
282 void insert(iterator it
, const_iterator first
, const_iterator last
);
283 size_type
max_size() const { return INT_MAX
; }
284 void pop_back() { RemoveAt(GetCount() - 1); }
285 void push_back(const_reference v
) { Add(v
); }
286 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
287 const_reverse_iterator
rbegin() const;
288 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
289 const_reverse_iterator
rend() const;
290 void reserve(size_type n
) /* base::reserve*/;
291 void resize(size_type n
, value_type v
= value_type());
292 size_type
size() const { return GetCount(); }
295 void Init(bool autoSort
); // common part of all ctors
296 void Copy(const wxArrayString
& src
); // copies the contents of another array
299 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
301 void DoSort(); // common part of all Sort() variants
303 size_t m_nSize
, // current size of the array
304 m_nCount
; // current number of elements
306 wxString
*m_pItems
; // pointer to data
308 bool m_autoSort
; // if true, keep the array always sorted
311 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
314 wxSortedArrayString() : wxArrayString(true)
316 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
322 // this class provides a temporary wxString* from a
324 class WXDLLIMPEXP_BASE wxCArrayString
327 wxCArrayString( const wxArrayString
& array
)
328 : m_array( array
), m_strings( NULL
)
330 ~wxCArrayString() { delete[] m_strings
; }
332 size_t GetCount() const { return m_array
.GetCount(); }
333 wxString
* GetStrings()
335 if( m_strings
) return m_strings
;
336 size_t count
= m_array
.GetCount();
337 m_strings
= new wxString
[count
];
338 for( size_t i
= 0; i
< count
; ++i
)
339 m_strings
[i
] = m_array
[i
];
343 const wxArrayString
& m_array
;
348 // ----------------------------------------------------------------------------
349 // helper functions for working with arrays
350 // ----------------------------------------------------------------------------
352 // by default, these functions use the escape character to escape the
353 // separators occuring inside the string to be joined, this can be disabled by
354 // passing '\0' as escape
356 WXDLLIMPEXP_BASE wxString
wxJoin(const wxArrayString
& arr
,
358 const wxChar escape
= wxT('\\'));
360 WXDLLIMPEXP_BASE wxArrayString
wxSplit(const wxString
& str
,
362 const wxChar escape
= wxT('\\'));
364 #endif // _WX_ARRSTR_H