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