| Index: base/utf_string_conversion_utils.cc
|
| ===================================================================
|
| --- base/utf_string_conversion_utils.cc (revision 31511)
|
| +++ base/utf_string_conversion_utils.cc (working copy)
|
| @@ -2,36 +2,18 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "base/utf_string_conversions.h"
|
| +#include "base/utf_string_conversion_utils.h"
|
|
|
| -#include <vector>
|
| -
|
| -#include "base/basictypes.h"
|
| -#include "base/logging.h"
|
| -#include "base/string_util.h"
|
| #include "base/third_party/icu/icu_utf.h"
|
|
|
| -namespace {
|
| +namespace base {
|
|
|
| -inline bool IsValidCodepoint(uint32 code_point) {
|
| - // Excludes the surrogate code points ([0xD800, 0xDFFF]) and
|
| - // codepoints larger than 0x10FFFF (the highest codepoint allowed).
|
| - // Non-characters and unassigned codepoints are allowed.
|
| - return code_point < 0xD800u ||
|
| - (code_point >= 0xE000u && code_point <= 0x10FFFFu);
|
| -}
|
| -
|
| // ReadUnicodeCharacter --------------------------------------------------------
|
|
|
| -// Reads a UTF-8 stream, placing the next code point into the given output
|
| -// |*code_point|. |src| represents the entire string to read, and |*char_index|
|
| -// is the character offset within the string to start reading at. |*char_index|
|
| -// will be updated to index the last character read, such that incrementing it
|
| -// (as in a for loop) will take the reader to the next character.
|
| -//
|
| -// Returns true on success. On false, |*code_point| will be invalid.
|
| -bool ReadUnicodeCharacter(const char* src, int32 src_len,
|
| - int32* char_index, uint32* code_point_out) {
|
| +bool ReadUnicodeCharacter(const char* src,
|
| + int32 src_len,
|
| + int32* char_index,
|
| + uint32* code_point_out) {
|
| // U8_NEXT expects to be able to use -1 to signal an error, so we must
|
| // use a signed type for code_point. But this function returns false
|
| // on error anyway, so code_point_out is unsigned.
|
| @@ -47,9 +29,10 @@
|
| return IsValidCodepoint(code_point);
|
| }
|
|
|
| -// Reads a UTF-16 character. The usage is the same as the 8-bit version above.
|
| -bool ReadUnicodeCharacter(const char16* src, int32 src_len,
|
| - int32* char_index, uint32* code_point) {
|
| +bool ReadUnicodeCharacter(const char16* src,
|
| + int32 src_len,
|
| + int32* char_index,
|
| + uint32* code_point) {
|
| if (CBU16_IS_SURROGATE(src[*char_index])) {
|
| if (!CBU16_IS_SURROGATE_LEAD(src[*char_index]) ||
|
| *char_index + 1 >= src_len ||
|
| @@ -71,9 +54,10 @@
|
| }
|
|
|
| #if defined(WCHAR_T_IS_UTF32)
|
| -// Reads UTF-32 character. The usage is the same as the 8-bit version above.
|
| -bool ReadUnicodeCharacter(const wchar_t* src, int32 src_len,
|
| - int32* char_index, uint32* code_point) {
|
| +bool ReadUnicodeCharacter(const wchar_t* src,
|
| + int32 src_len,
|
| + int32* char_index,
|
| + uint32* code_point) {
|
| // Conversion is easy since the source is 32-bit.
|
| *code_point = src[*char_index];
|
|
|
| @@ -84,8 +68,6 @@
|
|
|
| // WriteUnicodeCharacter -------------------------------------------------------
|
|
|
| -// Appends a UTF-8 character to the given 8-bit string. Returns the number of
|
| -// bytes written.
|
| size_t WriteUnicodeCharacter(uint32 code_point, std::string* output) {
|
| if (code_point <= 0x7f) {
|
| // Fast path the common case of one byte.
|
| @@ -93,6 +75,7 @@
|
| return 1;
|
| }
|
|
|
| +
|
| // CBU8_APPEND_UNSAFE can append up to 4 bytes.
|
| size_t char_offset = output->length();
|
| size_t original_char_offset = char_offset;
|
| @@ -106,8 +89,6 @@
|
| return char_offset - original_char_offset;
|
| }
|
|
|
| -// Appends the given code point as a UTF-16 character to the given 16-bit
|
| -// string. Returns the number of 16-bit values written.
|
| size_t WriteUnicodeCharacter(uint32 code_point, string16* output) {
|
| if (CBU16_LENGTH(code_point) == 1) {
|
| // Thie code point is in the Basic Multilingual Plane (BMP).
|
| @@ -121,67 +102,8 @@
|
| return CBU16_MAX_LENGTH;
|
| }
|
|
|
| -#if defined(WCHAR_T_IS_UTF32)
|
| -// Appends the given UTF-32 character to the given 32-bit string. Returns the
|
| -// number of 32-bit values written.
|
| -inline size_t WriteUnicodeCharacter(uint32 code_point, std::wstring* output) {
|
| - // This is the easy case, just append the character.
|
| - output->push_back(code_point);
|
| - return 1;
|
| -}
|
| -#endif // defined(WCHAR_T_IS_UTF32)
|
| -
|
| // Generalized Unicode converter -----------------------------------------------
|
|
|
| -// Converts the given source Unicode character type to the given destination
|
| -// Unicode character type as a STL string. The given input buffer and size
|
| -// determine the source, and the given output STL string will be replaced by
|
| -// the result.
|
| -template<typename SRC_CHAR, typename DEST_STRING>
|
| -bool ConvertUnicode(const SRC_CHAR* src,
|
| - size_t src_len,
|
| - DEST_STRING* output,
|
| - size_t* offset_for_adjustment) {
|
| - size_t output_offset =
|
| - (offset_for_adjustment && *offset_for_adjustment < src_len) ?
|
| - *offset_for_adjustment : DEST_STRING::npos;
|
| -
|
| - // ICU requires 32-bit numbers.
|
| - bool success = true;
|
| - int32 src_len32 = static_cast<int32>(src_len);
|
| - for (int32 i = 0; i < src_len32; i++) {
|
| - uint32 code_point;
|
| - size_t original_i = i;
|
| - size_t chars_written = 0;
|
| - if (ReadUnicodeCharacter(src, src_len32, &i, &code_point)) {
|
| - chars_written = WriteUnicodeCharacter(code_point, output);
|
| - } else {
|
| - // TODO(jungshik): consider adding 'Replacement character' (U+FFFD)
|
| - // in place of an invalid codepoint.
|
| - success = false;
|
| - }
|
| - if ((output_offset != DEST_STRING::npos) &&
|
| - (*offset_for_adjustment > original_i)) {
|
| - // NOTE: ReadUnicodeCharacter() adjusts |i| to point _at_ the last
|
| - // character read, not after it (so that incrementing it in the loop
|
| - // increment will place it at the right location), so we need to account
|
| - // for that in determining the amount that was read.
|
| - if (*offset_for_adjustment <= static_cast<size_t>(i))
|
| - output_offset = DEST_STRING::npos;
|
| - else
|
| - output_offset += chars_written - (i - original_i + 1);
|
| - }
|
| - }
|
| -
|
| - if (offset_for_adjustment)
|
| - *offset_for_adjustment = output_offset;
|
| - return success;
|
| -}
|
| -
|
| -// Guesses the length of the output in UTF-8 in bytes, clears that output
|
| -// string, and reserves that amount of space. We assume that the input
|
| -// character types are unsigned, which will be true for UTF-16 and -32 on our
|
| -// systems.
|
| template<typename CHAR>
|
| void PrepareForUTF8Output(const CHAR* src,
|
| size_t src_len,
|
| @@ -198,8 +120,10 @@
|
| }
|
| }
|
|
|
| -// Prepares an output buffer (containing either UTF-16 or -32 data) given some
|
| -// UTF-8 input that will be converted to it. See PrepareForUTF8Output().
|
| +// Instantiate versions we know callers will need.
|
| +template void PrepareForUTF8Output(const wchar_t*, size_t, std::string*);
|
| +template void PrepareForUTF8Output(const char16*, size_t, std::string*);
|
| +
|
| template<typename STRING>
|
| void PrepareForUTF16Or32Output(const char* src,
|
| size_t src_len,
|
| @@ -217,156 +141,8 @@
|
| }
|
| }
|
|
|
| -} // namespace
|
| +// Instantiate versions we know callers will need.
|
| +template void PrepareForUTF16Or32Output(const char*, size_t, std::wstring*);
|
| +template void PrepareForUTF16Or32Output(const char*, size_t, string16*);
|
|
|
| -// UTF-8 <-> Wide --------------------------------------------------------------
|
| -
|
| -bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) {
|
| - PrepareForUTF8Output(src, src_len, output);
|
| - return ConvertUnicode<wchar_t, std::string>(src, src_len, output, NULL);
|
| -}
|
| -
|
| -std::string WideToUTF8(const std::wstring& wide) {
|
| - std::string ret;
|
| - // Ignore the success flag of this call, it will do the best it can for
|
| - // invalid input, which is what we want here.
|
| - WideToUTF8(wide.data(), wide.length(), &ret);
|
| - return ret;
|
| -}
|
| -
|
| -bool UTF8ToWideAndAdjustOffset(const char* src,
|
| - size_t src_len,
|
| - std::wstring* output,
|
| - size_t* offset_for_adjustment) {
|
| - PrepareForUTF16Or32Output(src, src_len, output);
|
| - return ConvertUnicode<char, std::wstring>(src, src_len, output,
|
| - offset_for_adjustment);
|
| -}
|
| -
|
| -std::wstring UTF8ToWideAndAdjustOffset(const base::StringPiece& utf8,
|
| - size_t* offset_for_adjustment) {
|
| - std::wstring ret;
|
| - UTF8ToWideAndAdjustOffset(utf8.data(), utf8.length(), &ret,
|
| - offset_for_adjustment);
|
| - return ret;
|
| -}
|
| -
|
| -// UTF-16 <-> Wide -------------------------------------------------------------
|
| -
|
| -#if defined(WCHAR_T_IS_UTF16)
|
| -
|
| -// When wide == UTF-16, then conversions are a NOP.
|
| -bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) {
|
| - output->assign(src, src_len);
|
| - return true;
|
| -}
|
| -
|
| -string16 WideToUTF16(const std::wstring& wide) {
|
| - return wide;
|
| -}
|
| -
|
| -bool UTF16ToWideAndAdjustOffset(const char16* src,
|
| - size_t src_len,
|
| - std::wstring* output,
|
| - size_t* offset_for_adjustment) {
|
| - output->assign(src, src_len);
|
| - if (offset_for_adjustment && (*offset_for_adjustment >= src_len))
|
| - *offset_for_adjustment = std::wstring::npos;
|
| - return true;
|
| -}
|
| -
|
| -std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16,
|
| - size_t* offset_for_adjustment) {
|
| - if (offset_for_adjustment && (*offset_for_adjustment >= utf16.length()))
|
| - *offset_for_adjustment = std::wstring::npos;
|
| - return utf16;
|
| -}
|
| -
|
| -#elif defined(WCHAR_T_IS_UTF32)
|
| -
|
| -bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) {
|
| - output->clear();
|
| - // Assume that normally we won't have any non-BMP characters so the counts
|
| - // will be the same.
|
| - output->reserve(src_len);
|
| - return ConvertUnicode<wchar_t, string16>(src, src_len, output, NULL);
|
| -}
|
| -
|
| -string16 WideToUTF16(const std::wstring& wide) {
|
| - string16 ret;
|
| - WideToUTF16(wide.data(), wide.length(), &ret);
|
| - return ret;
|
| -}
|
| -
|
| -bool UTF16ToWideAndAdjustOffset(const char16* src,
|
| - size_t src_len,
|
| - std::wstring* output,
|
| - size_t* offset_for_adjustment) {
|
| - output->clear();
|
| - // Assume that normally we won't have any non-BMP characters so the counts
|
| - // will be the same.
|
| - output->reserve(src_len);
|
| - return ConvertUnicode<char16, std::wstring>(src, src_len, output,
|
| - offset_for_adjustment);
|
| -}
|
| -
|
| -std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16,
|
| - size_t* offset_for_adjustment) {
|
| - std::wstring ret;
|
| - UTF16ToWideAndAdjustOffset(utf16.data(), utf16.length(), &ret,
|
| - offset_for_adjustment);
|
| - return ret;
|
| -}
|
| -
|
| -#endif // defined(WCHAR_T_IS_UTF32)
|
| -
|
| -// UTF16 <-> UTF8 --------------------------------------------------------------
|
| -
|
| -#if defined(WCHAR_T_IS_UTF32)
|
| -
|
| -bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
|
| - PrepareForUTF16Or32Output(src, src_len, output);
|
| - return ConvertUnicode<char, string16>(src, src_len, output, NULL);
|
| -}
|
| -
|
| -string16 UTF8ToUTF16(const std::string& utf8) {
|
| - string16 ret;
|
| - // Ignore the success flag of this call, it will do the best it can for
|
| - // invalid input, which is what we want here.
|
| - UTF8ToUTF16(utf8.data(), utf8.length(), &ret);
|
| - return ret;
|
| -}
|
| -
|
| -bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
|
| - PrepareForUTF8Output(src, src_len, output);
|
| - return ConvertUnicode<char16, std::string>(src, src_len, output, NULL);
|
| -}
|
| -
|
| -std::string UTF16ToUTF8(const string16& utf16) {
|
| - std::string ret;
|
| - // Ignore the success flag of this call, it will do the best it can for
|
| - // invalid input, which is what we want here.
|
| - UTF16ToUTF8(utf16.data(), utf16.length(), &ret);
|
| - return ret;
|
| -}
|
| -
|
| -#elif defined(WCHAR_T_IS_UTF16)
|
| -// Easy case since we can use the "wide" versions we already wrote above.
|
| -
|
| -bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
|
| - return UTF8ToWide(src, src_len, output);
|
| -}
|
| -
|
| -string16 UTF8ToUTF16(const std::string& utf8) {
|
| - return UTF8ToWide(utf8);
|
| -}
|
| -
|
| -bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
|
| - return WideToUTF8(src, src_len, output);
|
| -}
|
| -
|
| -std::string UTF16ToUTF8(const string16& utf16) {
|
| - return WideToUTF8(utf16);
|
| -}
|
| -
|
| -#endif
|
| +} // namespace base
|
|
|