blob: 356064fb98fcac558ce13043bf3ed5d6d8a5c057 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.
[email protected]15af80e2008-08-07 03:11:424
[email protected]9fe1a5b2013-02-07 19:18:035#include "base/strings/sys_string_conversions.h"
[email protected]15af80e2008-08-07 03:11:426
7#include <windows.h>
avi84f37e12015-12-25 09:31:428#include <stdint.h>
[email protected]15af80e2008-08-07 03:11:429
[email protected]eb62f7262013-03-30 14:29:0010#include "base/strings/string_piece.h"
[email protected]4bdaceb42008-08-19 13:19:2411
[email protected]15af80e2008-08-07 03:11:4212namespace base {
13
14// Do not assert in this function since it is used by the asssertion code!
15std::string SysWideToUTF8(const std::wstring& wide) {
16 return SysWideToMultiByte(wide, CP_UTF8);
17}
18
19// Do not assert in this function since it is used by the asssertion code!
Reilly Grant39aecc32018-01-04 00:52:5220std::wstring SysUTF8ToWide(StringPiece utf8) {
[email protected]15af80e2008-08-07 03:11:4221 return SysMultiByteToWide(utf8, CP_UTF8);
22}
23
24std::string SysWideToNativeMB(const std::wstring& wide) {
25 return SysWideToMultiByte(wide, CP_ACP);
26}
27
Reilly Grant39aecc32018-01-04 00:52:5228std::wstring SysNativeMBToWide(StringPiece native_mb) {
[email protected]15af80e2008-08-07 03:11:4229 return SysMultiByteToWide(native_mb, CP_ACP);
30}
31
32// Do not assert in this function since it is used by the asssertion code!
Reilly Grant39aecc32018-01-04 00:52:5233std::wstring SysMultiByteToWide(StringPiece mb, uint32_t code_page) {
[email protected]4bdaceb42008-08-19 13:19:2434 if (mb.empty())
[email protected]15af80e2008-08-07 03:11:4235 return std::wstring();
36
[email protected]4bdaceb42008-08-19 13:19:2437 int mb_length = static_cast<int>(mb.length());
[email protected]15af80e2008-08-07 03:11:4238 // Compute the length of the buffer.
39 int charcount = MultiByteToWideChar(code_page, 0,
40 mb.data(), mb_length, NULL, 0);
41 if (charcount == 0)
42 return std::wstring();
43
44 std::wstring wide;
45 wide.resize(charcount);
46 MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount);
47
48 return wide;
49}
50
51// Do not assert in this function since it is used by the asssertion code!
avi84f37e12015-12-25 09:31:4252std::string SysWideToMultiByte(const std::wstring& wide, uint32_t code_page) {
[email protected]15af80e2008-08-07 03:11:4253 int wide_length = static_cast<int>(wide.length());
54 if (wide_length == 0)
55 return std::string();
56
57 // Compute the length of the buffer we'll need.
58 int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
59 NULL, 0, NULL, NULL);
60 if (charcount == 0)
61 return std::string();
62
63 std::string mb;
64 mb.resize(charcount);
65 WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
66 &mb[0], charcount, NULL, NULL);
67
68 return mb;
69}
70
71} // namespace base