Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / i18n / case_conversion.cc
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
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"
16
17 namespace base {
18 namespace i18n {
19
20 namespace {
21
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,
26                                       UErrorCode* error);
27
28 int32_t ToUpperMapper(UChar* dest, int32_t dest_capacity,
29                       const UChar* src, int32_t src_length,
30                       UErrorCode* error) {
31   // Use default locale.
32   return u_strToUpper(dest, dest_capacity, src, src_length, nullptr, error);
33 }
34
35 int32_t ToLowerMapper(UChar* dest, int32_t dest_capacity,
36                       const UChar* src, int32_t src_length,
37                       UErrorCode* error) {
38   // Use default locale.
39   return u_strToLower(dest, dest_capacity, src, src_length, nullptr, error);
40 }
41
42 int32_t FoldCaseMapper(UChar* dest, int32_t dest_capacity,
43                        const UChar* src, int32_t src_length,
44                        UErrorCode* error) {
45   return u_strFoldCase(dest, dest_capacity, src, src_length,
46                        U_FOLD_CASE_DEFAULT, error);
47 }
48
49 // Provides similar functionality as UnicodeString::caseMap but on
50 // std::u16string.
51 std::u16string CaseMap(StringPiece16 string, CaseMapperFunction case_mapper) {
52   std::u16string dest;
53   if (string.empty())
54     return dest;
55
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());
60
61   UErrorCode error;
62   do {
63     error = U_ZERO_ERROR;
64
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);
73   return dest;
74 }
75
76 }  // namespace
77
78 std::u16string ToLower(StringPiece16 string) {
79   return CaseMap(string, &ToLowerMapper);
80 }
81
82 std::u16string ToUpper(StringPiece16 string) {
83   return CaseMap(string, &ToUpperMapper);
84 }
85
86 std::u16string FoldCase(StringPiece16 string) {
87   return CaseMap(string, &FoldCaseMapper);
88 }
89
90 }  // namespace i18n
91 }  // namespace base