]> git.sesse.net Git - casparcg/blob - dependencies64/cef/include/base/cef_string16.h
* Merged html producer and updated to latest CEF version (does not have satisfactory...
[casparcg] / dependencies64 / cef / include / base / cef_string16.h
1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2013
2 // Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
18 //
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.
30
31 #ifndef CEF_INCLUDE_BASE_CEF_STRING16_H_
32 #define CEF_INCLUDE_BASE_CEF_STRING16_H_
33 #pragma once
34
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
46 // updated to match.
47 // WHAT:
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.
51 //
52 // WHY:
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.
55 //
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
62 // as UTF-32.
63 //
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.
67
68 #include <stdio.h>
69 #include <string>
70
71 #include "include/base/cef_basictypes.h"
72
73 #if defined(WCHAR_T_IS_UTF16)
74
75 namespace base {
76
77 typedef wchar_t char16;
78 typedef std::wstring string16;
79 typedef std::char_traits<wchar_t> string16_char_traits;
80
81 }  // namespace base
82
83 #elif defined(WCHAR_T_IS_UTF32)
84
85 #include <stdint.h>  // For uint16_t
86
87 #include "include/base/cef_macros.h"
88
89 namespace base {
90
91 typedef uint16_t char16;
92
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"
95 // instead of "c16").
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);
102
103 struct string16_char_traits {
104   typedef char16 char_type;
105   typedef int int_type;
106
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);
110
111   typedef std::streamoff off_type;
112   typedef mbstate_t state_type;
113   typedef std::fpos<state_type> pos_type;
114
115   static void assign(char_type& c1, const char_type& c2) {
116     c1 = c2;
117   }
118
119   static bool eq(const char_type& c1, const char_type& c2) {
120     return c1 == c2;
121   }
122   static bool lt(const char_type& c1, const char_type& c2) {
123     return c1 < c2;
124   }
125
126   static int compare(const char_type* s1, const char_type* s2, size_t n) {
127     return c16memcmp(s1, s2, n);
128   }
129
130   static size_t length(const char_type* s) {
131     return c16len(s);
132   }
133
134   static const char_type* find(const char_type* s, size_t n,
135                                const char_type& a) {
136     return c16memchr(s, a, n);
137   }
138
139   static char_type* move(char_type* s1, const char_type* s2, int_type n) {
140     return c16memmove(s1, s2, n);
141   }
142
143   static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
144     return c16memcpy(s1, s2, n);
145   }
146
147   static char_type* assign(char_type* s, size_t n, char_type a) {
148     return c16memset(s, a, n);
149   }
150
151   static int_type not_eof(const int_type& c) {
152     return eq_int_type(c, eof()) ? 0 : c;
153   }
154
155   static char_type to_char_type(const int_type& c) {
156     return char_type(c);
157   }
158
159   static int_type to_int_type(const char_type& c) {
160     return int_type(c);
161   }
162
163   static bool eq_int_type(const int_type& c1, const int_type& c2) {
164     return c1 == c2;
165   }
166
167   static int_type eof() {
168     return static_cast<int_type>(EOF);
169   }
170 };
171
172 typedef std::basic_string<char16, base::string16_char_traits> string16;
173
174 extern std::ostream& operator<<(std::ostream& out, const string16& str);
175
176 // This is required by googletest to print a readable output on test failures.
177 extern void PrintTo(const string16& str, std::ostream* out);
178
179 }  // namespace base
180
181 // The string class will be explicitly instantiated only once, in string16.cc.
182 //
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.
189 //
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.
193 //
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.
204 //
205 // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 .
206 //
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).
211 //
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.
217 //
218 // TODO(mark): File this bug with Apple and update this note with a bug number.
219
220 extern template
221 class std::basic_string<base::char16, base::string16_char_traits>;
222
223 #endif  // WCHAR_T_IS_UTF32
224
225 #endif  // !BUILDING_CEF_SHARED
226
227 #endif  // CEF_INCLUDE_BASE_CEF_STRING16_H_