1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxArrayString class
4 // Author: Mattia Barbon and Vadim Zeitlin
7 // Copyright: (c) 2003 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
15 #include "wx/string.h"
17 // these functions are only used in STL build now but we define them in any
18 // case for compatibility with the existing code outside of the library which
19 // could be using them
20 inline int wxCMPFUNC_CONV
wxStringSortAscending(wxString
* s1
, wxString
* s2
)
25 inline int wxCMPFUNC_CONV
wxStringSortDescending(wxString
* s1
, wxString
* s2
)
27 return wxStringSortAscending(s2
, s1
);
30 #if wxUSE_STD_CONTAINERS
32 #include "wx/dynarray.h"
34 typedef int (wxCMPFUNC_CONV
*CMPFUNCwxString
)(wxString
*, wxString
*);
35 typedef wxString _wxArraywxBaseArrayStringBase
;
36 _WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase
, wxBaseArrayStringBase
,
37 wxArray_SortFunction
<wxString
>,
38 class WXDLLIMPEXP_BASE
);
39 WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString
, wxArrayStringBase
,
40 wxBaseArrayStringBase
, WXDLLIMPEXP_BASE
);
41 _WX_DEFINE_SORTED_TYPEARRAY_2(wxString
, wxSortedArrayStringBase
,
42 wxBaseArrayStringBase
, = wxStringSortAscending
,
43 class WXDLLIMPEXP_BASE
, CMPFUNCwxString
);
45 class WXDLLIMPEXP_BASE wxArrayString
: public wxArrayStringBase
48 // type of function used by wxArrayString::Sort()
49 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
50 const wxString
& second
);
53 wxArrayString(const wxArrayString
& a
) : wxArrayStringBase(a
) { }
54 wxArrayString(size_t sz
, const char** a
);
55 wxArrayString(size_t sz
, const wchar_t** a
);
56 wxArrayString(size_t sz
, const wxString
* a
);
58 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
60 void Sort(bool reverseOrder
= false);
61 void Sort(CompareFunction function
);
62 void Sort(CMPFUNCwxString function
) { wxArrayStringBase::Sort(function
); }
64 size_t Add(const wxString
& string
, size_t copies
= 1)
66 wxArrayStringBase::Add(string
, copies
);
67 return size() - copies
;
71 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
74 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
76 wxSortedArrayString(const wxSortedArrayString
& array
)
77 : wxSortedArrayStringBase(array
)
79 wxSortedArrayString(const wxArrayString
& src
)
80 : wxSortedArrayStringBase(wxStringSortAscending
)
84 for ( size_t n
= 0; n
< src
.size(); n
++ )
88 int Index(const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
93 wxFAIL_MSG( "wxSortedArrayString::Insert() is not to be used" );
98 wxFAIL_MSG( "wxSortedArrayString::Sort() is not to be used" );
102 #else // if !wxUSE_STD_CONTAINERS
104 // this shouldn't be defined for compilers not supporting template methods or
105 // without std::distance()
107 // FIXME-VC6: currently it's only not defined for VC6 in DLL build as it
108 // doesn't export template methods from DLL correctly so even though
109 // it compiles them fine, we get link errors when using wxArrayString
110 #if !defined(__VISUALC6__) || !(defined(WXMAKINGDLL) || defined(WXUSINGDLL))
111 #define wxHAS_VECTOR_TEMPLATE_ASSIGN
114 #ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
115 #include "wx/beforestd.h"
117 #include "wx/afterstd.h"
118 #endif // wxHAS_VECTOR_TEMPLATE_ASSIGN
120 class WXDLLIMPEXP_BASE wxArrayString
123 // type of function used by wxArrayString::Sort()
124 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
125 const wxString
& second
);
126 // type of function used by wxArrayString::Sort(), for compatibility with
128 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
131 // constructors and destructor
133 wxArrayString() { Init(false); }
134 // if autoSort is true, the array is always sorted (in alphabetical order)
136 // NB: the reason for using int and not bool is that like this we can avoid
137 // using this ctor for implicit conversions from "const char *" (which
138 // we'd like to be implicitly converted to wxString instead!). This
139 // wouldn't be needed if the 'explicit' keyword was supported by all
140 // compilers, or if this was protected ctor for wxSortedArrayString,
141 // but we're stuck with it now.
142 wxEXPLICIT
wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
143 // C string array ctor
144 wxArrayString(size_t sz
, const char** a
);
145 wxArrayString(size_t sz
, const wchar_t** a
);
146 // wxString string array ctor
147 wxArrayString(size_t sz
, const wxString
* a
);
149 wxArrayString(const wxArrayString
& array
);
150 // assignment operator
151 wxArrayString
& operator=(const wxArrayString
& src
);
152 // not virtual, this class should not be derived from
156 // empties the list, but doesn't release memory
158 // empties the list and releases memory
160 // preallocates memory for given number of items
161 void Alloc(size_t nCount
);
162 // minimzes the memory usage (by freeing all extra memory)
166 // number of elements in the array
167 size_t GetCount() const { return m_nCount
; }
169 bool IsEmpty() const { return m_nCount
== 0; }
170 // number of elements in the array (GetCount is preferred API)
171 size_t Count() const { return m_nCount
; }
173 // items access (range checking is done in debug version)
174 // get item at position uiIndex
175 wxString
& Item(size_t nIndex
)
177 wxASSERT_MSG( nIndex
< m_nCount
,
178 wxT("wxArrayString: index out of bounds") );
180 return m_pItems
[nIndex
];
182 const wxString
& Item(size_t nIndex
) const { return const_cast<wxArrayString
*>(this)->Item(nIndex
); }
185 wxString
& operator[](size_t nIndex
) { return Item(nIndex
); }
186 const wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
190 wxASSERT_MSG( !IsEmpty(),
191 wxT("wxArrayString: index out of bounds") );
192 return Item(GetCount() - 1);
194 const wxString
& Last() const { return const_cast<wxArrayString
*>(this)->Last(); }
198 // Search the element in the array, starting from the beginning if
199 // bFromEnd is false or from end otherwise. If bCase, comparison is case
200 // sensitive (default). Returns index of the first item matched or
202 int Index (const wxString
& str
, bool bCase
= true, bool bFromEnd
= false) const;
203 // add new element at the end (if the array is not sorted), return its
205 size_t Add(const wxString
& str
, size_t nInsert
= 1);
206 // add new element at given position
207 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
208 // expand the array to have count elements
209 void SetCount(size_t count
);
210 // remove first item matching this value
211 void Remove(const wxString
& sz
);
212 // remove item by index
213 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
216 // sort array elements in alphabetical order (or reversed alphabetical
217 // order if reverseOrder parameter is true)
218 void Sort(bool reverseOrder
= false);
219 // sort array elements using specified comparison function
220 void Sort(CompareFunction compareFunction
);
221 void Sort(CompareFunction2 compareFunction
);
224 // compare two arrays case sensitively
225 bool operator==(const wxArrayString
& a
) const;
226 // compare two arrays case sensitively
227 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
229 // STL-like interface
230 typedef wxString value_type
;
231 typedef value_type
* pointer
;
232 typedef const value_type
* const_pointer
;
233 typedef value_type
* iterator
;
234 typedef const value_type
* const_iterator
;
235 typedef value_type
& reference
;
236 typedef const value_type
& const_reference
;
237 typedef int difference_type
;
238 typedef size_t size_type
;
240 // TODO: this code duplicates the one in dynarray.h
241 class reverse_iterator
243 typedef wxString value_type
;
244 typedef value_type
* pointer
;
245 typedef value_type
& reference
;
246 typedef reverse_iterator itor
;
247 friend itor
operator+(int o
, const itor
& it
);
248 friend itor
operator+(const itor
& it
, int o
);
249 friend itor
operator-(const itor
& it
, int o
);
250 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
253 reverse_iterator() : m_ptr(NULL
) { }
254 wxEXPLICIT
reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
255 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
256 reference
operator*() const { return *m_ptr
; }
257 pointer
operator->() const { return m_ptr
; }
258 itor
& operator++() { --m_ptr
; return *this; }
259 const itor
operator++(int)
260 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
261 itor
& operator--() { ++m_ptr
; return *this; }
262 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
263 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
264 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
267 class const_reverse_iterator
269 typedef wxString value_type
;
270 typedef const value_type
* pointer
;
271 typedef const value_type
& reference
;
272 typedef const_reverse_iterator itor
;
273 friend itor
operator+(int o
, const itor
& it
);
274 friend itor
operator+(const itor
& it
, int o
);
275 friend itor
operator-(const itor
& it
, int o
);
276 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
279 const_reverse_iterator() : m_ptr(NULL
) { }
280 wxEXPLICIT
const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
281 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
282 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
283 reference
operator*() const { return *m_ptr
; }
284 pointer
operator->() const { return m_ptr
; }
285 itor
& operator++() { --m_ptr
; return *this; }
286 const itor
operator++(int)
287 { itor tmp
= *this; --m_ptr
; return tmp
; }
288 itor
& operator--() { ++m_ptr
; return *this; }
289 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
290 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
291 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
294 wxArrayString(const_iterator first
, const_iterator last
)
295 { Init(false); assign(first
, last
); }
296 wxArrayString(size_type n
, const_reference v
) { Init(false); assign(n
, v
); }
298 #ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
299 template <class Iterator
>
300 void assign(Iterator first
, Iterator last
)
303 reserve(std::distance(first
, last
));
304 for(; first
!= last
; ++first
)
307 #else // !wxHAS_VECTOR_TEMPLATE_ASSIGN
308 void assign(const_iterator first
, const_iterator last
)
311 reserve(last
- first
);
312 for(; first
!= last
; ++first
)
315 #endif // wxHAS_VECTOR_TEMPLATE_ASSIGN/!wxHAS_VECTOR_TEMPLATE_ASSIGN
317 void assign(size_type n
, const_reference v
)
318 { clear(); Add(v
, n
); }
319 reference
back() { return *(end() - 1); }
320 const_reference
back() const { return *(end() - 1); }
321 iterator
begin() { return m_pItems
; }
322 const_iterator
begin() const { return m_pItems
; }
323 size_type
capacity() const { return m_nSize
; }
324 void clear() { Clear(); }
325 bool empty() const { return IsEmpty(); }
326 iterator
end() { return begin() + GetCount(); }
327 const_iterator
end() const { return begin() + GetCount(); }
328 iterator
erase(iterator first
, iterator last
)
330 size_t idx
= first
- begin();
331 RemoveAt(idx
, last
- first
);
332 return begin() + idx
;
334 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
335 reference
front() { return *begin(); }
336 const_reference
front() const { return *begin(); }
337 void insert(iterator it
, size_type n
, const_reference v
)
338 { Insert(v
, it
- begin(), n
); }
339 iterator
insert(iterator it
, const_reference v
= value_type())
340 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
341 void insert(iterator it
, const_iterator first
, const_iterator last
);
342 size_type
max_size() const { return INT_MAX
; }
343 void pop_back() { RemoveAt(GetCount() - 1); }
344 void push_back(const_reference v
) { Add(v
); }
345 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
346 const_reverse_iterator
rbegin() const
347 { return const_reverse_iterator(end() - 1); }
348 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
349 const_reverse_iterator
rend() const
350 { return const_reverse_iterator(begin() - 1); }
351 void reserve(size_type n
) /* base::reserve*/;
352 void resize(size_type n
, value_type v
= value_type());
353 size_type
size() const { return GetCount(); }
354 void swap(wxArrayString
& other
)
356 wxSwap(m_nSize
, other
.m_nSize
);
357 wxSwap(m_nCount
, other
.m_nCount
);
358 wxSwap(m_pItems
, other
.m_pItems
);
359 wxSwap(m_autoSort
, other
.m_autoSort
);
363 void Init(bool autoSort
); // common part of all ctors
364 void Copy(const wxArrayString
& src
); // copies the contents of another array
367 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
369 size_t m_nSize
, // current size of the array
370 m_nCount
; // current number of elements
372 wxString
*m_pItems
; // pointer to data
374 bool m_autoSort
; // if true, keep the array always sorted
377 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
380 wxSortedArrayString() : wxArrayString(true)
382 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
386 #endif // !wxUSE_STD_CONTAINERS
388 // this class provides a temporary wxString* from a
390 class WXDLLIMPEXP_BASE wxCArrayString
393 wxCArrayString( const wxArrayString
& array
)
394 : m_array( array
), m_strings( NULL
)
396 ~wxCArrayString() { delete[] m_strings
; }
398 size_t GetCount() const { return m_array
.GetCount(); }
399 wxString
* GetStrings()
401 if( m_strings
) return m_strings
;
402 size_t count
= m_array
.GetCount();
403 m_strings
= new wxString
[count
];
404 for( size_t i
= 0; i
< count
; ++i
)
405 m_strings
[i
] = m_array
[i
];
411 wxString
*r
= GetStrings();
417 const wxArrayString
& m_array
;
422 // ----------------------------------------------------------------------------
423 // helper functions for working with arrays
424 // ----------------------------------------------------------------------------
426 // by default, these functions use the escape character to escape the
427 // separators occurring inside the string to be joined, this can be disabled by
428 // passing '\0' as escape
430 WXDLLIMPEXP_BASE wxString
wxJoin(const wxArrayString
& arr
,
432 const wxChar escape
= wxT('\\'));
434 WXDLLIMPEXP_BASE wxArrayString
wxSplit(const wxString
& str
,
436 const wxChar escape
= wxT('\\'));
439 // ----------------------------------------------------------------------------
440 // This helper class allows to pass both C array of wxStrings or wxArrayString
441 // using the same interface.
443 // Use it when you have two methods taking wxArrayString or (int, wxString[]),
444 // that do the same thing. This class lets you iterate over input data in the
445 // same way whether it is a raw array of strings or wxArrayString.
447 // The object does not take ownership of the data -- internally it keeps
448 // pointers to the data, therefore the data must be disposed of by user
449 // and only after this object is destroyed. Usually it is not a problem as
450 // only temporary objects of this class are used.
451 // ----------------------------------------------------------------------------
453 class wxArrayStringsAdapter
456 // construct an adapter from a wxArrayString
457 wxArrayStringsAdapter(const wxArrayString
& strings
)
458 : m_type(wxSTRING_ARRAY
), m_size(strings
.size())
460 m_data
.array
= &strings
;
463 // construct an adapter from a wxString[]
464 wxArrayStringsAdapter(unsigned int n
, const wxString
*strings
)
465 : m_type(wxSTRING_POINTER
), m_size(n
)
467 m_data
.ptr
= strings
;
470 // construct an adapter from a single wxString
471 wxArrayStringsAdapter(const wxString
& s
)
472 : m_type(wxSTRING_POINTER
), m_size(1)
477 // default copy constructor is ok
479 // iteration interface
480 size_t GetCount() const { return m_size
; }
481 bool IsEmpty() const { return GetCount() == 0; }
482 const wxString
& operator[] (unsigned int i
) const
484 wxASSERT_MSG( i
< GetCount(), wxT("index out of bounds") );
485 if(m_type
== wxSTRING_POINTER
)
486 return m_data
.ptr
[i
];
487 return m_data
.array
->Item(i
);
489 wxArrayString
AsArrayString() const
491 if(m_type
== wxSTRING_ARRAY
)
492 return *m_data
.array
;
493 return wxArrayString(GetCount(), m_data
.ptr
);
497 // type of the data being held
498 enum wxStringContainerType
500 wxSTRING_ARRAY
, // wxArrayString
501 wxSTRING_POINTER
// wxString[]
504 wxStringContainerType m_type
;
508 const wxString
* ptr
;
509 const wxArrayString
* array
;
512 wxDECLARE_NO_ASSIGN_CLASS(wxArrayStringsAdapter
);
515 #endif // _WX_ARRSTR_H