1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2013
2 // Google Inc. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CEF_INCLUDE_BASE_CEF_STRING16_H_
32 #define CEF_INCLUDE_BASE_CEF_STRING16_H_
35 #if defined(BASE_STRINGS_STRING16_H_)
36 // Do nothing if the Chromium header has already been included.
37 // This can happen in cases where Chromium code is used directly by the
38 // client application. When using Chromium code directly always include
39 // the Chromium header first to avoid type conflicts.
40 #elif defined(BUILDING_CEF_SHARED)
41 // When building CEF include the Chromium header directly.
42 #include "base/strings/string16.h"
43 #else // !BUILDING_CEF_SHARED
44 // The following is substantially similar to the Chromium implementation.
45 // If the Chromium implementation diverges the below implementation should be
48 // A version of std::basic_string that provides 2-byte characters even when
49 // wchar_t is not implemented as a 2-byte type. You can access this class as
50 // string16. We also define char16, which string16 is based upon.
53 // On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2
54 // data. Plenty of existing code operates on strings encoded as UTF-16.
56 // On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make
57 // it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails
58 // at run time, because it calls some functions (like wcslen) that come from
59 // the system's native C library -- which was built with a 4-byte wchar_t!
60 // It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's
61 // entirely improper on those systems where the encoding of wchar_t is defined
64 // Here, we define string16, which is similar to std::wstring but replaces all
65 // libc functions with custom, 2-byte-char compatible routines. It is capable
66 // of carrying UTF-16-encoded data.
71 #include "include/base/cef_basictypes.h"
73 #if defined(WCHAR_T_IS_UTF16)
77 typedef wchar_t char16;
78 typedef std::wstring string16;
79 typedef std::char_traits<wchar_t> string16_char_traits;
83 #elif defined(WCHAR_T_IS_UTF32)
85 #include <stdint.h> // For uint16_t
87 #include "include/base/cef_macros.h"
91 typedef uint16_t char16;
93 // char16 versions of the functions required by string16_char_traits; these
94 // are based on the wide character functions of similar names ("w" or "wcs"
96 int c16memcmp(const char16* s1, const char16* s2, size_t n);
97 size_t c16len(const char16* s);
98 const char16* c16memchr(const char16* s, char16 c, size_t n);
99 char16* c16memmove(char16* s1, const char16* s2, size_t n);
100 char16* c16memcpy(char16* s1, const char16* s2, size_t n);
101 char16* c16memset(char16* s, char16 c, size_t n);
103 struct string16_char_traits {
104 typedef char16 char_type;
105 typedef int int_type;
107 // int_type needs to be able to hold each possible value of char_type, and in
108 // addition, the distinct value of eof().
109 COMPILE_ASSERT(sizeof(int_type) > sizeof(char_type), unexpected_type_width);
111 typedef std::streamoff off_type;
112 typedef mbstate_t state_type;
113 typedef std::fpos<state_type> pos_type;
115 static void assign(char_type& c1, const char_type& c2) {
119 static bool eq(const char_type& c1, const char_type& c2) {
122 static bool lt(const char_type& c1, const char_type& c2) {
126 static int compare(const char_type* s1, const char_type* s2, size_t n) {
127 return c16memcmp(s1, s2, n);
130 static size_t length(const char_type* s) {
134 static const char_type* find(const char_type* s, size_t n,
135 const char_type& a) {
136 return c16memchr(s, a, n);
139 static char_type* move(char_type* s1, const char_type* s2, int_type n) {
140 return c16memmove(s1, s2, n);
143 static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
144 return c16memcpy(s1, s2, n);
147 static char_type* assign(char_type* s, size_t n, char_type a) {
148 return c16memset(s, a, n);
151 static int_type not_eof(const int_type& c) {
152 return eq_int_type(c, eof()) ? 0 : c;
155 static char_type to_char_type(const int_type& c) {
159 static int_type to_int_type(const char_type& c) {
163 static bool eq_int_type(const int_type& c1, const int_type& c2) {
167 static int_type eof() {
168 return static_cast<int_type>(EOF);
172 typedef std::basic_string<char16, base::string16_char_traits> string16;
174 extern std::ostream& operator<<(std::ostream& out, const string16& str);
176 // This is required by googletest to print a readable output on test failures.
177 extern void PrintTo(const string16& str, std::ostream* out);
181 // The string class will be explicitly instantiated only once, in string16.cc.
183 // std::basic_string<> in GNU libstdc++ contains a static data member,
184 // _S_empty_rep_storage, to represent empty strings. When an operation such
185 // as assignment or destruction is performed on a string, causing its existing
186 // data member to be invalidated, it must not be freed if this static data
187 // member is being used. Otherwise, it counts as an attempt to free static
188 // (and not allocated) data, which is a memory error.
190 // Generally, due to C++ template magic, _S_empty_rep_storage will be marked
191 // as a coalesced symbol, meaning that the linker will combine multiple
192 // instances into a single one when generating output.
194 // If a string class is used by multiple shared libraries, a problem occurs.
195 // Each library will get its own copy of _S_empty_rep_storage. When strings
196 // are passed across a library boundary for alteration or destruction, memory
197 // errors will result. GNU libstdc++ contains a configuration option,
198 // --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which
199 // disables the static data member optimization, but it's a good optimization
200 // and non-STL code is generally at the mercy of the system's STL
201 // configuration. Fully-dynamic strings are not the default for GNU libstdc++
202 // libstdc++ itself or for the libstdc++ installations on the systems we care
203 // about, such as Mac OS X and relevant flavors of Linux.
205 // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 .
207 // To avoid problems, string classes need to be explicitly instantiated only
208 // once, in exactly one library. All other string users see it via an "extern"
209 // declaration. This is precisely how GNU libstdc++ handles
210 // std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring).
212 // This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2),
213 // in which the linker does not fully coalesce symbols when dead code
214 // stripping is enabled. This bug causes the memory errors described above
215 // to occur even when a std::basic_string<> does not cross shared library
216 // boundaries, such as in statically-linked executables.
218 // TODO(mark): File this bug with Apple and update this note with a bug number.
221 class std::basic_string<base::char16, base::string16_char_traits>;
223 #endif // WCHAR_T_IS_UTF32
225 #endif // !BUILDING_CEF_SHARED
227 #endif // CEF_INCLUDE_BASE_CEF_STRING16_H_