Commit 32ce6166 authored by Claudio DeSouza's avatar Claudio DeSouza Committed by Commit Bot

[base] Use StringPiece for codepage conversion

The basic codepage converion functions in base, namely, CodepageToUTF16,
CodepageToUTF16, and ConvertToUtf8AndNormalize are now taking
StringPiece as the input type. This allows more flexibility when using
theses interfaces, as well as providing the benefit of avoiding
unnecessary string copies.

Change-Id: I5922bd7cc3236f99fbac9393cba61afd9c475af3
Reviewed-on: https://chromium-review.googlesource.com/c/1349652
Commit-Queue: Nico Weber <thakis@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarKoji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610877}
parent 4671e20b
......@@ -80,11 +80,12 @@ void ToUnicodeCallbackSubstitute(const void* context,
// else ignore the reset, close and clone calls.
}
bool ConvertFromUTF16(UConverter* converter, const UChar* uchar_src,
int uchar_len, OnStringConversionError::Type on_error,
bool ConvertFromUTF16(UConverter* converter,
base::StringPiece16 src,
OnStringConversionError::Type on_error,
std::string* encoded) {
int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(uchar_len,
ucnv_getMaxCharSize(converter));
int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(
src.length(), ucnv_getMaxCharSize(converter));
encoded->resize(encoded_max_length);
UErrorCode status = U_ZERO_ERROR;
......@@ -105,8 +106,9 @@ bool ConvertFromUTF16(UConverter* converter, const UChar* uchar_src,
}
// ucnv_fromUChars returns size not including terminating null
int actual_size = ucnv_fromUChars(converter, &(*encoded)[0],
encoded_max_length, uchar_src, uchar_len, &status);
int actual_size =
ucnv_fromUChars(converter, &(*encoded)[0], encoded_max_length, src.data(),
src.length(), &status);
encoded->resize(actual_size);
ucnv_close(converter);
if (U_SUCCESS(status))
......@@ -140,7 +142,7 @@ void SetUpErrorHandlerForToUChars(OnStringConversionError::Type on_error,
// Codepage <-> Wide/UTF-16 ---------------------------------------------------
bool UTF16ToCodepage(const string16& utf16,
bool UTF16ToCodepage(base::StringPiece16 utf16,
const char* codepage_name,
OnStringConversionError::Type on_error,
std::string* encoded) {
......@@ -151,11 +153,10 @@ bool UTF16ToCodepage(const string16& utf16,
if (!U_SUCCESS(status))
return false;
return ConvertFromUTF16(converter, utf16.c_str(),
static_cast<int>(utf16.length()), on_error, encoded);
return ConvertFromUTF16(converter, utf16, on_error, encoded);
}
bool CodepageToUTF16(const std::string& encoded,
bool CodepageToUTF16(base::StringPiece encoded,
const char* codepage_name,
OnStringConversionError::Type on_error,
string16* utf16) {
......@@ -191,13 +192,13 @@ bool CodepageToUTF16(const std::string& encoded,
return true;
}
bool ConvertToUtf8AndNormalize(const std::string& text,
bool ConvertToUtf8AndNormalize(base::StringPiece text,
const std::string& charset,
std::string* result) {
result->clear();
string16 utf16;
if (!CodepageToUTF16(
text, charset.c_str(), OnStringConversionError::FAIL, &utf16))
if (!CodepageToUTF16(text, charset.c_str(), OnStringConversionError::FAIL,
&utf16))
return false;
UErrorCode status = U_ZERO_ERROR;
......
......@@ -10,6 +10,7 @@
#include "base/i18n/base_i18n_export.h"
#include "base/i18n/i18n_constants.h"
#include "base/strings/string16.h"
#include "base/strings/string_piece.h"
namespace base {
......@@ -37,18 +38,18 @@ class OnStringConversionError {
// Converts between UTF-16 strings and the encoding specified. If the
// encoding doesn't exist or the encoding fails (when on_error is FAIL),
// returns false.
BASE_I18N_EXPORT bool UTF16ToCodepage(const string16& utf16,
BASE_I18N_EXPORT bool UTF16ToCodepage(base::StringPiece16 utf16,
const char* codepage_name,
OnStringConversionError::Type on_error,
std::string* encoded);
BASE_I18N_EXPORT bool CodepageToUTF16(const std::string& encoded,
BASE_I18N_EXPORT bool CodepageToUTF16(base::StringPiece encoded,
const char* codepage_name,
OnStringConversionError::Type on_error,
string16* utf16);
// Converts from any codepage to UTF-8 and ensures the resulting UTF-8 is
// normalized.
BASE_I18N_EXPORT bool ConvertToUtf8AndNormalize(const std::string& text,
BASE_I18N_EXPORT bool ConvertToUtf8AndNormalize(base::StringPiece text,
const std::string& charset,
std::string* result);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment