Chromium Code Reviews| Index: third_party/libaddressinput/chromium/string_compare.cc |
| diff --git a/third_party/libaddressinput/chromium/string_compare.cc b/third_party/libaddressinput/chromium/string_compare.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..de4a4a5ebbd8d593abdf91583184730033a7b11d |
| --- /dev/null |
| +++ b/third_party/libaddressinput/chromium/string_compare.cc |
| @@ -0,0 +1,69 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "third_party/libaddressinput/src/cpp/src/util/string_compare.h" |
| + |
| +#include "base/basictypes.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "third_party/icu/source/i18n/unicode/coll.h" |
| + |
| +namespace i18n { |
| +namespace addressinput { |
| + |
| +class StringCompare::Impl { |
|
Evan Stade
2014/06/30 18:49:38
Why is this class an inner class of StringCompare
please use gerrit instead
2014/07/01 04:53:50
Done.
|
| + public: |
| + Impl() {} |
| + |
| + ~Impl() {} |
| + |
| + bool NaturalEquals(const std::string& a, const std::string& b) const { |
| + return NaturalCompare(a, b) == 0; |
| + } |
| + |
| + bool NaturalLess(const std::string& a, const std::string& b) const { |
| + return NaturalCompare(a, b) < 0; |
| + } |
| + |
| + private: |
| + int NaturalCompare(const std::string& a, const std::string& b) const { |
| + UErrorCode error_code = U_ZERO_ERROR; |
| + if (!collator_) { |
| + collator_.reset( |
| + icu::Collator::createInstance(icu::Locale::getRoot(), error_code)); |
| + DCHECK(U_SUCCESS(error_code)); |
| + collator_->setStrength(icu::Collator::PRIMARY); |
| + } |
| + error_code = U_ZERO_ERROR; |
| + int result = collator_->compareUTF8(a, b, error_code); |
| + DCHECK(U_SUCCESS(error_code)); |
| + return result; |
| + } |
| + |
| + // ::scoped_ptr is from "base/memory/scoped_ptr.h", which does not interfere |
| + // with ::i18n::addressinput::scoped_ptr from |
| + // <libaddressinput/util/scoped_ptr.h>. |
| + mutable ::scoped_ptr<icu::Collator> collator_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Impl); |
| +}; |
| + |
| +StringCompare::StringCompare() : impl_(new Impl) { |
|
Evan Stade
2014/06/30 18:49:38
nit: {} on one line
please use gerrit instead
2014/07/01 04:53:50
Done. (I have to remember to fix this after every
|
| +} |
| + |
| +StringCompare::~StringCompare() { |
|
Evan Stade
2014/06/30 18:49:38
nit: {} on one line
please use gerrit instead
2014/07/01 04:53:50
Done.
|
| +} |
| + |
| +bool StringCompare::NaturalEquals(const std::string& a, |
| + const std::string& b) const { |
| + return impl_->NaturalEquals(a, b); |
| +} |
| + |
| +bool StringCompare::NaturalLess(const std::string& a, |
| + const std::string& b) const { |
| + return impl_->NaturalLess(a, b); |
| +} |
| + |
| +} // namespace addressinput |
| +} // namespace i18n |