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 074cd40 commit 8a0687a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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
11 changes: 11 additions & 0 deletions folly/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ cpp_library(
],
)

cpp_library(
name = "fmt_utility",
headers = [
"FmtUtility.h",
],
exported_deps = [
"fbsource//third-party/fmt:fmt",
":cpp_attributes",
],
)

cpp_library(
name = "format",
srcs = [
Expand Down
47 changes: 47 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions folly/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,14 @@ cpp_unittest(
],
)

cpp_unittest(
name = "fmt_utility_test",
srcs = ["FmtUtilityTest.cpp"],
deps = [
"//folly:fmt_utility",
],
)

cpp_benchmark(
name = "format_benchmark",
srcs = ["FormatBenchmark.cpp"],
Expand Down
39 changes: 39 additions & 0 deletions 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 8a0687a

Please sign in to comment.