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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <functional> | 11 #include <functional> |
| 12 #include <memory> |
12 #include <string> | 13 #include <string> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
15 #include "base/i18n/string_compare.h" | 16 #include "base/i18n/string_compare.h" |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "third_party/icu/source/i18n/unicode/coll.h" | 17 #include "third_party/icu/source/i18n/unicode/coll.h" |
18 #include "ui/base/ui_base_export.h" | 18 #include "ui/base/ui_base_export.h" |
19 | 19 |
20 namespace l10n_util { | 20 namespace l10n_util { |
21 | 21 |
22 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to | 22 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
23 // operator (), comparing the string results using a collator. | 23 // operator (), comparing the string results using a collator. |
24 template <class T, class Method> | 24 template <class T, class Method> |
25 class StringMethodComparatorWithCollator { | 25 class StringMethodComparatorWithCollator { |
26 public: | 26 public: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 // Sorts the objects in |elements| using the method |method|, which must return | 59 // Sorts the objects in |elements| using the method |method|, which must return |
60 // a string. Sorting is done using a collator, unless a collator can not be | 60 // a string. Sorting is done using a collator, unless a collator can not be |
61 // found in which case the strings are sorted using the operator <. | 61 // found in which case the strings are sorted using the operator <. |
62 template <class T, class Method> | 62 template <class T, class Method> |
63 void SortStringsUsingMethod(const std::string& locale, | 63 void SortStringsUsingMethod(const std::string& locale, |
64 std::vector<T*>* elements, | 64 std::vector<T*>* elements, |
65 Method method) { | 65 Method method) { |
66 UErrorCode error = U_ZERO_ERROR; | 66 UErrorCode error = U_ZERO_ERROR; |
67 icu::Locale loc(locale.c_str()); | 67 icu::Locale loc(locale.c_str()); |
68 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); | 68 std::unique_ptr<icu::Collator> collator( |
| 69 icu::Collator::createInstance(loc, error)); |
69 if (U_FAILURE(error)) { | 70 if (U_FAILURE(error)) { |
70 sort(elements->begin(), elements->end(), | 71 sort(elements->begin(), elements->end(), |
71 StringMethodComparator<T, Method>(method)); | 72 StringMethodComparator<T, Method>(method)); |
72 return; | 73 return; |
73 } | 74 } |
74 | 75 |
75 std::sort(elements->begin(), elements->end(), | 76 std::sort(elements->begin(), elements->end(), |
76 StringMethodComparatorWithCollator<T, Method>(collator.get(), method)); | 77 StringMethodComparatorWithCollator<T, Method>(collator.get(), method)); |
77 } | 78 } |
78 | 79 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 template <class Element> | 122 template <class Element> |
122 void SortVectorWithStringKey(const std::string& locale, | 123 void SortVectorWithStringKey(const std::string& locale, |
123 std::vector<Element>* elements, | 124 std::vector<Element>* elements, |
124 size_t begin_index, | 125 size_t begin_index, |
125 size_t end_index, | 126 size_t end_index, |
126 bool needs_stable_sort) { | 127 bool needs_stable_sort) { |
127 DCHECK_LT(begin_index, end_index); | 128 DCHECK_LT(begin_index, end_index); |
128 DCHECK_LE(end_index, elements->size()); | 129 DCHECK_LE(end_index, elements->size()); |
129 UErrorCode error = U_ZERO_ERROR; | 130 UErrorCode error = U_ZERO_ERROR; |
130 icu::Locale loc(locale.c_str()); | 131 icu::Locale loc(locale.c_str()); |
131 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); | 132 std::unique_ptr<icu::Collator> collator( |
| 133 icu::Collator::createInstance(loc, error)); |
132 if (U_FAILURE(error)) | 134 if (U_FAILURE(error)) |
133 collator.reset(); | 135 collator.reset(); |
134 StringComparator<Element> c(collator.get()); | 136 StringComparator<Element> c(collator.get()); |
135 if (needs_stable_sort) { | 137 if (needs_stable_sort) { |
136 stable_sort(elements->begin() + begin_index, | 138 stable_sort(elements->begin() + begin_index, |
137 elements->begin() + end_index, | 139 elements->begin() + end_index, |
138 c); | 140 c); |
139 } else { | 141 } else { |
140 sort(elements->begin() + begin_index, elements->begin() + end_index, c); | 142 sort(elements->begin() + begin_index, elements->begin() + end_index, c); |
141 } | 143 } |
142 } | 144 } |
143 | 145 |
144 template <class Element> | 146 template <class Element> |
145 void SortVectorWithStringKey(const std::string& locale, | 147 void SortVectorWithStringKey(const std::string& locale, |
146 std::vector<Element>* elements, | 148 std::vector<Element>* elements, |
147 bool needs_stable_sort) { | 149 bool needs_stable_sort) { |
148 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), | 150 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
149 needs_stable_sort); | 151 needs_stable_sort); |
150 } | 152 } |
151 | 153 |
152 } // namespace l10n_util | 154 } // namespace l10n_util |
153 | 155 |
154 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ | 156 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ |
OLD | NEW |