Home | History | Annotate | Download | only in l10n
      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 <algorithm>
      9 #include <functional>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/i18n/string_compare.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "third_party/icu/source/i18n/unicode/coll.h"
     16 #include "ui/base/ui_base_export.h"
     17 
     18 namespace l10n_util {
     19 
     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 base::string16&,
     25                                   const base::string16&,
     26                                   bool> {
     27  public:
     28   StringMethodComparatorWithCollator(icu::Collator* collator, Method method)
     29       : collator_(collator),
     30         method_(method) { }
     31 
     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;
     36   }
     37 
     38  private:
     39   icu::Collator* collator_;
     40   Method method_;
     41 };
     42 
     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
     47     : public std::binary_function<const base::string16&,
     48                                   const base::string16&,
     49                                   bool> {
     50  public:
     51   explicit StringMethodComparator(Method method) : method_(method) { }
     52 
     53   // Returns true if lhs preceeds rhs.
     54   bool operator() (T* lhs_t, T* rhs_t) {
     55     return (lhs_t->*method_)() < (rhs_t->*method_)();
     56   }
     57 
     58  private:
     59   Method method_;
     60 };
     61 
     62 // Sorts the objects in |elements| using the method |method|, which must return
     63 // a string. Sorting is done using a collator, unless a collator can not be
     64 // found in which case the strings are sorted using the operator <.
     65 template <class T, class Method>
     66 void SortStringsUsingMethod(const std::string& locale,
     67                             std::vector<T*>* elements,
     68                             Method method) {
     69   UErrorCode error = U_ZERO_ERROR;
     70   icu::Locale loc(locale.c_str());
     71   scoped_ptr<icu::Collator> collator(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 : public std::binary_function<const Element&,
     89                                                      const Element&,
     90                                                      bool> {
     91  public:
     92   explicit StringComparator(icu::Collator* collator)
     93       : collator_(collator) { }
     94 
     95   // Returns true if lhs precedes rhs.
     96   bool operator()(const Element& lhs, const Element& rhs) {
     97     const base::string16& lhs_string_key = lhs.GetStringKey();
     98     const base::string16& rhs_string_key = rhs.GetStringKey();
     99 
    100     return StringComparator<base::string16>(collator_)(lhs_string_key,
    101                                                  rhs_string_key);
    102   }
    103 
    104  private:
    105   icu::Collator* collator_;
    106 };
    107 
    108 // Specialization of operator() method for base::string16 version.
    109 template <>
    110 UI_BASE_EXPORT inline bool StringComparator<base::string16>::operator()(
    111     const base::string16& lhs,
    112     const base::string16& rhs) {
    113   // If we can not get collator instance for specified locale, just do simple
    114   // string compare.
    115   if (!collator_)
    116     return lhs < rhs;
    117   return base::i18n::CompareString16WithCollator(collator_, lhs, rhs) ==
    118       UCOL_LESS;
    119 }
    120 
    121 // In place sorting of |elements| of a vector according to the string key of
    122 // each element in the vector by using collation rules for |locale|.
    123 // |begin_index| points to the start position of elements in the vector which
    124 // want to be sorted. |end_index| points to the end position of elements in the
    125 // vector which want to be sorted
    126 template <class Element>
    127 void SortVectorWithStringKey(const std::string& locale,
    128                              std::vector<Element>* elements,
    129                              unsigned int begin_index,
    130                              unsigned int end_index,
    131                              bool needs_stable_sort) {
    132   DCHECK(begin_index < end_index &&
    133          end_index <= static_cast<unsigned int>(elements->size()));
    134   UErrorCode error = U_ZERO_ERROR;
    135   icu::Locale loc(locale.c_str());
    136   scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
    137   if (U_FAILURE(error))
    138     collator.reset();
    139   StringComparator<Element> c(collator.get());
    140   if (needs_stable_sort) {
    141     stable_sort(elements->begin() + begin_index,
    142                 elements->begin() + end_index,
    143                 c);
    144   } else {
    145     sort(elements->begin() + begin_index, elements->begin() + end_index, c);
    146   }
    147 }
    148 
    149 template <class Element>
    150 void SortVectorWithStringKey(const std::string& locale,
    151                              std::vector<Element>* elements,
    152                              bool needs_stable_sort) {
    153   SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(),
    154                                    needs_stable_sort);
    155 }
    156 
    157 }  // namespace l10n_util
    158 
    159 #endif  // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_
    160