Chromium Code Reviews| Index: net/base/net_string_util_icu.cc |
| =================================================================== |
| --- net/base/net_string_util_icu.cc (revision 0) |
| +++ net/base/net_string_util_icu.cc (revision 0) |
| @@ -0,0 +1,60 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/net_string_util.h" |
| + |
| +#include "base/i18n/i18n_constants.h" |
| +#include "base/i18n/icu_string_conversions.h" |
| +#include "base/strings/string_util.h" |
| +#include "third_party/icu/source/common/unicode/ucnv.h" |
| + |
| +namespace net { |
| + |
| +bool ConvertToUtf8(const std::string& text, const char* charset, |
| + std::string* output) { |
| + UErrorCode err = U_ZERO_ERROR; |
| + UConverter* converter(ucnv_open(charset, &err)); |
| + output->clear(); |
| + if (U_FAILURE(err)) |
| + return false; |
| + |
| + // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8. |
| + // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes |
| + // in UTF-8. Therefore, the expansion ratio is 3 at most. Add one for a |
| + // trailing '\0'. |
| + size_t output_length = text.length() * 3 + 1; |
| + char* buf = WriteInto(output, output_length); |
|
mef
2014/04/28 19:19:51
could it return NULL?
mmenke
2014/04/28 19:29:18
No, see https://code.google.com/p/chromium/codesea
|
| + output_length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, output_length, |
| + text.data(), text.length(), &err); |
| + ucnv_close(converter); |
| + if (U_FAILURE(err)) { |
| + output->clear(); |
| + return false; |
| + } |
| + output->resize(output_length); |
| + return true; |
| +} |
| + |
| +bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset, |
| + std::string* output) { |
| + return base::ConvertToUtf8AndNormalize(text, charset, output); |
| +} |
| + |
| +bool ConvertLatin1ToUtf8AndNormalize(const std::string& text, |
| + std::string* output) { |
| + return net::ConvertToUtf8AndNormalize(text, base::kCodepageLatin1, output); |
| +} |
| + |
| +bool ConvertToUTF16(const std::string& text, const char* charset, |
| + base::string16* output) { |
| + return base::CodepageToUTF16(text, charset, |
| + base::OnStringConversionError::FAIL, output); |
| +} |
| + |
| +bool ConvertLatin1ToUTF16(const std::string& text, base::string16* output) { |
| + return base::CodepageToUTF16(text, base::kCodepageLatin1, |
| + base::OnStringConversionError::FAIL, output); |
| +} |
| + |
| +} // namespace net |
| Property changes on: net\base\net_string_util_icu.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |