sapling/eden/fs/utils/Utf8.cpp
Xavier Deguillard a3115dacd8 utils: add a constexpr utf8 checker
Summary:
In a future diff, paths will be validated to make sure they are valid utf8. The
path sanity checker needs to be constexpr to construct global paths, but the
utf8 functions aren't, so let's write one that is.

Reviewed By: chadaustin

Differential Revision: D25562681

fbshipit-source-id: e48ec835c2cc9dc01090918cc7ee8f61b6c05a20
2020-12-16 01:03:32 -08:00

31 lines
820 B
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.
*/
#include "eden/fs/utils/Utf8.h"
#include <folly/Unicode.h>
namespace facebook {
namespace eden {
std::string ensureValidUtf8(folly::ByteRange str) {
std::string output;
output.reserve(str.size());
const unsigned char* begin = str.begin();
const unsigned char* const end = str.end();
while (begin != end) {
// codePointToUtf8 returns a std::string which is inefficient for something
// that always fits in 32 bits, but with SSO it probably never allocates at
// least.
output += folly::codePointToUtf8(folly::utf8ToCodePoint(begin, end, true));
}
return output;
}
} // namespace eden
} // namespace facebook