From 82e115512d10d43ae900ec6bfef43ff544d165cd Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Fri, 1 Nov 2024 08:54:14 +0100 Subject: [PATCH] Utils: Fix insertion in EnvironmentModel Previously when the user tried to add a new environment variable to the list (e.g. in "Build Environment") it would add: NEWVAR=VALUE _=VALUE Instead of the correct "NEWVAR=VALUE" only. This was due to findInResultInsertPosition returning the position of a match instead of the position one past the match. Amends 727be63dacaff6a8327f2c74852a7d907f4ca7e3 Change-Id: I7ba5e059c532505a4fe24e77a0b8022737adaa20 Reviewed-by: Christian Kandeler --- src/libs/utils/environmentmodel.cpp | 47 ++++++++++++++++++----------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/libs/utils/environmentmodel.cpp b/src/libs/utils/environmentmodel.cpp index a2f9c7ab3cd..20efb301ccd 100644 --- a/src/libs/utils/environmentmodel.cpp +++ b/src/libs/utils/environmentmodel.cpp @@ -38,29 +38,42 @@ public: int findInChanges(const QString &name) const { - for (int i = 0; i < m_items.size(); ++i) { - if (m_items.at(i).name.compare(name, m_baseNameValueDictionary.nameCaseSensitivity()) - == 0) { - return i; - } - } - return -1; + const Qt::CaseSensitivity cs = m_baseNameValueDictionary.nameCaseSensitivity(); + + const auto compare = [&name, &cs](const EnvironmentItem &item) { + return item.name.compare(name, cs) == 0; + }; + + return Utils::indexOf(m_items, compare); } - int findInResultInsertPosition(const QString &name) const + // Compares each key in dictionary against `key` and checks the result with `compare`. + // Returns the index of the first key where the result of QString::compare() satisfies the + // `compare` function. + // Returns -1 if no such key is found. + static int findIndex( + const NameValueDictionary &dictionary, const QString &key, std::function compare) { - const auto it = m_resultNameValueDictionary.find(name); - if (it == m_resultNameValueDictionary.end()) - return m_resultNameValueDictionary.size(); - return std::distance(m_resultNameValueDictionary.begin(), it); + const Qt::CaseSensitivity cs = dictionary.nameCaseSensitivity(); + + const auto compareFunc = + [&key, cs, compare](const std::tuple &item) { + return compare(std::get<0>(item).compare(key, cs)); + }; + + return Utils::indexOf(dictionary, compareFunc); } - int findInResult(const QString &name) const + int findInResultInsertPosition(const QString &key) const { - const auto it = m_resultNameValueDictionary.find(name); - if (it == m_resultNameValueDictionary.end()) - return -1; - return std::distance(m_resultNameValueDictionary.begin(), it); + const auto compare = [](int compareResult) { return compareResult > 0; }; + return findIndex(m_resultNameValueDictionary, key, compare); + } + + int findInResult(const QString &key) const + { + const auto compare = [](int compareResult) { return compareResult == 0; }; + return findIndex(m_resultNameValueDictionary, key, compare); } NameValueDictionary m_baseNameValueDictionary;