Commit 4460753f authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Anton Likhtarov

Add shorthand functions to Format.h.

Summary:
[Folly] Add shorthand functions to Format.h.

This makes calling code simpler when it does not depend on the raw performance of writing to a stream.

Test Plan:
$ fbconfig -r folly/test
$ fbmake runtests

Reviewed By: tudorb@fb.com

Subscribers: folly@lists, dougw

FB internal diff: D1347353

Tasks: 4391110
parent d82763a6
...@@ -207,6 +207,15 @@ Formatter<false, Args...> format(StringPiece fmt, Args&&... args) { ...@@ -207,6 +207,15 @@ Formatter<false, Args...> format(StringPiece fmt, Args&&... args) {
fmt, std::forward<Args>(args)...); fmt, std::forward<Args>(args)...);
} }
/**
* Like format(), but immediately returns the formatted string instead of an
* intermediate format object.
*/
template <class... Args>
inline std::string sformat(StringPiece fmt, Args&&... args) {
return format(fmt, std::forward<Args>(args)...).str();
}
/** /**
* Create a formatter object from a dynamic format string. * Create a formatter object from a dynamic format string.
* *
...@@ -222,6 +231,15 @@ Formatter<false, Args...> formatChecked(StringPiece fmt, Args&&... args) { ...@@ -222,6 +231,15 @@ Formatter<false, Args...> formatChecked(StringPiece fmt, Args&&... args) {
return f; return f;
} }
/**
* Like formatChecked(), but immediately returns the formatted string instead of
* an intermediate format object.
*/
template <class... Args>
inline std::string sformatChecked(StringPiece fmt, Args&&... args) {
return formatChecked(fmt, std::forward<Args>(args)...).str();
}
/** /**
* Create a formatter object that takes one argument (of container type) * Create a formatter object that takes one argument (of container type)
* and uses that container to get argument values from. * and uses that container to get argument values from.
...@@ -241,6 +259,15 @@ Formatter<true, Container> vformat(StringPiece fmt, Container&& container) { ...@@ -241,6 +259,15 @@ Formatter<true, Container> vformat(StringPiece fmt, Container&& container) {
fmt, std::forward<Container>(container)); fmt, std::forward<Container>(container));
} }
/**
* Like vformat(), but immediately returns the formatted string instead of an
* intermediate format object.
*/
template <class Container>
inline std::string svformat(StringPiece fmt, Container&& container) {
return vformat(fmt, std::forward<Container>(container)).str();
}
/** /**
* Create a formatter object from a dynamic format string. * Create a formatter object from a dynamic format string.
* *
...@@ -257,6 +284,15 @@ Formatter<true, Container> vformatChecked(StringPiece fmt, ...@@ -257,6 +284,15 @@ Formatter<true, Container> vformatChecked(StringPiece fmt,
return f; return f;
} }
/**
* Like vformatChecked(), but immediately returns the formatted string instead
* of an intermediate format object.
*/
template <class Container>
inline std::string svformatChecked(StringPiece fmt, Container&& container) {
return vformatChecked(fmt, std::forward<Container>(container)).str();
}
/** /**
* Wrap a sequence or associative container so that out-of-range lookups * Wrap a sequence or associative container so that out-of-range lookups
* return a default value rather than throwing an exception. * return a default value rather than throwing an exception.
......
...@@ -19,7 +19,9 @@ Here are some code samples to get started: ...@@ -19,7 +19,9 @@ Here are some code samples to get started:
``` Cpp ``` Cpp
using folly::format; using folly::format;
using folly::sformat;
using folly::vformat; using folly::vformat;
using folly::svformat;
// Objects produced by format() can be streamed without creating // Objects produced by format() can be streamed without creating
// an intermediary string; {} yields the next argument using default // an intermediary string; {} yields the next argument using default
...@@ -27,6 +29,10 @@ using folly::vformat; ...@@ -27,6 +29,10 @@ using folly::vformat;
std::cout << format("The answers are {} and {}", 23, 42); std::cout << format("The answers are {} and {}", 23, 42);
// => "The answers are 23 and 42" // => "The answers are 23 and 42"
// If you just want the string, though, you're covered.
std::string result = sformat("The answers are {} and {}", 23, 42);
// => "The answers are 23 and 42"
// To insert a literal '{' or '}', just double it. // To insert a literal '{' or '}', just double it.
std::cout << format("{} {{}} {{{}}}", 23, 42); std::cout << format("{} {{}} {{{}}}", 23, 42);
// => "23 {} {42}" // => "23 {} {42}"
...@@ -54,6 +60,11 @@ std::cout << vformat("The only {what} is {value}", m); ...@@ -54,6 +60,11 @@ std::cout << vformat("The only {what} is {value}", m);
// same as // same as
std::cout << format("The only {0[what]} is {0[value]}", m); std::cout << format("The only {0[what]} is {0[value]}", m);
// => "The only answer is 42" // => "The only answer is 42"
// And if you just want the string,
std::string result = svformat("The only {what} is {value}", m);
// => "The only answer is 42"
std::string result = svformat("The only {0[what]} is {0[value]}", m);
// => "The only answer is 42"
// {} works for vformat too // {} works for vformat too
std::vector<int> v {42, 23}; std::vector<int> v {42, 23};
......
This diff is collapsed.
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