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 const nsCString& mKey;
PropertyComparator__anon673288bc0111::PropertyComparator14 explicit PropertyComparator(const nsCString& aKey) : mKey(aKey) {}
operator ()__anon673288bc0111::PropertyComparator15 int operator()(const nsUConvProp& aProperty) const {
16 return mKey.Compare(aProperty.mKey);
17 }
18 };
19
20 } // namespace
21
22 // static
SearchPropertyValue(const nsUConvProp aProperties[],int32_t aNumberOfProperties,const nsACString & aKey,nsACString & aValue)23 nsresult nsUConvPropertySearch::SearchPropertyValue(
24 const nsUConvProp aProperties[], int32_t aNumberOfProperties,
25 const nsACString& aKey, nsACString& aValue) {
26 using mozilla::BinarySearchIf;
27
28 const nsCString& flat = PromiseFlatCString(aKey);
29 size_t index;
30 if (BinarySearchIf(aProperties, 0, aNumberOfProperties,
31 PropertyComparator(flat), &index)) {
32 nsDependentCString val(aProperties[index].mValue,
33 aProperties[index].mValueLength);
34 aValue.Assign(val);
35 return NS_OK;
36 }
37
38 aValue.Truncate();
39 return NS_ERROR_FAILURE;
40 }
41