From ad7a738e6d9e878c5451939dbb2ba77f92e117b9 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 22 Mar 2022 15:02:20 +0100 Subject: [PATCH] Simplify parsing of a line to remove [...] enclosure Use a view of the line, within the larger data buffer, to avoid some complications. Change-Id: Iaf79068487ac6b92fe9e415fb63f6b0e9ad0c5a2 Reviewed-by: Marc Mutz Reviewed-by: Sona Kurazyan --- src/corelib/io/qsettings.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 8c7939ccc4..44faf0619c 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -1634,21 +1634,19 @@ bool QConfFileSettingsPrivate::readIniFile(QByteArrayView data, dataPos = 3; while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) { - char ch = data.at(lineStart); - if (ch == '[') { + QByteArrayView line = data.sliced(lineStart, lineLen); + if (line.startsWith('[')) { FLUSH_CURRENT_SECTION(); - // this is a section - QByteArrayView iniSection; - qsizetype idx = data.indexOf(']', lineStart); - if (idx == -1 || idx >= lineStart + lineLen) { + // This starts a new section. + qsizetype idx = line.indexOf(']'); + Q_ASSERT(idx == -1 || idx > 0); // line[0] is '[', not ']'. + Q_ASSERT(idx < lineLen); // (including -1 < lineLen, if no ']' present.) + if (idx < 0) { ok = false; - iniSection = data.sliced(lineStart + 1, lineLen - 1); - } else { - iniSection = data.sliced(lineStart + 1, idx - lineStart - 1); + idx = lineLen; // so line.first(idx) is just line } - - iniSection = iniSection.trimmed(); + QByteArrayView iniSection = line.first(idx).sliced(1).trimmed(); if (iniSection.compare("general", Qt::CaseInsensitive) == 0) { currentSection.clear();