arrstr.h 12 KB

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