| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/net_string_util.h" | 5 #include "net/base/net_string_util.h" |
| 6 | 6 |
| 7 #include "base/i18n/i18n_constants.h" | 7 #include "base/i18n/i18n_constants.h" |
| 8 #include "base/i18n/icu_string_conversions.h" | 8 #include "base/i18n/icu_string_conversions.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "third_party/icu/source/common/unicode/ucnv.h" | 10 #include "third_party/icu/source/common/unicode/ucnv.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 const char* const kLatin1 = base::kCodepageLatin1; |
| 15 |
| 14 bool ConvertToUtf8(const std::string& text, const char* charset, | 16 bool ConvertToUtf8(const std::string& text, const char* charset, |
| 15 std::string* output) { | 17 std::string* output) { |
| 16 output->clear(); | 18 output->clear(); |
| 17 | 19 |
| 18 UErrorCode err = U_ZERO_ERROR; | 20 UErrorCode err = U_ZERO_ERROR; |
| 19 UConverter* converter(ucnv_open(charset, &err)); | 21 UConverter* converter(ucnv_open(charset, &err)); |
| 20 if (U_FAILURE(err)) | 22 if (U_FAILURE(err)) |
| 21 return false; | 23 return false; |
| 22 | 24 |
| 23 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8. | 25 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 36 | 38 |
| 37 output->resize(output_length); | 39 output->resize(output_length); |
| 38 return true; | 40 return true; |
| 39 } | 41 } |
| 40 | 42 |
| 41 bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset, | 43 bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset, |
| 42 std::string* output) { | 44 std::string* output) { |
| 43 return base::ConvertToUtf8AndNormalize(text, charset, output); | 45 return base::ConvertToUtf8AndNormalize(text, charset, output); |
| 44 } | 46 } |
| 45 | 47 |
| 46 bool ConvertLatin1ToUtf8AndNormalize(const std::string& text, | |
| 47 std::string* output) { | |
| 48 return net::ConvertToUtf8AndNormalize(text, base::kCodepageLatin1, output); | |
| 49 } | |
| 50 | |
| 51 bool ConvertToUTF16(const std::string& text, const char* charset, | 48 bool ConvertToUTF16(const std::string& text, const char* charset, |
| 52 base::string16* output) { | 49 base::string16* output) { |
| 53 return base::CodepageToUTF16(text, charset, | 50 return base::CodepageToUTF16(text, charset, |
| 54 base::OnStringConversionError::FAIL, output); | 51 base::OnStringConversionError::FAIL, output); |
| 55 } | 52 } |
| 56 | 53 |
| 57 bool ConvertLatin1ToUTF16(const std::string& text, base::string16* output) { | 54 void ConvertToUTF16WithSubstitutions(const std::string& text, |
| 58 return base::CodepageToUTF16(text, base::kCodepageLatin1, | 55 const char* charset, |
| 59 base::OnStringConversionError::FAIL, output); | 56 base::string16* output) { |
| 57 return base::CodepageToUTF16(bytes, charset, |
| 58 base::OnStringConversionError::SUBSTITUTE, |
| 59 output); |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace net | 62 } // namespace net |
| OLD | NEW |