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