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