tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sys_string_conversions_win.cc (2227B)


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