sapling/eden/fs/utils/StringConv.cpp
Xavier Deguillard ee771fa740 utils: move multibyteToWideString to a cpp file
Summary:
There is no good reason for this function to be in a header, let's move it to
StringConv.cpp

Reviewed By: genevievehelsel

Differential Revision: D24000882

fbshipit-source-id: 5bb6bc3b9ef37232d38b8e35da693e12c0453ea1
2020-09-30 16:29:13 -07:00

41 lines
1.0 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#ifdef _WIN32
#include "eden/fs/utils/StringConv.h"
namespace facebook::eden {
std::wstring multibyteToWideString(folly::StringPiece multiBytePiece) {
if (multiBytePiece.empty()) {
return L"";
}
int inputSize = folly::to_narrow(folly::to_signed(multiBytePiece.size()));
// To avoid extra copy or using max size buffers we should get the size
// first and allocate the right size buffer.
int size = MultiByteToWideChar(
CP_UTF8, 0, multiBytePiece.data(), inputSize, nullptr, 0);
if (size > 0) {
std::wstring wideString(size, 0);
int resultSize = MultiByteToWideChar(
CP_UTF8, 0, multiBytePiece.data(), inputSize, wideString.data(), size);
if (size == resultSize) {
return wideString;
}
}
throw makeWin32ErrorExplicit(
GetLastError(), "Failed to convert char to wide char");
}
} // namespace facebook::eden
#endif