]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: arrstr.h | |
e54c96f1 | 3 | // Purpose: interface of wxArrayString |
23324ae1 | 4 | // Author: wxWidgets team |
526954c5 | 5 | // Licence: wxWindows licence |
23324ae1 FM |
6 | ///////////////////////////////////////////////////////////////////////////// |
7 | ||
8 | /** | |
9 | @class wxArrayString | |
7c913512 | 10 | |
39fb8056 FM |
11 | wxArrayString is an efficient container for storing wxString objects. |
12 | ||
13 | It has the same features as all wxArray classes, i.e. it dynamically expands | |
14 | when new items are added to it (so it is as easy to use as a linked list), | |
15 | but the access time to the elements is constant, instead of being linear in | |
16 | number of elements as in the case of linked lists. It is also very size | |
17 | efficient and doesn't take more space than a C array @e wxString[] type | |
18 | (wxArrayString uses its knowledge of internals of wxString class to achieve this). | |
19 | ||
20 | This class is used in the same way as other dynamic arrays(), except that no | |
71f8a117 | 21 | ::WX_DEFINE_ARRAY declaration is needed for it. |
39fb8056 FM |
22 | When a string is added or inserted in the array, a copy of the string is created, |
23 | so the original string may be safely deleted (e.g. if it was a @e wxChar * | |
24 | pointer the memory it was using can be freed immediately after this). | |
25 | In general, there is no need to worry about string memory deallocation when using | |
23324ae1 | 26 | this class - it will always free the memory it uses itself. |
7c913512 | 27 | |
39fb8056 FM |
28 | The references returned by wxArrayString::Item, wxArrayString::Last or |
29 | wxArrayString::operator[] are not constant, so the array elements may | |
30 | be modified in place like this: | |
7c913512 | 31 | |
23324ae1 FM |
32 | @code |
33 | array.Last().MakeUpper(); | |
34 | @endcode | |
7c913512 | 35 | |
39fb8056 FM |
36 | @note none of the methods of wxArrayString is virtual including its |
37 | destructor, so this class should not be used as a base class. | |
38 | ||
39 | Although this is not true strictly speaking, this class may be considered as | |
40 | a specialization of wxArray class for the wxString member data: it is not | |
41 | implemented like this, but it does have all of the wxArray functions. | |
7c913512 | 42 | |
d11c9d86 VZ |
43 | It also has the full set of <tt>std::vector<wxString></tt> compatible |
44 | methods, including nested @c iterator and @c const_iterator classes which | |
45 | should be used in the new code for forward compatibility with the future | |
46 | wxWidgets versions. | |
b7c50e37 | 47 | |
23324ae1 FM |
48 | @library{wxbase} |
49 | @category{containers} | |
7c913512 | 50 | |
f045c7f5 | 51 | @see wxArray<T>, wxString, @ref overview_string |
23324ae1 FM |
52 | */ |
53 | class wxArrayString : public wxArray | |
54 | { | |
55 | public: | |
f045c7f5 FM |
56 | /** |
57 | The function type used with wxArrayString::Sort function. | |
58 | */ | |
59 | typedef int (*CompareFunction)(const wxString& first, const wxString& second); | |
60 | ||
23324ae1 | 61 | /** |
39fb8056 | 62 | Default constructor. |
23324ae1 FM |
63 | */ |
64 | wxArrayString(); | |
39fb8056 FM |
65 | |
66 | /** | |
b7c50e37 | 67 | Copy constructor. |
39fb8056 | 68 | */ |
7c913512 | 69 | wxArrayString(const wxArrayString& array); |
39fb8056 FM |
70 | |
71 | //@{ | |
72 | /** | |
71f8a117 | 73 | Constructor from a C string array. Pass a size @a sz and an array @a arr. |
39fb8056 | 74 | **/ |
7c913512 FM |
75 | wxArrayString(size_t sz, const char** arr); |
76 | wxArrayString(size_t sz, const wchar_t** arr); | |
23324ae1 FM |
77 | //@} |
78 | ||
79 | /** | |
71f8a117 | 80 | Constructor from a wxString array. Pass a size @a sz and array @a arr. |
39fb8056 FM |
81 | */ |
82 | wxArrayString(size_t sz, const wxString* arr); | |
83 | ||
84 | /** | |
85 | Destructor frees memory occupied by the array strings. For performance | |
23324ae1 FM |
86 | reasons it is not virtual, so this class should not be derived from. |
87 | */ | |
88 | ~wxArrayString(); | |
89 | ||
90 | /** | |
4cc4bfaf | 91 | Appends the given number of @a copies of the new item @a str to the |
23324ae1 | 92 | array and returns the index of the first new item in the array. |
39fb8056 | 93 | |
39fb8056 | 94 | @see Insert() |
23324ae1 | 95 | */ |
4cc4bfaf | 96 | size_t Add(const wxString& str, size_t copies = 1); |
23324ae1 FM |
97 | |
98 | /** | |
76e9224e | 99 | Preallocates enough memory to store @a nCount items. |
39fb8056 | 100 | |
76e9224e FM |
101 | This function may be used to improve array class performance before |
102 | adding a known number of items consecutively. | |
23324ae1 FM |
103 | */ |
104 | void Alloc(size_t nCount); | |
105 | ||
106 | /** | |
107 | Clears the array contents and frees memory. | |
39fb8056 FM |
108 | |
109 | @see Empty() | |
23324ae1 FM |
110 | */ |
111 | void Clear(); | |
112 | ||
113 | /** | |
39fb8056 | 114 | Empties the array: after a call to this function GetCount() will return 0. |
3c4f71cc | 115 | However, this function does not free the memory used by the array and so |
39fb8056 FM |
116 | should be used when the array is going to be reused for storing other strings. |
117 | Otherwise, you should use Clear() to empty the array and free memory. | |
23324ae1 FM |
118 | */ |
119 | void Empty(); | |
120 | ||
121 | /** | |
122 | Returns the number of items in the array. | |
123 | */ | |
328f5751 | 124 | size_t GetCount() const; |
23324ae1 FM |
125 | |
126 | /** | |
39fb8056 | 127 | Search the element in the array, starting from the beginning if @a bFromEnd |
71f8a117 | 128 | is @false or from end otherwise. If @a bCase, comparison is case sensitive |
39fb8056 FM |
129 | (default), otherwise the case is ignored. |
130 | ||
b7c50e37 | 131 | This function uses linear search for wxArrayString. |
23324ae1 FM |
132 | Returns index of the first item matched or @c wxNOT_FOUND if there is no match. |
133 | */ | |
8d483c9b | 134 | int Index(const wxString& sz, bool bCase = true, bool bFromEnd = false) const; |
23324ae1 FM |
135 | |
136 | /** | |
4cc4bfaf | 137 | Insert the given number of @a copies of the new element in the array before the |
71f8a117 | 138 | position @a nIndex. Thus, for example, to insert the string in the beginning of |
39fb8056 FM |
139 | the array you would write: |
140 | ||
141 | @code | |
142 | Insert("foo", 0); | |
143 | @endcode | |
3c4f71cc | 144 | |
71f8a117 | 145 | If @a nIndex is equal to GetCount() this function behaves as Add(). |
23324ae1 | 146 | */ |
382f12e4 | 147 | void Insert(wxString lItem, size_t nIndex, size_t copies = 1); |
23324ae1 FM |
148 | |
149 | /** | |
150 | Returns @true if the array is empty, @false otherwise. This function returns the | |
71f8a117 | 151 | same result as GetCount() == 0 but is probably easier to read. |
23324ae1 | 152 | */ |
8d483c9b | 153 | bool IsEmpty() const; |
23324ae1 FM |
154 | |
155 | /** | |
8d483c9b | 156 | Return the array element at position @a nIndex. An assert failure will |
23324ae1 FM |
157 | result from an attempt to access an element beyond the end of array in debug |
158 | mode, but no check is done in release mode. | |
39fb8056 FM |
159 | |
160 | @see operator[] for the operator version. | |
23324ae1 | 161 | */ |
1007200e VZ |
162 | //@{ |
163 | wxString& Item(size_t nIndex); | |
164 | const wxString& Item(size_t nIndex) const; | |
165 | //@} | |
23324ae1 FM |
166 | |
167 | /** | |
168 | Returns the last element of the array. Attempt to access the last element of | |
169 | an empty array will result in assert failure in debug build, however no checks | |
170 | are done in release mode. | |
171 | */ | |
1007200e VZ |
172 | //@{ |
173 | wxString& Last(); | |
174 | const wxString& Last() const; | |
175 | //@} | |
23324ae1 FM |
176 | |
177 | /** | |
178 | Removes the first item matching this value. An assert failure is provoked by | |
179 | an attempt to remove an element which does not exist in debug build. | |
39fb8056 FM |
180 | |
181 | @see Index() | |
23324ae1 FM |
182 | */ |
183 | void Remove(const wxString& sz); | |
184 | ||
185 | /** | |
4cc4bfaf | 186 | Removes @a count items starting at position @a nIndex from the array. |
23324ae1 FM |
187 | */ |
188 | void RemoveAt(size_t nIndex, size_t count = 1); | |
189 | ||
190 | /** | |
76e9224e FM |
191 | Releases the extra memory allocated by the array. |
192 | This function is useful to minimize the array memory consumption. | |
39fb8056 | 193 | |
76e9224e | 194 | @see Alloc() |
23324ae1 FM |
195 | */ |
196 | void Shrink(); | |
197 | ||
39fb8056 FM |
198 | /** |
199 | Sorts the array in alphabetical order or in reverse alphabetical order if | |
200 | @a reverseOrder is @true. The sort is case-sensitive. | |
39fb8056 | 201 | */ |
39fb8056 FM |
202 | void Sort(bool reverseOrder = false); |
203 | ||
23324ae1 | 204 | /** |
4cc4bfaf | 205 | Sorts the array using the specified @a compareFunction for item comparison. |
f045c7f5 | 206 | @a CompareFunction is defined as a function taking two <em>const wxString&</em> |
3c4f71cc | 207 | parameters and returning an @e int value less than, equal to or greater |
39fb8056 | 208 | than 0 if the first string is less than, equal to or greater than the |
23324ae1 | 209 | second one. |
39fb8056 FM |
210 | |
211 | Example: | |
212 | The following example sorts strings by their length. | |
213 | ||
214 | @code | |
215 | static int CompareStringLen(const wxString& first, const wxString& second) | |
216 | { | |
217 | return first.length() - second.length(); | |
218 | } | |
3c4f71cc | 219 | |
39fb8056 | 220 | ... |
3c4f71cc | 221 | |
39fb8056 | 222 | wxArrayString array; |
3c4f71cc | 223 | |
39fb8056 FM |
224 | array.Add("one"); |
225 | array.Add("two"); | |
226 | array.Add("three"); | |
227 | array.Add("four"); | |
3c4f71cc | 228 | |
39fb8056 FM |
229 | array.Sort(CompareStringLen); |
230 | @endcode | |
23324ae1 | 231 | */ |
7c913512 | 232 | void Sort(CompareFunction compareFunction); |
23324ae1 FM |
233 | |
234 | /** | |
235 | Compares 2 arrays respecting the case. Returns @true if the arrays have | |
236 | different number of elements or if the elements don't match pairwise. | |
237 | */ | |
328f5751 | 238 | bool operator !=(const wxArrayString& array) const; |
23324ae1 FM |
239 | |
240 | /** | |
241 | Assignment operator. | |
242 | */ | |
8d483c9b | 243 | wxArrayString& operator=(const wxArrayString&); |
23324ae1 FM |
244 | |
245 | /** | |
246 | Compares 2 arrays respecting the case. Returns @true only if the arrays have | |
247 | the same number of elements and the same strings in the same order. | |
248 | */ | |
328f5751 | 249 | bool operator ==(const wxArrayString& array) const; |
23324ae1 FM |
250 | |
251 | /** | |
8d483c9b | 252 | Return the array element at position @a nIndex. An assert failure will |
39fb8056 FM |
253 | result from an attempt to access an element beyond the end of array in |
254 | debug mode, but no check is done in release mode. | |
255 | ||
256 | This is the operator version of the Item() method. | |
23324ae1 | 257 | */ |
8d483c9b | 258 | wxString& operator[](size_t nIndex) const; |
23324ae1 FM |
259 | }; |
260 | ||
261 | ||
b7c50e37 FM |
262 | /** |
263 | @class wxSortedArrayString | |
b7c50e37 FM |
264 | |
265 | wxSortedArrayString is an efficient container for storing wxString objects | |
266 | which always keeps the string in alphabetical order. | |
267 | ||
570a6d96 | 268 | wxSortedArrayString uses binary search in its wxSortedArrayString::Index() method |
b7c50e37 FM |
269 | (instead of linear search for wxArrayString::Index()) which makes it much more |
270 | efficient if you add strings to the array rarely (because, of course, you have | |
271 | to pay for Index() efficiency by having Add() be slower) but search for them | |
272 | often. Several methods should not be used with sorted array (basically, all | |
273 | those which break the order of items) which is mentioned in their description. | |
274 | ||
b7c50e37 FM |
275 | @library{wxbase} |
276 | @category{containers} | |
277 | ||
278 | @see wxArray, wxString, @ref overview_string | |
279 | */ | |
570a6d96 | 280 | class wxSortedArrayString : public wxArray |
b7c50e37 FM |
281 | { |
282 | public: | |
b7c50e37 | 283 | /** |
e5c48ccd VZ |
284 | Conversion constructor. |
285 | ||
286 | Constructs a sorted array with the same contents as the (possibly | |
287 | unsorted) "array" argument. | |
b7c50e37 | 288 | */ |
e5c48ccd | 289 | wxSortedArrayString(const wxArrayString& array); |
b7c50e37 FM |
290 | |
291 | /** | |
292 | @copydoc wxArrayString::Add() | |
293 | ||
294 | @warning | |
295 | For sorted arrays, the index of the inserted item will not be, in general, | |
296 | equal to GetCount() - 1 because the item is inserted at the correct position | |
297 | to keep the array sorted and not appended. | |
298 | */ | |
299 | size_t Add(const wxString& str, size_t copies = 1); | |
300 | ||
301 | ||
302 | /** | |
303 | @copydoc wxArrayString::Index() | |
304 | ||
3c4f71cc | 305 | This function uses binary search for wxSortedArrayString, but it ignores |
b7c50e37 FM |
306 | the @a bCase and @a bFromEnd parameters. |
307 | */ | |
308 | int Index(const wxString& sz, bool bCase = true, | |
fadc2df6 | 309 | bool bFromEnd = false) const; |
b7c50e37 FM |
310 | |
311 | /** | |
f045c7f5 | 312 | @warning This function should not be used with sorted arrays because it |
b7c50e37 FM |
313 | could break the order of items and, for example, subsequent calls |
314 | to Index() would then not work! | |
795ac80e VZ |
315 | |
316 | @warning In STL mode, Insert is private and simply invokes wxFAIL_MSG. | |
b7c50e37 FM |
317 | */ |
318 | void Insert(const wxString& str, size_t nIndex, | |
319 | size_t copies = 1); | |
320 | ||
b02f1c86 | 321 | //@{ |
b7c50e37 | 322 | /** |
f045c7f5 | 323 | @warning This function should not be used with sorted array because it could |
b7c50e37 | 324 | break the order of items and, for example, subsequent calls to Index() |
b02f1c86 FM |
325 | would then not work! Also, sorting a wxSortedArrayString doesn't make |
326 | sense because its elements are always already sorted. | |
795ac80e VZ |
327 | |
328 | @warning In STL mode, Sort is private and simply invokes wxFAIL_MSG. | |
b7c50e37 FM |
329 | */ |
330 | void Sort(bool reverseOrder = false); | |
b7c50e37 | 331 | void Sort(CompareFunction compareFunction); |
b02f1c86 | 332 | //@} |
b7c50e37 FM |
333 | }; |
334 | ||
e54c96f1 | 335 | |
23324ae1 FM |
336 | // ============================================================================ |
337 | // Global functions/macros | |
338 | // ============================================================================ | |
339 | ||
b21126db | 340 | /** @addtogroup group_funcmacro_string */ |
39fb8056 FM |
341 | //@{ |
342 | ||
23324ae1 | 343 | /** |
4cc4bfaf | 344 | Splits the given wxString object using the separator @a sep and returns the |
23324ae1 | 345 | result as a wxArrayString. |
39fb8056 | 346 | |
4cc4bfaf | 347 | If the @a escape character is non-@NULL, then the occurrences of @a sep |
39fb8056 | 348 | immediately prefixed with @a escape are not considered as separators. |
23324ae1 FM |
349 | Note that empty tokens will be generated if there are two or more adjacent |
350 | separators. | |
7c913512 | 351 | |
e54c96f1 | 352 | @see wxJoin() |
3950d49c BP |
353 | |
354 | @header{wx/arrstr.h} | |
23324ae1 FM |
355 | */ |
356 | wxArrayString wxSplit(const wxString& str, const wxChar sep, | |
39fb8056 | 357 | const wxChar escape = '\\'); |
23324ae1 FM |
358 | |
359 | /** | |
39fb8056 FM |
360 | Concatenate all lines of the given wxArrayString object using the separator |
361 | @a sep and returns the result as a wxString. | |
362 | ||
4cc4bfaf | 363 | If the @a escape character is non-@NULL, then it's used as prefix for each |
71f8a117 | 364 | occurrence of @a sep in the strings contained in @a arr before joining them |
3950d49c BP |
365 | which is necessary in order to be able to recover the original array |
366 | contents from the string later using wxSplit(). | |
39fb8056 FM |
367 | |
368 | @see wxSplit() | |
3950d49c BP |
369 | |
370 | @header{wx/arrstr.h} | |
23324ae1 FM |
371 | */ |
372 | wxString wxJoin(const wxArrayString& arr, const wxChar sep, | |
39fb8056 | 373 | const wxChar escape = '\\'); |
23324ae1 | 374 | |
39fb8056 | 375 | //@} |
3950d49c | 376 |