OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/i18n/case_conversion.h" | 5 #include "base/i18n/case_conversion.h" |
6 | 6 |
| 7 #include "base/numerics/safe_conversions.h" |
7 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/string_util.h" |
| 10 #include "third_party/icu/source/common/unicode/uchar.h" |
8 #include "third_party/icu/source/common/unicode/unistr.h" | 11 #include "third_party/icu/source/common/unicode/unistr.h" |
| 12 #include "third_party/icu/source/common/unicode/ustring.h" |
9 | 13 |
10 namespace base { | 14 namespace base { |
11 namespace i18n { | 15 namespace i18n { |
12 | 16 |
13 string16 ToLower(const StringPiece16& string) { | 17 namespace { |
14 icu::UnicodeString unicode_string(string.data(), string.size()); | 18 |
15 unicode_string.toLower(); | 19 // Provides a uniform interface for upper/lower/folding which take take |
16 return string16(unicode_string.getBuffer(), unicode_string.length()); | 20 // slightly varying parameters. |
| 21 typedef int32_t (*CaseMapperFunction)(UChar* dest, |
| 22 int32_t dest_capacity, |
| 23 const UChar* src, |
| 24 int32_t src_length, |
| 25 UErrorCode* error); |
| 26 |
| 27 int32_t ToUpperMapper(UChar* dest, |
| 28 int32_t dest_capacity, |
| 29 const UChar* src, |
| 30 int32_t src_length, |
| 31 UErrorCode* error) { |
| 32 // Use default locale. |
| 33 return u_strToUpper(dest, dest_capacity, src, src_length, NULL, error); |
17 } | 34 } |
18 | 35 |
19 string16 ToUpper(const StringPiece16& string) { | 36 int32_t ToLowerMapper(UChar* dest, |
20 icu::UnicodeString unicode_string(string.data(), string.size()); | 37 int32_t dest_capacity, |
21 unicode_string.toUpper(); | 38 const UChar* src, |
22 return string16(unicode_string.getBuffer(), unicode_string.length()); | 39 int32_t src_length, |
| 40 UErrorCode* error) { |
| 41 // Use default locale. |
| 42 return u_strToLower(dest, dest_capacity, src, src_length, NULL, error); |
| 43 } |
| 44 |
| 45 int32_t FoldCaseMapper(UChar* dest, |
| 46 int32_t dest_capacity, |
| 47 const UChar* src, |
| 48 int32_t src_length, |
| 49 UErrorCode* error) { |
| 50 return u_strFoldCase(dest, dest_capacity, src, src_length, |
| 51 U_FOLD_CASE_DEFAULT, error); |
| 52 } |
| 53 |
| 54 // Provides similar functionality as UnicodeString::caseMap but on string16. |
| 55 string16 CaseMap(StringPiece16 string, CaseMapperFunction case_mapper) { |
| 56 string16 dest; |
| 57 if (string.empty()) |
| 58 return dest; |
| 59 |
| 60 // Provide an initial guess that the string length won't change. The typical |
| 61 // strings we use will very rarely change length in this process, so don't |
| 62 // optimize for that case. |
| 63 dest.resize(string.size()); |
| 64 |
| 65 UErrorCode error; |
| 66 do { |
| 67 error = U_ZERO_ERROR; |
| 68 |
| 69 // ICU won't terminate the string if there's not enough room for the null |
| 70 // terminator, but will otherwise. So we don't need to save room for that. |
| 71 // Don't use WriteInto, which assumes null terminators. |
| 72 int32_t new_length = case_mapper( |
| 73 &dest[0], saturated_cast<int32_t>(dest.size()), string.data(), |
| 74 saturated_cast<int32_t>(string.size()), &error); |
| 75 dest.resize(new_length); |
| 76 } while (error == U_BUFFER_OVERFLOW_ERROR); |
| 77 return dest; |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 string16 ToLower(StringPiece16 string) { |
| 83 return CaseMap(string, &ToLowerMapper); |
| 84 } |
| 85 |
| 86 string16 ToUpper(StringPiece16 string) { |
| 87 return CaseMap(string, &ToUpperMapper); |
| 88 } |
| 89 |
| 90 string16 FoldCase(StringPiece16 string) { |
| 91 return CaseMap(string, &FoldCaseMapper); |
23 } | 92 } |
24 | 93 |
25 } // namespace i18n | 94 } // namespace i18n |
26 } // namespace base | 95 } // namespace base |
OLD | NEW |