1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsUConvPropertySearch.h"
8 #include "mozilla/BinarySearch.h"
12 struct PropertyComparator
{
13 const nsCString
& mKey
;
14 explicit PropertyComparator(const nsCString
& aKey
) : mKey(aKey
) {}
15 int operator()(const nsUConvProp
& aProperty
) const {
16 return Compare(mKey
, nsDependentCString(aProperty
.mKey
));
23 nsresult
nsUConvPropertySearch::SearchPropertyValue(
24 const nsUConvProp aProperties
[], int32_t aNumberOfProperties
,
25 const nsACString
& aKey
, nsACString
& aValue
) {
26 using mozilla::BinarySearchIf
;
28 const nsCString
& flat
= PromiseFlatCString(aKey
);
30 if (BinarySearchIf(aProperties
, 0, aNumberOfProperties
,
31 PropertyComparator(flat
), &index
)) {
32 nsDependentCString
val(aProperties
[index
].mValue
,
33 aProperties
[index
].mValueLength
);
39 return NS_ERROR_FAILURE
;