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