From a64ff6d116402494021b12420b91401a87a0adb0 Mon Sep 17 00:00:00 2001 From: Blake Kaplan Date: Tue, 14 Nov 2017 16:34:42 -0800 Subject: [PATCH] Bug 1417279 - Remove an odd second case from nsArray::IndexOf. r=froydnj Now that nsArray uses nsCOMArray under the hood, we don't have to do weird ForwardEnumeration hacks to start IndexOf at a non-zero index. MozReview-Commit-ID: 3ReDV0BT0hn --HG-- extra : rebase_source : 399a988d00fabaa314eb7592b2aacf277a2ca477 --- xpcom/ds/nsArray.cpp | 48 +++----------------------------------------- 1 file changed, 3 insertions(+), 45 deletions(-) diff --git a/xpcom/ds/nsArray.cpp b/xpcom/ds/nsArray.cpp index be654ae31b04..e31a646bb0ea 100644 --- a/xpcom/ds/nsArray.cpp +++ b/xpcom/ds/nsArray.cpp @@ -8,17 +8,6 @@ #include "nsArrayEnumerator.h" #include "nsThreadUtils.h" -// used by IndexOf() -struct MOZ_STACK_CLASS findIndexOfClosure -{ - // This is only used for pointer comparison, so we can just use a void*. - void* targetElement; - uint32_t startIndex; - uint32_t resultIndex; -}; - -static bool FindElementCallback(void* aElement, void* aClosure); - NS_INTERFACE_MAP_BEGIN(nsArray) NS_INTERFACE_MAP_ENTRY(nsIArray) NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions) @@ -80,24 +69,12 @@ NS_IMETHODIMP nsArrayBase::IndexOf(uint32_t aStartIndex, nsISupports* aElement, uint32_t* aResult) { - // optimize for the common case by forwarding to mArray - if (aStartIndex == 0) { - uint32_t idx = mArray.IndexOf(aElement); - if (idx == UINT32_MAX) { - return NS_ERROR_FAILURE; - } - - *aResult = idx; - return NS_OK; - } - - findIndexOfClosure closure = { aElement, aStartIndex, 0 }; - bool notFound = mArray.EnumerateForwards(FindElementCallback, &closure); - if (notFound) { + int32_t idx = mArray.IndexOf(aElement, aStartIndex); + if (idx == -1) { return NS_ERROR_FAILURE; } - *aResult = closure.resultIndex; + *aResult = static_cast(idx); return NS_OK; } @@ -160,25 +137,6 @@ nsArrayBase::GetElementAt(uint32_t aIndex, nsISupports** aResult) return NS_OK; } -// -// static helper routines -// -bool -FindElementCallback(void* aElement, void* aClosure) -{ - findIndexOfClosure* closure = static_cast(aClosure); - nsISupports* element = static_cast(aElement); - - // don't start searching until we're past the startIndex - if (closure->resultIndex >= closure->startIndex && - element == closure->targetElement) { - return false; // stop! We found it - } - closure->resultIndex++; - - return true; -} - nsresult nsArrayBase::XPCOMConstructor(nsISupports* aOuter, const nsIID& aIID, void** aResult)