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> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/i18n/string_compare.h" | 13 #include "base/i18n/string_compare.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "third_party/icu/source/i18n/unicode/coll.h" | 15 #include "third_party/icu/source/i18n/unicode/coll.h" |
16 #include "ui/base/ui_export.h" | 16 #include "ui/base/ui_export.h" |
17 | 17 |
18 namespace l10n_util { | 18 namespace l10n_util { |
19 | 19 |
20 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to | 20 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
21 // operator (), comparing the string results using a collator. | 21 // operator (), comparing the string results using a collator. |
22 template <class T, class Method> | 22 template <class T, class Method> |
23 class StringMethodComparatorWithCollator | 23 class StringMethodComparatorWithCollator |
24 : public std::binary_function<const string16&, | 24 : public std::binary_function<const base::string16&, |
25 const string16&, | 25 const base::string16&, |
26 bool> { | 26 bool> { |
27 public: | 27 public: |
28 StringMethodComparatorWithCollator(icu::Collator* collator, Method method) | 28 StringMethodComparatorWithCollator(icu::Collator* collator, Method method) |
29 : collator_(collator), | 29 : collator_(collator), |
30 method_(method) { } | 30 method_(method) { } |
31 | 31 |
32 // Returns true if lhs preceeds rhs. | 32 // Returns true if lhs preceeds rhs. |
33 bool operator() (T* lhs_t, T* rhs_t) { | 33 bool operator() (T* lhs_t, T* rhs_t) { |
34 return base::i18n::CompareString16WithCollator(collator_, | 34 return base::i18n::CompareString16WithCollator(collator_, |
35 (lhs_t->*method_)(), (rhs_t->*method_)()) == UCOL_LESS; | 35 (lhs_t->*method_)(), (rhs_t->*method_)()) == UCOL_LESS; |
36 } | 36 } |
37 | 37 |
38 private: | 38 private: |
39 icu::Collator* collator_; | 39 icu::Collator* collator_; |
40 Method method_; | 40 Method method_; |
41 }; | 41 }; |
42 | 42 |
43 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to | 43 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
44 // operator (), comparing the string results using <. | 44 // operator (), comparing the string results using <. |
45 template <class T, class Method> | 45 template <class T, class Method> |
46 class StringMethodComparator : public std::binary_function<const string16&, | 46 class StringMethodComparator |
47 const string16&, | 47 : public std::binary_function<const base::string16&, |
48 bool> { | 48 const base::string16&, |
| 49 bool> { |
49 public: | 50 public: |
50 explicit StringMethodComparator(Method method) : method_(method) { } | 51 explicit StringMethodComparator(Method method) : method_(method) { } |
51 | 52 |
52 // Returns true if lhs preceeds rhs. | 53 // Returns true if lhs preceeds rhs. |
53 bool operator() (T* lhs_t, T* rhs_t) { | 54 bool operator() (T* lhs_t, T* rhs_t) { |
54 return (lhs_t->*method_)() < (rhs_t->*method_)(); | 55 return (lhs_t->*method_)() < (rhs_t->*method_)(); |
55 } | 56 } |
56 | 57 |
57 private: | 58 private: |
58 Method method_; | 59 Method method_; |
(...skipping 27 matching lines...) Expand all Loading... |
86 template <class Element> | 87 template <class Element> |
87 class StringComparator : public std::binary_function<const Element&, | 88 class StringComparator : public std::binary_function<const Element&, |
88 const Element&, | 89 const Element&, |
89 bool> { | 90 bool> { |
90 public: | 91 public: |
91 explicit StringComparator(icu::Collator* collator) | 92 explicit StringComparator(icu::Collator* collator) |
92 : collator_(collator) { } | 93 : collator_(collator) { } |
93 | 94 |
94 // Returns true if lhs precedes rhs. | 95 // Returns true if lhs precedes rhs. |
95 bool operator()(const Element& lhs, const Element& rhs) { | 96 bool operator()(const Element& lhs, const Element& rhs) { |
96 const string16& lhs_string_key = lhs.GetStringKey(); | 97 const base::string16& lhs_string_key = lhs.GetStringKey(); |
97 const string16& rhs_string_key = rhs.GetStringKey(); | 98 const base::string16& rhs_string_key = rhs.GetStringKey(); |
98 | 99 |
99 return StringComparator<string16>(collator_)(lhs_string_key, | 100 return StringComparator<base::string16>(collator_)(lhs_string_key, |
100 rhs_string_key); | 101 rhs_string_key); |
101 } | 102 } |
102 | 103 |
103 private: | 104 private: |
104 icu::Collator* collator_; | 105 icu::Collator* collator_; |
105 }; | 106 }; |
106 | 107 |
107 // Specialization of operator() method for string16 version. | 108 // Specialization of operator() method for string16 version. |
108 template <> UI_EXPORT | 109 template <> UI_EXPORT |
109 bool StringComparator<string16>::operator()(const string16& lhs, | 110 bool StringComparator<base::string16>::operator()(const base::string16& lhs, |
110 const string16& rhs); | 111 const base::string16& rhs); |
111 | 112 |
112 // 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 |
113 // each element in the vector by using collation rules for |locale|. | 114 // each element in the vector by using collation rules for |locale|. |
114 // |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 |
115 // 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 |
116 // vector which want to be sorted | 117 // vector which want to be sorted |
117 template <class Element> | 118 template <class Element> |
118 void SortVectorWithStringKey(const std::string& locale, | 119 void SortVectorWithStringKey(const std::string& locale, |
119 std::vector<Element>* elements, | 120 std::vector<Element>* elements, |
120 unsigned int begin_index, | 121 unsigned int begin_index, |
(...skipping 20 matching lines...) Expand all Loading... |
141 void SortVectorWithStringKey(const std::string& locale, | 142 void SortVectorWithStringKey(const std::string& locale, |
142 std::vector<Element>* elements, | 143 std::vector<Element>* elements, |
143 bool needs_stable_sort) { | 144 bool needs_stable_sort) { |
144 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), | 145 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
145 needs_stable_sort); | 146 needs_stable_sort); |
146 } | 147 } |
147 | 148 |
148 } // namespace l10n_util | 149 } // namespace l10n_util |
149 | 150 |
150 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ | 151 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ |
OLD | NEW |