LCOV - code coverage report
Current view: top level - ipc/chromium/src/base - string16.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 17 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13