refact: add some comments to DesktopEntry::parseGroupHeader

1. remove extra - in that regex;
2. add some comments;
3. correct that typo in method name.
This commit is contained in:
black-desk 2023-08-25 11:35:59 +08:00 committed by Comix
parent 9f6ab28672
commit 17536bca3b
2 changed files with 13 additions and 5 deletions

View File

@ -15,14 +15,22 @@
#include <chrono> #include <chrono>
#include <cstdio> #include <cstdio>
auto DesktopEntry::parserGroupHeader(const QString &str) noexcept auto DesktopEntry::parseGroupHeader(const QString &str) noexcept
{ {
// https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#group-header
auto groupHeader = str.sliced(1, str.size() - 2).trimmed(); auto groupHeader = str.sliced(1, str.size() - 2).trimmed();
decltype(m_entryMap)::iterator it{m_entryMap.end()}; decltype(m_entryMap)::iterator it{m_entryMap.end()};
QRegularExpression re{R"([^\x20-\x5a-\x5e-\x7e\x5c])"}; // NOTE:
// This regex match '[', ']', control characters
// and all non-ascii characters.
// They are invalid in group header.
// https://regex101.com/r/bZhHZo/1
QRegularExpression re{R"([^\x20-\x5a\x5e-\x7e\x5c])"};
auto matcher = re.match(groupHeader); auto matcher = re.match(groupHeader);
if (matcher.hasMatch()) { if (matcher.hasMatch()) {
qWarning() << "group header invalid:" << str;
return it; return it;
} }
@ -31,6 +39,7 @@ auto DesktopEntry::parserGroupHeader(const QString &str) noexcept
it = m_entryMap.insert(groupHeader, {}); it = m_entryMap.insert(groupHeader, {});
} }
qWarning() << "group header already exists:" << str;
return it; return it;
} }
@ -336,10 +345,9 @@ DesktopErrorCode DesktopEntry::parse(QTextStream &stream) noexcept
} }
if (line.startsWith('[')) { if (line.startsWith('[')) {
auto group = parserGroupHeader(line); auto group = parseGroupHeader(line);
if (group == m_entryMap.end()) { if (group == m_entryMap.end()) {
qWarning() << "groupName format error or already exists:" << line;
return DesktopErrorCode::InvalidFormat; return DesktopErrorCode::InvalidFormat;
} }
currentGroup = group; currentGroup = group;

View File

@ -130,7 +130,7 @@ private:
EntryContext m_context{EntryContext::EntryOuter}; EntryContext m_context{EntryContext::EntryOuter};
QMap<QString, QMap<QString, Value>> m_entryMap; QMap<QString, QMap<QString, Value>> m_entryMap;
auto parserGroupHeader(const QString &str) noexcept; auto parseGroupHeader(const QString &str) noexcept;
[[nodiscard]] bool checkMainEntryValidation() const noexcept; [[nodiscard]] bool checkMainEntryValidation() const noexcept;
static bool skipCheck(const QString &line) noexcept; static bool skipCheck(const QString &line) noexcept;
static DesktopErrorCode parseEntry(const QString &str, decltype(m_entryMap)::iterator &currentGroup) noexcept; static DesktopErrorCode parseEntry(const QString &str, decltype(m_entryMap)::iterator &currentGroup) noexcept;