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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 return; | 75 return; |
76 } | 76 } |
77 | 77 |
78 std::sort(elements->begin(), elements->end(), | 78 std::sort(elements->begin(), elements->end(), |
79 StringMethodComparatorWithCollator<T, Method>(collator.get(), method)); | 79 StringMethodComparatorWithCollator<T, Method>(collator.get(), method)); |
80 } | 80 } |
81 | 81 |
82 // Compares two elements' string keys and returns true if the first element's | 82 // Compares two elements' string keys and returns true if the first element's |
83 // string key is less than the second element's string key. The Element must | 83 // string key is less than the second element's string key. The Element must |
84 // have a method like the follow format to return the string key. | 84 // have a method like the follow format to return the string key. |
85 // const string16& GetStringKey() const; | 85 // const base::string16& GetStringKey() const; |
86 // This uses the locale specified in the constructor. | 86 // This uses the locale specified in the constructor. |
87 template <class Element> | 87 template <class Element> |
88 class StringComparator : public std::binary_function<const Element&, | 88 class StringComparator : public std::binary_function<const Element&, |
89 const Element&, | 89 const Element&, |
90 bool> { | 90 bool> { |
91 public: | 91 public: |
92 explicit StringComparator(icu::Collator* collator) | 92 explicit StringComparator(icu::Collator* collator) |
93 : collator_(collator) { } | 93 : collator_(collator) { } |
94 | 94 |
95 // Returns true if lhs precedes rhs. | 95 // Returns true if lhs precedes rhs. |
96 bool operator()(const Element& lhs, const Element& rhs) { | 96 bool operator()(const Element& lhs, const Element& rhs) { |
97 const base::string16& lhs_string_key = lhs.GetStringKey(); | 97 const base::string16& lhs_string_key = lhs.GetStringKey(); |
98 const base::string16& rhs_string_key = rhs.GetStringKey(); | 98 const base::string16& rhs_string_key = rhs.GetStringKey(); |
99 | 99 |
100 return StringComparator<base::string16>(collator_)(lhs_string_key, | 100 return StringComparator<base::string16>(collator_)(lhs_string_key, |
101 rhs_string_key); | 101 rhs_string_key); |
102 } | 102 } |
103 | 103 |
104 private: | 104 private: |
105 icu::Collator* collator_; | 105 icu::Collator* collator_; |
106 }; | 106 }; |
107 | 107 |
108 // Specialization of operator() method for string16 version. | 108 // Specialization of operator() method for base::string16 version. |
109 template <> UI_EXPORT | 109 template <> UI_EXPORT |
110 bool StringComparator<base::string16>::operator()(const base::string16& lhs, | 110 bool StringComparator<base::string16>::operator()(const base::string16& lhs, |
111 const base::string16& rhs); | 111 const base::string16& rhs); |
112 | 112 |
113 // In place sorting of |elements| of a vector according to the string key of | 113 // In place sorting of |elements| of a vector according to the string key of |
114 // each element in the vector by using collation rules for |locale|. | 114 // each element in the vector by using collation rules for |locale|. |
115 // |begin_index| points to the start position of elements in the vector which | 115 // |begin_index| points to the start position of elements in the vector which |
116 // want to be sorted. |end_index| points to the end position of elements in the | 116 // want to be sorted. |end_index| points to the end position of elements in the |
117 // vector which want to be sorted | 117 // vector which want to be sorted |
118 template <class Element> | 118 template <class Element> |
(...skipping 23 matching lines...) Expand all Loading... |
142 void SortVectorWithStringKey(const std::string& locale, | 142 void SortVectorWithStringKey(const std::string& locale, |
143 std::vector<Element>* elements, | 143 std::vector<Element>* elements, |
144 bool needs_stable_sort) { | 144 bool needs_stable_sort) { |
145 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), | 145 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
146 needs_stable_sort); | 146 needs_stable_sort); |
147 } | 147 } |
148 | 148 |
149 } // namespace l10n_util | 149 } // namespace l10n_util |
150 | 150 |
151 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ | 151 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ |
OLD | NEW |