Commit d7ba0791 authored by Alex Snast's avatar Alex Snast Committed by Facebook GitHub Bot

Enable FixedString construction from a string_view

Summary: `FixedString` should be constructable from a `std::string_view`. This allows passing `std::string_view` as key to `.emplace` in heterogeneous maps where the key is a `FixedString`.

Reviewed By: yfeldblum

Differential Revision: D29456948

fbshipit-source-id: 4d2e428c7de05c1dc34b50727f8107aec915b632
parent aa605f8e
......@@ -669,6 +669,21 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
: BasicFixedString{
that, detail::fixedstring::checkOverflow(count, N), Indices{}} {}
#if FOLLY_HAS_STRING_VIEW
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
* Construct from a `std::basic_string_view<Char>`
* \param that The source basic_string_view
* \pre `that.size() <= N`
* \post `size() == that.size()`
* \post `0 == strncmp(data(), that.begin(), size())`
* \post `at(size()) == Char(0)`
* \throw std::out_of_range when that.size() > N
*/
constexpr /* implicit */ BasicFixedString(
std::basic_string_view<Char> that) noexcept(false)
: BasicFixedString{that.data(), that.size()} {}
#endif
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
* Construct an BasicFixedString that contains `count` characters, all
* of which are `ch`.
......
......@@ -109,6 +109,16 @@ TEST(FixedStringCtorTest, FromStringOffsetAndCount) {
// constexpr folly::FixedString<5> s4{s, 6, 6};
}
#if FOLLY_HAS_STRING_VIEW
TEST(FixedStringCtorTest, FromStringView) {
constexpr folly::FixedString<11> s{
std::string_view{"hello world"},
};
static_assert(s == "hello world", "");
static_assert(s.size() == 11u, "");
}
#endif
TEST(FixedStringCtorTest, FromInitializerList) {
constexpr folly::FixedString<11> s{
'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment