michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/strings/utf_string_conversion_utils.h" michael@0: michael@0: #include "base/third_party/icu/icu_utf.h" michael@0: michael@0: namespace base { michael@0: michael@0: // ReadUnicodeCharacter -------------------------------------------------------- michael@0: michael@0: bool ReadUnicodeCharacter(const char* src, michael@0: int32 src_len, michael@0: int32* char_index, michael@0: uint32* code_point_out) { michael@0: // U8_NEXT expects to be able to use -1 to signal an error, so we must michael@0: // use a signed type for code_point. But this function returns false michael@0: // on error anyway, so code_point_out is unsigned. michael@0: int32 code_point; michael@0: CBU8_NEXT(src, *char_index, src_len, code_point); michael@0: *code_point_out = static_cast(code_point); michael@0: michael@0: // The ICU macro above moves to the next char, we want to point to the last michael@0: // char consumed. michael@0: (*char_index)--; michael@0: michael@0: // Validate the decoded value. michael@0: return IsValidCodepoint(code_point); michael@0: } michael@0: michael@0: bool ReadUnicodeCharacter(const char16* src, michael@0: int32 src_len, michael@0: int32* char_index, michael@0: uint32* code_point) { michael@0: if (CBU16_IS_SURROGATE(src[*char_index])) { michael@0: if (!CBU16_IS_SURROGATE_LEAD(src[*char_index]) || michael@0: *char_index + 1 >= src_len || michael@0: !CBU16_IS_TRAIL(src[*char_index + 1])) { michael@0: // Invalid surrogate pair. michael@0: return false; michael@0: } michael@0: michael@0: // Valid surrogate pair. michael@0: *code_point = CBU16_GET_SUPPLEMENTARY(src[*char_index], michael@0: src[*char_index + 1]); michael@0: (*char_index)++; michael@0: } else { michael@0: // Not a surrogate, just one 16-bit word. michael@0: *code_point = src[*char_index]; michael@0: } michael@0: michael@0: return IsValidCodepoint(*code_point); michael@0: } michael@0: michael@0: #if defined(WCHAR_T_IS_UTF32) michael@0: bool ReadUnicodeCharacter(const wchar_t* src, michael@0: int32 src_len, michael@0: int32* char_index, michael@0: uint32* code_point) { michael@0: // Conversion is easy since the source is 32-bit. michael@0: *code_point = src[*char_index]; michael@0: michael@0: // Validate the value. michael@0: return IsValidCodepoint(*code_point); michael@0: } michael@0: #endif // defined(WCHAR_T_IS_UTF32) michael@0: michael@0: // WriteUnicodeCharacter ------------------------------------------------------- michael@0: michael@0: size_t WriteUnicodeCharacter(uint32 code_point, std::string* output) { michael@0: if (code_point <= 0x7f) { michael@0: // Fast path the common case of one byte. michael@0: output->push_back(code_point); michael@0: return 1; michael@0: } michael@0: michael@0: michael@0: // CBU8_APPEND_UNSAFE can append up to 4 bytes. michael@0: size_t char_offset = output->length(); michael@0: size_t original_char_offset = char_offset; michael@0: output->resize(char_offset + CBU8_MAX_LENGTH); michael@0: michael@0: CBU8_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); michael@0: michael@0: // CBU8_APPEND_UNSAFE will advance our pointer past the inserted character, so michael@0: // it will represent the new length of the string. michael@0: output->resize(char_offset); michael@0: return char_offset - original_char_offset; michael@0: } michael@0: michael@0: size_t WriteUnicodeCharacter(uint32 code_point, string16* output) { michael@0: if (CBU16_LENGTH(code_point) == 1) { michael@0: // Thie code point is in the Basic Multilingual Plane (BMP). michael@0: output->push_back(static_cast(code_point)); michael@0: return 1; michael@0: } michael@0: // Non-BMP characters use a double-character encoding. michael@0: size_t char_offset = output->length(); michael@0: output->resize(char_offset + CBU16_MAX_LENGTH); michael@0: CBU16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); michael@0: return CBU16_MAX_LENGTH; michael@0: } michael@0: michael@0: // Generalized Unicode converter ----------------------------------------------- michael@0: michael@0: template michael@0: void PrepareForUTF8Output(const CHAR* src, michael@0: size_t src_len, michael@0: std::string* output) { michael@0: output->clear(); michael@0: if (src_len == 0) michael@0: return; michael@0: if (src[0] < 0x80) { michael@0: // Assume that the entire input will be ASCII. michael@0: output->reserve(src_len); michael@0: } else { michael@0: // Assume that the entire input is non-ASCII and will have 3 bytes per char. michael@0: output->reserve(src_len * 3); michael@0: } michael@0: } michael@0: michael@0: // Instantiate versions we know callers will need. michael@0: template void PrepareForUTF8Output(const wchar_t*, size_t, std::string*); michael@0: template void PrepareForUTF8Output(const char16*, size_t, std::string*); michael@0: michael@0: template michael@0: void PrepareForUTF16Or32Output(const char* src, michael@0: size_t src_len, michael@0: STRING* output) { michael@0: output->clear(); michael@0: if (src_len == 0) michael@0: return; michael@0: if (static_cast(src[0]) < 0x80) { michael@0: // Assume the input is all ASCII, which means 1:1 correspondence. michael@0: output->reserve(src_len); michael@0: } else { michael@0: // Otherwise assume that the UTF-8 sequences will have 2 bytes for each michael@0: // character. michael@0: output->reserve(src_len / 2); michael@0: } michael@0: } michael@0: michael@0: // Instantiate versions we know callers will need. michael@0: template void PrepareForUTF16Or32Output(const char*, size_t, std::wstring*); michael@0: template void PrepareForUTF16Or32Output(const char*, size_t, string16*); michael@0: michael@0: } // namespace base