| 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 bool ConvertToUtf8(const std::string& text, const char* charset, | 14 bool ConvertToUtf8(const std::string& text, |
| 15 const char* charset, |
| 15 std::string* output) { | 16 std::string* output) { |
| 16 output->clear(); | 17 output->clear(); |
| 17 | 18 |
| 18 UErrorCode err = U_ZERO_ERROR; | 19 UErrorCode err = U_ZERO_ERROR; |
| 19 UConverter* converter(ucnv_open(charset, &err)); | 20 UConverter* converter(ucnv_open(charset, &err)); |
| 20 if (U_FAILURE(err)) | 21 if (U_FAILURE(err)) |
| 21 return false; | 22 return false; |
| 22 | 23 |
| 23 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8. | 24 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8. |
| 24 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes | 25 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes |
| 25 // in UTF-8. Therefore, the expansion ratio is 3 at most. Add one for a | 26 // in UTF-8. Therefore, the expansion ratio is 3 at most. Add one for a |
| 26 // trailing '\0'. | 27 // trailing '\0'. |
| 27 size_t output_length = text.length() * 3 + 1; | 28 size_t output_length = text.length() * 3 + 1; |
| 28 char* buf = WriteInto(output, output_length); | 29 char* buf = WriteInto(output, output_length); |
| 29 output_length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, output_length, | 30 output_length = ucnv_toAlgorithmic(UCNV_UTF8, |
| 30 text.data(), text.length(), &err); | 31 converter, |
| 32 buf, |
| 33 output_length, |
| 34 text.data(), |
| 35 text.length(), |
| 36 &err); |
| 31 ucnv_close(converter); | 37 ucnv_close(converter); |
| 32 if (U_FAILURE(err)) { | 38 if (U_FAILURE(err)) { |
| 33 output->clear(); | 39 output->clear(); |
| 34 return false; | 40 return false; |
| 35 } | 41 } |
| 36 | 42 |
| 37 output->resize(output_length); | 43 output->resize(output_length); |
| 38 return true; | 44 return true; |
| 39 } | 45 } |
| 40 | 46 |
| 41 bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset, | 47 bool ConvertToUtf8AndNormalize(const std::string& text, |
| 48 const char* charset, |
| 42 std::string* output) { | 49 std::string* output) { |
| 43 return base::ConvertToUtf8AndNormalize(text, charset, output); | 50 return base::ConvertToUtf8AndNormalize(text, charset, output); |
| 44 } | 51 } |
| 45 | 52 |
| 46 bool ConvertLatin1ToUtf8AndNormalize(const std::string& text, | 53 bool ConvertLatin1ToUtf8AndNormalize(const std::string& text, |
| 47 std::string* output) { | 54 std::string* output) { |
| 48 return net::ConvertToUtf8AndNormalize(text, base::kCodepageLatin1, output); | 55 return net::ConvertToUtf8AndNormalize(text, base::kCodepageLatin1, output); |
| 49 } | 56 } |
| 50 | 57 |
| 51 bool ConvertToUTF16(const std::string& text, const char* charset, | 58 bool ConvertToUTF16(const std::string& text, |
| 59 const char* charset, |
| 52 base::string16* output) { | 60 base::string16* output) { |
| 53 return base::CodepageToUTF16(text, charset, | 61 return base::CodepageToUTF16( |
| 54 base::OnStringConversionError::FAIL, output); | 62 text, charset, base::OnStringConversionError::FAIL, output); |
| 55 } | 63 } |
| 56 | 64 |
| 57 bool ConvertLatin1ToUTF16(const std::string& text, base::string16* output) { | 65 bool ConvertLatin1ToUTF16(const std::string& text, base::string16* output) { |
| 58 return base::CodepageToUTF16(text, base::kCodepageLatin1, | 66 return base::CodepageToUTF16( |
| 59 base::OnStringConversionError::FAIL, output); | 67 text, base::kCodepageLatin1, base::OnStringConversionError::FAIL, output); |
| 60 } | 68 } |
| 61 | 69 |
| 62 } // namespace net | 70 } // namespace net |
| OLD | NEW |