Make fbstring libgcc-safe
[folly.git] / folly / String.h
index 241d4dbf6aac26479d88853cf3514a4f99eac137..d59477f05440e578928a9605e14257a56487545b 100644 (file)
 #include <string>
 #include <boost/type_traits.hpp>
 
+#ifdef FOLLY_HAVE_DEPRECATED_ASSOC
 #ifdef _GLIBCXX_SYMVER
 #include <ext/hash_set>
 #include <ext/hash_map>
 #endif
+#endif
 
 #include <unordered_set>
 #include <unordered_map>
@@ -173,16 +175,16 @@ String uriUnescape(StringPiece str, UriEscapeMode mode = UriEscapeMode::ALL) {
  * resulting string, and the second appends the produced characters to
  * the specified string and returns a reference to it.
  */
-std::string stringPrintf(const char* format, ...)
-  __attribute__ ((format (printf, 1, 2)));
+std::string stringPrintf(FOLLY_PRINTF_FORMAT const char* format, ...)
+  FOLLY_PRINTF_FORMAT_ATTR(1, 2);
 
-/** Similar to stringPrintf, with different signiture.
-  */
-void stringPrintf(std::string* out, const char* fmt, ...)
-  __attribute__ ((format (printf, 2, 3)));
+/* Similar to stringPrintf, with different signature. */
+void stringPrintf(std::string* out, FOLLY_PRINTF_FORMAT const char* fmt, ...)
+  FOLLY_PRINTF_FORMAT_ATTR(2, 3);
 
-std::string& stringAppendf(std::string* output, const char* format, ...)
-  __attribute__ ((format (printf, 2, 3)));
+std::string& stringAppendf(std::string* output,
+                          FOLLY_PRINTF_FORMAT const char* format, ...)
+  FOLLY_PRINTF_FORMAT_ATTR(2, 3);
 
 /**
  * Backslashify a string, that is, replace non-printable characters
@@ -268,7 +270,8 @@ bool unhexlify(const InputString& input, OutputString& output);
  *   PRETTY_UNITS_METRIC - k, M, G, etc (goes up by 10^3 = 1000 each time)
  *   PRETTY_UNITS_BINARY - k, M, G, etc (goes up by 2^10 = 1024 each time)
  *   PRETTY_UNITS_BINARY_IEC - Ki, Mi, Gi, etc
- *
+ *   PRETTY_SI           - full SI metric prefixes from yocto to Yotta
+ *                         http://en.wikipedia.org/wiki/Metric_prefix
  * @author Mark Rabkin <mrabkin@fb.com>
  */
 enum PrettyType {
@@ -284,11 +287,38 @@ enum PrettyType {
   PRETTY_UNITS_BINARY,
   PRETTY_UNITS_BINARY_IEC,
 
-  PRETTY_NUM_TYPES
+  PRETTY_SI,
+  PRETTY_NUM_TYPES,
 };
 
 std::string prettyPrint(double val, PrettyType, bool addSpace = true);
 
+/**
+ * This utility converts StringPiece in pretty format (look above) to double,
+ * with progress information. Alters the  StringPiece parameter
+ * to get rid of the already-parsed characters.
+ * Expects string in form <floating point number> {space}* [<suffix>]
+ * If string is not in correct format, utility finds longest valid prefix and
+ * if there at least one, returns double value based on that prefix and
+ * modifies string to what is left after parsing. Throws and std::range_error
+ * exception if there is no correct parse.
+ * Examples(for PRETTY_UNITS_METRIC):
+ * '10M' => 10 000 000
+ * '10 M' => 10 000 000
+ * '10' => 10
+ * '10 Mx' => 10 000 000, prettyString == "x"
+ * 'abc' => throws std::range_error
+ */
+double prettyToDouble(folly::StringPiece *const prettyString,
+                      const PrettyType type);
+
+/*
+ * Same as prettyToDouble(folly::StringPiece*, PrettyType), but
+ * expects whole string to be correctly parseable. Throws std::range_error
+ * otherwise
+ */
+double prettyToDouble(folly::StringPiece prettyString, const PrettyType type);
+
 /**
  * Write a hex dump of size bytes starting at ptr to out.
  *
@@ -384,16 +414,24 @@ void splitTo(const Delim& delimiter,
              bool ignoreEmpty = false);
 
 /*
- * Split a string into a fixed number of pieces by delimiter. Returns 'true' if
- * the fields were all successfully populated.
+ * Split a string into a fixed number of string pieces and/or numeric types
+ * by delimiter. Any numeric type that folly::to<> can convert to from a
+ * string piece is supported as a target. Returns 'true' if the fields were
+ * all successfully populated.
  *
- * Example:
+ * Examples:
  *
  *  folly::StringPiece name, key, value;
  *  if (folly::split('\t', line, name, key, value))
  *    ...
  *
- * The 'exact' template paremeter specifies how the function behaves when too
+ *  folly::StringPiece name;
+ *  double value;
+ *  int id;
+ *  if (folly::split('\t', line, name, value, id))
+ *    ...
+ *
+ * The 'exact' template parameter specifies how the function behaves when too
  * many fields are present in the input string. When 'exact' is set to its
  * default value of 'true', a call to split will fail if the number of fields in
  * the input string does not exactly match the number of output parameters
@@ -403,14 +441,24 @@ void splitTo(const Delim& delimiter,
  *  folly::StringPiece x, y.
  *  if (folly::split<false>(':', "a:b:c", x, y))
  *    assert(x == "a" && y == "b:c");
+ *
+ * Note that this will likely not work if the last field's target is of numeric
+ * type, in which case folly::to<> will throw an exception.
  */
+template <class T>
+using IsSplitTargetType = std::integral_constant<bool,
+  std::is_arithmetic<T>::value ||
+  std::is_same<T, StringPiece>::value>;
+
 template<bool exact = true,
          class Delim,
-         class... StringPieces>
-bool split(const Delim& delimiter,
-           StringPiece input,
-           StringPiece& outHead,
-           StringPieces&... outTail);
+         class OutputType,
+         class... OutputTypes>
+typename std::enable_if<IsSplitTargetType<OutputType>::value, bool>::type
+split(const Delim& delimiter,
+      StringPiece input,
+      OutputType& outHead,
+      OutputTypes&... outTail);
 
 /*
  * Join list of tokens.
@@ -472,6 +520,7 @@ struct hash<std::basic_string<C> > : private hash<const C*> {
 
 }
 
+#if FOLLY_HAVE_DEPRECATED_ASSOC
 #if defined(_GLIBCXX_SYMVER) && !defined(__BIONIC__)
 namespace __gnu_cxx {
 
@@ -484,6 +533,7 @@ struct hash<std::basic_string<C> > : private hash<const C*> {
 
 }
 #endif
+#endif
 
 // Hook into boost's type traits
 namespace boost {