Skip to content

Commit

Permalink
mark legacy svformat as deleted
Browse files Browse the repository at this point in the history
Reviewed By: Mizuchi, skrueger

Differential Revision: D67628110

fbshipit-source-id: d9665246bd8f4fe286d084713f511199af061c25
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jan 7, 2025
1 parent 521d9b3 commit 9d7d9fb
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 60 deletions.
25 changes: 1 addition & 24 deletions folly/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Formatter;
template <class... Args>
Formatter<false, Args...> format(StringPiece fmt, Args&&... args);
template <class C>
std::string svformat(StringPiece fmt, C&& container);
std::string svformat(StringPiece fmt, C&& container) = delete;
template <class T, class Enable = void>
class FormatValue;

Expand Down Expand Up @@ -302,29 +302,6 @@ inline std::string sformat(StringPiece fmt, Args&&... args) {
return Formatter<false, Args...>(fmt, static_cast<Args&&>(args)...).str();
}

/**
* Create a formatter object that takes one argument (of container type)
* and uses that container to get argument values from.
*
* std::map<string, string> map { {"hello", "world"}, {"answer", "42"} };
*
* The following are equivalent:
* sformat("{0[hello]} {0[answer]}", map);
*
* svformat("{hello} {answer}", map);
*
* but the latter is cleaner.
*/
template <class Container>
[[deprecated(
"Use fmt::format instead of folly::svformat for better performance, build "
"times and compatibility with std::format")]] //
inline std::string
svformat(StringPiece fmt, Container&& container) {
return Formatter<true, Container>(fmt, static_cast<Container&&>(container))
.str();
}

