| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // File utilities that use the ICU library go in this file. | 5 // File utilities that use the ICU library go in this file. |
| 6 | 6 |
| 7 #include "base/i18n/file_util_icu.h" | 7 #include "base/i18n/file_util_icu.h" |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 static LocaleAwareComparator* GetInstance() { | 83 static LocaleAwareComparator* GetInstance() { |
| 84 return Singleton<LocaleAwareComparator>::get(); | 84 return Singleton<LocaleAwareComparator>::get(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Note: A similar function is available in l10n_util. | 87 // Note: A similar function is available in l10n_util. |
| 88 // We cannot use it because base should not depend on l10n_util. | 88 // We cannot use it because base should not depend on l10n_util. |
| 89 // TODO(yuzo): Move some of l10n_util to base. | 89 // TODO(yuzo): Move some of l10n_util to base. |
| 90 int Compare(const string16& a, const string16& b) { | 90 int Compare(const string16& a, const string16& b) { |
| 91 // We are not sure if Collator::compare is thread-safe. | 91 // We are not sure if Collator::compare is thread-safe. |
| 92 // Use an AutoLock just in case. | 92 // Use an AutoLock just in case. |
| 93 AutoLock auto_lock(lock_); | 93 base::AutoLock auto_lock(lock_); |
| 94 | 94 |
| 95 UErrorCode error_code = U_ZERO_ERROR; | 95 UErrorCode error_code = U_ZERO_ERROR; |
| 96 UCollationResult result = collator_->compare( | 96 UCollationResult result = collator_->compare( |
| 97 static_cast<const UChar*>(a.c_str()), | 97 static_cast<const UChar*>(a.c_str()), |
| 98 static_cast<int>(a.length()), | 98 static_cast<int>(a.length()), |
| 99 static_cast<const UChar*>(b.c_str()), | 99 static_cast<const UChar*>(b.c_str()), |
| 100 static_cast<int>(b.length()), | 100 static_cast<int>(b.length()), |
| 101 error_code); | 101 error_code); |
| 102 DCHECK(U_SUCCESS(error_code)); | 102 DCHECK(U_SUCCESS(error_code)); |
| 103 return result; | 103 return result; |
| 104 } | 104 } |
| 105 | 105 |
| 106 private: | 106 private: |
| 107 LocaleAwareComparator() { | 107 LocaleAwareComparator() { |
| 108 UErrorCode error_code = U_ZERO_ERROR; | 108 UErrorCode error_code = U_ZERO_ERROR; |
| 109 // Use the default collator. The default locale should have been properly | 109 // Use the default collator. The default locale should have been properly |
| 110 // set by the time this constructor is called. | 110 // set by the time this constructor is called. |
| 111 collator_.reset(icu::Collator::createInstance(error_code)); | 111 collator_.reset(icu::Collator::createInstance(error_code)); |
| 112 DCHECK(U_SUCCESS(error_code)); | 112 DCHECK(U_SUCCESS(error_code)); |
| 113 // Make it case-sensitive. | 113 // Make it case-sensitive. |
| 114 collator_->setStrength(icu::Collator::TERTIARY); | 114 collator_->setStrength(icu::Collator::TERTIARY); |
| 115 // Note: We do not set UCOL_NORMALIZATION_MODE attribute. In other words, we | 115 // Note: We do not set UCOL_NORMALIZATION_MODE attribute. In other words, we |
| 116 // do not pay performance penalty to guarantee sort order correctness for | 116 // do not pay performance penalty to guarantee sort order correctness for |
| 117 // non-FCD (http://unicode.org/notes/tn5/#FCD) file names. This should be a | 117 // non-FCD (http://unicode.org/notes/tn5/#FCD) file names. This should be a |
| 118 // reasonable tradeoff because such file names should be rare and the sort | 118 // reasonable tradeoff because such file names should be rare and the sort |
| 119 // order doesn't change much anyway. | 119 // order doesn't change much anyway. |
| 120 } | 120 } |
| 121 | 121 |
| 122 scoped_ptr<icu::Collator> collator_; | 122 scoped_ptr<icu::Collator> collator_; |
| 123 Lock lock_; | 123 base::Lock lock_; |
| 124 friend struct DefaultSingletonTraits<LocaleAwareComparator>; | 124 friend struct DefaultSingletonTraits<LocaleAwareComparator>; |
| 125 | 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(LocaleAwareComparator); | 126 DISALLOW_COPY_AND_ASSIGN(LocaleAwareComparator); |
| 127 }; | 127 }; |
| 128 | 128 |
| 129 } // namespace | 129 } // namespace |
| 130 | 130 |
| 131 namespace file_util { | 131 namespace file_util { |
| 132 | 132 |
| 133 bool IsFilenameLegal(const string16& file_name) { | 133 bool IsFilenameLegal(const string16& file_name) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 // TODO(yuzo): Perhaps we should define SysNativeMBToUTF16? | 191 // TODO(yuzo): Perhaps we should define SysNativeMBToUTF16? |
| 192 return LocaleAwareComparator::GetInstance()->Compare( | 192 return LocaleAwareComparator::GetInstance()->Compare( |
| 193 WideToUTF16(base::SysNativeMBToWide(a.value().c_str())), | 193 WideToUTF16(base::SysNativeMBToWide(a.value().c_str())), |
| 194 WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))) < 0; | 194 WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))) < 0; |
| 195 #else | 195 #else |
| 196 #error Not implemented on your system | 196 #error Not implemented on your system |
| 197 #endif | 197 #endif |
| 198 } | 198 } |
| 199 | 199 |
| 200 } // namespace | 200 } // namespace |
| OLD | NEW |