| 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 APP_L10N_UTIL_H_ | 8 #ifndef APP_L10N_UTIL_H_ |
| 9 #define APP_L10N_UTIL_H_ | 9 #define APP_L10N_UTIL_H_ |
| 10 | 10 |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 // Returns the default text alignment to be used when drawing text on a | 254 // Returns the default text alignment to be used when drawing text on a |
| 255 // gfx::Canvas based on the directionality of the system locale language. This | 255 // gfx::Canvas based on the directionality of the system locale language. This |
| 256 // function is used by gfx::Canvas::DrawStringInt when the text alignment is | 256 // function is used by gfx::Canvas::DrawStringInt when the text alignment is |
| 257 // not specified. | 257 // not specified. |
| 258 // | 258 // |
| 259 // This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or | 259 // This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or |
| 260 // gfx::Canvas::TEXT_ALIGN_RIGHT. | 260 // gfx::Canvas::TEXT_ALIGN_RIGHT. |
| 261 int DefaultCanvasTextAlignment(); | 261 int DefaultCanvasTextAlignment(); |
| 262 | 262 |
| 263 // Compares the two strings using the specified collator. | 263 // Compares the two strings using the specified collator. |
| 264 UCollationResult CompareStringWithCollator(const Collator* collator, | 264 UCollationResult CompareStringWithCollator(const icu::Collator* collator, |
| 265 const std::wstring& lhs, | 265 const std::wstring& lhs, |
| 266 const std::wstring& rhs); | 266 const std::wstring& rhs); |
| 267 | 267 |
| 268 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to | 268 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
| 269 // operator (), comparing the string results using a collator. | 269 // operator (), comparing the string results using a collator. |
| 270 template <class T, class Method> | 270 template <class T, class Method> |
| 271 class StringMethodComparatorWithCollator : | 271 class StringMethodComparatorWithCollator : |
| 272 public std::binary_function<const std::wstring&, | 272 public std::binary_function<const std::wstring&, |
| 273 const std::wstring&, | 273 const std::wstring&, |
| 274 bool> { | 274 bool> { |
| 275 public: | 275 public: |
| 276 StringMethodComparatorWithCollator(Collator* collator, Method method) | 276 StringMethodComparatorWithCollator(icu::Collator* collator, Method method) |
| 277 : collator_(collator), | 277 : collator_(collator), |
| 278 method_(method) { } | 278 method_(method) { } |
| 279 | 279 |
| 280 // Returns true if lhs preceeds rhs. | 280 // Returns true if lhs preceeds rhs. |
| 281 bool operator() (T* lhs_t, T* rhs_t) { | 281 bool operator() (T* lhs_t, T* rhs_t) { |
| 282 return CompareStringWithCollator(collator_, (lhs_t->*method_)(), | 282 return CompareStringWithCollator(collator_, (lhs_t->*method_)(), |
| 283 (rhs_t->*method_)()) == UCOL_LESS; | 283 (rhs_t->*method_)()) == UCOL_LESS; |
| 284 } | 284 } |
| 285 | 285 |
| 286 private: | 286 private: |
| 287 Collator* collator_; | 287 icu::Collator* collator_; |
| 288 Method method_; | 288 Method method_; |
| 289 }; | 289 }; |
| 290 | 290 |
| 291 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to | 291 // Used by SortStringsUsingMethod. Invokes a method on the objects passed to |
| 292 // operator (), comparing the string results using <. | 292 // operator (), comparing the string results using <. |
| 293 template <class T, class Method> | 293 template <class T, class Method> |
| 294 class StringMethodComparator : public std::binary_function<const std::wstring&, | 294 class StringMethodComparator : public std::binary_function<const std::wstring&, |
| 295 const std::wstring&, | 295 const std::wstring&, |
| 296 bool> { | 296 bool> { |
| 297 public: | 297 public: |
| 298 explicit StringMethodComparator(Method method) : method_(method) { } | 298 explicit StringMethodComparator(Method method) : method_(method) { } |
| 299 | 299 |
| 300 // Returns true if lhs preceeds rhs. | 300 // Returns true if lhs preceeds rhs. |
| 301 bool operator() (T* lhs_t, T* rhs_t) { | 301 bool operator() (T* lhs_t, T* rhs_t) { |
| 302 return (lhs_t->*method_)() < (rhs_t->*method_)(); | 302 return (lhs_t->*method_)() < (rhs_t->*method_)(); |
| 303 } | 303 } |
| 304 | 304 |
| 305 private: | 305 private: |
| 306 Method method_; | 306 Method method_; |
| 307 }; | 307 }; |
| 308 | 308 |
| 309 // Sorts the objects in |elements| using the method |method|, which must return | 309 // Sorts the objects in |elements| using the method |method|, which must return |
| 310 // a string. Sorting is done using a collator, unless a collator can not be | 310 // a string. Sorting is done using a collator, unless a collator can not be |
| 311 // found in which case the strings are sorted using the operator <. | 311 // found in which case the strings are sorted using the operator <. |
| 312 template <class T, class Method> | 312 template <class T, class Method> |
| 313 void SortStringsUsingMethod(const std::wstring& locale, | 313 void SortStringsUsingMethod(const std::wstring& locale, |
| 314 std::vector<T*>* elements, | 314 std::vector<T*>* elements, |
| 315 Method method) { | 315 Method method) { |
| 316 UErrorCode error = U_ZERO_ERROR; | 316 UErrorCode error = U_ZERO_ERROR; |
| 317 Locale loc(WideToUTF8(locale).c_str()); | 317 icu::Locale loc(WideToUTF8(locale).c_str()); |
| 318 scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); | 318 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); |
| 319 if (U_FAILURE(error)) { | 319 if (U_FAILURE(error)) { |
| 320 sort(elements->begin(), elements->end(), | 320 sort(elements->begin(), elements->end(), |
| 321 StringMethodComparator<T,Method>(method)); | 321 StringMethodComparator<T,Method>(method)); |
| 322 return; | 322 return; |
| 323 } | 323 } |
| 324 | 324 |
| 325 std::sort(elements->begin(), elements->end(), | 325 std::sort(elements->begin(), elements->end(), |
| 326 StringMethodComparatorWithCollator<T,Method>(collator.get(), method)); | 326 StringMethodComparatorWithCollator<T,Method>(collator.get(), method)); |
| 327 } | 327 } |
| 328 | 328 |
| 329 // Compares two elements' string keys and returns true if the first element's | 329 // Compares two elements' string keys and returns true if the first element's |
| 330 // string key is less than the second element's string key. The Element must | 330 // string key is less than the second element's string key. The Element must |
| 331 // have a method like the follow format to return the string key. | 331 // have a method like the follow format to return the string key. |
| 332 // const std::wstring& GetStringKey() const; | 332 // const std::wstring& GetStringKey() const; |
| 333 // This uses the locale specified in the constructor. | 333 // This uses the locale specified in the constructor. |
| 334 template <class Element> | 334 template <class Element> |
| 335 class StringComparator : public std::binary_function<const Element&, | 335 class StringComparator : public std::binary_function<const Element&, |
| 336 const Element&, | 336 const Element&, |
| 337 bool> { | 337 bool> { |
| 338 public: | 338 public: |
| 339 explicit StringComparator(Collator* collator) | 339 explicit StringComparator(icu::Collator* collator) |
| 340 : collator_(collator) { } | 340 : collator_(collator) { } |
| 341 | 341 |
| 342 // Returns true if lhs precedes rhs. | 342 // Returns true if lhs precedes rhs. |
| 343 bool operator()(const Element& lhs, const Element& rhs) { | 343 bool operator()(const Element& lhs, const Element& rhs) { |
| 344 const std::wstring& lhs_string_key = lhs.GetStringKey(); | 344 const std::wstring& lhs_string_key = lhs.GetStringKey(); |
| 345 const std::wstring& rhs_string_key = rhs.GetStringKey(); | 345 const std::wstring& rhs_string_key = rhs.GetStringKey(); |
| 346 | 346 |
| 347 return StringComparator<std::wstring>(collator_)(lhs_string_key, | 347 return StringComparator<std::wstring>(collator_)(lhs_string_key, |
| 348 rhs_string_key); | 348 rhs_string_key); |
| 349 } | 349 } |
| 350 | 350 |
| 351 private: | 351 private: |
| 352 Collator* collator_; | 352 icu::Collator* collator_; |
| 353 }; | 353 }; |
| 354 | 354 |
| 355 // Specialization of operator() method for std::wstring version. | 355 // Specialization of operator() method for std::wstring version. |
| 356 template <> | 356 template <> |
| 357 bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, | 357 bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, |
| 358 const std::wstring& rhs); | 358 const std::wstring& rhs); |
| 359 | 359 |
| 360 // In place sorting of |elements| of a vector according to the string key of | 360 // In place sorting of |elements| of a vector according to the string key of |
| 361 // each element in the vector by using collation rules for |locale|. | 361 // each element in the vector by using collation rules for |locale|. |
| 362 // |begin_index| points to the start position of elements in the vector which | 362 // |begin_index| points to the start position of elements in the vector which |
| 363 // want to be sorted. |end_index| points to the end position of elements in the | 363 // want to be sorted. |end_index| points to the end position of elements in the |
| 364 // vector which want to be sorted | 364 // vector which want to be sorted |
| 365 template <class Element> | 365 template <class Element> |
| 366 void SortVectorWithStringKey(const std::string& locale, | 366 void SortVectorWithStringKey(const std::string& locale, |
| 367 std::vector<Element>* elements, | 367 std::vector<Element>* elements, |
| 368 unsigned int begin_index, | 368 unsigned int begin_index, |
| 369 unsigned int end_index, | 369 unsigned int end_index, |
| 370 bool needs_stable_sort) { | 370 bool needs_stable_sort) { |
| 371 DCHECK(begin_index >= 0 && begin_index < end_index && | 371 DCHECK(begin_index >= 0 && begin_index < end_index && |
| 372 end_index <= static_cast<unsigned int>(elements->size())); | 372 end_index <= static_cast<unsigned int>(elements->size())); |
| 373 UErrorCode error = U_ZERO_ERROR; | 373 UErrorCode error = U_ZERO_ERROR; |
| 374 Locale loc(locale.c_str()); | 374 icu::Locale loc(locale.c_str()); |
| 375 scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); | 375 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); |
| 376 if (U_FAILURE(error)) | 376 if (U_FAILURE(error)) |
| 377 collator.reset(); | 377 collator.reset(); |
| 378 StringComparator<Element> c(collator.get()); | 378 StringComparator<Element> c(collator.get()); |
| 379 if (needs_stable_sort) { | 379 if (needs_stable_sort) { |
| 380 stable_sort(elements->begin() + begin_index, | 380 stable_sort(elements->begin() + begin_index, |
| 381 elements->begin() + end_index, | 381 elements->begin() + end_index, |
| 382 c); | 382 c); |
| 383 } else { | 383 } else { |
| 384 sort(elements->begin() + begin_index, elements->begin() + end_index, c); | 384 sort(elements->begin() + begin_index, elements->begin() + end_index, c); |
| 385 } | 385 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 | 425 |
| 426 private: | 426 private: |
| 427 UBiDi* bidi_; | 427 UBiDi* bidi_; |
| 428 | 428 |
| 429 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); | 429 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); |
| 430 }; | 430 }; |
| 431 | 431 |
| 432 } | 432 } |
| 433 | 433 |
| 434 #endif // APP_L10N_UTIL_H_ | 434 #endif // APP_L10N_UTIL_H_ |
| OLD | NEW |