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
3 // found in the LICENSE file.
5 #ifndef APP_L10N_UTIL_COLLATOR_H_
6 #define APP_L10N_UTIL_COLLATOR_H_
13 #include "base/utf_string_conversions.h"
14 #include "unicode/coll.h"
18 // Compares the two strings using the specified collator.
19 UCollationResult
CompareStringWithCollator(const icu::Collator
* collator
,
20 const std::wstring
& lhs
,
21 const std::wstring
& rhs
);
23 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
24 // operator (), comparing the string results using a collator.
25 template <class T
, class Method
>
26 class StringMethodComparatorWithCollator
27 : public std::binary_function
<const std::wstring
&,
31 StringMethodComparatorWithCollator(icu::Collator
* collator
, Method method
)
32 : collator_(collator
),
35 // Returns true if lhs preceeds rhs.
36 bool operator() (T
* lhs_t
, T
* rhs_t
) {
37 return CompareStringWithCollator(collator_
, (lhs_t
->*method_
)(),
38 (rhs_t
->*method_
)()) == UCOL_LESS
;
42 icu::Collator
* collator_
;
46 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
47 // operator (), comparing the string results using <.
48 template <class T
, class Method
>
49 class StringMethodComparator
: public std::binary_function
<const std::wstring
&,
53 explicit StringMethodComparator(Method method
) : method_(method
) { }
55 // Returns true if lhs preceeds rhs.
56 bool operator() (T
* lhs_t
, T
* rhs_t
) {
57 return (lhs_t
->*method_
)() < (rhs_t
->*method_
)();
64 // Sorts the objects in |elements| using the method |method|, which must return
65 // a string. Sorting is done using a collator, unless a collator can not be
66 // found in which case the strings are sorted using the operator <.
67 template <class T
, class Method
>
68 void SortStringsUsingMethod(const std::wstring
& locale
,
69 std::vector
<T
*>* elements
,
71 UErrorCode error
= U_ZERO_ERROR
;
72 icu::Locale
loc(WideToUTF8(locale
).c_str());
73 scoped_ptr
<icu::Collator
> collator(icu::Collator::createInstance(loc
, error
));
74 if (U_FAILURE(error
)) {
75 sort(elements
->begin(), elements
->end(),
76 StringMethodComparator
<T
, Method
>(method
));
80 std::sort(elements
->begin(), elements
->end(),
81 StringMethodComparatorWithCollator
<T
, Method
>(collator
.get(), method
));
84 // Compares two elements' string keys and returns true if the first element's
85 // string key is less than the second element's string key. The Element must
86 // have a method like the follow format to return the string key.
87 // const std::wstring& GetStringKey() const;
88 // This uses the locale specified in the constructor.
89 template <class Element
>
90 class StringComparator
: public std::binary_function
<const Element
&,
94 explicit StringComparator(icu::Collator
* collator
)
95 : collator_(collator
) { }
97 // Returns true if lhs precedes rhs.
98 bool operator()(const Element
& lhs
, const Element
& rhs
) {
99 const std::wstring
& lhs_string_key
= lhs
.GetStringKey();
100 const std::wstring
& rhs_string_key
= rhs
.GetStringKey();
102 return StringComparator
<std::wstring
>(collator_
)(lhs_string_key
,
107 icu::Collator
* collator_
;
110 // Specialization of operator() method for std::wstring version.
112 bool StringComparator
<std::wstring
>::operator()(const std::wstring
& lhs
,
113 const std::wstring
& rhs
);
115 #if !defined(WCHAR_T_IS_UTF16)
116 // Specialization of operator() method for string16 version.
118 bool StringComparator
<string16
>::operator()(const string16
& lhs
,
119 const string16
& rhs
);
120 #endif // !defined(WCHAR_T_IS_UTF16)
122 // In place sorting of |elements| of a vector according to the string key of
123 // each element in the vector by using collation rules for |locale|.
124 // |begin_index| points to the start position of elements in the vector which
125 // want to be sorted. |end_index| points to the end position of elements in the
126 // vector which want to be sorted
127 template <class Element
>
128 void SortVectorWithStringKey(const std::string
& locale
,
129 std::vector
<Element
>* elements
,
130 unsigned int begin_index
,
131 unsigned int end_index
,
132 bool needs_stable_sort
) {
133 DCHECK(begin_index
< end_index
&&
134 end_index
<= static_cast<unsigned int>(elements
->size()));
135 UErrorCode error
= U_ZERO_ERROR
;
136 icu::Locale
loc(locale
.c_str());
137 scoped_ptr
<icu::Collator
> collator(icu::Collator::createInstance(loc
, error
));
138 if (U_FAILURE(error
))
140 StringComparator
<Element
> c(collator
.get());
141 if (needs_stable_sort
) {
142 stable_sort(elements
->begin() + begin_index
,
143 elements
->begin() + end_index
,
146 sort(elements
->begin() + begin_index
, elements
->begin() + end_index
, c
);
150 template <class Element
>
151 void SortVectorWithStringKey(const std::string
& locale
,
152 std::vector
<Element
>* elements
,
153 bool needs_stable_sort
) {
154 SortVectorWithStringKey
<Element
>(locale
, elements
, 0, elements
->size(),
158 } // namespace l10n_util
160 #endif // APP_L10N_UTIL_COLLATOR_H_