| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/strings/utf_string_conversion_utils.h" | 5 #include "base/strings/utf_string_conversion_utils.h" |
| 6 | 6 |
| 7 #include "base/third_party/icu/icu_utf.h" | 7 #include "base/third_party/icu/icu_utf.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 // ReadUnicodeCharacter -------------------------------------------------------- | 11 // ReadUnicodeCharacter -------------------------------------------------------- |
| 12 | 12 |
| 13 bool ReadUnicodeCharacter(const char* src, | 13 bool ReadUnicodeCharacter(const char* src, |
| 14 int32 src_len, | 14 int32_t src_len, |
| 15 int32* char_index, | 15 int32_t* char_index, |
| 16 uint32* code_point_out) { | 16 uint32_t* code_point_out) { |
| 17 // U8_NEXT expects to be able to use -1 to signal an error, so we must | 17 // U8_NEXT expects to be able to use -1 to signal an error, so we must |
| 18 // use a signed type for code_point. But this function returns false | 18 // use a signed type for code_point. But this function returns false |
| 19 // on error anyway, so code_point_out is unsigned. | 19 // on error anyway, so code_point_out is unsigned. |
| 20 int32 code_point; | 20 int32_t code_point; |
| 21 CBU8_NEXT(src, *char_index, src_len, code_point); | 21 CBU8_NEXT(src, *char_index, src_len, code_point); |
| 22 *code_point_out = static_cast<uint32>(code_point); | 22 *code_point_out = static_cast<uint32_t>(code_point); |
| 23 | 23 |
| 24 // The ICU macro above moves to the next char, we want to point to the last | 24 // The ICU macro above moves to the next char, we want to point to the last |
| 25 // char consumed. | 25 // char consumed. |
| 26 (*char_index)--; | 26 (*char_index)--; |
| 27 | 27 |
| 28 // Validate the decoded value. | 28 // Validate the decoded value. |
| 29 return IsValidCodepoint(code_point); | 29 return IsValidCodepoint(code_point); |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool ReadUnicodeCharacter(const char16* src, | 32 bool ReadUnicodeCharacter(const char16* src, |
| 33 int32 src_len, | 33 int32_t src_len, |
| 34 int32* char_index, | 34 int32_t* char_index, |
| 35 uint32* code_point) { | 35 uint32_t* code_point) { |
| 36 if (CBU16_IS_SURROGATE(src[*char_index])) { | 36 if (CBU16_IS_SURROGATE(src[*char_index])) { |
| 37 if (!CBU16_IS_SURROGATE_LEAD(src[*char_index]) || | 37 if (!CBU16_IS_SURROGATE_LEAD(src[*char_index]) || |
| 38 *char_index + 1 >= src_len || | 38 *char_index + 1 >= src_len || |
| 39 !CBU16_IS_TRAIL(src[*char_index + 1])) { | 39 !CBU16_IS_TRAIL(src[*char_index + 1])) { |
| 40 // Invalid surrogate pair. | 40 // Invalid surrogate pair. |
| 41 return false; | 41 return false; |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Valid surrogate pair. | 44 // Valid surrogate pair. |
| 45 *code_point = CBU16_GET_SUPPLEMENTARY(src[*char_index], | 45 *code_point = CBU16_GET_SUPPLEMENTARY(src[*char_index], |
| 46 src[*char_index + 1]); | 46 src[*char_index + 1]); |
| 47 (*char_index)++; | 47 (*char_index)++; |
| 48 } else { | 48 } else { |
| 49 // Not a surrogate, just one 16-bit word. | 49 // Not a surrogate, just one 16-bit word. |
| 50 *code_point = src[*char_index]; | 50 *code_point = src[*char_index]; |
| 51 } | 51 } |
| 52 | 52 |
| 53 return IsValidCodepoint(*code_point); | 53 return IsValidCodepoint(*code_point); |
| 54 } | 54 } |
| 55 | 55 |
| 56 #if defined(WCHAR_T_IS_UTF32) | 56 #if defined(WCHAR_T_IS_UTF32) |
| 57 bool ReadUnicodeCharacter(const wchar_t* src, | 57 bool ReadUnicodeCharacter(const wchar_t* src, |
| 58 int32 src_len, | 58 int32_t src_len, |
| 59 int32* char_index, | 59 int32_t* char_index, |
| 60 uint32* code_point) { | 60 uint32_t* code_point) { |
| 61 // Conversion is easy since the source is 32-bit. | 61 // Conversion is easy since the source is 32-bit. |
| 62 *code_point = src[*char_index]; | 62 *code_point = src[*char_index]; |
| 63 | 63 |
| 64 // Validate the value. | 64 // Validate the value. |
| 65 return IsValidCodepoint(*code_point); | 65 return IsValidCodepoint(*code_point); |
| 66 } | 66 } |
| 67 #endif // defined(WCHAR_T_IS_UTF32) | 67 #endif // defined(WCHAR_T_IS_UTF32) |
| 68 | 68 |
| 69 // WriteUnicodeCharacter ------------------------------------------------------- | 69 // WriteUnicodeCharacter ------------------------------------------------------- |
| 70 | 70 |
| 71 size_t WriteUnicodeCharacter(uint32 code_point, std::string* output) { | 71 size_t WriteUnicodeCharacter(uint32_t code_point, std::string* output) { |
| 72 if (code_point <= 0x7f) { | 72 if (code_point <= 0x7f) { |
| 73 // Fast path the common case of one byte. | 73 // Fast path the common case of one byte. |
| 74 output->push_back(static_cast<char>(code_point)); | 74 output->push_back(static_cast<char>(code_point)); |
| 75 return 1; | 75 return 1; |
| 76 } | 76 } |
| 77 | 77 |
| 78 | 78 |
| 79 // CBU8_APPEND_UNSAFE can append up to 4 bytes. | 79 // CBU8_APPEND_UNSAFE can append up to 4 bytes. |
| 80 size_t char_offset = output->length(); | 80 size_t char_offset = output->length(); |
| 81 size_t original_char_offset = char_offset; | 81 size_t original_char_offset = char_offset; |
| 82 output->resize(char_offset + CBU8_MAX_LENGTH); | 82 output->resize(char_offset + CBU8_MAX_LENGTH); |
| 83 | 83 |
| 84 CBU8_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); | 84 CBU8_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); |
| 85 | 85 |
| 86 // CBU8_APPEND_UNSAFE will advance our pointer past the inserted character, so | 86 // CBU8_APPEND_UNSAFE will advance our pointer past the inserted character, so |
| 87 // it will represent the new length of the string. | 87 // it will represent the new length of the string. |
| 88 output->resize(char_offset); | 88 output->resize(char_offset); |
| 89 return char_offset - original_char_offset; | 89 return char_offset - original_char_offset; |
| 90 } | 90 } |
| 91 | 91 |
| 92 size_t WriteUnicodeCharacter(uint32 code_point, string16* output) { | 92 size_t WriteUnicodeCharacter(uint32_t code_point, string16* output) { |
| 93 if (CBU16_LENGTH(code_point) == 1) { | 93 if (CBU16_LENGTH(code_point) == 1) { |
| 94 // Thie code point is in the Basic Multilingual Plane (BMP). | 94 // Thie code point is in the Basic Multilingual Plane (BMP). |
| 95 output->push_back(static_cast<char16>(code_point)); | 95 output->push_back(static_cast<char16>(code_point)); |
| 96 return 1; | 96 return 1; |
| 97 } | 97 } |
| 98 // Non-BMP characters use a double-character encoding. | 98 // Non-BMP characters use a double-character encoding. |
| 99 size_t char_offset = output->length(); | 99 size_t char_offset = output->length(); |
| 100 output->resize(char_offset + CBU16_MAX_LENGTH); | 100 output->resize(char_offset + CBU16_MAX_LENGTH); |
| 101 CBU16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); | 101 CBU16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); |
| 102 return CBU16_MAX_LENGTH; | 102 return CBU16_MAX_LENGTH; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // character. | 139 // character. |
| 140 output->reserve(src_len / 2); | 140 output->reserve(src_len / 2); |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 | 143 |
| 144 // Instantiate versions we know callers will need. | 144 // Instantiate versions we know callers will need. |
| 145 template void PrepareForUTF16Or32Output(const char*, size_t, std::wstring*); | 145 template void PrepareForUTF16Or32Output(const char*, size_t, std::wstring*); |
| 146 template void PrepareForUTF16Or32Output(const char*, size_t, string16*); | 146 template void PrepareForUTF16Or32Output(const char*, size_t, string16*); |
| 147 | 147 |
| 148 } // namespace base | 148 } // namespace base |
| OLD | NEW |