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/. */
4
5 #include "nsUConvPropertySearch.h"
6 #include "nsCRT.h"
7 #include "nsString.h"
8 #include "mozilla/BinarySearch.h"
9
10 namespace {
11
12 struct PropertyComparator
13 {
14 const nsCString& mKey;
PropertyComparator__anondc41b2230111::PropertyComparator15 explicit PropertyComparator(const nsCString& aKey) : mKey(aKey) {}
operator ()__anondc41b2230111::PropertyComparator16 int operator()(const nsUConvProp& aProperty) const {
17 return mKey.Compare(aProperty.mKey);
18 }
19 };
20
21 } // namespace
22
23 // static
24 nsresult
SearchPropertyValue(const nsUConvProp aProperties[],int32_t aNumberOfProperties,const nsACString & aKey,nsACString & aValue)25 nsUConvPropertySearch::SearchPropertyValue(const nsUConvProp aProperties[],
26 int32_t aNumberOfProperties,
27 const nsACString& aKey,
28 nsACString& aValue)
29 {
30 using mozilla::BinarySearchIf;
31
32 const nsCString& flat = PromiseFlatCString(aKey);
33 size_t index;
34 if (BinarySearchIf(aProperties, 0, aNumberOfProperties,
35 PropertyComparator(flat), &index)) {
36 nsDependentCString val(aProperties[index].mValue,
37 aProperties[index].mValueLength);
38 aValue.Assign(val);
39 return NS_OK;
40 }
41
42 aValue.Truncate();
43 return NS_ERROR_FAILURE;
44 }
45