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
3 // found in the LICENSE file.
5 #ifndef UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
6 #define UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
13 #include "base/i18n/string_compare.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "third_party/icu/public/i18n/unicode/coll.h"
16 #include "ui/base/ui_export.h"
20 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
21 // operator (), comparing the string results using a collator.
22 template <class T
, class Method
>
23 class StringMethodComparatorWithCollator
24 : public std::binary_function
<const string16
&,
28 StringMethodComparatorWithCollator(icu::Collator
* collator
, Method method
)
29 : collator_(collator
),
32 // Returns true if lhs preceeds rhs.
33 bool operator() (T
* lhs_t
, T
* rhs_t
) {
34 return base::i18n::CompareString16WithCollator(collator_
,
35 (lhs_t
->*method_
)(), (rhs_t
->*method_
)()) == UCOL_LESS
;
39 icu::Collator
* collator_
;
43 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
44 // operator (), comparing the string results using <.
45 template <class T
, class Method
>
46 class StringMethodComparator
: public std::binary_function
<const string16
&,
50 explicit StringMethodComparator(Method method
) : method_(method
) { }
52 // Returns true if lhs preceeds rhs.
53 bool operator() (T
* lhs_t
, T
* rhs_t
) {
54 return (lhs_t
->*method_
)() < (rhs_t
->*method_
)();
61 // Sorts the objects in |elements| using the method |method|, which must return
62 // a string. Sorting is done using a collator, unless a collator can not be
63 // found in which case the strings are sorted using the operator <.
64 template <class T
, class Method
>
65 void SortStringsUsingMethod(const std::string
& locale
,
66 std::vector
<T
*>* elements
,
68 UErrorCode error
= U_ZERO_ERROR
;
69 icu::Locale
loc(locale
.c_str());
70 scoped_ptr
<icu::Collator
> collator(icu::Collator::createInstance(loc
, error
));
71 if (U_FAILURE(error
)) {
72 sort(elements
->begin(), elements
->end(),
73 StringMethodComparator
<T
, Method
>(method
));
77 std::sort(elements
->begin(), elements
->end(),
78 StringMethodComparatorWithCollator
<T
, Method
>(collator
.get(), method
));
81 // Compares two elements' string keys and returns true if the first element's
82 // string key is less than the second element's string key. The Element must
83 // have a method like the follow format to return the string key.
84 // const string16& GetStringKey() const;
85 // This uses the locale specified in the constructor.
86 template <class Element
>
87 class StringComparator
: public std::binary_function
<const Element
&,
91 explicit StringComparator(icu::Collator
* collator
)
92 : collator_(collator
) { }
94 // Returns true if lhs precedes rhs.
95 bool operator()(const Element
& lhs
, const Element
& rhs
) {
96 const string16
& lhs_string_key
= lhs
.GetStringKey();
97 const string16
& rhs_string_key
= rhs
.GetStringKey();
99 return StringComparator
<string16
>(collator_
)(lhs_string_key
,
104 icu::Collator
* collator_
;
107 // Specialization of operator() method for string16 version.
108 template <> UI_EXPORT
109 bool StringComparator
<string16
>::operator()(const string16
& lhs
,
110 const string16
& rhs
);
112 // 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 // |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 // vector which want to be sorted
117 template <class Element
>
118 void SortVectorWithStringKey(const std::string
& locale
,
119 std::vector
<Element
>* elements
,
120 unsigned int begin_index
,
121 unsigned int end_index
,
122 bool needs_stable_sort
) {
123 DCHECK(begin_index
< end_index
&&
124 end_index
<= static_cast<unsigned int>(elements
->size()));
125 UErrorCode error
= U_ZERO_ERROR
;
126 icu::Locale
loc(locale
.c_str());
127 scoped_ptr
<icu::Collator
> collator(icu::Collator::createInstance(loc
, error
));
128 if (U_FAILURE(error
))
130 StringComparator
<Element
> c(collator
.get());
131 if (needs_stable_sort
) {
132 stable_sort(elements
->begin() + begin_index
,
133 elements
->begin() + end_index
,
136 sort(elements
->begin() + begin_index
, elements
->begin() + end_index
, c
);
140 template <class Element
>
141 void SortVectorWithStringKey(const std::string
& locale
,
142 std::vector
<Element
>* elements
,
143 bool needs_stable_sort
) {
144 SortVectorWithStringKey
<Element
>(locale
, elements
, 0, elements
->size(),
148 } // namespace l10n_util
150 #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_