Qt 6.x
The Qt SDK
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
206namespace {
207struct CaseInsensitiveLessThan {
208 typedef bool result_type;
209 result_type operator()(const QString &s1, const QString &s2) const
210 {
211 return s1.compare(s2, Qt::CaseInsensitive) < 0;
212 }
213};
214}
215
217{
218 if (cs == Qt::CaseSensitive)
219 std::sort(that->begin(), that->end());
220 else
221 std::sort(that->begin(), that->end(), CaseInsensitiveLessThan());
222}
223
224
252{
255 for (qsizetype i = 0; i < that->size(); ++i)
256 if (matcher.indexIn(that->at(i)) != -1)
257 res << that->at(i);
258 return res;
259}
260
261template<typename T>
263{
264 for (const auto &string : stringList) {
265 if (string.size() == str.size() && string.compare(str, cs) == 0)
266 return true;
267 }
268 return false;
269}
270
271
293{
294 return stringList_contains(*that, str, cs);
295}
296
310{
311 return stringList_contains(*that, str, cs);
312}
313
314
315#if QT_CONFIG(regularexpression)
325{
327 for (qsizetype i = 0; i < that->size(); ++i) {
328 if (that->at(i).contains(re))
329 res << that->at(i);
330 }
331 return res;
332}
333#endif // QT_CONFIG(regularexpression)
334
370{
371 for (qsizetype i = 0; i < that->size(); ++i)
372 (*that)[i].replace(before.data(), before.size(), after.data(), after.size(), cs);
373}
374
375#if QT_CONFIG(regularexpression)
400{
401 for (qsizetype i = 0; i < that->size(); ++i)
402 (*that)[i].replace(re, after);
403}
404#endif // QT_CONFIG(regularexpression)
405
407{
408 qsizetype result = 0;
409 if (!list.isEmpty()) {
410 for (const auto &e : list)
411 result += e.size() + seplen;
412 result -= seplen;
413 }
414 return result;
415}
416
433{
434 const qsizetype totalLength = accumulatedSize(*that, seplen);
435 const qsizetype size = that->size();
436
437 QString res;
438 if (totalLength == 0)
439 return res;
440 res.reserve(totalLength);
441 for (qsizetype i = 0; i < size; ++i) {
442 if (i)
443 res.append(sep, seplen);
444 res += that->at(i);
445 }
446 return res;
447}
448
455{
457 if (!list.isEmpty()) {
458 result.reserve(accumulatedSize(list, sep.size()));
459 const auto end = list.end();
460 auto it = list.begin();
461 result += *it;
462 while (++it != end) {
463 result += sep;
464 result += *it;
465 }
466 }
467 return result;
468}
469
476{
477 return QStringList_join(that, sep.data(), sep.size());
478}
479
517#if QT_CONFIG(regularexpression)
529qsizetype QtPrivate::QStringList_indexOf(const QStringList *that, const QRegularExpression &re, qsizetype from)
530{
531 if (from < 0)
532 from = qMax(from + that->size(), qsizetype(0));
533
535 QRegularExpression exactRe(exactPattern, re.patternOptions());
536
537 for (qsizetype i = from; i < that->size(); ++i) {
538 QRegularExpressionMatch m = exactRe.match(that->at(i));
539 if (m.hasMatch())
540 return i;
541 }
542 return -1;
543}
544
557qsizetype QtPrivate::QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, qsizetype from)
558{
559 if (from < 0)
560 from += that->size();
561 else if (from >= that->size())
562 from = that->size() - 1;
563
565 QRegularExpression exactRe(exactPattern, re.patternOptions());
566
567 for (qsizetype i = from; i >= 0; --i) {
568 QRegularExpressionMatch m = exactRe.match(that->at(i));
569 if (m.hasMatch())
570 return i;
571 }
572 return -1;
573}
574#endif // QT_CONFIG(regularexpression)
575
588{
589 QDuplicateTracker<QString> seen(that->size());
590 return that->removeIf([&](const QString &s) { return seen.hasSeen(s); });
591}
592
\inmodule QtCore
Definition qchar.h:48
bool hasSeen(const T &s)
bool isEmpty() const noexcept
Definition qlist.h:390
iterator end()
Definition qlist.h:609
iterator begin()
Definition qlist.h:608
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
PatternOptions patternOptions() const
Returns the pattern options for the regular expression.
QString pattern() const
Returns the pattern string of the regular expression.
static QString anchoredPattern(const QString &expression)
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:76
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:127
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
QString str
[2]
double e
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)
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)
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
GLuint res
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
static constexpr QChar sep
static bool stringList_contains(const QStringList &stringList, const T &str, Qt::CaseSensitivity cs)
static qsizetype accumulatedSize(const QStringList &list, qsizetype seplen)
#define s2
ptrdiff_t qsizetype
Definition qtypes.h:70
QList< int > list
[14]
static const auto matcher
[0]
QList< QString > stringList