16d528ed9Sopenharmony_ci// Copyright (c) 2011 The Chromium Authors. All rights reserved.
26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
36d528ed9Sopenharmony_ci// found in the LICENSE file.
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci#ifndef BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_
66d528ed9Sopenharmony_ci#define BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_
76d528ed9Sopenharmony_ci
86d528ed9Sopenharmony_ci// Low-level UTF handling functions. Most code will want to use the functions
96d528ed9Sopenharmony_ci// in utf_string_conversions.h
106d528ed9Sopenharmony_ci
116d528ed9Sopenharmony_ci#include <stddef.h>
126d528ed9Sopenharmony_ci#include <stdint.h>
136d528ed9Sopenharmony_ci
146d528ed9Sopenharmony_ci#include <string>
156d528ed9Sopenharmony_ci
166d528ed9Sopenharmony_cinamespace base {
176d528ed9Sopenharmony_ci
186d528ed9Sopenharmony_ciinline bool IsValidCodepoint(uint32_t code_point) {
196d528ed9Sopenharmony_ci  // Excludes the surrogate code points ([0xD800, 0xDFFF]) and
206d528ed9Sopenharmony_ci  // codepoints larger than 0x10FFFF (the highest codepoint allowed).
216d528ed9Sopenharmony_ci  // Non-characters and unassigned codepoints are allowed.
226d528ed9Sopenharmony_ci  return code_point < 0xD800u ||
236d528ed9Sopenharmony_ci         (code_point >= 0xE000u && code_point <= 0x10FFFFu);
246d528ed9Sopenharmony_ci}
256d528ed9Sopenharmony_ci
266d528ed9Sopenharmony_ciinline bool IsValidCharacter(uint32_t code_point) {
276d528ed9Sopenharmony_ci  // Excludes non-characters (U+FDD0..U+FDEF, and all codepoints ending in
286d528ed9Sopenharmony_ci  // 0xFFFE or 0xFFFF) from the set of valid code points.
296d528ed9Sopenharmony_ci  return code_point < 0xD800u ||
306d528ed9Sopenharmony_ci         (code_point >= 0xE000u && code_point < 0xFDD0u) ||
316d528ed9Sopenharmony_ci         (code_point > 0xFDEFu && code_point <= 0x10FFFFu &&
326d528ed9Sopenharmony_ci          (code_point & 0xFFFEu) != 0xFFFEu);
336d528ed9Sopenharmony_ci}
346d528ed9Sopenharmony_ci
356d528ed9Sopenharmony_ci// ReadUnicodeCharacter --------------------------------------------------------
366d528ed9Sopenharmony_ci
376d528ed9Sopenharmony_ci// Reads a UTF-8 stream, placing the next code point into the given output
386d528ed9Sopenharmony_ci// |*code_point|. |src| represents the entire string to read, and |*char_index|
396d528ed9Sopenharmony_ci// is the character offset within the string to start reading at. |*char_index|
406d528ed9Sopenharmony_ci// will be updated to index the last character read, such that incrementing it
416d528ed9Sopenharmony_ci// (as in a for loop) will take the reader to the next character.
426d528ed9Sopenharmony_ci//
436d528ed9Sopenharmony_ci// Returns true on success. On false, |*code_point| will be invalid.
446d528ed9Sopenharmony_cibool ReadUnicodeCharacter(const char* src,
456d528ed9Sopenharmony_ci                          int32_t src_len,
466d528ed9Sopenharmony_ci                          int32_t* char_index,
476d528ed9Sopenharmony_ci                          uint32_t* code_point_out);
486d528ed9Sopenharmony_ci
496d528ed9Sopenharmony_ci// Reads a UTF-16 character. The usage is the same as the 8-bit version above.
506d528ed9Sopenharmony_cibool ReadUnicodeCharacter(const char16_t* src,
516d528ed9Sopenharmony_ci                          int32_t src_len,
526d528ed9Sopenharmony_ci                          int32_t* char_index,
536d528ed9Sopenharmony_ci                          uint32_t* code_point);
546d528ed9Sopenharmony_ci
556d528ed9Sopenharmony_ci// WriteUnicodeCharacter -------------------------------------------------------
566d528ed9Sopenharmony_ci
576d528ed9Sopenharmony_ci// Appends a UTF-8 character to the given 8-bit string.  Returns the number of
586d528ed9Sopenharmony_ci// bytes written.
596d528ed9Sopenharmony_cisize_t WriteUnicodeCharacter(uint32_t code_point, std::string* output);
606d528ed9Sopenharmony_ci
616d528ed9Sopenharmony_ci// Appends the given code point as a UTF-16 character to the given 16-bit
626d528ed9Sopenharmony_ci// string.  Returns the number of 16-bit values written.
636d528ed9Sopenharmony_cisize_t WriteUnicodeCharacter(uint32_t code_point, std::u16string* output);
646d528ed9Sopenharmony_ci
656d528ed9Sopenharmony_ci// Generalized Unicode converter -----------------------------------------------
666d528ed9Sopenharmony_ci
676d528ed9Sopenharmony_ci// Guesses the length of the output in UTF-8 in bytes, clears that output
686d528ed9Sopenharmony_ci// string, and reserves that amount of space.  We assume that the input
696d528ed9Sopenharmony_ci// character types are unsigned, which will be true for UTF-16 and -32 on our
706d528ed9Sopenharmony_ci// systems.
716d528ed9Sopenharmony_citemplate <typename CHAR>
726d528ed9Sopenharmony_civoid PrepareForUTF8Output(const CHAR* src, size_t src_len, std::string* output);
736d528ed9Sopenharmony_ci
746d528ed9Sopenharmony_ci// Prepares an output buffer (containing either UTF-16 or -32 data) given some
756d528ed9Sopenharmony_ci// UTF-8 input that will be converted to it.  See PrepareForUTF8Output().
766d528ed9Sopenharmony_citemplate <typename STRING>
776d528ed9Sopenharmony_civoid PrepareForUTF16Or32Output(const char* src, size_t src_len, STRING* output);
786d528ed9Sopenharmony_ci
796d528ed9Sopenharmony_ci}  // namespace base
806d528ed9Sopenharmony_ci
816d528ed9Sopenharmony_ci#endif  // BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_
82