blob: 2d95870c580819ef8a094ec55b268599b8fb3304 [file] [log] [blame]
[email protected]23bb71f2011-04-21 22:22:101// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b9f93832009-11-13 19:27:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a3f721892013-02-07 03:59:065#ifndef BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_
6#define BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_
[email protected]b9f93832009-11-13 19:27:487
brettw048988b2016-08-12 20:52:468// Low-level UTF handling functions. Most code will want to use the functions
9// in utf_string_conversions.h
[email protected]b9f93832009-11-13 19:27:4810
avi84f37e12015-12-25 09:31:4211#include <stddef.h>
12#include <stdint.h>
13
[email protected]0bea7252011-08-05 15:34:0014#include "base/base_export.h"
[email protected]c851cfd2013-06-10 20:11:1415#include "base/strings/string16.h"
[email protected]b9f93832009-11-13 19:27:4816
17namespace base {
18
avi84f37e12015-12-25 09:31:4219inline bool IsValidCodepoint(uint32_t code_point) {
[email protected]858d48872010-01-16 17:56:0820 // Excludes the surrogate code points ([0xD800, 0xDFFF]) and
21 // codepoints larger than 0x10FFFF (the highest codepoint allowed).
22 // Non-characters and unassigned codepoints are allowed.
23 return code_point < 0xD800u ||
24 (code_point >= 0xE000u && code_point <= 0x10FFFFu);
[email protected]b9f93832009-11-13 19:27:4825}
26
avi84f37e12015-12-25 09:31:4227inline bool IsValidCharacter(uint32_t code_point) {
[email protected]bce55e272010-02-26 23:39:5828 // Excludes non-characters (U+FDD0..U+FDEF, and all codepoints ending in
29 // 0xFFFE or 0xFFFF) from the set of valid code points.
30 return code_point < 0xD800u || (code_point >= 0xE000u &&
31 code_point < 0xFDD0u) || (code_point > 0xFDEFu &&
32 code_point <= 0x10FFFFu && (code_point & 0xFFFEu) != 0xFFFEu);
33}
34
[email protected]b9f93832009-11-13 19:27:4835// 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.
[email protected]0bea7252011-08-05 15:34:0044BASE_EXPORT bool ReadUnicodeCharacter(const char* src,
avi84f37e12015-12-25 09:31:4245 int32_t src_len,
46 int32_t* char_index,
47 uint32_t* code_point_out);
[email protected]b9f93832009-11-13 19:27:4848
49// Reads a UTF-16 character. The usage is the same as the 8-bit version above.
[email protected]0bea7252011-08-05 15:34:0050BASE_EXPORT bool ReadUnicodeCharacter(const char16* src,
avi84f37e12015-12-25 09:31:4251 int32_t src_len,
52 int32_t* char_index,
53 uint32_t* code_point);
[email protected]b9f93832009-11-13 19:27:4854
55#if defined(WCHAR_T_IS_UTF32)
56// Reads UTF-32 character. The usage is the same as the 8-bit version above.
[email protected]0bea7252011-08-05 15:34:0057BASE_EXPORT bool ReadUnicodeCharacter(const wchar_t* src,
avi84f37e12015-12-25 09:31:4258 int32_t src_len,
59 int32_t* char_index,
60 uint32_t* code_point);
[email protected]b9f93832009-11-13 19:27:4861#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.
avi84f37e12015-12-25 09:31:4267BASE_EXPORT size_t WriteUnicodeCharacter(uint32_t code_point,
[email protected]0bea7252011-08-05 15:34:0068 std::string* output);
[email protected]b9f93832009-11-13 19:27:4869
70// Appends the given code point as a UTF-16 character to the given 16-bit
71// string. Returns the number of 16-bit values written.
avi84f37e12015-12-25 09:31:4272BASE_EXPORT size_t WriteUnicodeCharacter(uint32_t code_point, string16* output);
[email protected]b9f93832009-11-13 19:27:4873
74#if defined(WCHAR_T_IS_UTF32)
75// Appends the given UTF-32 character to the given 32-bit string. Returns the
76// number of 32-bit values written.
avi84f37e12015-12-25 09:31:4277inline size_t WriteUnicodeCharacter(uint32_t code_point, std::wstring* output) {
[email protected]b9f93832009-11-13 19:27:4878 // This is the easy case, just append the character.
79 output->push_back(code_point);
80 return 1;
81}
82#endif // defined(WCHAR_T_IS_UTF32)
83
84// Generalized Unicode converter -----------------------------------------------
85
86// Guesses the length of the output in UTF-8 in bytes, clears that output
87// string, and reserves that amount of space. We assume that the input
88// character types are unsigned, which will be true for UTF-16 and -32 on our
89// systems.
90template<typename CHAR>
91void PrepareForUTF8Output(const CHAR* src, size_t src_len, std::string* output);
92
93// Prepares an output buffer (containing either UTF-16 or -32 data) given some
94// UTF-8 input that will be converted to it. See PrepareForUTF8Output().
95template<typename STRING>
96void PrepareForUTF16Or32Output(const char* src, size_t src_len, STRING* output);
97
98} // namespace base
99
[email protected]a3f721892013-02-07 03:59:06100#endif // BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_