• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef BASE_STRING16_H_
30 #define BASE_STRING16_H_
31 
32 // WHAT:
33 // A version of std::basic_string that provides 2-byte characters even when
34 // wchar_t is not implemented as a 2-byte type. You can access this class as
35 // string16. We also define char16, which string16 is based upon.
36 //
37 // WHY:
38 // On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2
39 // data. Plenty of existing code operates on strings encoded as UTF-16.
40 //
41 // On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make
42 // it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails
43 // at run time, because it calls some functions (like wcslen) that come from
44 // the system's native C library -- which was built with a 4-byte wchar_t!
45 // It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's
46 // entirely improper on those systems where the encoding of wchar_t is defined
47 // as UTF-32.
48 //
49 // Here, we define string16, which is similar to std::wstring but replaces all
50 // libc functions with custom, 2-byte-char compatible routines. It is capable
51 // of carrying UTF-16-encoded data.
52 
53 #include <string>
54 
55 #include "base/basictypes.h"
56 
57 #ifdef WIN32
58 
59 typedef wchar_t char16;
60 typedef std::wstring string16;
61 
62 #else  // !WIN32
63 
64 typedef uint16 char16;
65 
66 namespace base {
67 
68 // char16 versions of the functions required by string16_char_traits; these
69 // are based on the wide character functions of similar names ("w" or "wcs"
70 // instead of "c16").
71 int c16memcmp(const char16* s1, const char16* s2, size_t n);
72 size_t c16len(const char16* s);
73 const char16* c16memchr(const char16* s, char16 c, size_t n);
74 char16* c16memmove(char16* s1, const char16* s2, size_t n);
75 char16* c16memcpy(char16* s1, const char16* s2, size_t n);
76 char16* c16memset(char16* s, char16 c, size_t n);
77 
78 struct string16_char_traits {
79   typedef char16 char_type;
80   typedef int int_type;
81 
82   typedef std::streamoff off_type;
83   typedef mbstate_t state_type;
84   typedef std::fpos<state_type> pos_type;
85 
assignstring16_char_traits86   static void assign(char_type& c1, const char_type& c2) {
87     c1 = c2;
88   }
89 
eqstring16_char_traits90   static bool eq(const char_type& c1, const char_type& c2) {
91     return c1 == c2;
92   }
ltstring16_char_traits93   static bool lt(const char_type& c1, const char_type& c2) {
94     return c1 < c2;
95   }
96 
comparestring16_char_traits97   static int compare(const char_type* s1, const char_type* s2, size_t n) {
98     return c16memcmp(s1, s2, n);
99   }
100 
lengthstring16_char_traits101   static size_t length(const char_type* s) {
102     return c16len(s);
103   }
104 
findstring16_char_traits105   static const char_type* find(const char_type* s, size_t n,
106                                const char_type& a) {
107     return c16memchr(s, a, n);
108   }
109 
movestring16_char_traits110   static char_type* move(char_type* s1, const char_type* s2, int_type n) {
111     return c16memmove(s1, s2, n);
112   }
113 
copystring16_char_traits114   static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
115     return c16memcpy(s1, s2, n);
116   }
117 
assignstring16_char_traits118   static char_type* assign(char_type* s, size_t n, char_type a) {
119     return c16memset(s, a, n);
120   }
121 
not_eofstring16_char_traits122   static int_type not_eof(const int_type& c) {
123     return eq_int_type(c, eof()) ? 0 : c;
124   }
125 
to_char_typestring16_char_traits126   static char_type to_char_type(const int_type& c) {
127     return char_type(c);
128   }
129 
to_int_typestring16_char_traits130   static int_type to_int_type(const char_type& c) {
131     return int_type(c);
132   }
133 
eq_int_typestring16_char_traits134   static bool eq_int_type(const int_type& c1, const int_type& c2) {
135     return c1 == c2;
136   }
137 
eofstring16_char_traits138   static int_type eof() {
139     return static_cast<int_type>(EOF);
140   }
141 };
142 
143 }  // namespace base
144 
145 // The string class will be explicitly instantiated only once, in string16.cc.
146 //
147 // std::basic_string<> in GNU libstdc++ contains a static data member,
148 // _S_empty_rep_storage, to represent empty strings.  When an operation such
149 // as assignment or destruction is performed on a string, causing its existing
150 // data member to be invalidated, it must not be freed if this static data
151 // member is being used.  Otherwise, it counts as an attempt to free static
152 // (and not allocated) data, which is a memory error.
153 //
154 // Generally, due to C++ template magic, _S_empty_rep_storage will be marked
155 // as a coalesced symbol, meaning that the linker will combine multiple
156 // instances into a single one when generating output.
157 //
158 // If a string class is used by multiple shared libraries, a problem occurs.
159 // Each library will get its own copy of _S_empty_rep_storage.  When strings
160 // are passed across a library boundary for alteration or destruction, memory
161 // errors will result.  GNU libstdc++ contains a configuration option,
162 // --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which
163 // disables the static data member optimization, but it's a good optimization
164 // and non-STL code is generally at the mercy of the system's STL
165 // configuration.  Fully-dynamic strings are not the default for GNU libstdc++
166 // libstdc++ itself or for the libstdc++ installations on the systems we care
167 // about, such as Mac OS X and relevant flavors of Linux.
168 //
169 // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 .
170 //
171 // To avoid problems, string classes need to be explicitly instantiated only
172 // once, in exactly one library.  All other string users see it via an "extern"
173 // declaration.  This is precisely how GNU libstdc++ handles
174 // std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring).
175 //
176 // This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2),
177 // in which the linker does not fully coalesce symbols when dead code
178 // stripping is enabled.  This bug causes the memory errors described above
179 // to occur even when a std::basic_string<> does not cross shared library
180 // boundaries, such as in statically-linked executables.
181 //
182 // TODO(mark): File this bug with Apple and update this note with a bug number.
183 
184 extern template class std::basic_string<char16, base::string16_char_traits>;
185 
186 typedef std::basic_string<char16, base::string16_char_traits> string16;
187 
188 extern std::ostream& operator<<(std::ostream& out, const string16& str);
189 
190 #endif  // !WIN32
191 
192 #endif  // BASE_STRING16_H_
193