From c252529e896578a65c5644f5a47247c89915afe9 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 27 Nov 2019 10:16:39 +0000 Subject: [PATCH] Bug 1596129 - Make use of std::find_if in IDBObjectStore::Index. r=dom-workers-and-storage-reviewers,ttung Depends on D52866 Differential Revision: https://phabricator.services.mozilla.com/D52867 --HG-- extra : moz-landing-system : lando --- dom/indexedDB/IDBObjectStore.cpp | 45 ++++++++++++++------------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/dom/indexedDB/IDBObjectStore.cpp b/dom/indexedDB/IDBObjectStore.cpp index 84054d888cdb..fc7e26f771b6 100644 --- a/dom/indexedDB/IDBObjectStore.cpp +++ b/dom/indexedDB/IDBObjectStore.cpp @@ -1802,43 +1802,38 @@ already_AddRefed IDBObjectStore::Index(const nsAString& aName, return nullptr; } - const nsTArray& indexes = mSpec->indexes(); + const nsTArray& indexMetadatas = mSpec->indexes(); - const IndexMetadata* metadata = nullptr; + const auto endIndexMetadatas = indexMetadatas.cend(); + const auto foundMetadata = + std::find_if(indexMetadatas.cbegin(), endIndexMetadatas, + [&aName](const auto& indexMetadata) { + return indexMetadata.name() == aName; + }); - for (uint32_t idxCount = indexes.Length(), idxIndex = 0; idxIndex < idxCount; - idxIndex++) { - const IndexMetadata& index = indexes[idxIndex]; - if (index.name() == aName) { - metadata = &index; - break; - } - } - - if (!metadata) { + if (foundMetadata == endIndexMetadatas) { aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR); return nullptr; } - const int64_t desiredId = metadata->id(); + const IndexMetadata& metadata = *foundMetadata; + + const auto endIndexes = mIndexes.cend(); + const auto foundIndex = + std::find_if(mIndexes.cbegin(), endIndexes, + [desiredId = metadata.id()](const auto& index) { + return index->Id() == desiredId; + }); RefPtr index; - for (uint32_t idxCount = mIndexes.Length(), idxIndex = 0; idxIndex < idxCount; - idxIndex++) { - RefPtr& existingIndex = mIndexes[idxIndex]; - - if (existingIndex->Id() == desiredId) { - index = existingIndex; - break; - } - } - - if (!index) { - index = IDBIndex::Create(this, *metadata); + if (foundIndex == endIndexes) { + index = IDBIndex::Create(this, metadata); MOZ_ASSERT(index); mIndexes.AppendElement(index); + } else { + index = *foundIndex; } return index.forget();