diff options
author | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-24 05:14:04 +0000 |
---|---|---|
committer | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-24 05:14:04 +0000 |
commit | 07ae2c333da12779c87eefcd65f4272e389766f3 (patch) | |
tree | d13dc876544bd5b65e00f1e4db8573c4551dcf8d /chrome/common/l10n_util.h | |
parent | a53ef0492a5c34542d5b1d607f848fb7b45002ba (diff) | |
download | chromium_src-07ae2c333da12779c87eefcd65f4272e389766f3.zip chromium_src-07ae2c333da12779c87eefcd65f4272e389766f3.tar.gz chromium_src-07ae2c333da12779c87eefcd65f4272e389766f3.tar.bz2 |
Adds a method to sort the elements of a vector by invoking a method on
each that returns a string. The strings are then compared using a
collator, or operator < if no collator is found.
I'm going to use this for sorting bookmarks.
BUG=1750
TEST=none yet
Review URL: http://codereview.chromium.org/20484
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10252 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/l10n_util.h')
-rw-r--r-- | chrome/common/l10n_util.h | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/chrome/common/l10n_util.h b/chrome/common/l10n_util.h index c0a5d0d..51160cd 100644 --- a/chrome/common/l10n_util.h +++ b/chrome/common/l10n_util.h @@ -23,9 +23,11 @@ #include "base/scoped_ptr.h" #include "base/string16.h" #include "base/string_util.h" -#include "third_party/icu38/public/common/unicode/ubidi.h" #include "unicode/coll.h" #include "unicode/locid.h" +#include "unicode/rbbi.h" +#include "unicode/ubidi.h" +#include "unicode/uchar.h" class FilePath; class PrefService; @@ -198,6 +200,72 @@ int DefaultCanvasTextAlignment(); void HWNDSetRTLLayout(HWND hwnd); #endif +// Compares the two strings using the specified collator. +UCollationResult CompareStringWithCollator(const Collator* collator, + const std::wstring& lhs, + const std::wstring& 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> +class StringMethodComparatorWithCollator : + public std::binary_function<const std::wstring&, + const std::wstring&, + bool> { + public: + StringMethodComparatorWithCollator(Collator* collator, Method method) + : collator_(collator), + method_(method) { } + + // Returns true if lhs preceeds rhs. + bool operator() (T* lhs_t, T* rhs_t) { + return CompareStringWithCollator(collator_, (lhs_t->*method_)(), + (rhs_t->*method_)()) == UCOL_LESS; + } + + private: + Collator* collator_; + Method method_; +}; + +// Used by SortStringsUsingMethod. Invokes a method on the objects passed to +// operator (), comparing the string results using <. +template <class T, class Method> +class StringMethodComparator : public std::binary_function<const std::wstring&, + const std::wstring&, + bool> { + public: + explicit StringMethodComparator(Method method) : method_(method) { } + + // Returns true if lhs preceeds rhs. + bool operator() (T* lhs_t, T* rhs_t) { + return (lhs_t->*method_)() < (rhs_t->*method_)(); + } + + private: + Method method_; +}; + +// Sorts the objects in |elements| using the method |method|, which must return +// a string. Sorting is done using a collator, unless a collator can not be +// found in which case the strings are sorted using the operator <. +template <class T, class Method> +void SortStringsUsingMethod(const std::wstring& locale, + std::vector<T*>* elements, + Method method) { + UErrorCode error = U_ZERO_ERROR; + Locale loc(WideToUTF8(locale).c_str()); + scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); + if (U_FAILURE(error)) { + sort(elements->begin(), elements->end(), + StringMethodComparator<T,Method>(method)); + return; + } + + std::sort(elements->begin(), elements->end(), + StringMethodComparatorWithCollator<T,Method>(collator.get(), method)); +} + // Compares two elements' string keys and returns true if the first element's // string key is less than the second element's string key. The Element must // have a method like the follow format to return the string key. |