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 char** a
);
46 wxArrayString(size_t sz
, const wchar_t** a
);
47 wxArrayString(size_t sz
, const wxString
* a
);
49 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
51 void Sort(bool reverseOrder
= false);
52 void Sort(CompareFunction function
);
53 void Sort(CMPFUNCwxString function
) { wxArrayStringBase::Sort(function
); }
55 size_t Add(const wxString
& string
, size_t copies
= 1)
57 wxArrayStringBase::Add(string
, copies
);
58 return size() - copies
;
62 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
65 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
67 wxSortedArrayString(const wxSortedArrayString
& array
)
68 : wxSortedArrayStringBase(array
)
70 wxSortedArrayString(const wxArrayString
& src
)
71 : wxSortedArrayStringBase(wxStringSortAscending
)
75 for ( size_t n
= 0; n
< src
.size(); n
++ )
79 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
82 #else // if !wxUSE_STL
84 class WXDLLIMPEXP_BASE wxArrayString
87 // type of function used by wxArrayString::Sort()
88 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
89 const wxString
& second
);
90 // type of function used by wxArrayString::Sort(), for compatibility with
92 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
95 // constructors and destructor
97 wxArrayString() { Init(false); }
98 // if autoSort is true, the array is always sorted (in alphabetical order)
100 // NB: the reason for using int and not bool is that like this we can avoid
101 // using this ctor for implicit conversions from "const char *" (which
102 // we'd like to be implicitly converted to wxString instead!)
104 // of course, using explicit would be even better - if all compilers
106 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
107 // C string array ctor
108 wxArrayString(size_t sz
, const char** a
);
109 wxArrayString(size_t sz
, const wchar_t** a
);
110 // wxString string array ctor
111 wxArrayString(size_t sz
, const wxString
* a
);
113 wxArrayString(const wxArrayString
& array
);
114 // assignment operator
115 wxArrayString
& operator=(const wxArrayString
& src
);
116 // not virtual, this class should not be derived from
120 // empties the list, but doesn't release memory
122 // empties the list and releases memory
124 // preallocates memory for given number of items
125 void Alloc(size_t nCount
);
126 // minimzes the memory usage (by freeing all extra memory)
130 // number of elements in the array
131 size_t GetCount() const { return m_nCount
; }
133 bool IsEmpty() const { return m_nCount
== 0; }
134 // number of elements in the array (GetCount is preferred API)
135 size_t Count() const { return m_nCount
; }
137 // items access (range checking is done in debug version)
138 // get item at position uiIndex
139 wxString
& Item(size_t nIndex
) const
141 wxASSERT_MSG( nIndex
< m_nCount
,
142 _T("wxArrayString: index out of bounds") );
144 return m_pItems
[nIndex
];
148 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
150 wxString
& Last() const
152 wxASSERT_MSG( !IsEmpty(),
153 _T("wxArrayString: index out of bounds") );
154 return Item(GetCount() - 1);
159 // Search the element in the array, starting from the beginning if
160 // bFromEnd is false or from end otherwise. If bCase, comparison is case
161 // sensitive (default). Returns index of the first item matched or
163 int Index (const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
164 // add new element at the end (if the array is not sorted), return its
166 size_t Add(const wxString
& str
, size_t nInsert
= 1);
167 // add new element at given position
168 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
169 // expand the array to have count elements
170 void SetCount(size_t count
);
171 // remove first item matching this value
172 void Remove(const wxString
& sz
);
173 // remove item by index
174 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
177 // sort array elements in alphabetical order (or reversed alphabetical
178 // order if reverseOrder parameter is true)
179 void Sort(bool reverseOrder
= false);
180 // sort array elements using specified comparaison function
181 void Sort(CompareFunction compareFunction
);
182 void Sort(CompareFunction2 compareFunction
);
185 // compare two arrays case sensitively
186 bool operator==(const wxArrayString
& a
) const;
187 // compare two arrays case sensitively
188 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
190 // STL-like interface
191 typedef wxString value_type
;
192 typedef value_type
* pointer
;
193 typedef const value_type
* const_pointer
;
194 typedef value_type
* iterator
;
195 typedef const value_type
* const_iterator
;
196 typedef value_type
& reference
;
197 typedef const value_type
& const_reference
;
198 typedef int difference_type
;
199 typedef size_t size_type
;
201 // TODO: this code duplicates the one in dynarray.h
202 class reverse_iterator
204 typedef wxString value_type
;
205 typedef value_type
* pointer
;
206 typedef value_type
& reference
;
207 typedef reverse_iterator itor
;
208 friend itor
operator+(int o
, const itor
& it
);
209 friend itor
operator+(const itor
& it
, int o
);
210 friend itor
operator-(const itor
& it
, int o
);
211 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
214 reverse_iterator() : m_ptr(NULL
) { }
215 reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
216 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
217 reference
operator*() const { return *m_ptr
; }
218 pointer
operator->() const { return m_ptr
; }
219 itor
& operator++() { --m_ptr
; return *this; }
220 const itor
operator++(int)
221 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
222 itor
& operator--() { ++m_ptr
; return *this; }
223 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
224 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
225 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
228 class const_reverse_iterator
230 typedef wxString value_type
;
231 typedef const value_type
* pointer
;
232 typedef const value_type
& reference
;
233 typedef const_reverse_iterator itor
;
234 friend itor
operator+(int o
, const itor
& it
);
235 friend itor
operator+(const itor
& it
, int o
);
236 friend itor
operator-(const itor
& it
, int o
);
237 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
240 const_reverse_iterator() : m_ptr(NULL
) { }
241 const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
242 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
243 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
244 reference
operator*() const { return *m_ptr
; }
245 pointer
operator->() const { return m_ptr
; }
246 itor
& operator++() { --m_ptr
; return *this; }
247 const itor
operator++(int)
248 { itor tmp
= *this; --m_ptr
; return tmp
; }
249 itor
& operator--() { ++m_ptr
; return *this; }
250 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
251 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
252 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
255 wxArrayString(const_iterator first
, const_iterator last
)
256 { Init(false); assign(first
, last
); }
257 wxArrayString(size_type n
, const_reference v
) { Init(false); assign(n
, v
); }
258 void assign(const_iterator first
, const_iterator last
);
259 void assign(size_type n
, const_reference v
)
260 { clear(); Add(v
, n
); }
261 reference
back() { return *(end() - 1); }
262 const_reference
back() const { return *(end() - 1); }
263 iterator
begin() { return m_pItems
; }
264 const_iterator
begin() const { return m_pItems
; }
265 size_type
capacity() const { return m_nSize
; }
266 void clear() { Clear(); }
267 bool empty() const { return IsEmpty(); }
268 iterator
end() { return begin() + GetCount(); }
269 const_iterator
end() const { return begin() + GetCount(); }
270 iterator
erase(iterator first
, iterator last
)
272 size_t idx
= first
- begin();
273 RemoveAt(idx
, last
- first
);
274 return begin() + idx
;
276 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
277 reference
front() { return *begin(); }
278 const_reference
front() const { return *begin(); }
279 void insert(iterator it
, size_type n
, const_reference v
)
280 { Insert(v
, it
- begin(), n
); }
281 iterator
insert(iterator it
, const_reference v
= value_type())
282 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
283 void insert(iterator it
, const_iterator first
, const_iterator last
);
284 size_type
max_size() const { return INT_MAX
; }
285 void pop_back() { RemoveAt(GetCount() - 1); }
286 void push_back(const_reference v
) { Add(v
); }
287 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
288 const_reverse_iterator
rbegin() const;
289 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
290 const_reverse_iterator
rend() const;
291 void reserve(size_type n
) /* base::reserve*/;
292 void resize(size_type n
, value_type v
= value_type());
293 size_type
size() const { return GetCount(); }
296 void Init(bool autoSort
); // common part of all ctors
297 void Copy(const wxArrayString
& src
); // copies the contents of another array
300 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
302 void DoSort(); // common part of all Sort() variants
304 size_t m_nSize
, // current size of the array
305 m_nCount
; // current number of elements
307 wxString
*m_pItems
; // pointer to data
309 bool m_autoSort
; // if true, keep the array always sorted
312 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
315 wxSortedArrayString() : wxArrayString(true)
317 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
323 // this class provides a temporary wxString* from a
325 class WXDLLIMPEXP_BASE wxCArrayString
328 wxCArrayString( const wxArrayString
& array
)
329 : m_array( array
), m_strings( NULL
)
331 ~wxCArrayString() { delete[] m_strings
; }
333 size_t GetCount() const { return m_array
.GetCount(); }
334 wxString
* GetStrings()
336 if( m_strings
) return m_strings
;
337 size_t count
= m_array
.GetCount();
338 m_strings
= new wxString
[count
];
339 for( size_t i
= 0; i
< count
; ++i
)
340 m_strings
[i
] = m_array
[i
];
344 const wxArrayString
& m_array
;
349 // ----------------------------------------------------------------------------
350 // helper functions for working with arrays
351 // ----------------------------------------------------------------------------
353 // by default, these functions use the escape character to escape the
354 // separators occuring inside the string to be joined, this can be disabled by
355 // passing '\0' as escape
357 WXDLLIMPEXP_BASE wxString
wxJoin(const wxArrayString
& arr
,
359 const wxChar escape
= wxT('\\'));
361 WXDLLIMPEXP_BASE wxArrayString
wxSplit(const wxString
& str
,
363 const wxChar escape
= wxT('\\'));
365 #endif // _WX_ARRSTR_H