Make fbstring libgcc-safe
[folly.git] / folly / Format.h
index 11e2462a01f0e0fe81b47d8b504c5f19f6565f1c..7cc83413a93ff18ac1d568e400a5f1a6362512d7 100644 (file)
@@ -26,7 +26,7 @@
 #include <map>
 #include <unordered_map>
 
-#include <double-conversion.h>
+#include <double-conversion/double-conversion.h>
 
 #include "folly/FBVector.h"
 #include "folly/Conv.h"
@@ -128,7 +128,7 @@ class Formatter {
       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;
 
@@ -207,6 +207,15 @@ Formatter<false, Args...> format(StringPiece fmt, 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.
  *
@@ -222,6 +231,15 @@ Formatter<false, Args...> formatChecked(StringPiece fmt, Args&&... args) {
   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.
@@ -241,6 +259,15 @@ Formatter<true, Container> vformat(StringPiece fmt, 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.
  *
@@ -257,6 +284,40 @@ Formatter<true, Container> vformatChecked(StringPiece fmt,
   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.
  *