Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / base / l10n / l10n_util_collator.h
bloba87784cf9d80908f2c3bcdb98a55089c9127ee3e
1 // Copyright (c) 2011 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 UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
6 #define UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
8 #include <algorithm>
9 #include <functional>
10 #include <string>
11 #include <vector>
13 #include "base/i18n/string_compare.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "third_party/icu/source/i18n/unicode/coll.h"
16 #include "ui/base/ui_base_export.h"
18 namespace l10n_util {
20 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
21 // operator (), comparing the string results using a collator.
22 template <class T, class Method>
23 class StringMethodComparatorWithCollator
24 : public std::binary_function<const base::string16&,
25 const base::string16&,
26 bool> {
27 public:
28 StringMethodComparatorWithCollator(icu::Collator* collator, Method method)
29 : collator_(collator),
30 method_(method) { }
32 // Returns true if lhs preceeds rhs.
33 bool operator() (T* lhs_t, T* rhs_t) {
34 return base::i18n::CompareString16WithCollator(
35 *collator_, (lhs_t->*method_)(), (rhs_t->*method_)()) ==
36 UCOL_LESS;
39 private:
40 icu::Collator* collator_;
41 Method method_;
44 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
45 // operator (), comparing the string results using <.
46 template <class T, class Method>
47 class StringMethodComparator
48 : public std::binary_function<const base::string16&,
49 const base::string16&,
50 bool> {
51 public:
52 explicit StringMethodComparator(Method method) : method_(method) { }
54 // Returns true if lhs preceeds rhs.
55 bool operator() (T* lhs_t, T* rhs_t) {
56 return (lhs_t->*method_)() < (rhs_t->*method_)();
59 private:
60 Method method_;
63 // Sorts the objects in |elements| using the method |method|, which must return
64 // a string. Sorting is done using a collator, unless a collator can not be
65 // found in which case the strings are sorted using the operator <.
66 template <class T, class Method>
67 void SortStringsUsingMethod(const std::string& locale,
68 std::vector<T*>* elements,
69 Method method) {
70 UErrorCode error = U_ZERO_ERROR;
71 icu::Locale loc(locale.c_str());
72 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
73 if (U_FAILURE(error)) {
74 sort(elements->begin(), elements->end(),
75 StringMethodComparator<T, Method>(method));
76 return;
79 std::sort(elements->begin(), elements->end(),
80 StringMethodComparatorWithCollator<T, Method>(collator.get(), method));
83 // Compares two elements' string keys and returns true if the first element's
84 // string key is less than the second element's string key. The Element must
85 // have a method like the follow format to return the string key.
86 // const base::string16& GetStringKey() const;
87 // This uses the locale specified in the constructor.
88 template <class Element>
89 class StringComparator : public std::binary_function<const Element&,
90 const Element&,
91 bool> {
92 public:
93 explicit StringComparator(icu::Collator* collator)
94 : collator_(collator) { }
96 // Returns true if lhs precedes rhs.
97 bool operator()(const Element& lhs, const Element& rhs) {
98 const base::string16& lhs_string_key = lhs.GetStringKey();
99 const base::string16& rhs_string_key = rhs.GetStringKey();
101 return StringComparator<base::string16>(collator_)(lhs_string_key,
102 rhs_string_key);
105 private:
106 icu::Collator* collator_;
109 // Specialization of operator() method for base::string16 version.
110 template <>
111 UI_BASE_EXPORT inline bool StringComparator<base::string16>::operator()(
112 const base::string16& lhs,
113 const base::string16& rhs) {
114 // If we can not get collator instance for specified locale, just do simple
115 // string compare.
116 if (!collator_)
117 return lhs < rhs;
118 return base::i18n::CompareString16WithCollator(*collator_, lhs, rhs) ==
119 UCOL_LESS;
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 size_t begin_index,
131 size_t end_index,
132 bool needs_stable_sort) {
133 DCHECK_LT(begin_index, end_index);
134 DCHECK_LE(end_index, 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 // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_