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