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
This commit is contained in:
Xavier Deguillard 2020-09-30 16:27:50 -07:00 committed by Facebook GitHub Bot
parent 5048a060d6
commit ee771fa740
2 changed files with 45 additions and 26 deletions

View File

@ -0,0 +1,40 @@
/*
* 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

View File

@ -19,6 +19,9 @@
namespace facebook {
namespace eden {
/**
* Convert a wide string to a utf-8 encoded string.
*/
template <class MultiByteStringType>
MultiByteStringType wideToMultibyteString(std::wstring_view wideCharPiece) {
if (wideCharPiece.empty()) {
@ -52,33 +55,9 @@ MultiByteStringType wideToMultibyteString(std::wstring_view wideCharPiece) {
}
/**
* multibyteToWideString can take a multibyte char container like string,
* string_view, folly::StringPiece and return a widechar string in std::wstring.
* Convert a utf-8 encoded string to a wide string.
*/
static 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");
}
std::wstring multibyteToWideString(folly::StringPiece multiBytePiece);
} // namespace eden
} // namespace facebook