• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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. Add one for a
32   // trailing '\0'.
33   size_t output_length = text.length() * 3 + 1;
34   char* buf = base::WriteInto(output, output_length);
35   output_length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, output_length,
36                                      text.data(), text.length(), &err);
37   ucnv_close(converter);
38   if (U_FAILURE(err)) {
39     output->clear();
40     return false;
41   }
42 
43   output->resize(output_length);
44   return true;
45 }
46 
ConvertToUtf8AndNormalize(std::string_view text,const char * charset,std::string * output)47 bool ConvertToUtf8AndNormalize(std::string_view text,
48                                const char* charset,
49                                std::string* output) {
50   return base::ConvertToUtf8AndNormalize(text, charset, output);
51 }
52 
ConvertToUTF16(std::string_view text,const char * charset,std::u16string * output)53 bool ConvertToUTF16(std::string_view text,
54                     const char* charset,
55                     std::u16string* output) {
56   return base::CodepageToUTF16(text, charset,
57                                base::OnStringConversionError::FAIL, output);
58 }
59 
ConvertToUTF16WithSubstitutions(std::string_view text,const char * charset,std::u16string * output)60 bool ConvertToUTF16WithSubstitutions(std::string_view text,
61                                      const char* charset,
62                                      std::u16string* output) {
63   return base::CodepageToUTF16(
64       text, charset, base::OnStringConversionError::SUBSTITUTE, output);
65 }
66 
ToUpper(std::u16string_view str,std::u16string * output)67 bool ToUpper(std::u16string_view str, std::u16string* output) {
68   *output = base::i18n::ToUpper(str);
69   return true;
70 }
71 
72 }  // namespace net
73