| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains utility functions for dealing with localized | 5 // This file contains utility functions for dealing with localized |
| 6 // content. | 6 // content. |
| 7 | 7 |
| 8 #ifndef CHROME_COMMON_L10N_UTIL_H_ | 8 #ifndef CHROME_COMMON_L10N_UTIL_H_ |
| 9 #define CHROME_COMMON_L10N_UTIL_H_ | 9 #define CHROME_COMMON_L10N_UTIL_H_ |
| 10 | 10 |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 | 12 |
| 13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
| 14 #include <windows.h> | 14 #include <windows.h> |
| 15 #endif | 15 #endif |
| 16 #include <algorithm> | 16 #include <algorithm> |
| 17 #include <functional> | 17 #include <functional> |
| 18 #include <string> | 18 #include <string> |
| 19 #include <vector> | 19 #include <vector> |
| 20 | 20 |
| 21 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
| 22 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "base/scoped_ptr.h" | 23 #include "base/scoped_ptr.h" |
| 24 #include "base/string16.h" | 24 #include "base/string16.h" |
| 25 #include "base/string_util.h" | 25 #include "base/string_util.h" |
| 26 #include "third_party/icu38/public/common/unicode/ubidi.h" | |
| 27 #include "unicode/coll.h" | 26 #include "unicode/coll.h" |
| 28 #include "unicode/locid.h" | 27 #include "unicode/locid.h" |
| 28 #include "unicode/rbbi.h" |
| 29 #include "unicode/ubidi.h" |
| 30 #include "unicode/uchar.h" |
| 29 | 31 |
| 30 class FilePath; | 32 class FilePath; |
| 31 class PrefService; | 33 class PrefService; |
| 32 | 34 |
| 33 namespace l10n_util { | 35 namespace l10n_util { |
| 34 | 36 |
| 35 const char16 kRightToLeftMark = 0x200f; | 37 const char16 kRightToLeftMark = 0x200f; |
| 36 const char16 kLeftToRightMark = 0x200e; | 38 const char16 kLeftToRightMark = 0x200e; |
| 37 const char16 kLeftToRightEmbeddingMark = 0x202A; | 39 const char16 kLeftToRightEmbeddingMark = 0x202A; |
| 38 const char16 kRightToLeftEmbeddingMark = 0x202B; | 40 const char16 kRightToLeftEmbeddingMark = 0x202B; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 int DefaultCanvasTextAlignment(); | 193 int DefaultCanvasTextAlignment(); |
| 192 | 194 |
| 193 #if defined(OS_WIN) | 195 #if defined(OS_WIN) |
| 194 // Give an HWND, this function sets the WS_EX_LAYOUTRTL extended style for the | 196 // Give an HWND, this function sets the WS_EX_LAYOUTRTL extended style for the |
| 195 // underlying window. When this style is set, the UI for the window is going to | 197 // underlying window. When this style is set, the UI for the window is going to |
| 196 // be mirrored. This is generally done for the UI of right-to-left languages | 198 // be mirrored. This is generally done for the UI of right-to-left languages |
| 197 // such as Hebrew. | 199 // such as Hebrew. |
| 198 void HWNDSetRTLLayout(HWND hwnd); | 200 void HWNDSetRTLLayout(HWND hwnd); |
| 199 #endif | 201 #endif |
| 200 | 202 |
| 203 // Compares the two strings using the specified collator. |
| 204 UCollationResult CompareStringWithCollator(const Collator* collator, |
| 205 const std::wstring& lhs, |
| 206 const std::wstring& rhs); |
| 207 |
| 208 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
| 209 // operator (), comparing the string results using a collator. |
| 210 template <class T, class Method> |
| 211 class StringMethodComparatorWithCollator : |
| 212 public std::binary_function<const std::wstring&, |
| 213 const std::wstring&, |
| 214 bool> { |
| 215 public: |
| 216 StringMethodComparatorWithCollator(Collator* collator, Method method) |
| 217 : collator_(collator), |
| 218 method_(method) { } |
| 219 |
| 220 // Returns true if lhs preceeds rhs. |
| 221 bool operator() (T* lhs_t, T* rhs_t) { |
| 222 return CompareStringWithCollator(collator_, (lhs_t->*method_)(), |
| 223 (rhs_t->*method_)()) == UCOL_LESS; |
| 224 } |
| 225 |
| 226 private: |
| 227 Collator* collator_; |
| 228 Method method_; |
| 229 }; |
| 230 |
| 231 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
| 232 // operator (), comparing the string results using <. |
| 233 template <class T, class Method> |
| 234 class StringMethodComparator : public std::binary_function<const std::wstring&, |
| 235 const std::wstring&, |
| 236 bool> { |
| 237 public: |
| 238 explicit StringMethodComparator(Method method) : method_(method) { } |
| 239 |
| 240 // Returns true if lhs preceeds rhs. |
| 241 bool operator() (T* lhs_t, T* rhs_t) { |
| 242 return (lhs_t->*method_)() < (rhs_t->*method_)(); |
| 243 } |
| 244 |
| 245 private: |
| 246 Method method_; |
| 247 }; |
| 248 |
| 249 // Sorts the objects in |elements| using the method |method|, which must return |
| 250 // a string. Sorting is done using a collator, unless a collator can not be |
| 251 // found in which case the strings are sorted using the operator <. |
| 252 template <class T, class Method> |
| 253 void SortStringsUsingMethod(const std::wstring& locale, |
| 254 std::vector<T*>* elements, |
| 255 Method method) { |
| 256 UErrorCode error = U_ZERO_ERROR; |
| 257 Locale loc(WideToUTF8(locale).c_str()); |
| 258 scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); |
| 259 if (U_FAILURE(error)) { |
| 260 sort(elements->begin(), elements->end(), |
| 261 StringMethodComparator<T,Method>(method)); |
| 262 return; |
| 263 } |
| 264 |
| 265 std::sort(elements->begin(), elements->end(), |
| 266 StringMethodComparatorWithCollator<T,Method>(collator.get(), method)); |
| 267 } |
| 268 |
| 201 // Compares two elements' string keys and returns true if the first element's | 269 // Compares two elements' string keys and returns true if the first element's |
| 202 // string key is less than the second element's string key. The Element must | 270 // string key is less than the second element's string key. The Element must |
| 203 // have a method like the follow format to return the string key. | 271 // have a method like the follow format to return the string key. |
| 204 // const std::wstring& GetStringKey() const; | 272 // const std::wstring& GetStringKey() const; |
| 205 // This uses the locale specified in the constructor. | 273 // This uses the locale specified in the constructor. |
| 206 template <class Element> | 274 template <class Element> |
| 207 class StringComparator : public std::binary_function<const Element&, | 275 class StringComparator : public std::binary_function<const Element&, |
| 208 const Element&, | 276 const Element&, |
| 209 bool> { | 277 bool> { |
| 210 public: | 278 public: |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 364 |
| 297 private: | 365 private: |
| 298 UBiDi* bidi_; | 366 UBiDi* bidi_; |
| 299 | 367 |
| 300 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); | 368 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); |
| 301 }; | 369 }; |
| 302 | 370 |
| 303 } | 371 } |
| 304 | 372 |
| 305 #endif // CHROME_COMMON_L10N_UTIL_H_ | 373 #endif // CHROME_COMMON_L10N_UTIL_H_ |
| OLD | NEW |