2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: arrstr.h
|
|
|
|
// Purpose: documentation for wxArrayString class
|
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class wxArrayString
|
|
|
|
@wxheader{arrstr.h}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
|
|
|
wxArrayString is an efficient container for storing
|
|
|
|
wxString objects. It has the same features as all
|
2008-03-08 08:52:38 -05:00
|
|
|
wxArray classes, i.e. it dynamically expands when new items
|
|
|
|
are added to it (so it is as easy to use as a linked list), but the access
|
|
|
|
time to the elements is constant, instead of being linear in number of
|
|
|
|
elements as in the case of linked lists. It is also very size efficient and
|
|
|
|
doesn't take more space than a C array @e wxString[] type (wxArrayString
|
|
|
|
uses its knowledge of internals of wxString class to achieve this).
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
This class is used in the same way as other dynamic arrays,
|
|
|
|
except that no @e WX_DEFINE_ARRAY declaration is needed for it. When a
|
|
|
|
string is added or inserted in the array, a copy of the string is created, so
|
2008-03-08 09:43:31 -05:00
|
|
|
the original string may be safely deleted (e.g. if it was a @e wxChar *
|
2008-03-08 08:52:38 -05:00
|
|
|
pointer the memory it was using can be freed immediately after this). In
|
|
|
|
general, there is no need to worry about string memory deallocation when using
|
|
|
|
this class - it will always free the memory it uses itself.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
|
|
|
The references returned by wxArrayString::Item,
|
|
|
|
wxArrayString::Last or
|
2008-03-08 08:52:38 -05:00
|
|
|
@ref wxArrayString::operatorindex operator[] are not constant, so the
|
|
|
|
array elements may be modified in place like this
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@code
|
|
|
|
array.Last().MakeUpper();
|
|
|
|
@endcode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
There is also a variant of wxArrayString called wxSortedArrayString which has
|
|
|
|
exactly the same methods as wxArrayString, but which always keeps the string
|
2008-03-08 09:43:31 -05:00
|
|
|
in it in (alphabetical) order. wxSortedArrayString uses binary search in its
|
2008-03-08 08:52:38 -05:00
|
|
|
wxArrayString::Index function (instead of linear search for
|
|
|
|
wxArrayString::Index) which makes it much more efficient if you add strings to
|
|
|
|
the array rarely (because, of course, you have to pay for Index() efficiency
|
|
|
|
by having Add() be slower) but search for them often. Several methods should
|
|
|
|
not be used with sorted array (basically, all which break the order of items)
|
|
|
|
which is mentioned in their description.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Final word: none of the methods of wxArrayString is virtual including its
|
|
|
|
destructor, so this class should not be used as a base class.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{containers}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@seealso
|
|
|
|
wxArray, wxString, @ref overview_wxstringoverview "wxString overview"
|
|
|
|
*/
|
|
|
|
class wxArrayString : public wxArray
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//@{
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Constructor from a wxString array. Pass a size @a sz and array @e arr.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxArrayString();
|
2008-03-08 09:43:31 -05:00
|
|
|
wxArrayString(const wxArrayString& array);
|
|
|
|
wxArrayString(size_t sz, const char** arr);
|
|
|
|
wxArrayString(size_t sz, const wchar_t** arr);
|
|
|
|
wxArrayString(size_t sz, const wxString* arr);
|
2008-03-08 08:52:38 -05:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor frees memory occupied by the array strings. For the performance
|
|
|
|
reasons it is not virtual, so this class should not be derived from.
|
|
|
|
*/
|
|
|
|
~wxArrayString();
|
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Appends the given number of @a copies of the new item @a str to the
|
2008-03-08 08:52:38 -05:00
|
|
|
array and returns the index of the first new item in the array.
|
|
|
|
@b Warning: For sorted arrays, the index of the inserted item will not be,
|
|
|
|
in general, equal to GetCount() - 1 because
|
|
|
|
the item is inserted at the correct position to keep the array sorted and not
|
|
|
|
appended.
|
|
|
|
See also: Insert()
|
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
size_t Add(const wxString& str, size_t copies = 1);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Preallocates enough memory to store @a nCount items. This function may be
|
2008-03-08 08:52:38 -05:00
|
|
|
used to improve array class performance before adding a known number of items
|
|
|
|
consecutively.
|
|
|
|
See also: @ref wxArray::memorymanagement "Dynamic array memory management"
|
|
|
|
*/
|
|
|
|
void Alloc(size_t nCount);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Clears the array contents and frees memory.
|
|
|
|
See also: Empty()
|
|
|
|
*/
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Empties the array: after a call to this function
|
2008-03-08 08:52:38 -05:00
|
|
|
GetCount() will return 0. However, this
|
|
|
|
function does not free the memory used by the array and so should be used when
|
|
|
|
the array is going to be reused for storing other strings. Otherwise, you
|
|
|
|
should use Clear() to empty the array and free
|
|
|
|
memory.
|
|
|
|
*/
|
|
|
|
void Empty();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the number of items in the array.
|
|
|
|
*/
|
|
|
|
size_t GetCount();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Search the element in the array, starting from the beginning if
|
2008-03-09 08:33:59 -04:00
|
|
|
@a bFromEnd is @false or from end otherwise. If @e bCase, comparison is
|
2008-03-08 08:52:38 -05:00
|
|
|
case sensitive (default), otherwise the case is ignored.
|
|
|
|
This function uses linear search for wxArrayString and binary search for
|
2008-03-09 08:33:59 -04:00
|
|
|
wxSortedArrayString, but it ignores the @a bCase and @a bFromEnd
|
2008-03-08 08:52:38 -05:00
|
|
|
parameters in the latter case.
|
|
|
|
Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
|
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
int Index(const wxString& sz, bool bCase = true,
|
|
|
|
bool bFromEnd = false);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Insert the given number of @a copies of the new element in the array before the
|
2008-03-08 08:52:38 -05:00
|
|
|
position @e nIndex. Thus, for
|
|
|
|
example, to insert the string in the beginning of the array you would write
|
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
If @a nIndex is equal to @e GetCount() this function behaves as
|
|
|
|
Add().
|
2008-03-08 08:52:38 -05:00
|
|
|
@b Warning: this function should not be used with sorted arrays because it
|
2008-03-08 09:43:31 -05:00
|
|
|
could break the order of items and, for example, subsequent calls to
|
2008-03-08 08:52:38 -05:00
|
|
|
Index() would then not work!
|
|
|
|
*/
|
|
|
|
void Insert(const wxString& str, size_t nIndex,
|
|
|
|
size_t copies = 1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if the array is empty, @false otherwise. This function returns the
|
|
|
|
same result as @e GetCount() == 0 but is probably easier to read.
|
|
|
|
*/
|
|
|
|
bool IsEmpty();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the array element at position @e nIndex. An assert failure will
|
|
|
|
result from an attempt to access an element beyond the end of array in debug
|
|
|
|
mode, but no check is done in release mode.
|
|
|
|
See also @ref operatorindex() operator[] for the operator
|
|
|
|
version.
|
|
|
|
*/
|
|
|
|
wxString Item(size_t nIndex);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the last element of the array. Attempt to access the last element of
|
|
|
|
an empty array will result in assert failure in debug build, however no checks
|
|
|
|
are done in release mode.
|
|
|
|
*/
|
|
|
|
wxString Last();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Removes the first item matching this value. An assert failure is provoked by
|
|
|
|
an attempt to remove an element which does not exist in debug build.
|
|
|
|
See also: Index()
|
|
|
|
*/
|
|
|
|
void Remove(const wxString& sz);
|
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Removes @a count items starting at position @a nIndex from the array.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void RemoveAt(size_t nIndex, size_t count = 1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Releases the extra memory allocated by the array. This function is useful to
|
|
|
|
minimize the array memory consumption.
|
|
|
|
See also: Alloc(), @ref wxArray::memorymanagement "Dynamic array memory
|
|
|
|
management"
|
|
|
|
*/
|
|
|
|
void Shrink();
|
|
|
|
|
|
|
|
//@{
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Sorts the array using the specified @a compareFunction for item comparison.
|
2008-03-08 08:52:38 -05:00
|
|
|
@e CompareFunction is defined as a function taking two @e const
|
|
|
|
wxString parameters and returning an @e int value less than, equal to or
|
|
|
|
greater than 0 if the first string is less than, equal to or greater than the
|
|
|
|
second one.
|
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
void Sort(bool reverseOrder = false);
|
2008-03-08 09:43:31 -05:00
|
|
|
Warning:
|
|
|
|
void Sort(CompareFunction compareFunction);
|
2008-03-08 08:52:38 -05:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Compares 2 arrays respecting the case. Returns @true if the arrays have
|
|
|
|
different number of elements or if the elements don't match pairwise.
|
|
|
|
*/
|
|
|
|
bool operator !=(const wxArrayString& array);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Assignment operator.
|
|
|
|
*/
|
|
|
|
wxArrayString operator =(const wxArrayString& array);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Compares 2 arrays respecting the case. Returns @true only if the arrays have
|
|
|
|
the same number of elements and the same strings in the same order.
|
|
|
|
*/
|
|
|
|
bool operator ==(const wxArrayString& array);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the array element at position @e nIndex. An assert failure will
|
|
|
|
result from an attempt to access an element beyond the end of array in debug
|
|
|
|
mode, but no check is done in release mode.
|
|
|
|
This is the operator version of Item() method.
|
|
|
|
*/
|
|
|
|
wxString operator[](size_t nIndex);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// Global functions/macros
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Splits the given wxString object using the separator @a sep and returns the
|
2008-03-08 08:52:38 -05:00
|
|
|
result as a wxArrayString.
|
2008-03-09 08:33:59 -04:00
|
|
|
If the @a escape character is non-@NULL, then the occurrences of @a sep
|
2008-03-08 08:52:38 -05:00
|
|
|
immediately prefixed
|
2008-03-09 08:33:59 -04:00
|
|
|
with @a escape are not considered as separators.
|
2008-03-08 08:52:38 -05:00
|
|
|
Note that empty tokens will be generated if there are two or more adjacent
|
|
|
|
separators.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
@see wxJoin
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxArrayString wxSplit(const wxString& str, const wxChar sep,
|
|
|
|
const wxChar escape = '
|
2008-03-08 09:43:31 -05:00
|
|
|
');
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Concatenate all lines of the given wxArrayString object using the separator @a
|
2008-03-08 08:52:38 -05:00
|
|
|
sep and returns
|
|
|
|
the result as a wxString.
|
2008-03-09 08:33:59 -04:00
|
|
|
If the @a escape character is non-@NULL, then it's used as prefix for each
|
2008-03-08 08:52:38 -05:00
|
|
|
occurrence of @e sep
|
2008-03-09 08:33:59 -04:00
|
|
|
in the strings contained in @a arr before joining them which is necessary
|
2008-03-08 08:52:38 -05:00
|
|
|
in order to be able to recover the original array contents from the string
|
|
|
|
later using wxSplit.
|
|
|
|
*/
|
|
|
|
wxString wxJoin(const wxArrayString& arr, const wxChar sep,
|
|
|
|
const wxChar escape = '\');
|
|
|
|
|