1 // Copyright 2014 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 "net/base/net_string_util.h"
6
7 #include <string_view>
8
9 #include "base/i18n/case_conversion.h"
10 #include "base/i18n/i18n_constants.h"
11 #include "base/i18n/icu_string_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "third_party/icu/source/common/unicode/ucnv.h"
14
15 namespace net {
16
17 const char* const kCharsetLatin1 = base::kCodepageLatin1;
18
ConvertToUtf8(std::string_view text,const char * charset,std::string * output)19 bool ConvertToUtf8(std::string_view text,
20 const char* charset,
21 std::string* output) {
22 output->clear();
23
24 UErrorCode err = U_ZERO_ERROR;
25 UConverter* converter(ucnv_open(charset, &err));
26 if (U_FAILURE(err))
27 return false;
28
29 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8.
30 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes
31 // in UTF-8. Therefore, the expansion ratio is 3 at most.
32 output->resize(text.length() * 3);
33 size_t output_length =
34 ucnv_toAlgorithmic(UCNV_UTF8, converter, output->data(), output->length(),
35 text.data(), text.length(), &err);
36 ucnv_close(converter);
37 if (U_FAILURE(err)) {
38 output->clear();
39 return false;
40 }
41
42 output->resize(output_length);
43 return true;
44 }
45
ConvertToUtf8AndNormalize(std::string_view text,const char * charset,std::string * output)46 bool ConvertToUtf8AndNormalize(std::string_view text,
47 const char* charset,
48 std::string* output) {
49 return base::ConvertToUtf8AndNormalize(text, charset, output);
50 }
51
ConvertToUTF16(std::string_view text,const char * charset,std::u16string * output)52 bool ConvertToUTF16(std::string_view text,
53 const char* charset,
54 std::u16string* output) {
55 return base::CodepageToUTF16(text, charset,
56 base::OnStringConversionError::FAIL, output);
57 }
58
ConvertToUTF16WithSubstitutions(std::string_view text,const char * charset,std::u16string * output)59 bool ConvertToUTF16WithSubstitutions(std::string_view text,
60 const char* charset,
61 std::u16string* output) {
62 return base::CodepageToUTF16(
63 text, charset, base::OnStringConversionError::SUBSTITUTE, output);
64 }
65
ToUpper(std::u16string_view str,std::u16string * output)66 bool ToUpper(std::u16string_view str, std::u16string* output) {
67 *output = base::i18n::ToUpper(str);
68 return true;
69 }
70
71 } // namespace net
72