intl/locale/nsUConvPropertySearch.cpp
author Tom Prince <mozilla@hocat.ca>
Tue, 23 Jan 2018 14:48:27 -0700
changeset 471568 40e0bf2538b950377ecd07bfbca40208170be79a
parent 331773 835b1d9e49182f5a91e9dc931c19b836e14085ba
child 508163 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1456379: Remove references to `date` branch; r=aki Differential Revision: https://phabricator.services.mozilla.com/D1028

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsUConvPropertySearch.h"
#include "nsCRT.h"
#include "nsString.h"
#include "mozilla/BinarySearch.h"

namespace {

struct PropertyComparator
{
  const nsCString& mKey;
  explicit PropertyComparator(const nsCString& aKey) : mKey(aKey) {}
  int operator()(const nsUConvProp& aProperty) const {
    return mKey.Compare(aProperty.mKey);
  }
};

} // namespace

// static
nsresult
nsUConvPropertySearch::SearchPropertyValue(const nsUConvProp aProperties[],
                                           int32_t aNumberOfProperties,
                                           const nsACString& aKey,
                                           nsACString& aValue)
{
  using mozilla::BinarySearchIf;

  const nsCString& flat = PromiseFlatCString(aKey);
  size_t index;
  if (BinarySearchIf(aProperties, 0, aNumberOfProperties,
                     PropertyComparator(flat), &index)) {
    nsDependentCString val(aProperties[index].mValue,
                           aProperties[index].mValueLength);
    aValue.Assign(val);
    return NS_OK;
  }

  aValue.Truncate();
  return NS_ERROR_FAILURE;
}