string16.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2013 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_STRING16_H_
  5. #define BASE_STRINGS_STRING16_H_
  6. // WHAT:
  7. // A version of std::basic_string that provides 2-byte characters even when
  8. // wchar_t is not implemented as a 2-byte type. You can access this class as
  9. // string16. We also define char16, which string16 is based upon.
  10. //
  11. // WHY:
  12. // On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2
  13. // data. Plenty of existing code operates on strings encoded as UTF-16.
  14. //
  15. // On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make
  16. // it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails
  17. // at run time, because it calls some functions (like wcslen) that come from
  18. // the system's native C library -- which was built with a 4-byte wchar_t!
  19. // It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's
  20. // entirely improper on those systems where the encoding of wchar_t is defined
  21. // as UTF-32.
  22. //
  23. // Here, we define string16, which is similar to std::wstring but replaces all
  24. // libc functions with custom, 2-byte-char compatible routines. It is capable
  25. // of carrying UTF-16-encoded data.
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <functional>
  30. #include <string>
  31. #include "base/base_export.h"
  32. #include "build/build_config.h"
  33. #if defined(WCHAR_T_IS_UTF16)
  34. // Define a macro for wrapping construction of char16 arrays and string16s from
  35. // a literal string. This indirection allows for an easier migration of
  36. // base::char16 to char16_t on platforms where WCHAR_T_IS_UTF16, as only a one
  37. // character change to the macro will be necessary.
  38. // This macro does not exist when WCHAR_T_IS_UTF32, as it is currently not
  39. // possible to create a char array form a literal in this case.
  40. // TODO(https://crbug.com/911896): Remove this macro once base::char16 is
  41. // char16_t on all platforms.
  42. #define STRING16_LITERAL(x) L##x
  43. namespace base {
  44. typedef wchar_t char16;
  45. typedef std::wstring string16;
  46. } // namespace base
  47. #elif defined(WCHAR_T_IS_UTF32)
  48. #include <wchar.h> // for mbstate_t
  49. namespace base {
  50. typedef uint16_t char16;
  51. // char16 versions of the functions required by string16_char_traits; these
  52. // are based on the wide character functions of similar names ("w" or "wcs"
  53. // instead of "c16").
  54. BASE_EXPORT int c16memcmp(const char16* s1, const char16* s2, size_t n);
  55. BASE_EXPORT size_t c16len(const char16* s);
  56. BASE_EXPORT const char16* c16memchr(const char16* s, char16 c, size_t n);
  57. BASE_EXPORT char16* c16memmove(char16* s1, const char16* s2, size_t n);
  58. BASE_EXPORT char16* c16memcpy(char16* s1, const char16* s2, size_t n);
  59. BASE_EXPORT char16* c16memset(char16* s, char16 c, size_t n);
  60. // This namespace contains the implementation of base::string16 along with
  61. // things that need to be found via argument-dependent lookup from a
  62. // base::string16.
  63. namespace string16_internals {
  64. struct string16_char_traits {
  65. typedef char16 char_type;
  66. typedef int int_type;
  67. // int_type needs to be able to hold each possible value of char_type, and in
  68. // addition, the distinct value of eof().
  69. static_assert(sizeof(int_type) > sizeof(char_type),
  70. "int must be larger than 16 bits wide");
  71. typedef std::streamoff off_type;
  72. typedef mbstate_t state_type;
  73. typedef std::fpos<state_type> pos_type;
  74. static void assign(char_type& c1, const char_type& c2) {
  75. c1 = c2;
  76. }
  77. static bool eq(const char_type& c1, const char_type& c2) {
  78. return c1 == c2;
  79. }
  80. static bool lt(const char_type& c1, const char_type& c2) {
  81. return c1 < c2;
  82. }
  83. static int compare(const char_type* s1, const char_type* s2, size_t n) {
  84. return c16memcmp(s1, s2, n);
  85. }
  86. static size_t length(const char_type* s) {
  87. return c16len(s);
  88. }
  89. static const char_type* find(const char_type* s, size_t n,
  90. const char_type& a) {
  91. return c16memchr(s, a, n);
  92. }
  93. static char_type* move(char_type* s1, const char_type* s2, size_t n) {
  94. return c16memmove(s1, s2, n);
  95. }
  96. static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
  97. return c16memcpy(s1, s2, n);
  98. }
  99. static char_type* assign(char_type* s, size_t n, char_type a) {
  100. return c16memset(s, a, n);
  101. }
  102. static int_type not_eof(const int_type& c) {
  103. return eq_int_type(c, eof()) ? 0 : c;
  104. }
  105. static char_type to_char_type(const int_type& c) {
  106. return char_type(c);
  107. }
  108. static int_type to_int_type(const char_type& c) {
  109. return int_type(c);
  110. }
  111. static bool eq_int_type(const int_type& c1, const int_type& c2) {
  112. return c1 == c2;
  113. }
  114. static int_type eof() {
  115. return static_cast<int_type>(EOF);
  116. }
  117. };
  118. } // namespace string16_internals
  119. typedef std::basic_string<char16,
  120. base::string16_internals::string16_char_traits>
  121. string16;
  122. namespace string16_internals {
  123. BASE_EXPORT extern std::ostream& operator<<(std::ostream& out,
  124. const string16& str);
  125. // This is required by googletest to print a readable output on test failures.
  126. BASE_EXPORT extern void PrintTo(const string16& str, std::ostream* out);
  127. } // namespace string16_internals
  128. } // namespace base
  129. // The string class will be explicitly instantiated only once, in string16.cc.
  130. //
  131. // std::basic_string<> in GNU libstdc++ contains a static data member,
  132. // _S_empty_rep_storage, to represent empty strings. When an operation such
  133. // as assignment or destruction is performed on a string, causing its existing
  134. // data member to be invalidated, it must not be freed if this static data
  135. // member is being used. Otherwise, it counts as an attempt to free static
  136. // (and not allocated) data, which is a memory error.
  137. //
  138. // Generally, due to C++ template magic, _S_empty_rep_storage will be marked
  139. // as a coalesced symbol, meaning that the linker will combine multiple
  140. // instances into a single one when generating output.
  141. //
  142. // If a string class is used by multiple shared libraries, a problem occurs.
  143. // Each library will get its own copy of _S_empty_rep_storage. When strings
  144. // are passed across a library boundary for alteration or destruction, memory
  145. // errors will result. GNU libstdc++ contains a configuration option,
  146. // --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which
  147. // disables the static data member optimization, but it's a good optimization
  148. // and non-STL code is generally at the mercy of the system's STL
  149. // configuration. Fully-dynamic strings are not the default for GNU libstdc++
  150. // libstdc++ itself or for the libstdc++ installations on the systems we care
  151. // about, such as Mac OS X and relevant flavors of Linux.
  152. //
  153. // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 .
  154. //
  155. // To avoid problems, string classes need to be explicitly instantiated only
  156. // once, in exactly one library. All other string users see it via an "extern"
  157. // declaration. This is precisely how GNU libstdc++ handles
  158. // std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring).
  159. //
  160. // This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2),
  161. // in which the linker does not fully coalesce symbols when dead code
  162. // stripping is enabled. This bug causes the memory errors described above
  163. // to occur even when a std::basic_string<> does not cross shared library
  164. // boundaries, such as in statically-linked executables.
  165. //
  166. // TODO(mark): File this bug with Apple and update this note with a bug number.
  167. extern template class BASE_EXPORT
  168. std::basic_string<base::char16,
  169. base::string16_internals::string16_char_traits>;
  170. // Specialize std::hash for base::string16. Although the style guide forbids
  171. // this in general, it is necessary for consistency with WCHAR_T_IS_UTF16
  172. // platforms, where base::string16 is a type alias for std::wstring.
  173. namespace std {
  174. template <>
  175. struct hash<base::string16> {
  176. std::size_t operator()(const base::string16& s) const {
  177. std::size_t result = 0;
  178. for (base::char16 c : s)
  179. result = (result * 131) + c;
  180. return result;
  181. }
  182. };
  183. } // namespace std
  184. #endif // WCHAR_T_IS_UTF32
  185. #endif // BASE_STRINGS_STRING16_H_