OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ | 5 #ifndef UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ |
6 #define UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ | 6 #define UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <functional> | 9 #include <functional> |
10 #include <string> | 10 #include <string> |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // This uses the locale specified in the constructor. | 87 // This uses the locale specified in the constructor. |
88 template <class Element> | 88 template <class Element> |
89 class StringComparator : public std::binary_function<const Element&, | 89 class StringComparator : public std::binary_function<const Element&, |
90 const Element&, | 90 const Element&, |
91 bool> { | 91 bool> { |
92 public: | 92 public: |
93 explicit StringComparator(icu::Collator* collator) | 93 explicit StringComparator(icu::Collator* collator) |
94 : collator_(collator) { } | 94 : collator_(collator) { } |
95 | 95 |
96 // Returns true if lhs precedes rhs. | 96 // Returns true if lhs precedes rhs. |
97 bool operator()(const Element& lhs, const Element& rhs) { | 97 bool operator()(const Element& lhs, const Element& rhs) const { |
98 const base::string16& lhs_string_key = lhs.GetStringKey(); | 98 const base::string16& lhs_string_key = lhs.GetStringKey(); |
99 const base::string16& rhs_string_key = rhs.GetStringKey(); | 99 const base::string16& rhs_string_key = rhs.GetStringKey(); |
100 | 100 |
101 return StringComparator<base::string16>(collator_)(lhs_string_key, | 101 return StringComparator<base::string16>(collator_)(lhs_string_key, |
102 rhs_string_key); | 102 rhs_string_key); |
103 } | 103 } |
104 | 104 |
105 private: | 105 private: |
106 icu::Collator* collator_; | 106 icu::Collator* collator_; |
107 }; | 107 }; |
108 | 108 |
109 // Specialization of operator() method for base::string16 version. | 109 // Specialization of operator() method for base::string16 version. |
110 template <> | 110 template <> |
111 UI_BASE_EXPORT inline bool StringComparator<base::string16>::operator()( | 111 UI_BASE_EXPORT inline bool StringComparator<base::string16>::operator()( |
112 const base::string16& lhs, | 112 const base::string16& lhs, |
113 const base::string16& rhs) { | 113 const base::string16& rhs) const { |
114 // If we can not get collator instance for specified locale, just do simple | 114 // If we can not get collator instance for specified locale, just do simple |
115 // string compare. | 115 // string compare. |
116 if (!collator_) | 116 if (!collator_) |
117 return lhs < rhs; | 117 return lhs < rhs; |
118 return base::i18n::CompareString16WithCollator(*collator_, lhs, rhs) == | 118 return base::i18n::CompareString16WithCollator(*collator_, lhs, rhs) == |
119 UCOL_LESS; | 119 UCOL_LESS; |
120 } | 120 } |
121 | 121 |
122 // In place sorting of |elements| of a vector according to the string key of | 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|. | 123 // each element in the vector by using collation rules for |locale|. |
(...skipping 27 matching lines...) Expand all Loading... |
151 void SortVectorWithStringKey(const std::string& locale, | 151 void SortVectorWithStringKey(const std::string& locale, |
152 std::vector<Element>* elements, | 152 std::vector<Element>* elements, |
153 bool needs_stable_sort) { | 153 bool needs_stable_sort) { |
154 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), | 154 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
155 needs_stable_sort); | 155 needs_stable_sort); |
156 } | 156 } |
157 | 157 |
158 } // namespace l10n_util | 158 } // namespace l10n_util |
159 | 159 |
160 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ | 160 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ |
OLD | NEW |