nsUConvPropertySearch.cpp 1.3 KB

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