Skip to content

Commit

Permalink
fmt_make_format_args_from_map
Browse files Browse the repository at this point in the history
Summary: A helper function to make an object of format-args type suitable for passing to `fmt::vformat` from an input map.

Reviewed By: Gownta

Differential Revision: D67314712

fbshipit-source-id: 60fef13adc8b946917cbca7ffc5fbf3dffae882b
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Dec 18, 2024
1 parent 1375283 commit 5d060f7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions third-party/folly/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ if (BUILD_TESTS OR BUILD_BENCHMARKS)
BENCHMARK fingerprint_benchmark SOURCES FingerprintBenchmark.cpp
TEST fingerprint_test SOURCES FingerprintTest.cpp
TEST fixed_string_test SOURCES FixedStringTest.cpp
TEST fmt_utility_test SOURCES FmtUtilityTest.cpp
TEST format_other_test SOURCES FormatOtherTest.cpp
BENCHMARK format_benchmark SOURCES FormatBenchmark.cpp
TEST format_test SOURCES FormatTest.cpp
Expand Down
47 changes: 47 additions & 0 deletions third-party/folly/src/folly/FmtUtility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <fmt/args.h>

#include <folly/CppAttributes.h>

namespace folly {

/// fmt_make_format_args_from_map_fn
/// fmt_make_format_args_from_map
///
/// A helper function-object type and variable for making a format-args object
/// from a map.
///
/// May be useful for transitioning from legacy folly::svformat to fmt::vformat.
struct fmt_make_format_args_from_map_fn {
template <typename Map>
fmt::dynamic_format_arg_store<fmt::format_context> operator()(
[[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] Map const& map) const {
fmt::dynamic_format_arg_store<fmt::format_context> ret;
ret.reserve(map.size(), map.size());
for (auto const& [key, val] : map) {
ret.push_back(fmt::arg(key.c_str(), std::cref(val)));
}
return ret;
}
};
inline constexpr fmt_make_format_args_from_map_fn
fmt_make_format_args_from_map{};

} // namespace folly
39 changes: 39 additions & 0 deletions third-party/folly/src/folly/test/FmtUtilityTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <folly/FmtUtility.h>

#include <map>
#include <string>
#include <string_view>

#include <gtest/gtest.h>

using namespace std::literals;

struct FmtUtilityTest : testing::Test {};

TEST_F(FmtUtilityTest, fmt_make_format_args_from_map_fn) {
EXPECT_EQ(
"hello bob you silly goose",
fmt::vformat(
"hello {name} you {adj} goose",
folly::fmt_make_format_args_from_map(
std::map<std::string, std::string_view>{
{"name"s, "bob"sv},
{"adj"s, "silly"sv},
})));
}

0 comments on commit 5d060f7

Please sign in to comment.