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