1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxArrayString
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 wxArrayString is an efficient container for storing wxString objects.
15 It has the same features as all wxArray classes, i.e. it dynamically expands
16 when new items are added to it (so it is as easy to use as a linked list),
17 but the access time to the elements is constant, instead of being linear in
18 number of elements as in the case of linked lists. It is also very size
19 efficient and doesn't take more space than a C array @e wxString[] type
20 (wxArrayString uses its knowledge of internals of wxString class to achieve this).
22 This class is used in the same way as other dynamic arrays(), except that no
23 @e WX_DEFINE_ARRAY declaration is needed for it.
24 When a string is added or inserted in the array, a copy of the string is created,
25 so the original string may be safely deleted (e.g. if it was a @e wxChar *
26 pointer the memory it was using can be freed immediately after this).
27 In general, there is no need to worry about string memory deallocation when using
28 this class - it will always free the memory it uses itself.
30 The references returned by wxArrayString::Item, wxArrayString::Last or
31 wxArrayString::operator[] are not constant, so the array elements may
32 be modified in place like this:
35 array.Last().MakeUpper();
38 There is also a variant of wxArrayString called wxSortedArrayString which has
39 exactly the same methods as wxArrayString, but which always keeps the string
40 in it in (alphabetical) order. wxSortedArrayString uses binary search in its
41 wxArrayString::Index() function (instead of linear search for wxArrayString::Index())
42 which makes it much more efficient if you add strings to the array rarely
43 (because, of course, you have to pay for Index() efficiency by having Add() be
44 slower) but search for them often. Several methods should not be used with
45 sorted array (basically, all which break the order of items) which is
46 mentioned in their description.
48 @note none of the methods of wxArrayString is virtual including its
49 destructor, so this class should not be used as a base class.
51 Although this is not true strictly speaking, this class may be considered as
52 a specialization of wxArray class for the wxString member data: it is not
53 implemented like this, but it does have all of the wxArray functions.
58 @see wxArray, wxString, @ref overview_string
60 class wxArrayString
: public wxArray
69 Copy constructor. Note that when an array is assigned to a sorted array,
70 its contents is automatically sorted during construction.
72 wxArrayString(const wxArrayString
& array
);
76 Constructor from a C string array. Pass a size sz and array arr.
78 wxArrayString(size_t sz
, const char** arr
);
79 wxArrayString(size_t sz
, const wchar_t** arr
);
83 Constructor from a wxString array. Pass a size @a sz and array @e arr.
85 wxArrayString(size_t sz
, const wxString
* arr
);
88 Destructor frees memory occupied by the array strings. For performance
89 reasons it is not virtual, so this class should not be derived from.
94 Appends the given number of @a copies of the new item @a str to the
95 array and returns the index of the first new item in the array.
98 For sorted arrays, the index of the inserted item will not be, in general,
99 equal to GetCount() - 1 because the item is inserted at the correct position
100 to keep the array sorted and not appended.
104 size_t Add(const wxString
& str
, size_t copies
= 1);
107 Preallocates enough memory to store @a nCount items. This function may be
108 used to improve array class performance before adding a known number of items
113 @see @ref wxArray::memorymanagement "Dynamic array memory management"
115 void Alloc(size_t nCount
);
118 Clears the array contents and frees memory.
125 Empties the array: after a call to this function GetCount() will return 0.
126 However, this function does not free the memory used by the array and so
127 should be used when the array is going to be reused for storing other strings.
128 Otherwise, you should use Clear() to empty the array and free memory.
133 Returns the number of items in the array.
135 size_t GetCount() const;
138 Search the element in the array, starting from the beginning if @a bFromEnd
139 is @false or from end otherwise. If @e bCase, comparison is case sensitive
140 (default), otherwise the case is ignored.
142 This function uses linear search for wxArrayString and binary search for
143 wxSortedArrayString, but it ignores the @a bCase and @a bFromEnd parameters
145 Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
147 int Index(const wxString
& sz
, bool bCase
= true,
148 bool bFromEnd
= false);
151 Insert the given number of @a copies of the new element in the array before the
152 position @e nIndex. Thus, for example, to insert the string in the beginning of
153 the array you would write:
159 If @a nIndex is equal to @e GetCount() this function behaves as Add().
161 @warning this function should not be used with sorted arrays because it
162 could break the order of items and, for example, subsequent calls
163 to Index() would then not work!
165 void Insert(const wxString
& str
, size_t nIndex
,
169 Returns @true if the array is empty, @false otherwise. This function returns the
170 same result as @e GetCount() == 0 but is probably easier to read.
175 Return the array element at position @e nIndex. An assert failure will
176 result from an attempt to access an element beyond the end of array in debug
177 mode, but no check is done in release mode.
179 @see operator[] for the operator version.
181 wxString
Item(size_t nIndex
) const;
184 Returns the last element of the array. Attempt to access the last element of
185 an empty array will result in assert failure in debug build, however no checks
186 are done in release mode.
191 Removes the first item matching this value. An assert failure is provoked by
192 an attempt to remove an element which does not exist in debug build.
196 void Remove(const wxString
& sz
);
199 Removes @a count items starting at position @a nIndex from the array.
201 void RemoveAt(size_t nIndex
, size_t count
= 1);
204 Releases the extra memory allocated by the array. This function is useful to
205 minimize the array memory consumption.
209 @see Alloc(), @ref wxArray::memorymanagement "Dynamic array memory
215 Sorts the array in alphabetical order or in reverse alphabetical order if
216 @a reverseOrder is @true. The sort is case-sensitive.
218 @warning this function should not be used with sorted array because it could
219 break the order of items and, for example, subsequent calls to Index()
223 void Sort(bool reverseOrder
= false);
226 Sorts the array using the specified @a compareFunction for item comparison.
227 @e CompareFunction is defined as a function taking two @e const wxString
228 parameters and returning an @e int value less than, equal to or greater
229 than 0 if the first string is less than, equal to or greater than the
233 The following example sorts strings by their length.
236 static int CompareStringLen(const wxString& first, const wxString& second)
238 return first.length() - second.length();
250 array.Sort(CompareStringLen);
253 @warning this function should not be used with sorted array because
254 it could break the order of items and, for example, subsequent
255 calls to Index() would then not work!
257 void Sort(CompareFunction compareFunction
);
260 Compares 2 arrays respecting the case. Returns @true if the arrays have
261 different number of elements or if the elements don't match pairwise.
263 bool operator !=(const wxArrayString
& array
) const;
268 wxArrayString
operator =(const wxArrayString
& array
);
271 Compares 2 arrays respecting the case. Returns @true only if the arrays have
272 the same number of elements and the same strings in the same order.
274 bool operator ==(const wxArrayString
& array
) const;
277 Return the array element at position @e nIndex. An assert failure will
278 result from an attempt to access an element beyond the end of array in
279 debug mode, but no check is done in release mode.
281 This is the operator version of the Item() method.
283 wxString
operator[](size_t nIndex
);
288 // ============================================================================
289 // Global functions/macros
290 // ============================================================================
292 /** @ingroup group_funcmacro_string */
296 Splits the given wxString object using the separator @a sep and returns the
297 result as a wxArrayString.
299 If the @a escape character is non-@NULL, then the occurrences of @a sep
300 immediately prefixed with @a escape are not considered as separators.
301 Note that empty tokens will be generated if there are two or more adjacent
306 wxArrayString
wxSplit(const wxString
& str
, const wxChar sep
,
307 const wxChar escape
= '\\');
310 Concatenate all lines of the given wxArrayString object using the separator
311 @a sep and returns the result as a wxString.
313 If the @a escape character is non-@NULL, then it's used as prefix for each
314 occurrence of @e sep in the strings contained in @a arr before joining them
315 which is necessary in order to be able to recover the original array contents
316 from the string later using wxSplit().
320 wxString
wxJoin(const wxArrayString
& arr
, const wxChar sep
,
321 const wxChar escape
= '\\');