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