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 727be63dac

Change-Id: I7ba5e059c532505a4fe24e77a0b8022737adaa20
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-11-01 08:54:14 +01:00
parent 600d3288a6
commit 82e115512d

View File

@@ -38,29 +38,42 @@ public:
int findInChanges(const QString &name) const int findInChanges(const QString &name) const
{ {
for (int i = 0; i < m_items.size(); ++i) { const Qt::CaseSensitivity cs = m_baseNameValueDictionary.nameCaseSensitivity();
if (m_items.at(i).name.compare(name, m_baseNameValueDictionary.nameCaseSensitivity())
== 0) { const auto compare = [&name, &cs](const EnvironmentItem &item) {
return i; return item.name.compare(name, cs) == 0;
} };
}
return -1; 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<bool(int)> compare)
{ {
const auto it = m_resultNameValueDictionary.find(name); const Qt::CaseSensitivity cs = dictionary.nameCaseSensitivity();
if (it == m_resultNameValueDictionary.end())
return m_resultNameValueDictionary.size(); const auto compareFunc =
return std::distance(m_resultNameValueDictionary.begin(), it); [&key, cs, compare](const std::tuple<QString, QString, bool> &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); const auto compare = [](int compareResult) { return compareResult > 0; };
if (it == m_resultNameValueDictionary.end()) return findIndex(m_resultNameValueDictionary, key, compare);
return -1; }
return std::distance(m_resultNameValueDictionary.begin(), it);
int findInResult(const QString &key) const
{
const auto compare = [](int compareResult) { return compareResult == 0; };
return findIndex(m_resultNameValueDictionary, key, compare);
} }
NameValueDictionary m_baseNameValueDictionary; NameValueDictionary m_baseNameValueDictionary;