// Copyright 2011 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ #define UI_BASE_L10N_L10N_UTIL_COLLATOR_H_ #include <stddef.h> #include <algorithm> #include <functional> #include <memory> #include <string> #include <vector> #include "base/component_export.h" #include "base/i18n/string_compare.h" #include "base/memory/raw_ptr.h" #include "third_party/icu/source/i18n/unicode/coll.h" namespace l10n_util { // Used by SortStringsUsingMethod. Invokes a method on the objects passed to // operator (), comparing the string results using a collator. template <class T, class Method> class StringMethodComparatorWithCollator { … }; // Used by SortStringsUsingMethod. Invokes a method on the objects passed to // operator (), comparing the string results using <. template <class T, class Method> class StringMethodComparator { … }; // Sorts the objects in |elements| using the method |method|, which must return // a string. Sorting is done using a collator, unless a collator can not be // found in which case the strings are sorted using the operator <. template <class T, class Method> void SortStringsUsingMethod(const std::string& locale, std::vector<std::unique_ptr<T>>* elements, Method method) { … } // Compares two elements' string keys and returns true if the first element's // string key is less than the second element's string key. The Element must // have a method like the follow format to return the string key. // const std::u16string& GetStringKey() const; // This uses the locale specified in the constructor. template <class Element> class StringComparator { … }; // Specialization of operator() method for std::u16string version. template <> COMPONENT_EXPORT(UI_BASE) inline bool StringComparator<std::u16string>::operator()( const std::u16string& lhs, const std::u16string& rhs) const { … } // In place sorting of |elements| of a vector according to the string key of // each element in the vector by using collation rules for |locale|. // |begin_index| points to the start position of elements in the vector which // want to be sorted. |end_index| points to the end position of elements in the // vector which want to be sorted. template <class Element> void SortVectorWithStringKey(const std::string& locale, std::vector<Element>* elements, size_t begin_index, size_t end_index, bool needs_stable_sort) { … } template <class Element> void SortVectorWithStringKey(const std::string& locale, std::vector<Element>* elements, bool needs_stable_sort) { … } } // namespace l10n_util #endif // UI_BASE_L10N_L10N_UTIL_COLLATOR_H_