| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/i18n/case_conversion.h" | |
| 6 | |
| 7 #include "base/numerics/safe_conversions.h" | |
| 8 #include "base/strings/string16.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "third_party/icu/source/common/unicode/uchar.h" | |
| 11 #include "third_party/icu/source/common/unicode/unistr.h" | |
| 12 #include "third_party/icu/source/common/unicode/ustring.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace i18n { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Provides a uniform interface for upper/lower/folding which take take | |
| 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); | |
| 34 } | |
| 35 | |
| 36 int32_t ToLowerMapper(UChar* dest, | |
| 37 int32_t dest_capacity, | |
| 38 const UChar* src, | |
| 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); | |
| 92 } | |
| 93 | |
| 94 } // namespace i18n | |
| 95 } // namespace base | |
| OLD | NEW |