michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/sys_string_conversions.h" michael@0: michael@0: #include michael@0: michael@0: #include "base/string_piece.h" michael@0: michael@0: namespace base { michael@0: michael@0: // Do not assert in this function since it is used by the asssertion code! michael@0: std::string SysWideToUTF8(const std::wstring& wide) { michael@0: return SysWideToMultiByte(wide, CP_UTF8); michael@0: } michael@0: michael@0: // Do not assert in this function since it is used by the asssertion code! michael@0: std::wstring SysUTF8ToWide(const StringPiece& utf8) { michael@0: return SysMultiByteToWide(utf8, CP_UTF8); michael@0: } michael@0: michael@0: std::string SysWideToNativeMB(const std::wstring& wide) { michael@0: return SysWideToMultiByte(wide, CP_ACP); michael@0: } michael@0: michael@0: std::wstring SysNativeMBToWide(const StringPiece& native_mb) { michael@0: return SysMultiByteToWide(native_mb, CP_ACP); michael@0: } michael@0: michael@0: // Do not assert in this function since it is used by the asssertion code! michael@0: std::wstring SysMultiByteToWide(const StringPiece& mb, uint32_t code_page) { michael@0: if (mb.empty()) michael@0: return std::wstring(); michael@0: michael@0: int mb_length = static_cast(mb.length()); michael@0: // Compute the length of the buffer. michael@0: int charcount = MultiByteToWideChar(code_page, 0, michael@0: mb.data(), mb_length, NULL, 0); michael@0: if (charcount == 0) michael@0: return std::wstring(); michael@0: michael@0: std::wstring wide; michael@0: wide.resize(charcount); michael@0: MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount); michael@0: michael@0: return wide; michael@0: } michael@0: michael@0: // Do not assert in this function since it is used by the asssertion code! michael@0: std::string SysWideToMultiByte(const std::wstring& wide, uint32_t code_page) { michael@0: int wide_length = static_cast(wide.length()); michael@0: if (wide_length == 0) michael@0: return std::string(); michael@0: michael@0: // Compute the length of the buffer we'll need. michael@0: int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length, michael@0: NULL, 0, NULL, NULL); michael@0: if (charcount == 0) michael@0: return std::string(); michael@0: michael@0: std::string mb; michael@0: mb.resize(charcount); michael@0: WideCharToMultiByte(code_page, 0, wide.data(), wide_length, michael@0: &mb[0], charcount, NULL, NULL); michael@0: michael@0: return mb; michael@0: } michael@0: michael@0: } // namespace base