1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/arrstr.cpp
3 // Purpose: wxArrayString class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
13 // headers, declarations, constants
14 // ===========================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/arrstr.h"
24 #include "wx/thread.h"
26 // ============================================================================
28 // ============================================================================
30 wxArrayString::wxArrayString(size_t sz
, const wxChar
** a
)
35 for (size_t i
=0; i
< sz
; i
++)
39 wxArrayString::wxArrayString(size_t sz
, const wxString
* a
)
44 for (size_t i
=0; i
< sz
; i
++)
50 // size increment = min(50% of current size, ARRAY_MAXSIZE_INCREMENT)
51 #define ARRAY_MAXSIZE_INCREMENT 4096
53 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
54 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
58 void wxArrayString::Init(bool autoSort
)
63 m_autoSort
= autoSort
;
67 wxArrayString::wxArrayString(const wxArrayString
& src
)
74 // assignment operator
75 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
82 m_autoSort
= src
.m_autoSort
;
87 void wxArrayString::Copy(const wxArrayString
& src
)
89 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
92 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
97 void wxArrayString::Grow(size_t nIncrement
)
99 // only do it if no more place
100 if ( (m_nSize
- m_nCount
) < nIncrement
) {
101 // if ARRAY_DEFAULT_INITIAL_SIZE were set to 0, the initially empty would
103 #if ARRAY_DEFAULT_INITIAL_SIZE == 0
104 #error "ARRAY_DEFAULT_INITIAL_SIZE must be > 0!"
107 if ( m_nSize
== 0 ) {
108 // was empty, alloc some memory
109 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
110 if (m_nSize
< nIncrement
)
111 m_nSize
= nIncrement
;
112 m_pItems
= new wxString
[m_nSize
];
115 // otherwise when it's called for the first time, nIncrement would be 0
116 // and the array would never be expanded
117 // add 50% but not too much
118 size_t ndefIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
119 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
120 if ( ndefIncrement
> ARRAY_MAXSIZE_INCREMENT
)
121 ndefIncrement
= ARRAY_MAXSIZE_INCREMENT
;
122 if ( nIncrement
< ndefIncrement
)
123 nIncrement
= ndefIncrement
;
124 m_nSize
+= nIncrement
;
125 wxString
*pNew
= new wxString
[m_nSize
];
127 // copy data to new location
128 for ( size_t j
= 0; j
< m_nCount
; j
++ )
129 pNew
[j
] = m_pItems
[j
];
131 // delete old memory (but do not release the strings!)
139 // deletes all the strings from the list
140 void wxArrayString::Empty()
145 // as Empty, but also frees memory
146 void wxArrayString::Clear()
155 wxArrayString::~wxArrayString()
160 void wxArrayString::reserve(size_t nSize
)
165 // pre-allocates memory (frees the previous data!)
166 void wxArrayString::Alloc(size_t nSize
)
168 // only if old buffer was not big enough
169 if ( nSize
> m_nSize
) {
170 wxString
*pNew
= new wxString
[nSize
];
174 for ( size_t j
= 0; j
< m_nCount
; j
++ )
175 pNew
[j
] = m_pItems
[j
];
183 // minimizes the memory usage by freeing unused memory
184 void wxArrayString::Shrink()
186 // only do it if we have some memory to free
187 if( m_nCount
< m_nSize
) {
188 // allocates exactly as much memory as we need
189 wxString
*pNew
= new wxString
[m_nCount
];
191 // copy data to new location
192 for ( size_t j
= 0; j
< m_nCount
; j
++ )
193 pNew
[j
] = m_pItems
[j
];
199 // searches the array for an item (forward or backwards)
200 int wxArrayString::Index(const wxChar
*sz
, bool bCase
, bool bFromEnd
) const
203 // use binary search in the sorted array
204 wxASSERT_MSG( bCase
&& !bFromEnd
,
205 wxT("search parameters ignored for auto sorted array") );
214 res
= wxStrcmp(sz
, m_pItems
[i
]);
226 // use linear search in unsorted array
228 if ( m_nCount
> 0 ) {
229 size_t ui
= m_nCount
;
231 if ( m_pItems
[--ui
].IsSameAs(sz
, bCase
) )
238 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
239 if( m_pItems
[ui
].IsSameAs(sz
, bCase
) )
248 // add item at the end
249 size_t wxArrayString::Add(const wxString
& str
, size_t nInsert
)
252 // insert the string at the correct position to keep the array sorted
260 res
= str
.Cmp(m_pItems
[i
]);
271 wxASSERT_MSG( lo
== hi
, wxT("binary search broken") );
273 Insert(str
, lo
, nInsert
);
280 for (size_t i
= 0; i
< nInsert
; i
++)
283 m_pItems
[m_nCount
+ i
] = str
;
285 size_t ret
= m_nCount
;
291 // add item at the given position
292 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
, size_t nInsert
)
294 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Insert") );
295 wxCHECK_RET( m_nCount
<= m_nCount
+ nInsert
,
296 wxT("array size overflow in wxArrayString::Insert") );
300 for (int j
= m_nCount
- nIndex
- 1; j
>= 0; j
--)
301 m_pItems
[nIndex
+ nInsert
+ j
] = m_pItems
[nIndex
+ j
];
303 for (size_t i
= 0; i
< nInsert
; i
++)
305 m_pItems
[nIndex
+ i
] = str
;
310 // range insert (STL 23.2.4.3)
312 wxArrayString::insert(iterator it
, const_iterator first
, const_iterator last
)
314 const int idx
= it
- begin();
319 // reset "it" since it can change inside Grow()
322 while ( first
!= last
)
324 it
= insert(it
, *first
);
326 // insert returns an iterator to the last element inserted but we need
327 // insert the next after this one, that is before the next one
335 void wxArrayString::SetCount(size_t count
)
340 while ( m_nCount
< count
)
341 m_pItems
[m_nCount
++] = s
;
344 // removes item from array (by index)
345 void wxArrayString::RemoveAt(size_t nIndex
, size_t nRemove
)
347 wxCHECK_RET( nIndex
< m_nCount
, wxT("bad index in wxArrayString::Remove") );
348 wxCHECK_RET( nIndex
+ nRemove
<= m_nCount
,
349 wxT("removing too many elements in wxArrayString::Remove") );
351 for ( size_t j
= 0; j
< m_nCount
- nIndex
-nRemove
; j
++)
352 m_pItems
[nIndex
+ j
] = m_pItems
[nIndex
+ nRemove
+ j
];
357 // removes item from array (by value)
358 void wxArrayString::Remove(const wxChar
*sz
)
360 int iIndex
= Index(sz
);
362 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
363 wxT("removing inexistent element in wxArrayString::Remove") );
368 void wxArrayString::assign(const_iterator first
, const_iterator last
)
370 reserve(last
- first
);
371 for(; first
!= last
; ++first
)
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 // we can only sort one array at a time with the quick-sort based
382 // need a critical section to protect access to gs_compareFunction and
383 // gs_sortAscending variables
384 static wxCriticalSection gs_critsectStringSort
;
385 #endif // wxUSE_THREADS
387 // function to use for string comparaison
388 static wxArrayString::CompareFunction gs_compareFunction
= NULL
;
390 // if we don't use the compare function, this flag tells us if we sort the
391 // array in ascending or descending order
392 static bool gs_sortAscending
= true;
394 // function which is called by quick sort
395 extern "C" int wxC_CALLING_CONV
// LINKAGEMODE
396 wxStringCompareFunction(const void *first
, const void *second
)
398 wxString
*strFirst
= (wxString
*)first
;
399 wxString
*strSecond
= (wxString
*)second
;
401 if ( gs_compareFunction
) {
402 return gs_compareFunction(*strFirst
, *strSecond
);
405 // maybe we should use wxStrcoll
406 int result
= strFirst
->Cmp(*strSecond
);
408 return gs_sortAscending
? result
: -result
;
412 // sort array elements using passed comparaison function
413 void wxArrayString::Sort(CompareFunction compareFunction
)
415 wxCRIT_SECT_LOCKER(lockCmpFunc
, gs_critsectStringSort
);
417 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
418 gs_compareFunction
= compareFunction
;
422 // reset it to NULL so that Sort(bool) will work the next time
423 gs_compareFunction
= NULL
;
428 typedef int (wxC_CALLING_CONV
* wxStringCompareFn
)(const void *first
,
432 void wxArrayString::Sort(CompareFunction2 compareFunction
)
434 qsort(m_pItems
, m_nCount
, sizeof(wxString
), (wxStringCompareFn
)compareFunction
);
437 void wxArrayString::Sort(bool reverseOrder
)
439 Sort(reverseOrder
? wxStringSortDescending
: wxStringSortAscending
);
442 void wxArrayString::DoSort()
444 wxCHECK_RET( !m_autoSort
, wxT("can't use this method with sorted arrays") );
446 qsort(m_pItems
, m_nCount
, sizeof(wxString
), wxStringCompareFunction
);
449 bool wxArrayString::operator==(const wxArrayString
& a
) const
451 if ( m_nCount
!= a
.m_nCount
)
454 for ( size_t n
= 0; n
< m_nCount
; n
++ )
456 if ( Item(n
) != a
[n
] )
465 int wxCMPFUNC_CONV
wxStringSortAscending(wxString
* s1
, wxString
* s2
)
470 int wxCMPFUNC_CONV
wxStringSortDescending(wxString
* s1
, wxString
* s2
)
472 return -s1
->Cmp(*s2
);
477 // ===========================================================================
478 // wxJoin and wxSplit
479 // ===========================================================================
481 #include "wx/tokenzr.h"
483 wxString
wxJoin(const wxArrayString
& arr
, const wxChar sep
, const wxChar escape
)
485 size_t count
= arr
.size();
487 return wxEmptyString
;
491 // pre-allocate memory using the estimation of the average length of the
492 // strings in the given array: this is very imprecise, of course, but
493 // better than nothing
494 str
.reserve(count
*(arr
[0].length() + arr
[count
-1].length()) / 2);
496 if ( escape
== wxT('\0') )
498 // escaping is disabled:
499 for ( size_t i
= 0; i
< count
; i
++ )
506 else // use escape character
508 for ( size_t n
= 0; n
< count
; n
++ )
513 for ( wxString::const_iterator i
= arr
[n
].begin(),
518 const wxChar ch
= *i
;
520 str
+= escape
; // escape this separator
526 str
.Shrink(); // release extra memory if we allocated too much
530 wxArrayString
wxSplit(const wxString
& str
, const wxChar sep
, const wxChar escape
)
532 if ( escape
== wxT('\0') )
534 // simple case: we don't need to honour the escape character
535 return wxStringTokenize(str
, sep
, wxTOKEN_RET_EMPTY_ALL
);
540 wxChar prev
= wxT('\0');
542 for ( wxString::const_iterator i
= str
.begin(),
547 const wxChar ch
= *i
;
551 if ( prev
== escape
)
553 // remove the escape character and don't consider this
554 // occurrence of 'sep' as a real separator
555 *curr
.rbegin() = sep
;
557 else // real separator
563 else // normal character
571 // add the last token
572 if ( !curr
.empty() || prev
== sep
)