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