Qt 4.8
qstringlist.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qstringlist.h>
43 #include <qset.h>
44 
46 
253 {
254  qSort(*that);
255 }
256 
257 
258 #ifdef QT3_SUPPORT
259 
284 #ifndef QT_NO_REGEXP
285 
300 #endif
301 #endif // QT3_SUPPORT
302 
327 {
328  QStringMatcher matcher(str, cs);
329  QStringList res;
330  for (int i = 0; i < that->size(); ++i)
331  if (matcher.indexIn(that->at(i)) != -1)
332  res << that->at(i);
333  return res;
334 }
335 
336 
351 {
352  for (int i = 0; i < that->size(); ++i) {
353  const QString & string = that->at(i);
354  if (string.length() == str.length() && str.compare(string, cs) == 0)
355  return QBool(true);
356  }
357  return QBool(false);
358 }
359 
360 #ifndef QT_NO_REGEXP
361 
373 {
374  QStringList res;
375  for (int i = 0; i < that->size(); ++i)
376  if (that->at(i).contains(rx))
377  res << that->at(i);
378  return res;
379 }
380 #endif
381 
401  const QString &after, Qt::CaseSensitivity cs)
402 {
403  for (int i = 0; i < that->size(); ++i)
404  (*that)[i].replace(before, after, cs);
405 }
406 
407 
408 #ifndef QT_NO_REGEXP
409 
435 {
436  for (int i = 0; i < that->size(); ++i)
437  (*that)[i].replace(rx, after);
438 }
439 #endif
440 
454 {
455  int totalLength = 0;
456  const int size = that->size();
457 
458  for (int i = 0; i < size; ++i)
459  totalLength += that->at(i).size();
460 
461  if(size > 0)
462  totalLength += sep.size() * (size - 1);
463 
464  QString res;
465  if (totalLength == 0)
466  return res;
467  res.reserve(totalLength);
468  for (int i = 0; i < that->size(); ++i) {
469  if (i)
470  res += sep;
471  res += that->at(i);
472  }
473  return res;
474 }
475 
512 #ifndef QT_NO_DATASTREAM
513 
537 #endif // QT_NO_DATASTREAM
538 
606 #ifndef QT_NO_REGEXP
607 static int indexOfMutating(const QStringList *that, QRegExp &rx, int from)
608 {
609  if (from < 0)
610  from = qMax(from + that->size(), 0);
611  for (int i = from; i < that->size(); ++i) {
612  if (rx.exactMatch(that->at(i)))
613  return i;
614  }
615  return -1;
616 }
617 
618 static int lastIndexOfMutating(const QStringList *that, QRegExp &rx, int from)
619 {
620  if (from < 0)
621  from += that->size();
622  else if (from >= that->size())
623  from = that->size() - 1;
624  for (int i = from; i >= 0; --i) {
625  if (rx.exactMatch(that->at(i)))
626  return i;
627  }
628  return -1;
629 }
630 
645 int QtPrivate::QStringList_indexOf(const QStringList *that, const QRegExp &rx, int from)
646 {
647  QRegExp rx2(rx);
648  return indexOfMutating(that, rx2, from);
649 }
650 
670 int QtPrivate::QStringList_indexOf(const QStringList *that, QRegExp &rx, int from)
671 {
672  return indexOfMutating(that, rx, from);
673 }
674 
690 int QtPrivate::QStringList_lastIndexOf(const QStringList *that, const QRegExp &rx, int from)
691 {
692  QRegExp rx2(rx);
693  return lastIndexOfMutating(that, rx2, from);
694 }
695 
717 {
718  return lastIndexOfMutating(that, rx, from);
719 }
720 #endif
721 
766 {
767  int n = that->size();
768  int j = 0;
769  QSet<QString> seen;
770  seen.reserve(n);
771  for (int i = 0; i < n; ++i) {
772  const QString &s = that->at(i);
773  if (seen.contains(s))
774  continue;
775  seen.insert(s);
776  if (j != i)
777  (*that)[j] = s;
778  ++j;
779  }
780  if (n != j)
781  that->erase(that->begin() + j, that->end());
782  return n - j;
783 }
784 
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
void Q_CORE_EXPORT QStringList_sort(QStringList *that)
int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, const QRegExp &rx, int from)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QBool Q_CORE_EXPORT QStringList_contains(const QStringList *that, const QString &str, Qt::CaseSensitivity cs)
static int lastIndexOfMutating(const QStringList *that, QRegExp &rx, int from)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QString &before, const QString &after, Qt::CaseSensitivity cs)
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QStringMatcher class holds a sequence of characters that can be quickly matched in a Unicode stri...
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QString &str, Qt::CaseSensitivity cs)
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
int Q_CORE_EXPORT QStringList_removeDuplicates(QStringList *that)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static int indexOfMutating(const QStringList *that, QRegExp &rx, int from)
bool contains(const T &value) const
Definition: qset.h:91
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QString Q_CORE_EXPORT QStringList_join(const QStringList *that, const QString &sep)
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
const_iterator insert(const T &value)
Definition: qset.h:179
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
CaseSensitivity
Definition: qnamespace.h:1451
iterator erase(iterator pos)
Removes the item associated with the iterator pos from the list, and returns an iterator to the next ...
Definition: qlist.h:464
int compare(const QString &s) const
Definition: qstring.cpp:5037
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
int indexIn(const QString &str, int from=0) const
Searches the string str from character position from (default 0, i.e.
int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, const QRegExp &rx, int from)
bool exactMatch(const QString &str) const
Returns true if str is matched exactly by this regular expression; otherwise returns false...
Definition: qregexp.cpp:4094
void reserve(int size)
Definition: qset.h:241