-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <memory>
-#include <vector>
-#include "base/logging.h"
+#include "base/check.h"
+#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/icu/source/common/unicode/normalizer2.h"
// 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;
ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, nullptr,
nullptr, nullptr, &status);
break;
- default:
- NOTREACHED();
}
// 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))
ucnv_setToUCallBack(converter, ToUnicodeCallbackSubstitute, nullptr,
nullptr, nullptr, status);
break;
- default:
- NOTREACHED();
}
}
// 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) {
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) {
+ std::u16string* utf16) {
utf16->clear();
UErrorCode status = U_ZERO_ERROR;
size_t uchar_max_length = encoded.length() + 1;
SetUpErrorHandlerForToUChars(on_error, converter, &status);
- std::unique_ptr<char16[]> buffer(new char16[uchar_max_length]);
- int actual_size = ucnv_toUChars(converter, buffer.get(),
- static_cast<int>(uchar_max_length), encoded.data(),
- static_cast<int>(encoded.length()), &status);
+ std::unique_ptr<char16_t[]> buffer(new char16_t[uchar_max_length]);
+ int actual_size = ucnv_toUChars(
+ converter, buffer.get(), static_cast<int>(uchar_max_length),
+ encoded.data(), static_cast<int>(encoded.length()), &status);
ucnv_close(converter);
if (!U_SUCCESS(status)) {
utf16->clear(); // Make sure the output is empty on error.
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))
+ std::u16string utf16;
+ if (!CodepageToUTF16(text, charset.c_str(), OnStringConversionError::FAIL,
+ &utf16))
return false;
UErrorCode status = U_ZERO_ERROR;