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 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #include "WindowsUnicode.h"
18 mozilla::UniquePtr
<char[]> WideToUTF8(const wchar_t* aStr
,
19 const size_t aStrLenExclNul
) {
20 int numConv
= ::WideCharToMultiByte(CP_UTF8
, 0, aStr
, aStrLenExclNul
, nullptr,
26 // Include room for the null terminator by adding one
27 auto buf
= mozilla::MakeUnique
<char[]>(numConv
+ 1);
29 numConv
= ::WideCharToMultiByte(CP_UTF8
, 0, aStr
, aStrLenExclNul
, buf
.get(),
30 numConv
, nullptr, nullptr);
35 // Add null termination. numConv does not include the terminator, so we don't
36 // subtract 1 when indexing into buf.
42 mozilla::UniquePtr
<char[]> WideToUTF8(const wchar_t* aStr
) {
43 return WideToUTF8(aStr
, wcslen(aStr
));
46 mozilla::UniquePtr
<char[]> WideToUTF8(const std::wstring
& aStr
) {
47 return WideToUTF8(aStr
.data(), aStr
.length());
50 mozilla::UniquePtr
<char[]> WideToUTF8(PCUNICODE_STRING aStr
) {
55 return WideToUTF8(aStr
->Buffer
, aStr
->Length
/ sizeof(WCHAR
));
59 } // namespace mozilla