diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2014-11-08 12:28:16 +0200 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2014-11-20 15:37:18 +0100 |
commit | 6bded1695b0b1b562a0dbf8d50f469a1c6326ebc (patch) | |
tree | 996d1a6ec3aec3a0fee511b452a47079bdde7398 | |
parent | 1d9d8123de4e40abe2ceb2e99c82634d0a2ecc65 (diff) |
Fix QString::section() behavior on negative and out-of-range indexes
Change-Id: I3bff6ba73b15ee810bb11b2902d11244c3205b2a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/tools/qstring.cpp | 57 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qstring/tst_qstring.cpp | 22 |
2 files changed, 61 insertions, 18 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 543c75668f..ed581e43e9 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -3863,28 +3863,31 @@ QString QString::section(const QString &sep, int start, int end, SectionFlags fl { QStringList sections = split(sep, KeepEmptyParts, (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive); - if (sections.isEmpty()) - return QString(); + const int sectionsSize = sections.size(); + if (!(flags & SectionSkipEmpty)) { if (start < 0) - start += sections.count(); + start += sectionsSize; if (end < 0) - end += sections.count(); + end += sectionsSize; } else { int skip = 0; - for (int k=0; k<sections.size(); ++k) { + for (int k = 0; k < sectionsSize; ++k) { if (sections.at(k).isEmpty()) skip++; } if (start < 0) - start += sections.count() - skip; + start += sectionsSize - skip; if (end < 0) - end += sections.count() - skip; + end += sectionsSize - skip; } + if (start >= sectionsSize || end < 0 || start > end) + return QString(); + int x = 0; QString ret; int first_i = start, last_i = end; - for (int i = 0; x <= end && i < sections.size(); ++i) { + for (int i = 0; x <= end && i < sectionsSize; ++i) { QString section = sections.at(i); const bool empty = section.isEmpty(); if (x >= start) { @@ -3892,16 +3895,16 @@ QString QString::section(const QString &sep, int start, int end, SectionFlags fl first_i = i; if(x == end) last_i = i; - if(x > start) + if (x > start && i > 0) ret += sep; ret += section; } if (!empty || !(flags & SectionSkipEmpty)) x++; } - if((flags & SectionIncludeLeadingSep) && first_i) + if ((flags & SectionIncludeLeadingSep) && first_i > 0) ret.prepend(sep); - if((flags & SectionIncludeTrailingSep) && last_i < sections.size()-1) + if ((flags & SectionIncludeTrailingSep) && last_i < sectionsSize - 1) ret += sep; return ret; } @@ -3919,15 +3922,32 @@ static QString extractSections(const QList<qt_section_chunk> §ions, int end, QString::SectionFlags flags) { - if (start < 0) - start += sections.count(); - if (end < 0) - end += sections.count(); + const int sectionsSize = sections.size(); + + if (!(flags & QString::SectionSkipEmpty)) { + if (start < 0) + start += sectionsSize; + if (end < 0) + end += sectionsSize; + } else { + int skip = 0; + for (int k = 0; k < sectionsSize; ++k) { + const qt_section_chunk §ion = sections.at(k); + if (section.length == section.string.length()) + skip++; + } + if (start < 0) + start += sectionsSize - skip; + if (end < 0) + end += sectionsSize - skip; + } + if (start >= sectionsSize || end < 0 || start > end) + return QString(); QString ret; int x = 0; int first_i = start, last_i = end; - for (int i = 0; x <= end && i < sections.size(); ++i) { + for (int i = 0; x <= end && i < sectionsSize; ++i) { const qt_section_chunk §ion = sections.at(i); const bool empty = (section.length == section.string.length()); if (x >= start) { @@ -3944,12 +3964,13 @@ static QString extractSections(const QList<qt_section_chunk> §ions, x++; } - if ((flags & QString::SectionIncludeLeadingSep) && first_i < sections.size()) { + if ((flags & QString::SectionIncludeLeadingSep) && first_i >= 0) { const qt_section_chunk §ion = sections.at(first_i); ret.prepend(section.string.left(section.length)); } - if ((flags & QString::SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) { + if ((flags & QString::SectionIncludeTrailingSep) + && last_i < sectionsSize - 1) { const qt_section_chunk §ion = sections.at(last_i+1); ret += section.string.left(section.length); } diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index 3edc9cc965..ea40c64c89 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -4389,6 +4389,28 @@ void tst_QString::section_data() << QString("o") << 1 << 2 << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep) << QString("o1o2o") << false; + QTest::newRow( "range1" ) << QString("o1o2o") + << QString("o") << -5 << -5 + << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep) + << QString() << false; + QTest::newRow( "range2" ) << QString("oo1o2o") + << QString("o") << -5 << 1 + << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep + |QString::SectionSkipEmpty) + << QString("oo1o2o") << false; + QTest::newRow( "range3" ) << QString("o1o2o") + << QString("o") << 2 << 1 + << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep) + << QString() << false; + QTest::newRow( "range4" ) << QString("o1o2o") + << QString("o") << 4 << 4 + << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep) + << QString() << false; + QTest::newRow( "range5" ) << QString("o1oo2o") + << QString("o") << -2 << -1 + << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep + |QString::SectionSkipEmpty) + << QString("o1oo2o") << false; QTest::newRow( "rx1" ) << QString("o1o2o") << QString("[a-z]") << 0 << 0 << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep) |