/**
* Exception class thrown when a format key is not found in the given
* associative container keyed by strings. We inherit std::out_of_range for
Expand Down
3 changes: 0 additions & 3 deletions folly/test/FormatOtherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ void testFormatSeq() {
T v{10, 20, 30};
EXPECT_EQ("30 10", sformat("{0[2]} {0[0]}", v));
EXPECT_EQ("0020", sformat("{0[1]:04}", v));
EXPECT_EQ("0020", svformat("{1:04}", v));
EXPECT_EQ("10 20", svformat("{} {}", v));
EXPECT_EQ("10 20 0030", svformat("{} {} {:04}", v));
}

} // namespace
Expand Down
33 changes: 0 additions & 33 deletions folly/test/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,67 +118,41 @@ TEST(Format, Simple) {

std::vector<int> v1{10, 20, 30};
EXPECT_EQ("0020", sformat("{0[1]:04}", v1));
EXPECT_EQ("0020", svformat("{1:04}", v1));
EXPECT_EQ("10 20", svformat("{} {}", v1));

const std::vector<int> v2 = v1;
EXPECT_EQ("0020", sformat("{0[1]:04}", v2));
EXPECT_EQ("0020", svformat("{1:04}", v2));
EXPECT_THROW(sformat("{0[3]:04}", v2), std::out_of_range);
EXPECT_THROW(svformat("{3:04}", v2), std::out_of_range);
EXPECT_EQ("0020", sformat("{0[1]:04}", defaulted(v2, 42)));
EXPECT_EQ("0020", svformat("{1:04}", defaulted(v2, 42)));
EXPECT_EQ("0042", sformat("{0[3]:04}", defaulted(v2, 42)));
EXPECT_EQ("0042", svformat("{3:04}", defaulted(v2, 42)));

{
const int p[] = {10, 20, 30};
const int* q = p;
EXPECT_EQ("0020", sformat("{0[1]:04}", p));
EXPECT_EQ("0020", svformat("{1:04}", p));
EXPECT_EQ("0020", sformat("{0[1]:04}", q));
EXPECT_EQ("0020", svformat("{1:04}", q));
EXPECT_NE("", sformat("{}", q));

EXPECT_EQ("0x", sformat("{}", p).substr(0, 2));
EXPECT_EQ("10", svformat("{}", p));
EXPECT_EQ("0x", sformat("{}", q).substr(0, 2));
EXPECT_EQ("10", svformat("{}", q));
q = nullptr;
EXPECT_EQ("(null)", sformat("{}", q));
}

std::map<int, std::string> m{{10, "hello"}, {20, "world"}};
EXPECT_EQ("worldXX", sformat("{[20]:X<7}", m));
EXPECT_EQ("worldXX", svformat("{20:X<7}", m));
EXPECT_THROW(sformat("{[42]:X<7}", m), std::out_of_range);
EXPECT_THROW(svformat("{42:X<7}", m), std::out_of_range);
EXPECT_EQ("worldXX", sformat("{[20]:X<7}", defaulted(m, "meow")));
EXPECT_EQ("worldXX", svformat("{20:X<7}", defaulted(m, "meow")));
EXPECT_EQ("meowXXX", sformat("{[42]:X<7}", defaulted(m, "meow")));
EXPECT_EQ("meowXXX", svformat("{42:X<7}", defaulted(m, "meow")));

std::map<std::string, std::string> m2{{"hello", "world"}};
EXPECT_EQ("worldXX", sformat("{[hello]:X<7}", m2));
EXPECT_EQ("worldXX", svformat("{hello:X<7}", m2));
EXPECT_THROW(sformat("{[none]:X<7}", m2), std::out_of_range);
EXPECT_THROW(svformat("{none:X<7}", m2), std::out_of_range);
EXPECT_EQ("worldXX", sformat("{[hello]:X<7}", defaulted(m2, "meow")));
EXPECT_EQ("worldXX", svformat("{hello:X<7}", defaulted(m2, "meow")));
EXPECT_EQ("meowXXX", sformat("{[none]:X<7}", defaulted(m2, "meow")));
EXPECT_EQ("meowXXX", svformat("{none:X<7}", defaulted(m2, "meow")));
try {
svformat("{none:X<7}", m2);
EXPECT_FALSE(true) << "svformat should throw on missing key";
} catch (const FormatKeyNotFoundException& e) {
EXPECT_STREQ("none", e.key());
}

// Test indexing in strings
EXPECT_EQ("61 62", sformat("{0[0]:x} {0[1]:x}", "abcde"));
EXPECT_EQ("61 62", svformat("{0:x} {1:x}", "abcde"));
EXPECT_EQ("61 62", sformat("{0[0]:x} {0[1]:x}", std::string("abcde")));
EXPECT_EQ("61 62", svformat("{0:x} {1:x}", std::string("abcde")));

// Test booleans
EXPECT_EQ("true", sformat("{}", true));
Expand All @@ -190,14 +164,12 @@ TEST(Format, Simple) {
{
std::pair<int, std::string> p{42, "hello"};
EXPECT_EQ(" 42 hello ", sformat("{0[0]:6} {0[1]:6}", p));
EXPECT_EQ(" 42 hello ", svformat("{:6} {:6}", p));
}

// Test tuples
{
std::tuple<int, std::string, int> t{42, "hello", 23};
EXPECT_EQ(" 42 hello 23", sformat("{0[0]:6} {0[1]:6} {0[2]:6}", t));
EXPECT_EQ(" 42 hello 23", svformat("{:6} {:6} {:6}", t));
}

// Test writing to stream
Expand Down Expand Up @@ -434,7 +406,6 @@ TEST(Format, OutOfBounds) {
std::map<std::string, int> map{{"hello", 0}, {"world", 1}};
EXPECT_EQ("hello = 0", sformat("hello = {[hello]}", map));
EXPECT_THROW(sformat("{[nope]}", map), std::out_of_range);
EXPECT_THROW(svformat("{nope}", map), std::out_of_range);
}

TEST(Format, BogusFormatString) {
Expand All @@ -458,10 +429,6 @@ TEST(Format, BogusFormatString) {
sformat("{0:*}", 12, "ok"),
"cannot provide value arg index without width arg index");

std::vector<int> v{1, 2, 3};
EXPECT_FORMAT_ERROR(
svformat("{:*}", v), "dynamic field width not supported in vformat()");

// This one fails in detail::enforceWhitespace(), which throws
// std::range_error
EXPECT_FORMAT_ERROR(sformat("{0[test}"), "argument index must be integer");
Expand Down

0 comments on commit 9d7d9fb

Please sign in to comment.