| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/strings/sys_string_conversions.h" | 5 #include "base/strings/sys_string_conversions.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <stdint.h> |
| 8 | 9 |
| 9 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 | 13 |
| 13 // Do not assert in this function since it is used by the asssertion code! | 14 // Do not assert in this function since it is used by the asssertion code! |
| 14 std::string SysWideToUTF8(const std::wstring& wide) { | 15 std::string SysWideToUTF8(const std::wstring& wide) { |
| 15 return SysWideToMultiByte(wide, CP_UTF8); | 16 return SysWideToMultiByte(wide, CP_UTF8); |
| 16 } | 17 } |
| 17 | 18 |
| 18 // Do not assert in this function since it is used by the asssertion code! | 19 // Do not assert in this function since it is used by the asssertion code! |
| 19 std::wstring SysUTF8ToWide(const StringPiece& utf8) { | 20 std::wstring SysUTF8ToWide(const StringPiece& utf8) { |
| 20 return SysMultiByteToWide(utf8, CP_UTF8); | 21 return SysMultiByteToWide(utf8, CP_UTF8); |
| 21 } | 22 } |
| 22 | 23 |
| 23 std::string SysWideToNativeMB(const std::wstring& wide) { | 24 std::string SysWideToNativeMB(const std::wstring& wide) { |
| 24 return SysWideToMultiByte(wide, CP_ACP); | 25 return SysWideToMultiByte(wide, CP_ACP); |
| 25 } | 26 } |
| 26 | 27 |
| 27 std::wstring SysNativeMBToWide(const StringPiece& native_mb) { | 28 std::wstring SysNativeMBToWide(const StringPiece& native_mb) { |
| 28 return SysMultiByteToWide(native_mb, CP_ACP); | 29 return SysMultiByteToWide(native_mb, CP_ACP); |
| 29 } | 30 } |
| 30 | 31 |
| 31 // Do not assert in this function since it is used by the asssertion code! | 32 // Do not assert in this function since it is used by the asssertion code! |
| 32 std::wstring SysMultiByteToWide(const StringPiece& mb, uint32 code_page) { | 33 std::wstring SysMultiByteToWide(const StringPiece& mb, uint32_t code_page) { |
| 33 if (mb.empty()) | 34 if (mb.empty()) |
| 34 return std::wstring(); | 35 return std::wstring(); |
| 35 | 36 |
| 36 int mb_length = static_cast<int>(mb.length()); | 37 int mb_length = static_cast<int>(mb.length()); |
| 37 // Compute the length of the buffer. | 38 // Compute the length of the buffer. |
| 38 int charcount = MultiByteToWideChar(code_page, 0, | 39 int charcount = MultiByteToWideChar(code_page, 0, |
| 39 mb.data(), mb_length, NULL, 0); | 40 mb.data(), mb_length, NULL, 0); |
| 40 if (charcount == 0) | 41 if (charcount == 0) |
| 41 return std::wstring(); | 42 return std::wstring(); |
| 42 | 43 |
| 43 std::wstring wide; | 44 std::wstring wide; |
| 44 wide.resize(charcount); | 45 wide.resize(charcount); |
| 45 MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount); | 46 MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount); |
| 46 | 47 |
| 47 return wide; | 48 return wide; |
| 48 } | 49 } |
| 49 | 50 |
| 50 // Do not assert in this function since it is used by the asssertion code! | 51 // Do not assert in this function since it is used by the asssertion code! |
| 51 std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page) { | 52 std::string SysWideToMultiByte(const std::wstring& wide, uint32_t code_page) { |
| 52 int wide_length = static_cast<int>(wide.length()); | 53 int wide_length = static_cast<int>(wide.length()); |
| 53 if (wide_length == 0) | 54 if (wide_length == 0) |
| 54 return std::string(); | 55 return std::string(); |
| 55 | 56 |
| 56 // Compute the length of the buffer we'll need. | 57 // Compute the length of the buffer we'll need. |
| 57 int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length, | 58 int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length, |
| 58 NULL, 0, NULL, NULL); | 59 NULL, 0, NULL, NULL); |
| 59 if (charcount == 0) | 60 if (charcount == 0) |
| 60 return std::string(); | 61 return std::string(); |
| 61 | 62 |
| 62 std::string mb; | 63 std::string mb; |
| 63 mb.resize(charcount); | 64 mb.resize(charcount); |
| 64 WideCharToMultiByte(code_page, 0, wide.data(), wide_length, | 65 WideCharToMultiByte(code_page, 0, wide.data(), wide_length, |
| 65 &mb[0], charcount, NULL, NULL); | 66 &mb[0], charcount, NULL, NULL); |
| 66 | 67 |
| 67 return mb; | 68 return mb; |
| 68 } | 69 } |
| 69 | 70 |
| 70 } // namespace base | 71 } // namespace base |
| OLD | NEW |