ipc/chromium/src/base/sys_string_conversions_win.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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.
     5 #include "base/sys_string_conversions.h"
     7 #include <windows.h>
     9 #include "base/string_piece.h"
    11 namespace base {
    13 // Do not assert in this function since it is used by the asssertion code!
    14 std::string SysWideToUTF8(const std::wstring& wide) {
    15   return SysWideToMultiByte(wide, CP_UTF8);
    16 }
    18 // Do not assert in this function since it is used by the asssertion code!
    19 std::wstring SysUTF8ToWide(const StringPiece& utf8) {
    20   return SysMultiByteToWide(utf8, CP_UTF8);
    21 }
    23 std::string SysWideToNativeMB(const std::wstring& wide) {
    24   return SysWideToMultiByte(wide, CP_ACP);
    25 }
    27 std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
    28   return SysMultiByteToWide(native_mb, CP_ACP);
    29 }
    31 // Do not assert in this function since it is used by the asssertion code!
    32 std::wstring SysMultiByteToWide(const StringPiece& mb, uint32_t code_page) {
    33   if (mb.empty())
    34     return std::wstring();
    36   int mb_length = static_cast<int>(mb.length());
    37   // Compute the length of the buffer.
    38   int charcount = MultiByteToWideChar(code_page, 0,
    39                                       mb.data(), mb_length, NULL, 0);
    40   if (charcount == 0)
    41     return std::wstring();
    43   std::wstring wide;
    44   wide.resize(charcount);
    45   MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount);
    47   return wide;
    48 }
    50 // Do not assert in this function since it is used by the asssertion code!
    51 std::string SysWideToMultiByte(const std::wstring& wide, uint32_t code_page) {
    52   int wide_length = static_cast<int>(wide.length());
    53   if (wide_length == 0)
    54     return std::string();
    56   // Compute the length of the buffer we'll need.
    57   int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
    58                                       NULL, 0, NULL, NULL);
    59   if (charcount == 0)
    60     return std::string();
    62   std::string mb;
    63   mb.resize(charcount);
    64   WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
    65                       &mb[0], charcount, NULL, NULL);
    67   return mb;
    68 }
    70 }  // namespace base

mercurial