1 // Copyright 2011 The Chromium Authors
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"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "third_party/icu/source/common/unicode/uchar.h"
14 #include "third_party/icu/source/common/unicode/unistr.h"
15 #include "third_party/icu/source/common/unicode/ustring.h"
22 // Provides a uniform interface for upper/lower/folding which take take
23 // slightly varying parameters.
24 typedef int32_t (*CaseMapperFunction)(UChar* dest, int32_t dest_capacity,
25 const UChar* src, int32_t src_length,
28 int32_t ToUpperMapper(UChar* dest, int32_t dest_capacity,
29 const UChar* src, int32_t src_length,
31 // Use default locale.
32 return u_strToUpper(dest, dest_capacity, src, src_length, nullptr, error);
35 int32_t ToLowerMapper(UChar* dest, int32_t dest_capacity,
36 const UChar* src, int32_t src_length,
38 // Use default locale.
39 return u_strToLower(dest, dest_capacity, src, src_length, nullptr, error);
42 int32_t FoldCaseMapper(UChar* dest, int32_t dest_capacity,
43 const UChar* src, int32_t src_length,
45 return u_strFoldCase(dest, dest_capacity, src, src_length,
46 U_FOLD_CASE_DEFAULT, error);
49 // Provides similar functionality as UnicodeString::caseMap but on
51 std::u16string CaseMap(StringPiece16 string, CaseMapperFunction case_mapper) {
56 // Provide an initial guess that the string length won't change. The typical
57 // strings we use will very rarely change length in this process, so don't
58 // optimize for that case.
59 dest.resize(string.size());
65 // ICU won't terminate the string if there's not enough room for the null
66 // terminator, but will otherwise. So we don't need to save room for that.
67 // Don't use WriteInto, which assumes null terminators.
68 int32_t new_length = case_mapper(
69 &dest[0], saturated_cast<int32_t>(dest.size()), string.data(),
70 saturated_cast<int32_t>(string.size()), &error);
71 dest.resize(new_length);
72 } while (error == U_BUFFER_OVERFLOW_ERROR);
78 std::u16string ToLower(StringPiece16 string) {
79 return CaseMap(string, &ToLowerMapper);
82 std::u16string ToUpper(StringPiece16 string) {
83 return CaseMap(string, &ToUpperMapper);
86 std::u16string FoldCase(StringPiece16 string) {
87 return CaseMap(string, &FoldCaseMapper);