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