utf_string_conversion_utils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2011 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. #ifndef BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_
  5. #define BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_
  6. // Low-level UTF handling functions. Most code will want to use the functions
  7. // in utf_string_conversions.h
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include "base/base_export.h"
  11. #include "base/strings/string16.h"
  12. namespace base {
  13. inline bool IsValidCodepoint(uint32_t code_point) {
  14. // Excludes code points that are not Unicode scalar values, i.e.
  15. // surrogate code points ([0xD800, 0xDFFF]). Additionally, excludes
  16. // code points larger than 0x10FFFF (the highest codepoint allowed).
  17. // Non-characters and unassigned code points are allowed.
  18. // https://unicode.org/glossary/#unicode_scalar_value
  19. return code_point < 0xD800u ||
  20. (code_point >= 0xE000u && code_point <= 0x10FFFFu);
  21. }
  22. inline bool IsValidCharacter(uint32_t code_point) {
  23. // Excludes non-characters (U+FDD0..U+FDEF, and all code points
  24. // ending in 0xFFFE or 0xFFFF) from the set of valid code points.
  25. // https://unicode.org/faq/private_use.html#nonchar1
  26. return code_point < 0xD800u || (code_point >= 0xE000u &&
  27. code_point < 0xFDD0u) || (code_point > 0xFDEFu &&
  28. code_point <= 0x10FFFFu && (code_point & 0xFFFEu) != 0xFFFEu);
  29. }
  30. // ReadUnicodeCharacter --------------------------------------------------------
  31. // Reads a UTF-8 stream, placing the next code point into the given output
  32. // |*code_point|. |src| represents the entire string to read, and |*char_index|
  33. // is the character offset within the string to start reading at. |*char_index|
  34. // will be updated to index the last character read, such that incrementing it
  35. // (as in a for loop) will take the reader to the next character.
  36. //
  37. // Returns true on success. On false, |*code_point| will be invalid.
  38. BASE_EXPORT bool ReadUnicodeCharacter(const char* src,
  39. int32_t src_len,
  40. int32_t* char_index,
  41. uint32_t* code_point_out);
  42. // Reads a UTF-16 character. The usage is the same as the 8-bit version above.
  43. BASE_EXPORT bool ReadUnicodeCharacter(const char16* src,
  44. int32_t src_len,
  45. int32_t* char_index,
  46. uint32_t* code_point);
  47. #if defined(WCHAR_T_IS_UTF32)
  48. // Reads UTF-32 character. The usage is the same as the 8-bit version above.
  49. BASE_EXPORT bool ReadUnicodeCharacter(const wchar_t* src,
  50. int32_t src_len,
  51. int32_t* char_index,
  52. uint32_t* code_point);
  53. #endif // defined(WCHAR_T_IS_UTF32)
  54. // WriteUnicodeCharacter -------------------------------------------------------
  55. // Appends a UTF-8 character to the given 8-bit string. Returns the number of
  56. // bytes written.
  57. BASE_EXPORT size_t WriteUnicodeCharacter(uint32_t code_point,
  58. std::string* output);
  59. // Appends the given code point as a UTF-16 character to the given 16-bit
  60. // string. Returns the number of 16-bit values written.
  61. BASE_EXPORT size_t WriteUnicodeCharacter(uint32_t code_point, string16* output);
  62. #if defined(WCHAR_T_IS_UTF32)
  63. // Appends the given UTF-32 character to the given 32-bit string. Returns the
  64. // number of 32-bit values written.
  65. inline size_t WriteUnicodeCharacter(uint32_t code_point, std::wstring* output) {
  66. // This is the easy case, just append the character.
  67. output->push_back(code_point);
  68. return 1;
  69. }
  70. #endif // defined(WCHAR_T_IS_UTF32)
  71. // Generalized Unicode converter -----------------------------------------------
  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. // Prepares an output buffer (containing either UTF-16 or -32 data) given some
  79. // UTF-8 input that will be converted to it. See PrepareForUTF8Output().
  80. template<typename STRING>
  81. void PrepareForUTF16Or32Output(const char* src, size_t src_len, STRING* output);
  82. } // namespace base
  83. #endif // BASE_STRINGS_UTF_STRING_CONVERSION_UTILS_H_