2018-08-22 03:50:48 +03:00
|
|
|
/*
|
2019-06-20 02:58:25 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-08-22 03:50:48 +03:00
|
|
|
*
|
2019-06-20 02:58:25 +03:00
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
2018-08-22 03:50:48 +03:00
|
|
|
*/
|
2019-10-11 15:26:59 +03:00
|
|
|
|
2018-08-22 03:50:48 +03:00
|
|
|
#pragma once
|
2018-11-26 19:16:38 +03:00
|
|
|
#include "folly/portability/Windows.h"
|
2018-08-22 03:50:48 +03:00
|
|
|
|
2018-11-26 19:16:38 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2018-08-22 03:50:48 +03:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2020-09-17 19:06:09 +03:00
|
|
|
#include "eden/fs/utils/WinError.h"
|
2019-11-18 22:28:44 +03:00
|
|
|
#include "folly/Range.h"
|
|
|
|
#include "folly/String.h"
|
2018-08-22 03:50:48 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
2018-11-26 19:16:38 +03:00
|
|
|
namespace eden {
|
2018-08-22 03:50:48 +03:00
|
|
|
|
2020-10-01 02:27:50 +03:00
|
|
|
/**
|
|
|
|
* Convert a wide string to a utf-8 encoded string.
|
|
|
|
*/
|
2020-08-10 18:51:37 +03:00
|
|
|
template <class MultiByteStringType>
|
|
|
|
MultiByteStringType wideToMultibyteString(std::wstring_view wideCharPiece) {
|
2019-11-18 22:28:44 +03:00
|
|
|
if (wideCharPiece.empty()) {
|
2020-02-11 22:11:03 +03:00
|
|
|
return MultiByteStringType{};
|
2018-08-22 03:50:48 +03:00
|
|
|
}
|
|
|
|
|
2020-03-26 20:27:21 +03:00
|
|
|
int inputSize = folly::to_narrow(folly::to_signed(wideCharPiece.size()));
|
|
|
|
|
2018-11-26 19:16:38 +03:00
|
|
|
// To avoid extra copy or using max size buffers we should get the size first
|
|
|
|
// and allocate the right size buffer.
|
2019-11-18 22:28:44 +03:00
|
|
|
int size = WideCharToMultiByte(
|
2020-03-26 20:27:21 +03:00
|
|
|
CP_UTF8, 0, wideCharPiece.data(), inputSize, nullptr, 0, 0, 0);
|
2018-11-26 19:16:38 +03:00
|
|
|
|
|
|
|
if (size > 0) {
|
2020-02-11 22:11:03 +03:00
|
|
|
MultiByteStringType multiByteString(size, 0);
|
2020-03-26 20:27:21 +03:00
|
|
|
int resultSize = WideCharToMultiByte(
|
2019-11-18 22:28:44 +03:00
|
|
|
CP_UTF8,
|
|
|
|
0,
|
|
|
|
wideCharPiece.data(),
|
2020-03-26 20:27:21 +03:00
|
|
|
inputSize,
|
2019-11-18 22:28:44 +03:00
|
|
|
multiByteString.data(),
|
2020-03-26 20:27:21 +03:00
|
|
|
size,
|
2019-11-18 22:28:44 +03:00
|
|
|
0,
|
|
|
|
0);
|
2020-03-26 20:27:21 +03:00
|
|
|
if (size == resultSize) {
|
2018-11-26 19:16:38 +03:00
|
|
|
return multiByteString;
|
|
|
|
}
|
2018-08-22 03:50:48 +03:00
|
|
|
}
|
2019-03-13 02:27:51 +03:00
|
|
|
throw makeWin32ErrorExplicit(
|
|
|
|
GetLastError(), "Failed to convert wide char to char");
|
2018-11-26 19:16:38 +03:00
|
|
|
}
|
2018-08-22 03:50:48 +03:00
|
|
|
|
2019-11-18 22:28:44 +03:00
|
|
|
/**
|
2020-10-01 02:27:50 +03:00
|
|
|
* Convert a utf-8 encoded string to a wide string.
|
2019-11-18 22:28:44 +03:00
|
|
|
*/
|
2020-10-01 02:27:50 +03:00
|
|
|
std::wstring multibyteToWideString(folly::StringPiece multiBytePiece);
|
2018-11-26 19:16:38 +03:00
|
|
|
|
|
|
|
} // namespace eden
|
2018-08-22 03:50:48 +03:00
|
|
|
} // namespace facebook
|