Index: chrome/common/l10n_util.h |
=================================================================== |
--- chrome/common/l10n_util.h (revision 10076) |
+++ chrome/common/l10n_util.h (working copy) |
@@ -13,11 +13,17 @@ |
#if defined(OS_WIN) |
#include <windows.h> |
#endif |
+#include <algorithm> |
#include <string> |
#include <vector> |
#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "base/scoped_ptr.h" |
+#include "base/string_util.h" |
#include "third_party/icu38/public/common/unicode/ubidi.h" |
+#include "unicode/coll.h" |
+#include "unicode/locid.h" |
class PrefService; |
@@ -172,6 +178,73 @@ |
void HWNDSetRTLLayout(HWND hwnd); |
#endif |
+// 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. |
+// const std::wstring& GetStringKey() const; |
+// This uses the locale specified in the constructor. |
+template <class Element> |
+class StringComparator : public std::binary_function<const Element&, |
+ const Element&, |
+ bool> { |
+ public: |
+ explicit StringComparator(Collator* collator) |
+ : collator_(collator) { } |
+ |
+ // Returns true if lhs precedes rhs. |
+ bool operator()(const Element& lhs, const Element& rhs) { |
+ const std::wstring& lhs_string_key = lhs.GetStringKey(); |
+ const std::wstring& rhs_string_key = rhs.GetStringKey(); |
+ |
+ return StringComparator<std::wstring>(collator_)(lhs_string_key, |
+ rhs_string_key); |
+ } |
+ |
+ private: |
+ Collator* collator_; |
+}; |
+ |
+// Specialization of operator() method for std::wstring version. |
+template <> |
+bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, |
+ const std::wstring& rhs); |
+ |
+// In place sorting of |elements| of a vector according to the string key of |
+// each element in the vector by using collation rules for |locale|. |
+// |begin_index| points to the start position of elements in the vector which |
+// want to be sorted. |end_index| points to the end position of elements in the |
+// vector which want to be sorted |
+template <class Element> |
+void SortVectorWithStringKey(const std::wstring& locale, |
+ std::vector<Element>* elements, |
+ unsigned int begin_index, |
+ unsigned int end_index, |
+ bool needs_stable_sort) { |
+ DCHECK(begin_index >= 0 && begin_index < end_index && |
+ end_index <= static_cast<unsigned int>(elements->size())); |
+ UErrorCode error = U_ZERO_ERROR; |
+ Locale loc(WideToASCII(locale).c_str()); |
+ scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); |
+ if (U_FAILURE(error)) |
+ collator.reset(); |
+ StringComparator<Element> c(collator.get()); |
+ if (needs_stable_sort) { |
+ stable_sort(elements->begin() + begin_index, |
+ elements->begin() + end_index, |
+ c); |
+ } else { |
+ sort(elements->begin() + begin_index, elements->begin() + end_index, c); |
+ } |
+} |
+ |
+template <class Element> |
+void SortVectorWithStringKey(const std::wstring& locale, |
+ std::vector<Element>* elements, |
+ bool needs_stable_sort) { |
+ SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
+ needs_stable_sort); |
+} |
+ |
// In place sorting of strings using collation rules for |locale|. |
void SortStrings(const std::wstring& locale, |
std::vector<std::wstring>* strings); |