#include <map>
#include <unordered_map>
-#include <double-conversion.h>
+#include <double-conversion/double-conversion.h>
-#include "folly/FBVector.h"
-#include "folly/Conv.h"
-#include "folly/Range.h"
-#include "folly/Traits.h"
-#include "folly/Likely.h"
-#include "folly/String.h"
-#include "folly/small_vector.h"
-#include "folly/FormatArg.h"
+#include <folly/FBVector.h>
+#include <folly/Conv.h>
+#include <folly/Range.h>
+#include <folly/Traits.h>
+#include <folly/Likely.h>
+#include <folly/String.h>
+#include <folly/small_vector.h>
+#include <folly/FormatArg.h>
// Ignore shadowing warnings within this file, so includers can use -Wshadow.
#pragma GCC diagnostic push
typename std::decay<Args>::type>...> ValueTuple;
static constexpr size_t valueCount = std::tuple_size<ValueTuple>::value;
- void handleFormatStrError() const FOLLY_NORETURN;
+ FOLLY_NORETURN void handleFormatStrError() const;
template <class Output>
void appendOutput(Output& out) const;
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.
*
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)
* and uses that container to get argument values from.
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.
*
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
+ * return a default value rather than throwing an exception.
+ *
+ * Usage:
+ * format("[no_such_key"], defaulted(map, 42)) -> 42
+ */
+namespace detail {
+template <class Container, class Value> struct DefaultValueWrapper {
+ DefaultValueWrapper(const Container& container, const Value& defaultValue)
+ : container(container),
+ defaultValue(defaultValue) {
+ }
+
+ const Container& container;
+ const Value& defaultValue;
+};
+} // namespace
+
+template <class Container, class Value>
+detail::DefaultValueWrapper<Container, Value>
+defaulted(const Container& c, const Value& v) {
+ return detail::DefaultValueWrapper<Container, Value>(c, v);
+}
+
/**
* Append formatted output to a string.
*
format(fmt, std::forward<Args>(args)...).appendTo(*out);
}
+template <class Str, class... Args>
+typename std::enable_if<IsSomeString<Str>::value>::type
+formatChecked(Str* out, StringPiece fmt, Args&&... args) {
+ formatChecked(fmt, std::forward<Args>(args)...).appendTo(*out);
+}
+
/**
* Append vformatted output to a string.
*/
vformat(fmt, std::forward<Container>(container)).appendTo(*out);
}
+template <class Str, class Container>
+typename std::enable_if<IsSomeString<Str>::value>::type
+vformatChecked(Str* out, StringPiece fmt, Container&& container) {
+ vformatChecked(fmt, std::forward<Container>(container)).appendTo(*out);
+}
+
/**
* Utilities for all format value specializations.
*/
} // namespace folly
-#include "folly/Format-inl.h"
+#include <folly/Format-inl.h>
#pragma GCC diagnostic pop