1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxArrayString
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 wxArrayString is an efficient container for storing
14 wxString objects. It has the same features as all
15 wxArray classes, i.e. it dynamically expands when new items
16 are added to it (so it is as easy to use as a linked list), but the access
17 time to the elements is constant, instead of being linear in number of
18 elements as in the case of linked lists. It is also very size efficient and
19 doesn't take more space than a C array @e wxString[] type (wxArrayString
20 uses its knowledge of internals of wxString class to achieve this).
22 This class is used in the same way as other dynamic arrays(),
23 except that no @e WX_DEFINE_ARRAY declaration is needed for it. When a
24 string is added or inserted in the array, a copy of the string is created, so
25 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). In
27 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,
31 wxArrayString::Last or
32 @ref wxArrayString::operatorindex operator[] are not constant, so the
33 array elements may be modified in place like this
36 array.Last().MakeUpper();
39 There is also a variant of wxArrayString called wxSortedArrayString which has
40 exactly the same methods as wxArrayString, but which always keeps the string
41 in it in (alphabetical) order. wxSortedArrayString uses binary search in its
42 wxArrayString::Index function (instead of linear search for
43 wxArrayString::Index) which makes it much more efficient if you add strings to
44 the array rarely (because, of course, you have to pay for Index() efficiency
45 by having Add() be slower) but search for them often. Several methods should
46 not be used with sorted array (basically, all which break the order of items)
47 which is mentioned in their description.
49 Final word: none of the methods of wxArrayString is virtual including its
50 destructor, so this class should not be used as a base class.
55 @see wxArray, wxString, @ref overview_wxstringoverview "wxString overview"
57 class wxArrayString
: public wxArray
62 Constructor from a wxString array. Pass a size @a sz and array @e arr.
65 wxArrayString(const wxArrayString
& array
);
66 wxArrayString(size_t sz
, const char** arr
);
67 wxArrayString(size_t sz
, const wchar_t** arr
);
68 wxArrayString(size_t sz
, const wxString
* arr
);
72 Destructor frees memory occupied by the array strings. For the performance
73 reasons it is not virtual, so this class should not be derived from.
78 Appends the given number of @a copies of the new item @a str to the
79 array and returns the index of the first new item in the array.
80 @b Warning: For sorted arrays, the index of the inserted item will not be,
81 in general, equal to GetCount() - 1 because
82 the item is inserted at the correct position to keep the array sorted and not
86 size_t Add(const wxString
& str
, size_t copies
= 1);
89 Preallocates enough memory to store @a nCount items. This function may be
90 used to improve array class performance before adding a known number of items
92 See also: @ref wxArray::memorymanagement "Dynamic array memory management"
94 void Alloc(size_t nCount
);
97 Clears the array contents and frees memory.
103 Empties the array: after a call to this function
104 GetCount() will return 0. However, this
105 function does not free the memory used by the array and so should be used when
106 the array is going to be reused for storing other strings. Otherwise, you
107 should use Clear() to empty the array and free
113 Returns the number of items in the array.
115 size_t GetCount() const;
118 Search the element in the array, starting from the beginning if
119 @a bFromEnd is @false or from end otherwise. If @e bCase, comparison is
120 case sensitive (default), otherwise the case is ignored.
121 This function uses linear search for wxArrayString and binary search for
122 wxSortedArrayString, but it ignores the @a bCase and @a bFromEnd
123 parameters in the latter case.
124 Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
126 int Index(const wxString
& sz
, bool bCase
= true,
127 bool bFromEnd
= false);
130 Insert the given number of @a copies of the new element in the array before the
131 position @e nIndex. Thus, for
132 example, to insert the string in the beginning of the array you would write
134 If @a nIndex is equal to @e GetCount() this function behaves as
136 @b Warning: this function should not be used with sorted arrays because it
137 could break the order of items and, for example, subsequent calls to
138 Index() would then not work!
140 void Insert(const wxString
& str
, size_t nIndex
,
144 Returns @true if the array is empty, @false otherwise. This function returns the
145 same result as @e GetCount() == 0 but is probably easier to read.
150 Return the array element at position @e nIndex. An assert failure will
151 result from an attempt to access an element beyond the end of array in debug
152 mode, but no check is done in release mode.
153 See also @ref operatorindex() operator[] for the operator
156 wxString
Item(size_t nIndex
) const;
159 Returns the last element of the array. Attempt to access the last element of
160 an empty array will result in assert failure in debug build, however no checks
161 are done in release mode.
166 Removes the first item matching this value. An assert failure is provoked by
167 an attempt to remove an element which does not exist in debug build.
170 void Remove(const wxString
& sz
);
173 Removes @a count items starting at position @a nIndex from the array.
175 void RemoveAt(size_t nIndex
, size_t count
= 1);
178 Releases the extra memory allocated by the array. This function is useful to
179 minimize the array memory consumption.
180 See also: Alloc(), @ref wxArray::memorymanagement "Dynamic array memory
187 Sorts the array using the specified @a compareFunction for item comparison.
188 @e CompareFunction is defined as a function taking two @e const
189 wxString parameters and returning an @e int value less than, equal to or
190 greater than 0 if the first string is less than, equal to or greater than the
193 void Sort(bool reverseOrder
= false);
195 void Sort(CompareFunction compareFunction
);
199 Compares 2 arrays respecting the case. Returns @true if the arrays have
200 different number of elements or if the elements don't match pairwise.
202 bool operator !=(const wxArrayString
& array
) const;
207 wxArrayString
operator =(const wxArrayString
& array
);
210 Compares 2 arrays respecting the case. Returns @true only if the arrays have
211 the same number of elements and the same strings in the same order.
213 bool operator ==(const wxArrayString
& array
) const;
216 Return the array element at position @e nIndex. An assert failure will
217 result from an attempt to access an element beyond the end of array in debug
218 mode, but no check is done in release mode.
219 This is the operator version of Item() method.
221 wxString
operator[](size_t nIndex
);
226 // ============================================================================
227 // Global functions/macros
228 // ============================================================================
231 Splits the given wxString object using the separator @a sep and returns the
232 result as a wxArrayString.
233 If the @a escape character is non-@NULL, then the occurrences of @a sep
235 with @a escape are not considered as separators.
236 Note that empty tokens will be generated if there are two or more adjacent
241 wxArrayString
wxSplit(const wxString
& str
, const wxChar sep
,
242 const wxChar escape
= '
246 Concatenate all lines of the given wxArrayString object using the separator @a
248 the result as a wxString.
249 If the @a escape character is non-@NULL, then it's used as prefix for each
251 in the strings contained in @a arr before joining them which is necessary
252 in order to be able to recover the original array contents from the string
253 later using wxSplit().
255 wxString
wxJoin(const wxArrayString
& arr
, const wxChar sep
,
256 const wxChar escape
= '\');