OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "third_party/libaddressinput/src/cpp/src/util/string_compare.h" |
| 6 |
| 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" |
| 12 |
| 13 namespace i18n { |
| 14 namespace addressinput { |
| 15 |
| 16 namespace { |
| 17 |
| 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); |
| 26 } |
| 27 |
| 28 ~IcuStringComparer() {} |
| 29 |
| 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; |
| 35 } |
| 36 |
| 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_; |
| 42 |
| 43 DISALLOW_COPY_AND_ASSIGN(IcuStringComparer); |
| 44 }; |
| 45 |
| 46 static base::LazyInstance<IcuStringComparer> g_comparer = |
| 47 LAZY_INSTANCE_INITIALIZER; |
| 48 |
| 49 } // namespace |
| 50 |
| 51 // Dummy required for scoped_ptr<Impl>. |
| 52 class StringCompare::Impl {}; |
| 53 |
| 54 StringCompare::StringCompare() {} |
| 55 |
| 56 StringCompare::~StringCompare() {} |
| 57 |
| 58 bool StringCompare::NaturalEquals(const std::string& a, |
| 59 const std::string& b) const { |
| 60 return g_comparer.Get().Compare(a, b) == 0; |
| 61 } |
| 62 |
| 63 bool StringCompare::NaturalLess(const std::string& a, |
| 64 const std::string& b) const { |
| 65 return g_comparer.Get().Compare(a, b) < 0; |
| 66 } |
| 67 |
| 68 } // namespace addressinput |
| 69 } // namespace i18n |
OLD | NEW |