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.
5 #include "base/i18n/case_conversion.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "third_party/icu/source/common/unicode/uchar.h"
13 #include "third_party/icu/source/common/unicode/unistr.h"
14 #include "third_party/icu/source/common/unicode/ustring.h"
21 // Provides a uniform interface for upper/lower/folding which take take
22 // slightly varying parameters.
23 typedef int32_t (*CaseMapperFunction)(UChar* dest, int32_t dest_capacity,
24 const UChar* src, int32_t src_length,
27 int32_t ToUpperMapper(UChar* dest, int32_t dest_capacity,
28 const UChar* src, int32_t src_length,
30 // Use default locale.
31 return u_strToUpper(dest, dest_capacity, src, src_length, nullptr, error);
34 int32_t ToLowerMapper(UChar* dest, int32_t dest_capacity,
35 const UChar* src, int32_t src_length,
37 // Use default locale.
38 return u_strToLower(dest, dest_capacity, src, src_length, nullptr, error);
41 int32_t FoldCaseMapper(UChar* dest, int32_t dest_capacity,
42 const UChar* src, int32_t src_length,
44 return u_strFoldCase(dest, dest_capacity, src, src_length,
45 U_FOLD_CASE_DEFAULT, error);
48 // Provides similar functionality as UnicodeString::caseMap but on string16.
49 string16 CaseMap(StringPiece16 string, CaseMapperFunction case_mapper) {
54 // Provide an initial guess that the string length won't change. The typical
55 // strings we use will very rarely change length in this process, so don't
56 // optimize for that case.
57 dest.resize(string.size());
63 // ICU won't terminate the string if there's not enough room for the null
64 // terminator, but will otherwise. So we don't need to save room for that.
65 // Don't use WriteInto, which assumes null terminators.
66 int32_t new_length = case_mapper(
67 &dest[0], saturated_cast<int32_t>(dest.size()),
68 string.data(), saturated_cast<int32_t>(string.size()),
70 dest.resize(new_length);
71 } while (error == U_BUFFER_OVERFLOW_ERROR);
77 string16 ToLower(StringPiece16 string) {
78 return CaseMap(string, &ToLowerMapper);
81 string16 ToUpper(StringPiece16 string) {
82 return CaseMap(string, &ToUpperMapper);
85 string16 FoldCase(StringPiece16 string) {
86 return CaseMap(string, &FoldCaseMapper);