• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 namespace base {
13 
IsValidCodepoint(uint32 code_point)14 inline 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 
22 // ReadUnicodeCharacter --------------------------------------------------------
23 
24 // Reads a UTF-8 stream, placing the next code point into the given output
25 // |*code_point|. |src| represents the entire string to read, and |*char_index|
26 // is the character offset within the string to start reading at. |*char_index|
27 // will be updated to index the last character read, such that incrementing it
28 // (as in a for loop) will take the reader to the next character.
29 //
30 // Returns true on success. On false, |*code_point| will be invalid.
31 bool ReadUnicodeCharacter(const char* src,
32                           int32 src_len,
33                           int32* char_index,
34                           uint32* code_point_out);
35 
36 // Reads a UTF-16 character. The usage is the same as the 8-bit version above.
37 bool ReadUnicodeCharacter(const char16* src,
38                           int32 src_len,
39                           int32* char_index,
40                           uint32* code_point);
41 
42 #if defined(WCHAR_T_IS_UTF32)
43 // Reads UTF-32 character. The usage is the same as the 8-bit version above.
44 bool ReadUnicodeCharacter(const wchar_t* src,
45                           int32 src_len,
46                           int32* char_index,
47                           uint32* code_point);
48 #endif  // defined(WCHAR_T_IS_UTF32)
49 
50 // WriteUnicodeCharacter -------------------------------------------------------
51 
52 // Appends a UTF-8 character to the given 8-bit string.  Returns the number of
53 // bytes written.
54 size_t WriteUnicodeCharacter(uint32 code_point, std::string* output);
55 
56 // Appends the given code point as a UTF-16 character to the given 16-bit
57 // string.  Returns the number of 16-bit values written.
58 size_t WriteUnicodeCharacter(uint32 code_point, string16* output);
59 
60 #if defined(WCHAR_T_IS_UTF32)
61 // Appends the given UTF-32 character to the given 32-bit string.  Returns the
62 // number of 32-bit values written.
WriteUnicodeCharacter(uint32 code_point,std::wstring * output)63 inline size_t WriteUnicodeCharacter(uint32 code_point, std::wstring* output) {
64   // This is the easy case, just append the character.
65   output->push_back(code_point);
66   return 1;
67 }
68 #endif  // defined(WCHAR_T_IS_UTF32)
69 
70 // Generalized Unicode converter -----------------------------------------------
71 
72 // Guesses the length of the output in UTF-8 in bytes, clears that output
73 // string, and reserves that amount of space.  We assume that the input
74 // character types are unsigned, which will be true for UTF-16 and -32 on our
75 // systems.
76 template<typename CHAR>
77 void PrepareForUTF8Output(const CHAR* src, size_t src_len, std::string* output);
78 
79 // Prepares an output buffer (containing either UTF-16 or -32 data) given some
80 // UTF-8 input that will be converted to it.  See PrepareForUTF8Output().
81 template<typename STRING>
82 void PrepareForUTF16Or32Output(const char* src, size_t src_len, STRING* output);
83 
84 }  // namespace base
85 
86 #endif  // BASE_UTF_STRING_CONVERSION_UTILS_H_
87