OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 APP_L10N_UTIL_COLLATOR_H_ | 5 #ifndef APP_L10N_UTIL_COLLATOR_H_ |
6 #define APP_L10N_UTIL_COLLATOR_H_ | 6 #define APP_L10N_UTIL_COLLATOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <functional> | 10 #include <functional> |
(...skipping 10 matching lines...) Expand all Loading... |
21 const std::wstring& lhs, | 21 const std::wstring& lhs, |
22 const std::wstring& rhs); | 22 const std::wstring& rhs); |
23 UCollationResult CompareString16WithCollator(const icu::Collator* collator, | 23 UCollationResult CompareString16WithCollator(const icu::Collator* collator, |
24 const string16& lhs, | 24 const string16& lhs, |
25 const string16& rhs); | 25 const string16& rhs); |
26 | 26 |
27 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to | 27 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
28 // operator (), comparing the string results using a collator. | 28 // operator (), comparing the string results using a collator. |
29 template <class T, class Method> | 29 template <class T, class Method> |
30 class StringMethodComparatorWithCollator | 30 class StringMethodComparatorWithCollator |
31 : public std::binary_function<const std::wstring&, | 31 : public std::binary_function<const string16&, |
32 const std::wstring&, | 32 const string16&, |
33 bool> { | 33 bool> { |
34 public: | 34 public: |
35 StringMethodComparatorWithCollator(icu::Collator* collator, Method method) | 35 StringMethodComparatorWithCollator(icu::Collator* collator, Method method) |
36 : collator_(collator), | 36 : collator_(collator), |
37 method_(method) { } | 37 method_(method) { } |
38 | 38 |
39 // Returns true if lhs preceeds rhs. | 39 // Returns true if lhs preceeds rhs. |
40 bool operator() (T* lhs_t, T* rhs_t) { | 40 bool operator() (T* lhs_t, T* rhs_t) { |
41 return CompareStringWithCollator(collator_, (lhs_t->*method_)(), | 41 return CompareString16WithCollator(collator_, (lhs_t->*method_)(), |
42 (rhs_t->*method_)()) == UCOL_LESS; | 42 (rhs_t->*method_)()) == UCOL_LESS; |
43 } | 43 } |
44 | 44 |
45 private: | 45 private: |
46 icu::Collator* collator_; | 46 icu::Collator* collator_; |
47 Method method_; | 47 Method method_; |
48 }; | 48 }; |
49 | 49 |
50 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to | 50 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
51 // operator (), comparing the string results using <. | 51 // operator (), comparing the string results using <. |
52 template <class T, class Method> | 52 template <class T, class Method> |
53 class StringMethodComparator : public std::binary_function<const std::wstring&, | 53 class StringMethodComparator : public std::binary_function<const string16&, |
54 const std::wstring&, | 54 const string16&, |
55 bool> { | 55 bool> { |
56 public: | 56 public: |
57 explicit StringMethodComparator(Method method) : method_(method) { } | 57 explicit StringMethodComparator(Method method) : method_(method) { } |
58 | 58 |
59 // Returns true if lhs preceeds rhs. | 59 // Returns true if lhs preceeds rhs. |
60 bool operator() (T* lhs_t, T* rhs_t) { | 60 bool operator() (T* lhs_t, T* rhs_t) { |
61 return (lhs_t->*method_)() < (rhs_t->*method_)(); | 61 return (lhs_t->*method_)() < (rhs_t->*method_)(); |
62 } | 62 } |
63 | 63 |
64 private: | 64 private: |
65 Method method_; | 65 Method method_; |
66 }; | 66 }; |
67 | 67 |
68 // Sorts the objects in |elements| using the method |method|, which must return | 68 // Sorts the objects in |elements| using the method |method|, which must return |
69 // a string. Sorting is done using a collator, unless a collator can not be | 69 // a string. Sorting is done using a collator, unless a collator can not be |
70 // found in which case the strings are sorted using the operator <. | 70 // found in which case the strings are sorted using the operator <. |
71 template <class T, class Method> | 71 template <class T, class Method> |
72 void SortStringsUsingMethod(const std::wstring& locale, | 72 void SortStringsUsingMethod(const std::string& locale, |
73 std::vector<T*>* elements, | 73 std::vector<T*>* elements, |
74 Method method) { | 74 Method method) { |
75 UErrorCode error = U_ZERO_ERROR; | 75 UErrorCode error = U_ZERO_ERROR; |
76 icu::Locale loc(WideToUTF8(locale).c_str()); | 76 icu::Locale loc(locale.c_str()); |
77 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); | 77 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); |
78 if (U_FAILURE(error)) { | 78 if (U_FAILURE(error)) { |
79 sort(elements->begin(), elements->end(), | 79 sort(elements->begin(), elements->end(), |
80 StringMethodComparator<T, Method>(method)); | 80 StringMethodComparator<T, Method>(method)); |
81 return; | 81 return; |
82 } | 82 } |
83 | 83 |
84 std::sort(elements->begin(), elements->end(), | 84 std::sort(elements->begin(), elements->end(), |
85 StringMethodComparatorWithCollator<T, Method>(collator.get(), method)); | 85 StringMethodComparatorWithCollator<T, Method>(collator.get(), method)); |
86 } | 86 } |
87 | 87 |
88 // Compares two elements' string keys and returns true if the first element's | 88 // Compares two elements' string keys and returns true if the first element's |
89 // string key is less than the second element's string key. The Element must | 89 // string key is less than the second element's string key. The Element must |
90 // have a method like the follow format to return the string key. | 90 // have a method like the follow format to return the string key. |
91 // const std::wstring& GetStringKey() const; | 91 // const string16& GetStringKey() const; |
92 // This uses the locale specified in the constructor. | 92 // This uses the locale specified in the constructor. |
93 template <class Element> | 93 template <class Element> |
94 class StringComparator : public std::binary_function<const Element&, | 94 class StringComparator : public std::binary_function<const Element&, |
95 const Element&, | 95 const Element&, |
96 bool> { | 96 bool> { |
97 public: | 97 public: |
98 explicit StringComparator(icu::Collator* collator) | 98 explicit StringComparator(icu::Collator* collator) |
99 : collator_(collator) { } | 99 : collator_(collator) { } |
100 | 100 |
101 // Returns true if lhs precedes rhs. | 101 // Returns true if lhs precedes rhs. |
102 bool operator()(const Element& lhs, const Element& rhs) { | 102 bool operator()(const Element& lhs, const Element& rhs) { |
103 const std::wstring& lhs_string_key = lhs.GetStringKey(); | 103 const string16& lhs_string_key = lhs.GetStringKey(); |
104 const std::wstring& rhs_string_key = rhs.GetStringKey(); | 104 const string16& rhs_string_key = rhs.GetStringKey(); |
105 | 105 |
106 return StringComparator<std::wstring>(collator_)(lhs_string_key, | 106 return StringComparator<string16>(collator_)(lhs_string_key, |
107 rhs_string_key); | 107 rhs_string_key); |
108 } | 108 } |
109 | 109 |
110 private: | 110 private: |
111 icu::Collator* collator_; | 111 icu::Collator* collator_; |
112 }; | 112 }; |
113 | 113 |
114 // Specialization of operator() method for std::wstring version. | |
115 template <> | |
116 bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, | |
117 const std::wstring& rhs); | |
118 | |
119 #if !defined(WCHAR_T_IS_UTF16) | |
120 // Specialization of operator() method for string16 version. | 114 // Specialization of operator() method for string16 version. |
121 template <> | 115 template <> |
122 bool StringComparator<string16>::operator()(const string16& lhs, | 116 bool StringComparator<string16>::operator()(const string16& lhs, |
123 const string16& rhs); | 117 const string16& rhs); |
124 #endif // !defined(WCHAR_T_IS_UTF16) | |
125 | 118 |
126 // In place sorting of |elements| of a vector according to the string key of | 119 // In place sorting of |elements| of a vector according to the string key of |
127 // each element in the vector by using collation rules for |locale|. | 120 // each element in the vector by using collation rules for |locale|. |
128 // |begin_index| points to the start position of elements in the vector which | 121 // |begin_index| points to the start position of elements in the vector which |
129 // want to be sorted. |end_index| points to the end position of elements in the | 122 // want to be sorted. |end_index| points to the end position of elements in the |
130 // vector which want to be sorted | 123 // vector which want to be sorted |
131 template <class Element> | 124 template <class Element> |
132 void SortVectorWithStringKey(const std::string& locale, | 125 void SortVectorWithStringKey(const std::string& locale, |
133 std::vector<Element>* elements, | 126 std::vector<Element>* elements, |
134 unsigned int begin_index, | 127 unsigned int begin_index, |
(...skipping 20 matching lines...) Expand all Loading... |
155 void SortVectorWithStringKey(const std::string& locale, | 148 void SortVectorWithStringKey(const std::string& locale, |
156 std::vector<Element>* elements, | 149 std::vector<Element>* elements, |
157 bool needs_stable_sort) { | 150 bool needs_stable_sort) { |
158 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), | 151 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
159 needs_stable_sort); | 152 needs_stable_sort); |
160 } | 153 } |
161 | 154 |
162 } // namespace l10n_util | 155 } // namespace l10n_util |
163 | 156 |
164 #endif // APP_L10N_UTIL_COLLATOR_H_ | 157 #endif // APP_L10N_UTIL_COLLATOR_H_ |
OLD | NEW |