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/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 11 |
| 12 namespace i18n { |
| 13 namespace addressinput { |
| 14 |
| 15 class StringCompare::Impl { |
| 16 public: |
| 17 Impl() {} |
| 18 |
| 19 ~Impl() {} |
| 20 |
| 21 bool NaturalEquals(const std::string& a, const std::string& b) const { |
| 22 return NaturalCompare(a, b) == 0; |
| 23 } |
| 24 |
| 25 bool NaturalLess(const std::string& a, const std::string& b) const { |
| 26 return NaturalCompare(a, b) < 0; |
| 27 } |
| 28 |
| 29 private: |
| 30 int NaturalCompare(const std::string& a, const std::string& b) const { |
| 31 UErrorCode error_code = U_ZERO_ERROR; |
| 32 if (!collator_) { |
| 33 collator_.reset( |
| 34 icu::Collator::createInstance(icu::Locale::getRoot(), error_code)); |
| 35 DCHECK(U_SUCCESS(error_code)); |
| 36 collator_->setStrength(icu::Collator::PRIMARY); |
| 37 } |
| 38 error_code = U_ZERO_ERROR; |
| 39 int result = collator_->compareUTF8(a, b, error_code); |
| 40 DCHECK(U_SUCCESS(error_code)); |
| 41 return result; |
| 42 } |
| 43 |
| 44 // ::scoped_ptr is from "base/memory/scoped_ptr.h", which does not interfere |
| 45 // with ::i18n::addressinput::scoped_ptr from |
| 46 // <libaddressinput/util/scoped_ptr.h>. |
| 47 mutable ::scoped_ptr<icu::Collator> collator_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(Impl); |
| 50 }; |
| 51 |
| 52 StringCompare::StringCompare() : impl_(new Impl) { |
| 53 } |
| 54 |
| 55 StringCompare::~StringCompare() { |
| 56 } |
| 57 |
| 58 bool StringCompare::NaturalEquals(const std::string& a, |
| 59 const std::string& b) const { |
| 60 return impl_->NaturalEquals(a, b); |
| 61 } |
| 62 |
| 63 bool StringCompare::NaturalLess(const std::string& a, |
| 64 const std::string& b) const { |
| 65 return impl_->NaturalLess(a, b); |
| 66 } |
| 67 |
| 68 } // namespace addressinput |
| 69 } // namespace i18n |
OLD | NEW |