diff options
author | hajimehoshi@chromium.org <hajimehoshi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-23 05:06:05 +0000 |
---|---|---|
committer | hajimehoshi@chromium.org <hajimehoshi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-23 05:06:05 +0000 |
commit | 9a08fe84ae32137085e9c2842f344d66bfefc9cb (patch) | |
tree | ead7ce7007acc08a7403f8cd7446f8f33ceef3b2 | |
parent | 8e4ee27694a54b6c7a7c6d2935d292d1df3f0e40 (diff) | |
download | chromium_src-9a08fe84ae32137085e9c2842f344d66bfefc9cb.zip chromium_src-9a08fe84ae32137085e9c2842f344d66bfefc9cb.tar.gz chromium_src-9a08fe84ae32137085e9c2842f344d66bfefc9cb.tar.bz2 |
Refactoring: Moved the function CompareString16WithCollator from ui/base/l10n to base/i18n. The function LocalAwareCompareFilenames at base/i18n/file_util_icu.cc needed a function to sort string with consideration of a locale, but it couldn't refer ui/base.
BUG=55883 (Indirectly)
TEST=Unit tests
Review URL: https://chromiumcodereview.appspot.com/14044002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195711 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/i18n/file_util_icu.cc | 71 | ||||
-rw-r--r-- | base/i18n/string_compare.cc | 29 | ||||
-rw-r--r-- | base/i18n/string_compare.h | 28 | ||||
-rw-r--r-- | chrome/browser/bookmarks/bookmark_model.cc | 5 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/cert_library.cc | 3 | ||||
-rw-r--r-- | chrome/browser/ui/autofill/country_combobox_model.cc | 1 | ||||
-rw-r--r-- | chrome/browser/ui/webui/options/certificate_manager_handler.cc | 5 | ||||
-rw-r--r-- | ui/base/l10n/l10n_util.cc | 19 | ||||
-rw-r--r-- | ui/base/l10n/l10n_util_collator.h | 12 | ||||
-rw-r--r-- | ui/base/models/table_model.cc | 4 |
11 files changed, 89 insertions, 90 deletions
diff --git a/base/base.gyp b/base/base.gyp index b026dcb..a0da4a8 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -322,6 +322,8 @@ 'i18n/number_formatting.h', 'i18n/rtl.cc', 'i18n/rtl.h', + 'i18n/string_compare.cc', + 'i18n/string_compare.h', 'i18n/string_search.cc', 'i18n/string_search.h', 'i18n/time_formatting.cc', diff --git a/base/i18n/file_util_icu.cc b/base/i18n/file_util_icu.cc index f023a4f..a29eb4b 100644 --- a/base/i18n/file_util_icu.cc +++ b/base/i18n/file_util_icu.cc @@ -8,6 +8,7 @@ #include "base/files/file_path.h" #include "base/i18n/icu_string_conversions.h" +#include "base/i18n/string_compare.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/memory/singleton.h" @@ -79,56 +80,6 @@ IllegalCharacters::IllegalCharacters() { set->freeze(); } -class LocaleAwareComparator { - public: - static LocaleAwareComparator* GetInstance() { - return Singleton<LocaleAwareComparator, - LeakySingletonTraits<LocaleAwareComparator> >::get(); - } - - // Note: A similar function is available in l10n_util. - // We cannot use it because base should not depend on l10n_util. - // TODO(yuzo): Move some of l10n_util to base. - int Compare(const string16& a, const string16& b) { - // We are not sure if Collator::compare is thread-safe. - // Use an AutoLock just in case. - base::AutoLock auto_lock(lock_); - - UErrorCode error_code = U_ZERO_ERROR; - UCollationResult result = collator_->compare( - static_cast<const UChar*>(a.c_str()), - static_cast<int>(a.length()), - static_cast<const UChar*>(b.c_str()), - static_cast<int>(b.length()), - error_code); - DCHECK(U_SUCCESS(error_code)); - return result; - } - - private: - friend struct DefaultSingletonTraits<LocaleAwareComparator>; - - LocaleAwareComparator() { - UErrorCode error_code = U_ZERO_ERROR; - // Use the default collator. The default locale should have been properly - // set by the time this constructor is called. - collator_.reset(icu::Collator::createInstance(error_code)); - DCHECK(U_SUCCESS(error_code)); - // Make it case-sensitive. - collator_->setStrength(icu::Collator::TERTIARY); - // Note: We do not set UCOL_NORMALIZATION_MODE attribute. In other words, we - // do not pay performance penalty to guarantee sort order correctness for - // non-FCD (http://unicode.org/notes/tn5/#FCD) file names. This should be a - // reasonable tradeoff because such file names should be rare and the sort - // order doesn't change much anyway. - } - - scoped_ptr<icu::Collator> collator_; - base::Lock lock_; - - DISALLOW_COPY_AND_ASSIGN(LocaleAwareComparator); -}; - } // namespace namespace file_util { @@ -182,20 +133,24 @@ void ReplaceIllegalCharactersInPath(base::FilePath::StringType* file_name, bool LocaleAwareCompareFilenames(const base::FilePath& a, const base::FilePath& b) { + UErrorCode error_code = U_ZERO_ERROR; + // Use the default collator. The default locale should have been properly + // set by the time this constructor is called. + scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code)); + DCHECK(U_SUCCESS(error_code)); + // Make it case-sensitive. + collator->setStrength(icu::Collator::TERTIARY); + #if defined(OS_WIN) - return LocaleAwareComparator::GetInstance()->Compare(a.value().c_str(), - b.value().c_str()) < 0; + return base::i18n::CompareString16WithCollator(collator.get(), + WideToUTF16(a.value()), WideToUTF16(b.value())) == UCOL_LESS; #elif defined(OS_POSIX) // On linux, the file system encoding is not defined. We assume // SysNativeMBToWide takes care of it. - // - // ICU's collator can take strings in OS native encoding. But we convert the - // strings to UTF-16 ourselves to ensure conversion consistency. - // TODO(yuzo): Perhaps we should define SysNativeMBToUTF16? - return LocaleAwareComparator::GetInstance()->Compare( + return base::i18n::CompareString16WithCollator(collator.get(), WideToUTF16(base::SysNativeMBToWide(a.value().c_str())), - WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))) < 0; + WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))) == UCOL_LESS; #else #error Not implemented on your system #endif diff --git a/base/i18n/string_compare.cc b/base/i18n/string_compare.cc new file mode 100644 index 0000000..41d19d8 --- /dev/null +++ b/base/i18n/string_compare.cc @@ -0,0 +1,29 @@ +// Copyright (c) 2013 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 "base/i18n/string_compare.h" + +#include "base/logging.h" +#include "base/utf_string_conversions.h" + +namespace base { +namespace i18n { + +// Compares the character data stored in two different string16 strings by +// specified Collator instance. +UCollationResult CompareString16WithCollator(const icu::Collator* collator, + const string16& lhs, + const string16& rhs) { + DCHECK(collator); + UErrorCode error = U_ZERO_ERROR; + UCollationResult result = collator->compare( + static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), + static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), + error); + DCHECK(U_SUCCESS(error)); + return result; +} + +} // namespace i18n +} // namespace base diff --git a/base/i18n/string_compare.h b/base/i18n/string_compare.h new file mode 100644 index 0000000..3c3d010 --- /dev/null +++ b/base/i18n/string_compare.h @@ -0,0 +1,28 @@ +// Copyright (c) 2013 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. + +#ifndef BASE_I18N_STRING_COMPARE_H_ +#define BASE_I18N_STRING_COMPARE_H_ + +#include <algorithm> +#include <string> +#include <vector> + +#include "base/i18n/base_i18n_export.h" +#include "base/string16.h" +#include "third_party/icu/public/i18n/unicode/coll.h" + +namespace base { +namespace i18n { + +// Compares the two strings using the specified collator. +BASE_I18N_EXPORT UCollationResult CompareString16WithCollator( + const icu::Collator* collator, + const string16& lhs, + const string16& rhs); + +} // namespace i18n +} // namespace base + +#endif // BASE_I18N_STRING_COMPARATOR_H_ diff --git a/chrome/browser/bookmarks/bookmark_model.cc b/chrome/browser/bookmarks/bookmark_model.cc index d23f645..1985c64 100644 --- a/chrome/browser/bookmarks/bookmark_model.cc +++ b/chrome/browser/bookmarks/bookmark_model.cc @@ -9,9 +9,11 @@ #include "base/bind.h" #include "base/bind_helpers.h" +#include "base/i18n/string_compare.h" #include "base/json/json_string_value_serializer.h" #include "base/sequenced_task_runner.h" #include "base/string_util.h" +#include "base/utf_string_conversions.h" #include "base/values.h" #include "build/build_config.h" #include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h" @@ -30,7 +32,6 @@ #include "content/public/browser/notification_source.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/l10n/l10n_util_collator.h" #include "ui/gfx/favicon_size.h" #include "ui/gfx/image/image_util.h" @@ -166,7 +167,7 @@ class SortComparator : public std::binary_function<const BookmarkNode*, // Types are the same, compare the names. if (!collator_) return n1->GetTitle() < n2->GetTitle(); - return l10n_util::CompareString16WithCollator( + return base::i18n::CompareString16WithCollator( collator_, n1->GetTitle(), n2->GetTitle()) == UCOL_LESS; } // Types differ, sort such that folders come first. diff --git a/chrome/browser/chromeos/cros/cert_library.cc b/chrome/browser/chromeos/cros/cert_library.cc index e2309f0..ab5b491 100644 --- a/chrome/browser/chromeos/cros/cert_library.cc +++ b/chrome/browser/chromeos/cros/cert_library.cc @@ -8,6 +8,7 @@ #include "base/chromeos/chromeos_version.h" #include "base/command_line.h" +#include "base/i18n/string_compare.h" #include "base/memory/weak_ptr.h" #include "base/observer_list_threadsafe.h" #include "base/string_util.h" @@ -343,7 +344,7 @@ class CertLibraryImpl string16 rhs_name = GetDisplayString(rhs.get(), false); if (collator_ == NULL) return lhs_name < rhs_name; - return l10n_util::CompareString16WithCollator( + return base::i18n::CompareString16WithCollator( collator_, lhs_name, rhs_name) == UCOL_LESS; } private: diff --git a/chrome/browser/ui/autofill/country_combobox_model.cc b/chrome/browser/ui/autofill/country_combobox_model.cc index fafa49c..4829efa 100644 --- a/chrome/browser/ui/autofill/country_combobox_model.cc +++ b/chrome/browser/ui/autofill/country_combobox_model.cc @@ -4,6 +4,7 @@ #include "chrome/browser/ui/autofill/country_combobox_model.h" +#include "base/utf_string_conversions.h" #include "chrome/browser/browser_process.h" #include "components/autofill/browser/autofill_country.h" #include "ui/base/l10n/l10n_util_collator.h" diff --git a/chrome/browser/ui/webui/options/certificate_manager_handler.cc b/chrome/browser/ui/webui/options/certificate_manager_handler.cc index f30eaec..1923b04 100644 --- a/chrome/browser/ui/webui/options/certificate_manager_handler.cc +++ b/chrome/browser/ui/webui/options/certificate_manager_handler.cc @@ -10,10 +10,12 @@ #include "base/bind.h" #include "base/bind_helpers.h" #include "base/file_util.h" // for FileAccessProvider +#include "base/i18n/string_compare.h" #include "base/id_map.h" #include "base/memory/scoped_vector.h" #include "base/safe_strerror_posix.h" #include "base/strings/string_number_conversions.h" +#include "base/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/certificate_viewer.h" @@ -28,7 +30,6 @@ #include "net/base/net_errors.h" #include "net/cert/x509_certificate.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/l10n/l10n_util_collator.h" #if defined(OS_CHROMEOS) #include "chromeos/dbus/cryptohome_client.h" @@ -87,7 +88,7 @@ struct DictionaryIdComparator { b_dict->GetString(kNameId, &b_str); if (collator_ == NULL) return a_str < b_str; - return l10n_util::CompareString16WithCollator( + return base::i18n::CompareString16WithCollator( collator_, a_str, b_str) == UCOL_LESS; } diff --git a/ui/base/l10n/l10n_util.cc b/ui/base/l10n/l10n_util.cc index 0b7b9b7..03abc81 100644 --- a/ui/base/l10n/l10n_util.cc +++ b/ui/base/l10n/l10n_util.cc @@ -14,6 +14,7 @@ #include "base/file_util.h" #include "base/i18n/file_util_icu.h" #include "base/i18n/rtl.h" +#include "base/i18n/string_compare.h" #include "base/memory/scoped_ptr.h" #include "base/path_service.h" #include "base/string_number_conversions.h" @@ -777,21 +778,6 @@ string16 GetStringFUTF16Int(int message_id, int64 a) { return GetStringFUTF16(message_id, UTF8ToUTF16(base::Int64ToString(a))); } -// Compares the character data stored in two different string16 strings by -// specified Collator instance. -UCollationResult CompareString16WithCollator(const icu::Collator* collator, - const string16& lhs, - const string16& rhs) { - DCHECK(collator); - UErrorCode error = U_ZERO_ERROR; - UCollationResult result = collator->compare( - static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), - static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), - error); - DCHECK(U_SUCCESS(error)); - return result; -} - // Specialization of operator() method for string16 version. template <> bool StringComparator<string16>::operator()(const string16& lhs, @@ -800,7 +786,8 @@ bool StringComparator<string16>::operator()(const string16& lhs, // string compare. if (!collator_) return lhs < rhs; - return CompareString16WithCollator(collator_, lhs, rhs) == UCOL_LESS; + return base::i18n::CompareString16WithCollator(collator_, lhs, rhs) == + UCOL_LESS; }; void SortStrings16(const std::string& locale, diff --git a/ui/base/l10n/l10n_util_collator.h b/ui/base/l10n/l10n_util_collator.h index fb43692..44e4a77 100644 --- a/ui/base/l10n/l10n_util_collator.h +++ b/ui/base/l10n/l10n_util_collator.h @@ -10,19 +10,13 @@ #include <string> #include <vector> +#include "base/i18n/string_compare.h" #include "base/memory/scoped_ptr.h" -#include "base/utf_string_conversions.h" #include "third_party/icu/public/i18n/unicode/coll.h" #include "ui/base/ui_export.h" namespace l10n_util { -// Compares the two strings using the specified collator. -UI_EXPORT UCollationResult CompareString16WithCollator( - const icu::Collator* collator, - const string16& lhs, - const string16& rhs); - // Used by SortStringsUsingMethod. Invokes a method on the objects passed to // operator (), comparing the string results using a collator. template <class T, class Method> @@ -37,8 +31,8 @@ class StringMethodComparatorWithCollator // Returns true if lhs preceeds rhs. bool operator() (T* lhs_t, T* rhs_t) { - return CompareString16WithCollator(collator_, (lhs_t->*method_)(), - (rhs_t->*method_)()) == UCOL_LESS; + return base::i18n::CompareString16WithCollator(collator_, + (lhs_t->*method_)(), (rhs_t->*method_)()) == UCOL_LESS; } private: diff --git a/ui/base/models/table_model.cc b/ui/base/models/table_model.cc index f59e7dd..47520f0 100644 --- a/ui/base/models/table_model.cc +++ b/ui/base/models/table_model.cc @@ -4,9 +4,9 @@ #include "ui/base/models/table_model.h" +#include "base/i18n/string_compare.h" #include "base/logging.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/l10n/l10n_util_collator.h" #include "ui/gfx/image/image_skia.h" namespace ui { @@ -76,7 +76,7 @@ int TableModel::CompareValues(int row1, int row2, int column_id) { icu::Collator* collator = GetCollator(); if (collator) - return l10n_util::CompareString16WithCollator(collator, value1, value2); + return base::i18n::CompareString16WithCollator(collator, value1, value2); NOTREACHED(); return 0; |