mirror of
https://github.com/facebook/sapling.git
synced 2024-10-11 01:07:15 +03:00
ee771fa740
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
64 lines
1.5 KiB
C++
64 lines
1.5 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.
|
|
*/
|
|
|
|
#pragma once
|
|
#include "folly/portability/Windows.h"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <memory>
|
|
#include <string>
|
|
#include "eden/fs/utils/WinError.h"
|
|
#include "folly/Range.h"
|
|
#include "folly/String.h"
|
|
|
|
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()) {
|
|
return MultiByteStringType{};
|
|
}
|
|
|
|
int inputSize = folly::to_narrow(folly::to_signed(wideCharPiece.size()));
|
|
|
|
// To avoid extra copy or using max size buffers we should get the size first
|
|
// and allocate the right size buffer.
|
|
int size = WideCharToMultiByte(
|
|
CP_UTF8, 0, wideCharPiece.data(), inputSize, nullptr, 0, 0, 0);
|
|
|
|
if (size > 0) {
|
|
MultiByteStringType multiByteString(size, 0);
|
|
int resultSize = WideCharToMultiByte(
|
|
CP_UTF8,
|
|
0,
|
|
wideCharPiece.data(),
|
|
inputSize,
|
|
multiByteString.data(),
|
|
size,
|
|
0,
|
|
0);
|
|
if (size == resultSize) {
|
|
return multiByteString;
|
|
}
|
|
}
|
|
throw makeWin32ErrorExplicit(
|
|
GetLastError(), "Failed to convert wide char to char");
|
|
}
|
|
|
|
/**
|
|
* Convert a utf-8 encoded string to a wide string.
|
|
*/
|
|
std::wstring multibyteToWideString(folly::StringPiece multiBytePiece);
|
|
|
|
} // namespace eden
|
|
} // namespace facebook
|