1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxArrayString
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
12 wxArrayString is an efficient container for storing wxString objects.
14 It has the same features as all wxArray classes, i.e. it dynamically expands
15 when new items are added to it (so it is as easy to use as a linked list),
16 but the access time to the elements is constant, instead of being linear in
17 number of elements as in the case of linked lists. It is also very size
18 efficient and doesn't take more space than a C array @e wxString[] type
19 (wxArrayString uses its knowledge of internals of wxString class to achieve this).
21 This class is used in the same way as other dynamic arrays(), except that no
22 ::WX_DEFINE_ARRAY declaration is needed for it.
23 When a string is added or inserted in the array, a copy of the string is created,
24 so the original string may be safely deleted (e.g. if it was a @e wxChar *
25 pointer the memory it was using can be freed immediately after this).
26 In general, there is no need to worry about string memory deallocation when using
27 this class - it will always free the memory it uses itself.
29 The references returned by wxArrayString::Item, wxArrayString::Last or
30 wxArrayString::operator[] are not constant, so the array elements may
31 be modified in place like this:
34 array.Last().MakeUpper();
37 @note none of the methods of wxArrayString is virtual including its
38 destructor, so this class should not be used as a base class.
40 Although this is not true strictly speaking, this class may be considered as
41 a specialization of wxArray class for the wxString member data: it is not
42 implemented like this, but it does have all of the wxArray functions.
44 It also has the full set of <tt>std::vector<wxString></tt> compatible
45 methods, including nested @c iterator and @c const_iterator classes which
46 should be used in the new code for forward compatibility with the future
52 @see wxArray<T>, wxString, @ref overview_string
54 class wxArrayString
: public wxArray
58 The function type used with wxArrayString::Sort function.
60 typedef int (*CompareFunction
)(const wxString
& first
, const wxString
& second
);
70 wxArrayString(const wxArrayString
& array
);
74 Constructor from a C string array. Pass a size @a sz and an array @a arr.
76 wxArrayString(size_t sz
, const char** arr
);
77 wxArrayString(size_t sz
, const wchar_t** arr
);
81 Constructor from a wxString array. Pass a size @a sz and array @a arr.
83 wxArrayString(size_t sz
, const wxString
* arr
);
86 Destructor frees memory occupied by the array strings. For performance
87 reasons it is not virtual, so this class should not be derived from.
92 Appends the given number of @a copies of the new item @a str to the
93 array and returns the index of the first new item in the array.
97 size_t Add(const wxString
& str
, size_t copies
= 1);
100 Preallocates enough memory to store @a nCount items.
102 This function may be used to improve array class performance before
103 adding a known number of items consecutively.
105 void Alloc(size_t nCount
);
108 Clears the array contents and frees memory.
115 Empties the array: after a call to this function GetCount() will return 0.
116 However, this function does not free the memory used by the array and so
117 should be used when the array is going to be reused for storing other strings.
118 Otherwise, you should use Clear() to empty the array and free memory.
123 Returns the number of items in the array.
125 size_t GetCount() const;
128 Search the element in the array, starting from the beginning if @a bFromEnd
129 is @false or from end otherwise. If @a bCase, comparison is case sensitive
130 (default), otherwise the case is ignored.
132 This function uses linear search for wxArrayString.
133 Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
135 int Index(const wxString
& sz
, bool bCase
= true, bool bFromEnd
= false) const;
138 Insert the given number of @a copies of the new element in the array before the
139 position @a nIndex. Thus, for example, to insert the string in the beginning of
140 the array you would write:
146 If @a nIndex is equal to GetCount() this function behaves as Add().
148 void Insert(wxString lItem
, size_t nIndex
, size_t copies
= 1);
151 Returns @true if the array is empty, @false otherwise. This function returns the
152 same result as GetCount() == 0 but is probably easier to read.
154 bool IsEmpty() const;
157 Return the array element at position @a nIndex. An assert failure will
158 result from an attempt to access an element beyond the end of array in debug
159 mode, but no check is done in release mode.
161 @see operator[] for the operator version.
164 wxString
& Item(size_t nIndex
);
165 const wxString
& Item(size_t nIndex
) const;
169 Returns the last element of the array. Attempt to access the last element of
170 an empty array will result in assert failure in debug build, however no checks
171 are done in release mode.
175 const wxString
& Last() const;
179 Removes the first item matching this value. An assert failure is provoked by
180 an attempt to remove an element which does not exist in debug build.
184 void Remove(const wxString
& sz
);
187 Removes @a count items starting at position @a nIndex from the array.
189 void RemoveAt(size_t nIndex
, size_t count
= 1);
192 Releases the extra memory allocated by the array.
193 This function is useful to minimize the array memory consumption.
200 Sorts the array in alphabetical order or in reverse alphabetical order if
201 @a reverseOrder is @true. The sort is case-sensitive.
203 void Sort(bool reverseOrder
= false);
206 Sorts the array using the specified @a compareFunction for item comparison.
207 @a CompareFunction is defined as a function taking two <em>const wxString&</em>
208 parameters and returning an @e int value less than, equal to or greater
209 than 0 if the first string is less than, equal to or greater than the
213 The following example sorts strings by their length.
216 static int CompareStringLen(const wxString& first, const wxString& second)
218 return first.length() - second.length();
230 array.Sort(CompareStringLen);
233 void Sort(CompareFunction compareFunction
);
236 Compares 2 arrays respecting the case. Returns @true if the arrays have
237 different number of elements or if the elements don't match pairwise.
239 bool operator !=(const wxArrayString
& array
) const;
244 wxArrayString
& operator=(const wxArrayString
&);
247 Compares 2 arrays respecting the case. Returns @true only if the arrays have
248 the same number of elements and the same strings in the same order.
250 bool operator ==(const wxArrayString
& array
) const;
253 Return the array element at position @a nIndex. An assert failure will
254 result from an attempt to access an element beyond the end of array in
255 debug mode, but no check is done in release mode.
257 This is the operator version of the Item() method.
259 wxString
& operator[](size_t nIndex
) const;
264 @class wxSortedArrayString
266 wxSortedArrayString is an efficient container for storing wxString objects
267 which always keeps the string in alphabetical order.
269 wxSortedArrayString uses binary search in its wxArrayString::Index() function
270 (instead of linear search for wxArrayString::Index()) which makes it much more
271 efficient if you add strings to the array rarely (because, of course, you have
272 to pay for Index() efficiency by having Add() be slower) but search for them
273 often. Several methods should not be used with sorted array (basically, all
274 those which break the order of items) which is mentioned in their description.
276 @todo what about STL? who does it integrates?
279 @category{containers}
281 @see wxArray, wxString, @ref overview_string
283 class wxSortedArrayString
: public wxArrayString
288 Conversion constructor.
290 Constructs a sorted array with the same contents as the (possibly
291 unsorted) "array" argument.
293 wxSortedArrayString(const wxArrayString
& array
);
296 @copydoc wxArrayString::Add()
299 For sorted arrays, the index of the inserted item will not be, in general,
300 equal to GetCount() - 1 because the item is inserted at the correct position
301 to keep the array sorted and not appended.
303 size_t Add(const wxString
& str
, size_t copies
= 1);
307 @copydoc wxArrayString::Index()
309 This function uses binary search for wxSortedArrayString, but it ignores
310 the @a bCase and @a bFromEnd parameters.
312 int Index(const wxString
& sz
, bool bCase
= true,
313 bool bFromEnd
= false) const;
316 @warning This function should not be used with sorted arrays because it
317 could break the order of items and, for example, subsequent calls
318 to Index() would then not work!
320 @warning In STL mode, Insert is private and simply invokes wxFAIL_MSG.
322 void Insert(const wxString
& str
, size_t nIndex
,
327 @warning This function should not be used with sorted array because it could
328 break the order of items and, for example, subsequent calls to Index()
329 would then not work! Also, sorting a wxSortedArrayString doesn't make
330 sense because its elements are always already sorted.
332 @warning In STL mode, Sort is private and simply invokes wxFAIL_MSG.
334 void Sort(bool reverseOrder
= false);
335 void Sort(CompareFunction compareFunction
);
340 // ============================================================================
341 // Global functions/macros
342 // ============================================================================
344 /** @addtogroup group_funcmacro_string */
348 Splits the given wxString object using the separator @a sep and returns the
349 result as a wxArrayString.
351 If the @a escape character is non-@NULL, then the occurrences of @a sep
352 immediately prefixed with @a escape are not considered as separators.
353 Note that empty tokens will be generated if there are two or more adjacent
360 wxArrayString
wxSplit(const wxString
& str
, const wxChar sep
,
361 const wxChar escape
= '\\');
364 Concatenate all lines of the given wxArrayString object using the separator
365 @a sep and returns the result as a wxString.
367 If the @a escape character is non-@NULL, then it's used as prefix for each
368 occurrence of @a sep in the strings contained in @a arr before joining them
369 which is necessary in order to be able to recover the original array
370 contents from the string later using wxSplit().
376 wxString
wxJoin(const wxArrayString
& arr
, const wxChar sep
,
377 const wxChar escape
= '\\');