Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qstringlist.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qstringlist.h>
5#include <qset.h>
6#if QT_CONFIG(regularexpression)
7# include <qregularexpression.h>
8#endif
9#include <private/qduplicatetracker_p.h>
10
11#include <algorithm>
13
194
197
210{
211 if (cs == Qt::CaseSensitive) {
212 std::sort(that->begin(), that->end());
213 } else {
214 auto CISCompare = [](const auto &s1, const auto &s2) {
215 return s1.compare(s2, Qt::CaseInsensitive) < 0;
216 };
217 std::sort(that->begin(), that->end(), CISCompare);
218 }
219}
220
221
240template <typename String>
241static QStringList filter_helper(const QStringList &that, const String &needle, Qt::CaseSensitivity cs)
242{
244 for (const auto &s : that) {
245 if (s.contains(needle, cs))
246 res.append(s);
247 }
248 return res;
249}
250
261
280{
282 for (const auto &s : that) {
283 if (matcher.indexIn(s) != -1)
284 res.append(s);
285 }
286 return res;
287}
288
297{
298 return filter_helper(that, needle, cs);
299}
300
301template<typename T>
303{
304 for (const auto &string : stringList) {
305 if (string.size() == str.size() && string.compare(str, cs) == 0)
306 return true;
307 }
308 return false;
309}
310
311
335{
336 return stringList_contains(*that, str, cs);
337}
338
356
357
358#if QT_CONFIG(regularexpression)
368{
370 for (const auto &str : *that) {
371 if (str.contains(re))
372 res.append(str);
373 }
374 return res;
375}
376#endif // QT_CONFIG(regularexpression)
377
414{
415 // Before potentially detaching "that" list, check if any string contains "before"
416 qsizetype i = -1;
417 for (qsizetype j = 0; j < that->size(); ++j) {
418 if (that->at(j).contains(before, cs)) {
419 i = j;
420 break;
421 }
422 }
423 if (i == -1)
424 return;
425
426 for (; i < that->size(); ++i)
427 (*that)[i].replace(before.data(), before.size(), after.data(), after.size(), cs);
428}
429
430#if QT_CONFIG(regularexpression)
455 const QString &after)
456{
457 // Before potentially detaching "that" list, check if any string contains "before"
458 qsizetype i = -1;
459 for (qsizetype j = 0; j < that->size(); ++j) {
460 if (that->at(j).contains(re)) {
461 i = j;
462 break;
463 }
464 }
465 if (i == -1)
466 return;
467
468 for (; i < that->size(); ++i)
469 (*that)[i].replace(re, after);
470}
471#endif // QT_CONFIG(regularexpression)
472
474{
475 qsizetype result = 0;
476 if (!list.isEmpty()) {
477 for (const auto &e : list)
478 result += e.size() + seplen;
479 result -= seplen;
480 }
481 return result;
482}
483
500{
501 const qsizetype totalLength = accumulatedSize(*that, seplen);
502 const qsizetype size = that->size();
503
504 QString res;
505 if (totalLength == 0)
506 return res;
507 res.reserve(totalLength);
508 for (qsizetype i = 0; i < size; ++i) {
509 if (i)
510 res.append(sep, seplen);
511 res += that->at(i);
512 }
513 return res;
514}
515
522{
524 if (!list.isEmpty()) {
526 const auto end = list.end();
527 auto it = list.begin();
528 result += *it;
529 while (++it != end) {
530 result += sep;
531 result += *it;
532 }
533 }
534 return result;
535}
536
543{
544 return QStringList_join(that, sep.data(), sep.size());
545}
546
596
601
605template <typename String>
606qsizetype indexOf_helper(const QStringList &that, String needle, qsizetype from,
608{
609 if (from < 0) // Historical behavior
610 from = qMax(from + that.size(), 0);
611
612 if (from >= that.size())
613 return -1;
614
615 for (qsizetype i = from; i < that.size(); ++i) {
616 if (needle.compare(that.at(i), cs) == 0)
617 return i;
618 }
619 return -1;
620}
621
624{
625 return indexOf_helper(that, needle, from, cs);
626}
627
630{
631 return indexOf_helper(that, needle, from, cs);
632}
633
651template <typename String>
652qsizetype lastIndexof_helper(const QStringList &that, String needle, qsizetype from,
654{
655 if (from < 0)
656 from += that.size();
657 else if (from >= that.size())
658 from = that.size() - 1;
659
660 for (qsizetype i = from; i >= 0; --i) {
661 if (needle.compare(that.at(i), cs) == 0)
662 return i;
663 }
664
665 return -1;
666}
667
670{
671 return lastIndexof_helper(that, needle, from, cs);
672}
673
676{
677 return lastIndexof_helper(that, needle, from, cs);
678}
679
680#if QT_CONFIG(regularexpression)
693{
694 if (from < 0)
695 from = qMax(from + that->size(), qsizetype(0));
696
697 QString exactPattern = QRegularExpression::anchoredPattern(re.pattern());
698 QRegularExpression exactRe(exactPattern, re.patternOptions());
699
700 for (qsizetype i = from; i < that->size(); ++i) {
701 QRegularExpressionMatch m = exactRe.match(that->at(i));
702 if (m.hasMatch())
703 return i;
704 }
705 return -1;
706}
707
721{
722 if (from < 0)
723 from += that->size();
724 else if (from >= that->size())
725 from = that->size() - 1;
726
727 QString exactPattern = QRegularExpression::anchoredPattern(re.pattern());
728 QRegularExpression exactRe(exactPattern, re.patternOptions());
729
730 for (qsizetype i = from; i >= 0; --i) {
731 QRegularExpressionMatch m = exactRe.match(that->at(i));
732 if (m.hasMatch())
733 return i;
734 }
735 return -1;
736}
737#endif // QT_CONFIG(regularexpression)
738
751{
752 QDuplicateTracker<QString> seen(that->size());
753 return that->removeIf([&](const QString &s) { return seen.hasSeen(s); });
754}
755
\inmodule QtCore
bool isEmpty() const noexcept
Definition qlist.h:401
iterator end()
Definition qlist.h:626
iterator begin()
Definition qlist.h:625
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
static QString anchoredPattern(const QString &expression)
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
constexpr qsizetype size() const noexcept
Returns the size of this string view, in UTF-16 code units (that is, surrogate pairs count as two for...
const_pointer data() const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void reserve(qsizetype size)
Ensures the string has space for at least size characters.
Definition qstring.h:1325
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
bool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.h:1369
QString str
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, QStringView before, QStringView after, Qt::CaseSensitivity cs)
bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QStringView str, Qt::CaseSensitivity cs)
qsizetype Q_CORE_EXPORT QStringList_indexOf(const QStringList &that, QStringView str, qsizetype from, Qt::CaseSensitivity cs)
QString Q_CORE_EXPORT QStringList_join(const QStringList *that, QStringView sep)
qsizetype Q_CORE_EXPORT QStringList_removeDuplicates(QStringList *that)
QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, QStringView str, Qt::CaseSensitivity cs)
void Q_CORE_EXPORT QStringList_sort(QStringList *that, Qt::CaseSensitivity cs)
Q_CORE_EXPORT qsizetype QStringList_lastIndexOf(const QStringList &that, QStringView str, qsizetype from, Qt::CaseSensitivity cs)
CaseSensitivity
@ CaseInsensitive
@ CaseSensitive
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
const GLfloat * m
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLuint end
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLdouble s
[6]
Definition qopenglext.h:235
GLuint res
GLuint64EXT * result
[6]
static constexpr QChar sep
qsizetype indexOf_helper(const QStringList &that, String needle, qsizetype from, Qt::CaseSensitivity cs)
static QStringList filter_helper(const QStringList &that, const String &needle, Qt::CaseSensitivity cs)
qsizetype lastIndexof_helper(const QStringList &that, String needle, qsizetype from, Qt::CaseSensitivity cs)
static bool stringList_contains(const QStringList &stringList, const T &str, Qt::CaseSensitivity cs)
static qsizetype accumulatedSize(const QStringList &list, qsizetype seplen)
#define s2
#define s1
ptrdiff_t qsizetype
Definition qtypes.h:165
QList< int > list
[14]
static const auto matcher
[0]
QList< QString > stringList