Add ICU message format support
[chromium-blink-merge.git] / third_party / libaddressinput / chromium / string_compare.cc
blob9d247eb8039dad0360328428d5d2fcd93b688967
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "third_party/libaddressinput/src/cpp/src/util/string_compare.h"
7 #include "base/basictypes.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "third_party/icu/source/i18n/unicode/coll.h"
13 namespace i18n {
14 namespace addressinput {
16 namespace {
18 class IcuStringComparer {
19 public:
20 IcuStringComparer() {
21 UErrorCode error_code = U_ZERO_ERROR;
22 collator_.reset(
23 icu::Collator::createInstance(icu::Locale::getRoot(), error_code));
24 DCHECK(U_SUCCESS(error_code));
25 collator_->setStrength(icu::Collator::PRIMARY);
28 ~IcuStringComparer() {}
30 int Compare(const std::string& a, const std::string& b) const {
31 UErrorCode error_code = U_ZERO_ERROR;
32 int result = collator_->compareUTF8(a, b, error_code);
33 DCHECK(U_SUCCESS(error_code));
34 return result;
37 private:
38 // ::scoped_ptr is from "base/memory/scoped_ptr.h", which does not interfere
39 // with ::i18n::addressinput::scoped_ptr from
40 // <libaddressinput/util/scoped_ptr.h>.
41 ::scoped_ptr<icu::Collator> collator_;
43 DISALLOW_COPY_AND_ASSIGN(IcuStringComparer);
46 static base::LazyInstance<IcuStringComparer> g_comparer =
47 LAZY_INSTANCE_INITIALIZER;
49 } // namespace
51 // Dummy required for scoped_ptr<Impl>.
52 class StringCompare::Impl {};
54 StringCompare::StringCompare() {}
56 StringCompare::~StringCompare() {}
58 bool StringCompare::NaturalEquals(const std::string& a,
59 const std::string& b) const {
60 return g_comparer.Get().Compare(a, b) == 0;
63 bool StringCompare::NaturalLess(const std::string& a,
64 const std::string& b) const {
65 return g_comparer.Get().Compare(a, b) < 0;
68 } // namespace addressinput
69 } // namespace i18n