nsUConvPropertySearch.cpp (1203B)
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; 14 explicit PropertyComparator(const nsCString& aKey) : mKey(aKey) {} 15 int operator()(const nsUConvProp& aProperty) const { 16 return Compare(mKey, nsDependentCString(aProperty.mKey)); 17 } 18 }; 19 20 } // namespace 21 22 // static 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 }