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.
4 
5 #ifndef UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
6 #define UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
7 
8 #include <stddef.h>
9 
10 #include <algorithm>
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "base/i18n/string_compare.h"
17 #include "third_party/icu/source/i18n/unicode/coll.h"
18 #include "ui/base/ui_base_export.h"
19 
20 namespace l10n_util {
21 
22 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
23 // operator (), comparing the string results using a collator.
24 template <class T, class Method>
25 class StringMethodComparatorWithCollator {
26  public:
StringMethodComparatorWithCollator(icu::Collator * collator,Method method)27   StringMethodComparatorWithCollator(icu::Collator* collator, Method method)
28       : collator_(collator),
29         method_(method) { }
30 
31   // Returns true if lhs precedes rhs.
operator()32   bool operator()(const std::unique_ptr<T>& lhs_t,
33                   const std::unique_ptr<T>& rhs_t) {
34     return base::i18n::CompareString16WithCollator(
35                *collator_, (lhs_t.get()->*method_)(),
36                (rhs_t.get()->*method_)()) == UCOL_LESS;
37   }
38 
39  private:
40   icu::Collator* collator_;
41   Method method_;
42 };
43 
44 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to
45 // operator (), comparing the string results using <.
46 template <class T, class Method>
47 class StringMethodComparator {
48  public:
StringMethodComparator(Method method)49   explicit StringMethodComparator(Method method) : method_(method) { }
50 
51   // Returns true if lhs precedes rhs.
operator()52   bool operator()(const std::unique_ptr<T>& lhs_t,
53                   const std::unique_ptr<T>& rhs_t) {
54     return (lhs_t.get()->*method_)() < (rhs_t.get()->*method_)();
55   }
56 
57  private:
58   Method method_;
59 };
60 
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>
SortStringsUsingMethod(const std::string & locale,std::vector<std::unique_ptr<T>> * elements,Method method)65 void SortStringsUsingMethod(const std::string& locale,
66                             std::vector<std::unique_ptr<T>>* elements,
67                             Method method) {
68   UErrorCode error = U_ZERO_ERROR;
69   icu::Locale loc(locale.c_str());
70   std::unique_ptr<icu::Collator> collator(
71       icu::Collator::createInstance(loc, error));
72   if (U_FAILURE(error)) {
73     sort(elements->begin(), elements->end(),
74          StringMethodComparator<T, Method>(method));
75     return;
76   }
77 
78   std::sort(elements->begin(), elements->end(),
79       StringMethodComparatorWithCollator<T, Method>(collator.get(), method));
80 }
81 
82 // Compares two elements' string keys and returns true if the first element's
83 // string key is less than the second element's string key. The Element must
84 // have a method like the follow format to return the string key.
85 // const base::string16& GetStringKey() const;
86 // This uses the locale specified in the constructor.
87 template <class Element>
88 class StringComparator {
89  public:
StringComparator(icu::Collator * collator)90   explicit StringComparator(icu::Collator* collator)
91       : collator_(collator) { }
92 
93   // Returns true if lhs precedes rhs.
operator()94   bool operator()(const Element& lhs, const Element& rhs) const {
95     const base::string16& lhs_string_key = lhs.GetStringKey();
96     const base::string16& rhs_string_key = rhs.GetStringKey();
97 
98     return StringComparator<base::string16>(collator_)(lhs_string_key,
99                                                        rhs_string_key);
100   }
101 
102  private:
103   icu::Collator* collator_;
104 };
105 
106 // Specialization of operator() method for base::string16 version.
107 template <>
operator()108 UI_BASE_EXPORT inline bool StringComparator<base::string16>::operator()(
109     const base::string16& lhs,
110     const base::string16& rhs) const {
111   // If we can not get collator instance for specified locale, just do simple
112   // string compare.
113   if (!collator_)
114     return lhs < rhs;
115   return base::i18n::CompareString16WithCollator(*collator_, lhs, rhs) ==
116          UCOL_LESS;
117 }
118 
119 // In place sorting of |elements| of a vector according to the string key of
120 // each element in the vector by using collation rules for |locale|.
121 // |begin_index| points to the start position of elements in the vector which
122 // want to be sorted. |end_index| points to the end position of elements in the
123 // vector which want to be sorted.
124 template <class Element>
SortVectorWithStringKey(const std::string & locale,std::vector<Element> * elements,size_t begin_index,size_t end_index,bool needs_stable_sort)125 void SortVectorWithStringKey(const std::string& locale,
126                              std::vector<Element>* elements,
127                              size_t begin_index,
128                              size_t end_index,
129                              bool needs_stable_sort) {
130   DCHECK_LT(begin_index, end_index);
131   DCHECK_LE(end_index, elements->size());
132   UErrorCode error = U_ZERO_ERROR;
133   icu::Locale loc(locale.c_str());
134   std::unique_ptr<icu::Collator> collator(
135       icu::Collator::createInstance(loc, error));
136   if (U_FAILURE(error))
137     collator.reset();
138   StringComparator<Element> c(collator.get());
139   if (needs_stable_sort) {
140     stable_sort(elements->begin() + begin_index,
141                 elements->begin() + end_index,
142                 c);
143   } else {
144     sort(elements->begin() + begin_index, elements->begin() + end_index, c);
145   }
146 }
147 
148 template <class Element>
SortVectorWithStringKey(const std::string & locale,std::vector<Element> * elements,bool needs_stable_sort)149 void SortVectorWithStringKey(const std::string& locale,
150                              std::vector<Element>* elements,
151                              bool needs_stable_sort) {
152   SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(),
153                                    needs_stable_sort);
154 }
155 
156 }  // namespace l10n_util
157 
158 #endif  // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
159