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