1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file. 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef BASE_STRINGS_STRING16_H_ 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define BASE_STRINGS_STRING16_H_ 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// WHAT: 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// A version of std::basic_string that provides 2-byte characters even when 10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// wchar_t is not implemented as a 2-byte type. You can access this class as 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// string16. We also define char16, which string16 is based upon. 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// WHY: 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// data. Plenty of existing code operates on strings encoded as UTF-16. 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// at run time, because it calls some functions (like wcslen) that come from 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the system's native C library -- which was built with a 4-byte wchar_t! 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's 22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// entirely improper on those systems where the encoding of wchar_t is defined 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// as UTF-32. 24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Here, we define string16, which is similar to std::wstring but replaces all 26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// libc functions with custom, 2-byte-char compatible routines. It is capable 27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// of carrying UTF-16-encoded data. 28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <stdio.h> 30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <string> 31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/base_export.h" 33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h" 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(WCHAR_T_IS_UTF16) 36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace base { 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef wchar_t char16; 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef std::wstring string16; 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef std::char_traits<wchar_t> string16_char_traits; 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // namespace base 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#elif defined(WCHAR_T_IS_UTF32) 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace base { 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef uint16 char16; 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// char16 versions of the functions required by string16_char_traits; these 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// are based on the wide character functions of similar names ("w" or "wcs" 53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// instead of "c16"). 54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT int c16memcmp(const char16* s1, const char16* s2, size_t n); 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT size_t c16len(const char16* s); 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT const char16* c16memchr(const char16* s, char16 c, size_t n); 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT char16* c16memmove(char16* s1, const char16* s2, size_t n); 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT char16* c16memcpy(char16* s1, const char16* s2, size_t n); 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT char16* c16memset(char16* s, char16 c, size_t n); 60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct string16_char_traits { 62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef char16 char_type; 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef int int_type; 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // int_type needs to be able to hold each possible value of char_type, and in 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // addition, the distinct value of eof(). 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) COMPILE_ASSERT(sizeof(int_type) > sizeof(char_type), unexpected_type_width); 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef std::streamoff off_type; 70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef mbstate_t state_type; 71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef std::fpos<state_type> pos_type; 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static void assign(char_type& c1, const char_type& c2) { 74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) c1 = c2; 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static bool eq(const char_type& c1, const char_type& c2) { 78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c1 == c2; 79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static bool lt(const char_type& c1, const char_type& c2) { 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c1 < c2; 82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static int compare(const char_type* s1, const char_type* s2, size_t n) { 85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c16memcmp(s1, s2, n); 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static size_t length(const char_type* s) { 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c16len(s); 90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static const char_type* find(const char_type* s, size_t n, 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char_type& a) { 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c16memchr(s, a, n); 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static char_type* move(char_type* s1, const char_type* s2, int_type n) { 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c16memmove(s1, s2, n); 99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static char_type* copy(char_type* s1, const char_type* s2, size_t n) { 102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c16memcpy(s1, s2, n); 103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static char_type* assign(char_type* s, size_t n, char_type a) { 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c16memset(s, a, n); 107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static int_type not_eof(const int_type& c) { 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return eq_int_type(c, eof()) ? 0 : c; 111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static char_type to_char_type(const int_type& c) { 114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return char_type(c); 115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static int_type to_int_type(const char_type& c) { 118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return int_type(c); 119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static bool eq_int_type(const int_type& c1, const int_type& c2) { 122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c1 == c2; 123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static int_type eof() { 126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return static_cast<int_type>(EOF); 127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef std::basic_string<char16, base::string16_char_traits> string16; 131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT extern std::ostream& operator<<(std::ostream& out, 133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const string16& str); 134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// This is required by googletest to print a readable output on test failures. 136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT extern void PrintTo(const string16& str, std::ostream* out); 137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // namespace base 139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The string class will be explicitly instantiated only once, in string16.cc. 141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// std::basic_string<> in GNU libstdc++ contains a static data member, 143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// _S_empty_rep_storage, to represent empty strings. When an operation such 144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// as assignment or destruction is performed on a string, causing its existing 145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// data member to be invalidated, it must not be freed if this static data 146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// member is being used. Otherwise, it counts as an attempt to free static 147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (and not allocated) data, which is a memory error. 148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Generally, due to C++ template magic, _S_empty_rep_storage will be marked 150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// as a coalesced symbol, meaning that the linker will combine multiple 151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// instances into a single one when generating output. 152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// If a string class is used by multiple shared libraries, a problem occurs. 154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Each library will get its own copy of _S_empty_rep_storage. When strings 155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// are passed across a library boundary for alteration or destruction, memory 156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// errors will result. GNU libstdc++ contains a configuration option, 157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which 158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// disables the static data member optimization, but it's a good optimization 159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// and non-STL code is generally at the mercy of the system's STL 160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// configuration. Fully-dynamic strings are not the default for GNU libstdc++ 161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// libstdc++ itself or for the libstdc++ installations on the systems we care 162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// about, such as Mac OS X and relevant flavors of Linux. 163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 . 165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// To avoid problems, string classes need to be explicitly instantiated only 167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// once, in exactly one library. All other string users see it via an "extern" 168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// declaration. This is precisely how GNU libstdc++ handles 169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring). 170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2), 172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// in which the linker does not fully coalesce symbols when dead code 173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// stripping is enabled. This bug causes the memory errors described above 174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// to occur even when a std::basic_string<> does not cross shared library 175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// boundaries, such as in statically-linked executables. 176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// TODO(mark): File this bug with Apple and update this note with a bug number. 178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)extern template 180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class BASE_EXPORT std::basic_string<base::char16, base::string16_char_traits>; 181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif // WCHAR_T_IS_UTF32 183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// TODO(brettw) update users of string16 to use the namespace and remove 185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// this "using". 186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using base::char16; 187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using base::string16; 188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif // BASE_STRINGS_STRING16_H_ 190