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