[email protected] | 23bb71f | 2011-04-21 22:22:10 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a3f72189 | 2013-02-07 03:59:06 | [diff] [blame^] | 5 | #ifndef BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_ |
| 6 | #define BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_ |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 7 | |
| 8 | // This should only be used by the various UTF string conversion files. |
| 9 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 10 | #include "base/base_export.h" |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 11 | #include "base/string16.h" |
| 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | inline bool IsValidCodepoint(uint32 code_point) { |
[email protected] | 858d4887 | 2010-01-16 17:56:08 | [diff] [blame] | 16 | // Excludes the surrogate code points ([0xD800, 0xDFFF]) and |
| 17 | // codepoints larger than 0x10FFFF (the highest codepoint allowed). |
| 18 | // Non-characters and unassigned codepoints are allowed. |
| 19 | return code_point < 0xD800u || |
| 20 | (code_point >= 0xE000u && code_point <= 0x10FFFFu); |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 21 | } |
| 22 | |
[email protected] | bce55e27 | 2010-02-26 23:39:58 | [diff] [blame] | 23 | inline bool IsValidCharacter(uint32 code_point) { |
| 24 | // Excludes non-characters (U+FDD0..U+FDEF, and all codepoints ending in |
| 25 | // 0xFFFE or 0xFFFF) from the set of valid code points. |
| 26 | return code_point < 0xD800u || (code_point >= 0xE000u && |
| 27 | code_point < 0xFDD0u) || (code_point > 0xFDEFu && |
| 28 | code_point <= 0x10FFFFu && (code_point & 0xFFFEu) != 0xFFFEu); |
| 29 | } |
| 30 | |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 31 | // ReadUnicodeCharacter -------------------------------------------------------- |
| 32 | |
| 33 | // Reads a UTF-8 stream, placing the next code point into the given output |
| 34 | // |*code_point|. |src| represents the entire string to read, and |*char_index| |
| 35 | // is the character offset within the string to start reading at. |*char_index| |
| 36 | // will be updated to index the last character read, such that incrementing it |
| 37 | // (as in a for loop) will take the reader to the next character. |
| 38 | // |
| 39 | // Returns true on success. On false, |*code_point| will be invalid. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 40 | BASE_EXPORT bool ReadUnicodeCharacter(const char* src, |
| 41 | int32 src_len, |
| 42 | int32* char_index, |
| 43 | uint32* code_point_out); |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 44 | |
| 45 | // Reads a UTF-16 character. The usage is the same as the 8-bit version above. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 46 | BASE_EXPORT bool ReadUnicodeCharacter(const char16* src, |
| 47 | int32 src_len, |
| 48 | int32* char_index, |
| 49 | uint32* code_point); |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 50 | |
| 51 | #if defined(WCHAR_T_IS_UTF32) |
| 52 | // Reads UTF-32 character. The usage is the same as the 8-bit version above. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 53 | BASE_EXPORT bool ReadUnicodeCharacter(const wchar_t* src, |
| 54 | int32 src_len, |
| 55 | int32* char_index, |
| 56 | uint32* code_point); |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 57 | #endif // defined(WCHAR_T_IS_UTF32) |
| 58 | |
| 59 | // WriteUnicodeCharacter ------------------------------------------------------- |
| 60 | |
| 61 | // Appends a UTF-8 character to the given 8-bit string. Returns the number of |
| 62 | // bytes written. |
[email protected] | 23bb71f | 2011-04-21 22:22:10 | [diff] [blame] | 63 | // TODO(brettw) Bug 79631: This function should not be exposed. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 64 | BASE_EXPORT size_t WriteUnicodeCharacter(uint32 code_point, |
| 65 | std::string* output); |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 66 | |
| 67 | // Appends the given code point as a UTF-16 character to the given 16-bit |
| 68 | // string. Returns the number of 16-bit values written. |
[email protected] | 3757776a | 2011-12-04 03:14:54 | [diff] [blame] | 69 | BASE_EXPORT size_t WriteUnicodeCharacter(uint32 code_point, string16* output); |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 70 | |
| 71 | #if defined(WCHAR_T_IS_UTF32) |
| 72 | // Appends the given UTF-32 character to the given 32-bit string. Returns the |
| 73 | // number of 32-bit values written. |
| 74 | inline size_t WriteUnicodeCharacter(uint32 code_point, std::wstring* output) { |
| 75 | // This is the easy case, just append the character. |
| 76 | output->push_back(code_point); |
| 77 | return 1; |
| 78 | } |
| 79 | #endif // defined(WCHAR_T_IS_UTF32) |
| 80 | |
| 81 | // Generalized Unicode converter ----------------------------------------------- |
| 82 | |
| 83 | // Guesses the length of the output in UTF-8 in bytes, clears that output |
| 84 | // string, and reserves that amount of space. We assume that the input |
| 85 | // character types are unsigned, which will be true for UTF-16 and -32 on our |
| 86 | // systems. |
| 87 | template<typename CHAR> |
| 88 | void PrepareForUTF8Output(const CHAR* src, size_t src_len, std::string* output); |
| 89 | |
| 90 | // Prepares an output buffer (containing either UTF-16 or -32 data) given some |
| 91 | // UTF-8 input that will be converted to it. See PrepareForUTF8Output(). |
| 92 | template<typename STRING> |
| 93 | void PrepareForUTF16Or32Output(const char* src, size_t src_len, STRING* output); |
| 94 | |
| 95 | } // namespace base |
| 96 | |
[email protected] | a3f72189 | 2013-02-07 03:59:06 | [diff] [blame^] | 97 | #endif // BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_ |