ipc/chromium/src/base/string16.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/string16.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,173 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_STRING16_H_
     1.9 +#define BASE_STRING16_H_
    1.10 +
    1.11 +// WHAT:
    1.12 +// A version of std::basic_string that provides 2-byte characters even when
    1.13 +// wchar_t is not implemented as a 2-byte type. You can access this class as
    1.14 +// string16. We also define char16, which string16 is based upon.
    1.15 +//
    1.16 +// WHY:
    1.17 +// On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2
    1.18 +// data. Plenty of existing code operates on strings encoded as UTF-16.
    1.19 +//
    1.20 +// On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make
    1.21 +// it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails
    1.22 +// at run time, because it calls some functions (like wcslen) that come from
    1.23 +// the system's native C library -- which was built with a 4-byte wchar_t!
    1.24 +// It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's
    1.25 +// entirely improper on those systems where the encoding of wchar_t is defined
    1.26 +// as UTF-32.
    1.27 +//
    1.28 +// Here, we define string16, which is similar to std::wstring but replaces all
    1.29 +// libc functions with custom, 2-byte-char compatible routines. It is capable
    1.30 +// of carrying UTF-16-encoded data.
    1.31 +
    1.32 +#include <stdio.h>
    1.33 +#include <string>
    1.34 +
    1.35 +#include "base/basictypes.h"
    1.36 +
    1.37 +#if defined(WCHAR_T_IS_UTF16)
    1.38 +
    1.39 +typedef wchar_t char16;
    1.40 +typedef std::wstring string16;
    1.41 +
    1.42 +#elif defined(WCHAR_T_IS_UTF32)
    1.43 +
    1.44 +typedef uint16_t char16;
    1.45 +
    1.46 +namespace base {
    1.47 +
    1.48 +// char16 versions of the functions required by string16_char_traits; these
    1.49 +// are based on the wide character functions of similar names ("w" or "wcs"
    1.50 +// instead of "c16").
    1.51 +int c16memcmp(const char16* s1, const char16* s2, size_t n);
    1.52 +size_t c16len(const char16* s);
    1.53 +const char16* c16memchr(const char16* s, char16 c, size_t n);
    1.54 +char16* c16memmove(char16* s1, const char16* s2, size_t n);
    1.55 +char16* c16memcpy(char16* s1, const char16* s2, size_t n);
    1.56 +char16* c16memset(char16* s, char16 c, size_t n);
    1.57 +
    1.58 +struct string16_char_traits {
    1.59 +  typedef char16 char_type;
    1.60 +  typedef int int_type;
    1.61 +
    1.62 +  // int_type needs to be able to hold each possible value of char_type, and in
    1.63 +  // addition, the distinct value of eof().
    1.64 +  COMPILE_ASSERT(sizeof(int_type) > sizeof(char_type), unexpected_type_width);
    1.65 +
    1.66 +  typedef std::streamoff off_type;
    1.67 +  typedef mbstate_t state_type;
    1.68 +  typedef std::fpos<state_type> pos_type;
    1.69 +
    1.70 +  static void assign(char_type& c1, const char_type& c2) {
    1.71 +    c1 = c2;
    1.72 +  }
    1.73 +
    1.74 +  static bool eq(const char_type& c1, const char_type& c2) {
    1.75 +    return c1 == c2;
    1.76 +  }
    1.77 +  static bool lt(const char_type& c1, const char_type& c2) {
    1.78 +    return c1 < c2;
    1.79 +  }
    1.80 +
    1.81 +  static int compare(const char_type* s1, const char_type* s2, size_t n) {
    1.82 +    return c16memcmp(s1, s2, n);
    1.83 +  }
    1.84 +
    1.85 +  static size_t length(const char_type* s) {
    1.86 +    return c16len(s);
    1.87 +  }
    1.88 +
    1.89 +  static const char_type* find(const char_type* s, size_t n,
    1.90 +                               const char_type& a) {
    1.91 +    return c16memchr(s, a, n);
    1.92 +  }
    1.93 +
    1.94 +  static char_type* move(char_type* s1, const char_type* s2, int_type n) {
    1.95 +    return c16memmove(s1, s2, n);
    1.96 +  }
    1.97 +
    1.98 +  static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
    1.99 +    return c16memcpy(s1, s2, n);
   1.100 +  }
   1.101 +
   1.102 +  static char_type* assign(char_type* s, size_t n, char_type a) {
   1.103 +    return c16memset(s, a, n);
   1.104 +  }
   1.105 +
   1.106 +  static int_type not_eof(const int_type& c) {
   1.107 +    return eq_int_type(c, eof()) ? 0 : c;
   1.108 +  }
   1.109 +
   1.110 +  static char_type to_char_type(const int_type& c) {
   1.111 +    return char_type(c);
   1.112 +  }
   1.113 +
   1.114 +  static int_type to_int_type(const char_type& c) {
   1.115 +    return int_type(c);
   1.116 +  }
   1.117 +
   1.118 +  static bool eq_int_type(const int_type& c1, const int_type& c2) {
   1.119 +    return c1 == c2;
   1.120 +  }
   1.121 +
   1.122 +  static int_type eof() {
   1.123 +    return static_cast<int_type>(EOF);
   1.124 +  }
   1.125 +};
   1.126 +
   1.127 +}  // namespace base
   1.128 +
   1.129 +// The string class will be explicitly instantiated only once, in string16.cc.
   1.130 +//
   1.131 +// std::basic_string<> in GNU libstdc++ contains a static data member,
   1.132 +// _S_empty_rep_storage, to represent empty strings.  When an operation such
   1.133 +// as assignment or destruction is performed on a string, causing its existing
   1.134 +// data member to be invalidated, it must not be freed if this static data
   1.135 +// member is being used.  Otherwise, it counts as an attempt to free static
   1.136 +// (and not allocated) data, which is a memory error.
   1.137 +//
   1.138 +// Generally, due to C++ template magic, _S_empty_rep_storage will be marked
   1.139 +// as a coalesced symbol, meaning that the linker will combine multiple
   1.140 +// instances into a single one when generating output.
   1.141 +//
   1.142 +// If a string class is used by multiple shared libraries, a problem occurs.
   1.143 +// Each library will get its own copy of _S_empty_rep_storage.  When strings
   1.144 +// are passed across a library boundary for alteration or destruction, memory
   1.145 +// errors will result.  GNU libstdc++ contains a configuration option,
   1.146 +// --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which
   1.147 +// disables the static data member optimization, but it's a good optimization
   1.148 +// and non-STL code is generally at the mercy of the system's STL
   1.149 +// configuration.  Fully-dynamic strings are not the default for GNU libstdc++
   1.150 +// libstdc++ itself or for the libstdc++ installations on the systems we care
   1.151 +// about, such as Mac OS X and relevant flavors of Linux.
   1.152 +//
   1.153 +// See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 .
   1.154 +//
   1.155 +// To avoid problems, string classes need to be explicitly instantiated only
   1.156 +// once, in exactly one library.  All other string users see it via an "extern"
   1.157 +// declaration.  This is precisely how GNU libstdc++ handles
   1.158 +// std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring).
   1.159 +//
   1.160 +// This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2),
   1.161 +// in which the linker does not fully coalesce symbols when dead code
   1.162 +// stripping is enabled.  This bug causes the memory errors described above
   1.163 +// to occur even when a std::basic_string<> does not cross shared library
   1.164 +// boundaries, such as in statically-linked executables.
   1.165 +//
   1.166 +// TODO(mark): File this bug with Apple and update this note with a bug number.
   1.167 +
   1.168 +extern template class std::basic_string<char16, base::string16_char_traits>;
   1.169 +
   1.170 +typedef std::basic_string<char16, base::string16_char_traits> string16;
   1.171 +
   1.172 +extern std::ostream& operator<<(std::ostream& out, const string16& str);
   1.173 +
   1.174 +#endif  // WCHAR_T_IS_UTF32
   1.175 +
   1.176 +#endif  // BASE_STRING16_H_

mercurial