WebKit Update 61491:61498.
[chromium-blink-merge.git] / app / l10n_util_collator.h
blob6964f43bccedcd0c29c8318e82ccf8aa7d18f9b8
1 // Copyright (c) 2009 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.
5 #ifndef APP_L10N_UTIL_COLLATOR_H_
6 #define APP_L10N_UTIL_COLLATOR_H_
8 #include <algorithm>
9 #include <functional>
10 #include <string>
11 #include <vector>
13 #include "base/utf_string_conversions.h"
14 #include "unicode/coll.h"
16 namespace l10n_util {
18 // Compares the two strings using the specified collator.
19 UCollationResult CompareStringWithCollator(const icu::Collator* collator,
20 const std::wstring& lhs,
21 const std::wstring& rhs);
23 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
24 // operator (), comparing the string results using a collator.
25 template <class T, class Method>
26 class StringMethodComparatorWithCollator
27 : public std::binary_function<const std::wstring&,
28 const std::wstring&,
29 bool> {
30 public:
31 StringMethodComparatorWithCollator(icu::Collator* collator, Method method)
32 : collator_(collator),
33 method_(method) { }
35 // Returns true if lhs preceeds rhs.
36 bool operator() (T* lhs_t, T* rhs_t) {
37 return CompareStringWithCollator(collator_, (lhs_t->*method_)(),
38 (rhs_t->*method_)()) == UCOL_LESS;
41 private:
42 icu::Collator* collator_;
43 Method method_;
46 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
47 // operator (), comparing the string results using <.
48 template <class T, class Method>
49 class StringMethodComparator : public std::binary_function<const std::wstring&,
50 const std::wstring&,
51 bool> {
52 public:
53 explicit StringMethodComparator(Method method) : method_(method) { }
55 // Returns true if lhs preceeds rhs.
56 bool operator() (T* lhs_t, T* rhs_t) {
57 return (lhs_t->*method_)() < (rhs_t->*method_)();
60 private:
61 Method method_;
64 // Sorts the objects in |elements| using the method |method|, which must return
65 // a string. Sorting is done using a collator, unless a collator can not be
66 // found in which case the strings are sorted using the operator <.
67 template <class T, class Method>
68 void SortStringsUsingMethod(const std::wstring& locale,
69 std::vector<T*>* elements,
70 Method method) {
71 UErrorCode error = U_ZERO_ERROR;
72 icu::Locale loc(WideToUTF8(locale).c_str());
73 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
74 if (U_FAILURE(error)) {
75 sort(elements->begin(), elements->end(),
76 StringMethodComparator<T, Method>(method));
77 return;
80 std::sort(elements->begin(), elements->end(),
81 StringMethodComparatorWithCollator<T, Method>(collator.get(), method));
84 // Compares two elements' string keys and returns true if the first element's
85 // string key is less than the second element's string key. The Element must
86 // have a method like the follow format to return the string key.
87 // const std::wstring& GetStringKey() const;
88 // This uses the locale specified in the constructor.
89 template <class Element>
90 class StringComparator : public std::binary_function<const Element&,
91 const Element&,
92 bool> {
93 public:
94 explicit StringComparator(icu::Collator* collator)
95 : collator_(collator) { }
97 // Returns true if lhs precedes rhs.
98 bool operator()(const Element& lhs, const Element& rhs) {
99 const std::wstring& lhs_string_key = lhs.GetStringKey();
100 const std::wstring& rhs_string_key = rhs.GetStringKey();
102 return StringComparator<std::wstring>(collator_)(lhs_string_key,
103 rhs_string_key);
106 private:
107 icu::Collator* collator_;
110 // Specialization of operator() method for std::wstring version.
111 template <>
112 bool StringComparator<std::wstring>::operator()(const std::wstring& lhs,
113 const std::wstring& rhs);
115 #if !defined(WCHAR_T_IS_UTF16)
116 // Specialization of operator() method for string16 version.
117 template <>
118 bool StringComparator<string16>::operator()(const string16& lhs,
119 const string16& rhs);
120 #endif // !defined(WCHAR_T_IS_UTF16)
122 // In place sorting of |elements| of a vector according to the string key of
123 // each element in the vector by using collation rules for |locale|.
124 // |begin_index| points to the start position of elements in the vector which
125 // want to be sorted. |end_index| points to the end position of elements in the
126 // vector which want to be sorted
127 template <class Element>
128 void SortVectorWithStringKey(const std::string& locale,
129 std::vector<Element>* elements,
130 unsigned int begin_index,
131 unsigned int end_index,
132 bool needs_stable_sort) {
133 DCHECK(begin_index < end_index &&
134 end_index <= static_cast<unsigned int>(elements->size()));
135 UErrorCode error = U_ZERO_ERROR;
136 icu::Locale loc(locale.c_str());
137 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
138 if (U_FAILURE(error))
139 collator.reset();
140 StringComparator<Element> c(collator.get());
141 if (needs_stable_sort) {
142 stable_sort(elements->begin() + begin_index,
143 elements->begin() + end_index,
145 } else {
146 sort(elements->begin() + begin_index, elements->begin() + end_index, c);
150 template <class Element>
151 void SortVectorWithStringKey(const std::string& locale,
152 std::vector<Element>* elements,
153 bool needs_stable_sort) {
154 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(),
155 needs_stable_sort);
158 } // namespace l10n_util
160 #endif // APP_L10N_UTIL_COLLATOR_H_