michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_STRING16_H_ michael@0: #define BASE_STRING16_H_ michael@0: michael@0: // WHAT: michael@0: // A version of std::basic_string that provides 2-byte characters even when michael@0: // wchar_t is not implemented as a 2-byte type. You can access this class as michael@0: // string16. We also define char16, which string16 is based upon. michael@0: // michael@0: // WHY: michael@0: // On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2 michael@0: // data. Plenty of existing code operates on strings encoded as UTF-16. michael@0: // michael@0: // On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make michael@0: // it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails michael@0: // at run time, because it calls some functions (like wcslen) that come from michael@0: // the system's native C library -- which was built with a 4-byte wchar_t! michael@0: // It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's michael@0: // entirely improper on those systems where the encoding of wchar_t is defined michael@0: // as UTF-32. michael@0: // michael@0: // Here, we define string16, which is similar to std::wstring but replaces all michael@0: // libc functions with custom, 2-byte-char compatible routines. It is capable michael@0: // of carrying UTF-16-encoded data. michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #if defined(WCHAR_T_IS_UTF16) michael@0: michael@0: typedef wchar_t char16; michael@0: typedef std::wstring string16; michael@0: michael@0: #elif defined(WCHAR_T_IS_UTF32) michael@0: michael@0: typedef uint16_t char16; michael@0: michael@0: namespace base { michael@0: michael@0: // char16 versions of the functions required by string16_char_traits; these michael@0: // are based on the wide character functions of similar names ("w" or "wcs" michael@0: // instead of "c16"). michael@0: int c16memcmp(const char16* s1, const char16* s2, size_t n); michael@0: size_t c16len(const char16* s); michael@0: const char16* c16memchr(const char16* s, char16 c, size_t n); michael@0: char16* c16memmove(char16* s1, const char16* s2, size_t n); michael@0: char16* c16memcpy(char16* s1, const char16* s2, size_t n); michael@0: char16* c16memset(char16* s, char16 c, size_t n); michael@0: michael@0: struct string16_char_traits { michael@0: typedef char16 char_type; michael@0: typedef int int_type; michael@0: michael@0: // int_type needs to be able to hold each possible value of char_type, and in michael@0: // addition, the distinct value of eof(). michael@0: COMPILE_ASSERT(sizeof(int_type) > sizeof(char_type), unexpected_type_width); michael@0: michael@0: typedef std::streamoff off_type; michael@0: typedef mbstate_t state_type; michael@0: typedef std::fpos pos_type; michael@0: michael@0: static void assign(char_type& c1, const char_type& c2) { michael@0: c1 = c2; michael@0: } michael@0: michael@0: static bool eq(const char_type& c1, const char_type& c2) { michael@0: return c1 == c2; michael@0: } michael@0: static bool lt(const char_type& c1, const char_type& c2) { michael@0: return c1 < c2; michael@0: } michael@0: michael@0: static int compare(const char_type* s1, const char_type* s2, size_t n) { michael@0: return c16memcmp(s1, s2, n); michael@0: } michael@0: michael@0: static size_t length(const char_type* s) { michael@0: return c16len(s); michael@0: } michael@0: michael@0: static const char_type* find(const char_type* s, size_t n, michael@0: const char_type& a) { michael@0: return c16memchr(s, a, n); michael@0: } michael@0: michael@0: static char_type* move(char_type* s1, const char_type* s2, int_type n) { michael@0: return c16memmove(s1, s2, n); michael@0: } michael@0: michael@0: static char_type* copy(char_type* s1, const char_type* s2, size_t n) { michael@0: return c16memcpy(s1, s2, n); michael@0: } michael@0: michael@0: static char_type* assign(char_type* s, size_t n, char_type a) { michael@0: return c16memset(s, a, n); michael@0: } michael@0: michael@0: static int_type not_eof(const int_type& c) { michael@0: return eq_int_type(c, eof()) ? 0 : c; michael@0: } michael@0: michael@0: static char_type to_char_type(const int_type& c) { michael@0: return char_type(c); michael@0: } michael@0: michael@0: static int_type to_int_type(const char_type& c) { michael@0: return int_type(c); michael@0: } michael@0: michael@0: static bool eq_int_type(const int_type& c1, const int_type& c2) { michael@0: return c1 == c2; michael@0: } michael@0: michael@0: static int_type eof() { michael@0: return static_cast(EOF); michael@0: } michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: // The string class will be explicitly instantiated only once, in string16.cc. michael@0: // michael@0: // std::basic_string<> in GNU libstdc++ contains a static data member, michael@0: // _S_empty_rep_storage, to represent empty strings. When an operation such michael@0: // as assignment or destruction is performed on a string, causing its existing michael@0: // data member to be invalidated, it must not be freed if this static data michael@0: // member is being used. Otherwise, it counts as an attempt to free static michael@0: // (and not allocated) data, which is a memory error. michael@0: // michael@0: // Generally, due to C++ template magic, _S_empty_rep_storage will be marked michael@0: // as a coalesced symbol, meaning that the linker will combine multiple michael@0: // instances into a single one when generating output. michael@0: // michael@0: // If a string class is used by multiple shared libraries, a problem occurs. michael@0: // Each library will get its own copy of _S_empty_rep_storage. When strings michael@0: // are passed across a library boundary for alteration or destruction, memory michael@0: // errors will result. GNU libstdc++ contains a configuration option, michael@0: // --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which michael@0: // disables the static data member optimization, but it's a good optimization michael@0: // and non-STL code is generally at the mercy of the system's STL michael@0: // configuration. Fully-dynamic strings are not the default for GNU libstdc++ michael@0: // libstdc++ itself or for the libstdc++ installations on the systems we care michael@0: // about, such as Mac OS X and relevant flavors of Linux. michael@0: // michael@0: // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 . michael@0: // michael@0: // To avoid problems, string classes need to be explicitly instantiated only michael@0: // once, in exactly one library. All other string users see it via an "extern" michael@0: // declaration. This is precisely how GNU libstdc++ handles michael@0: // std::basic_string (string) and std::basic_string (wstring). michael@0: // michael@0: // This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2), michael@0: // in which the linker does not fully coalesce symbols when dead code michael@0: // stripping is enabled. This bug causes the memory errors described above michael@0: // to occur even when a std::basic_string<> does not cross shared library michael@0: // boundaries, such as in statically-linked executables. michael@0: // michael@0: // TODO(mark): File this bug with Apple and update this note with a bug number. michael@0: michael@0: extern template class std::basic_string; michael@0: michael@0: typedef std::basic_string string16; michael@0: michael@0: extern std::ostream& operator<<(std::ostream& out, const string16& str); michael@0: michael@0: #endif // WCHAR_T_IS_UTF32 michael@0: michael@0: #endif // BASE_STRING16_H_