michael@0: // Copyright (c) 2011 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: #ifndef BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_ michael@0: #define BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_ michael@0: michael@0: // This should only be used by the various UTF string conversion files. michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/strings/string16.h" michael@0: michael@0: namespace base { michael@0: michael@0: inline bool IsValidCodepoint(uint32 code_point) { michael@0: // Excludes the surrogate code points ([0xD800, 0xDFFF]) and michael@0: // codepoints larger than 0x10FFFF (the highest codepoint allowed). michael@0: // Non-characters and unassigned codepoints are allowed. michael@0: return code_point < 0xD800u || michael@0: (code_point >= 0xE000u && code_point <= 0x10FFFFu); michael@0: } michael@0: michael@0: inline bool IsValidCharacter(uint32 code_point) { michael@0: // Excludes non-characters (U+FDD0..U+FDEF, and all codepoints ending in michael@0: // 0xFFFE or 0xFFFF) from the set of valid code points. michael@0: return code_point < 0xD800u || (code_point >= 0xE000u && michael@0: code_point < 0xFDD0u) || (code_point > 0xFDEFu && michael@0: code_point <= 0x10FFFFu && (code_point & 0xFFFEu) != 0xFFFEu); michael@0: } michael@0: michael@0: // ReadUnicodeCharacter -------------------------------------------------------- michael@0: michael@0: // Reads a UTF-8 stream, placing the next code point into the given output michael@0: // |*code_point|. |src| represents the entire string to read, and |*char_index| michael@0: // is the character offset within the string to start reading at. |*char_index| michael@0: // will be updated to index the last character read, such that incrementing it michael@0: // (as in a for loop) will take the reader to the next character. michael@0: // michael@0: // Returns true on success. On false, |*code_point| will be invalid. michael@0: BASE_EXPORT bool ReadUnicodeCharacter(const char* src, michael@0: int32 src_len, michael@0: int32* char_index, michael@0: uint32* code_point_out); michael@0: michael@0: // Reads a UTF-16 character. The usage is the same as the 8-bit version above. michael@0: BASE_EXPORT bool ReadUnicodeCharacter(const char16* src, michael@0: int32 src_len, michael@0: int32* char_index, michael@0: uint32* code_point); michael@0: michael@0: #if defined(WCHAR_T_IS_UTF32) michael@0: // Reads UTF-32 character. The usage is the same as the 8-bit version above. michael@0: BASE_EXPORT bool ReadUnicodeCharacter(const wchar_t* src, michael@0: int32 src_len, michael@0: int32* char_index, michael@0: uint32* code_point); michael@0: #endif // defined(WCHAR_T_IS_UTF32) michael@0: michael@0: // WriteUnicodeCharacter ------------------------------------------------------- michael@0: michael@0: // Appends a UTF-8 character to the given 8-bit string. Returns the number of michael@0: // bytes written. michael@0: // TODO(brettw) Bug 79631: This function should not be exposed. michael@0: BASE_EXPORT size_t WriteUnicodeCharacter(uint32 code_point, michael@0: std::string* output); michael@0: michael@0: // Appends the given code point as a UTF-16 character to the given 16-bit michael@0: // string. Returns the number of 16-bit values written. michael@0: BASE_EXPORT size_t WriteUnicodeCharacter(uint32 code_point, string16* output); michael@0: michael@0: #if defined(WCHAR_T_IS_UTF32) michael@0: // Appends the given UTF-32 character to the given 32-bit string. Returns the michael@0: // number of 32-bit values written. michael@0: inline size_t WriteUnicodeCharacter(uint32 code_point, std::wstring* output) { michael@0: // This is the easy case, just append the character. michael@0: output->push_back(code_point); michael@0: return 1; michael@0: } michael@0: #endif // defined(WCHAR_T_IS_UTF32) michael@0: michael@0: // Generalized Unicode converter ----------------------------------------------- michael@0: michael@0: // Guesses the length of the output in UTF-8 in bytes, clears that output michael@0: // string, and reserves that amount of space. We assume that the input michael@0: // character types are unsigned, which will be true for UTF-16 and -32 on our michael@0: // systems. michael@0: template michael@0: void PrepareForUTF8Output(const CHAR* src, size_t src_len, std::string* output); michael@0: michael@0: // Prepares an output buffer (containing either UTF-16 or -32 data) given some michael@0: // UTF-8 input that will be converted to it. See PrepareForUTF8Output(). michael@0: template michael@0: void PrepareForUTF16Or32Output(const char* src, size_t src_len, STRING* output); michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_