(wangle) s/continuation_/callback/ (missed some)
[folly.git] / folly / String.cpp
index e58432b469981bcbf9250605aea546c3d1d81cf4..c80c6821c511157400c541177a3ee57223164d3a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include "folly/String.h"
-#include "folly/Format.h"
+#include <folly/String.h>
+#include <folly/Format.h>
 
 #include <cerrno>
 #include <cstdarg>
 #include <cstring>
 #include <stdexcept>
 #include <iterator>
+#include <cctype>
 #include <glog/logging.h>
 
-#undef FOLLY_DEMANGLE
-#if defined(__GNUG__) && __GNUG__ >= 4
-# include <cxxabi.h>
-# define FOLLY_DEMANGLE 1
-#endif
-
 namespace folly {
 
 namespace {
@@ -132,7 +127,16 @@ const PrettySuffix kPrettyTimeSuffixes[] = {
   { 0, 0 },
 };
 
-const PrettySuffix kPrettyBytesSuffixes[] = {
+const PrettySuffix kPrettyBytesMetricSuffixes[] = {
+  { "TB", 1e12L },
+  { "GB", 1e9L },
+  { "MB", 1e6L },
+  { "kB", 1e3L },
+  { "B ", 0L },
+  { 0, 0 },
+};
+
+const PrettySuffix kPrettyBytesBinarySuffixes[] = {
   { "TB", int64_t(1) << 40 },
   { "GB", int64_t(1) << 30 },
   { "MB", int64_t(1) << 20 },
@@ -141,12 +145,12 @@ const PrettySuffix kPrettyBytesSuffixes[] = {
   { 0, 0 },
 };
 
-const PrettySuffix kPrettyBytesMetricSuffixes[] = {
-  { "TB", 1e12L },
-  { "GB", 1e9L },
-  { "MB", 1e6L },
-  { "kB", 1e3L },
-  { "B ", 0L },
+const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = {
+  { "TiB", int64_t(1) << 40 },
+  { "GiB", int64_t(1) << 30 },
+  { "MiB", int64_t(1) << 20 },
+  { "KiB", int64_t(1) << 10 },
+  { "B  ", 0L },
   { 0, 0 },
 };
 
@@ -168,12 +172,49 @@ const PrettySuffix kPrettyUnitsBinarySuffixes[] = {
   { 0, 0 },
 };
 
+const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = {
+  { "Ti", int64_t(1) << 40 },
+  { "Gi", int64_t(1) << 30 },
+  { "Mi", int64_t(1) << 20 },
+  { "Ki", int64_t(1) << 10 },
+  { "  ", 0 },
+  { 0, 0 },
+};
+
+const PrettySuffix kPrettySISuffixes[] = {
+  { "Y", 1e24L },
+  { "Z", 1e21L },
+  { "E", 1e18L },
+  { "P", 1e15L },
+  { "T", 1e12L },
+  { "G", 1e9L },
+  { "M", 1e6L },
+  { "k", 1e3L },
+  { "h", 1e2L },
+  { "da", 1e1L },
+  { "d", 1e-1L },
+  { "c", 1e-2L },
+  { "m", 1e-3L },
+  { "u", 1e-6L },
+  { "n", 1e-9L },
+  { "p", 1e-12L },
+  { "f", 1e-15L },
+  { "a", 1e-18L },
+  { "z", 1e-21L },
+  { "y", 1e-24L },
+  { " ", 0 },
+  { 0, 0} 
+};
+
 const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
   kPrettyTimeSuffixes,
-  kPrettyBytesSuffixes,
   kPrettyBytesMetricSuffixes,
+  kPrettyBytesBinarySuffixes,
+  kPrettyBytesBinaryIECSuffixes,
   kPrettyUnitsMetricSuffixes,
   kPrettyUnitsBinarySuffixes,
+  kPrettyUnitsBinaryIECSuffixes,
+  kPrettySISuffixes,
 };
 
 }  // namespace
@@ -204,6 +245,50 @@ std::string prettyPrint(double val, PrettyType type, bool addSpace) {
   return std::string(buf);
 }
 
+//TODO:
+//1) Benchmark & optimize
+double prettyToDouble(folly::StringPiece *const prettyString, 
+                      const PrettyType type) {
+  double value = folly::to<double>(prettyString);
+  while (prettyString->size() > 0 && std::isspace(prettyString->front())) {
+    prettyString->advance(1); //Skipping spaces between number and suffix
+  }
+  const PrettySuffix* suffixes = kPrettySuffixes[type];
+  int longestPrefixLen = -1;
+  int bestPrefixId = -1;
+  for (int j = 0 ; suffixes[j].suffix; ++j) {
+    if (suffixes[j].suffix[0] == ' '){//Checking for " " -> number rule.
+      if (longestPrefixLen == -1) {
+        longestPrefixLen = 0; //No characters to skip
+        bestPrefixId = j;
+      }
+    } else if (prettyString->startsWith(suffixes[j].suffix)) {
+      int suffixLen = strlen(suffixes[j].suffix);
+      //We are looking for a longest suffix matching prefix of the string
+      //after numeric value. We need this in case suffixes have common prefix.
+      if (suffixLen > longestPrefixLen) {
+        longestPrefixLen = suffixLen;
+        bestPrefixId = j;
+      }
+    }
+  }
+  if (bestPrefixId == -1) { //No valid suffix rule found
+    throw std::invalid_argument(folly::to<std::string>(
+            "Unable to parse suffix \"",
+            prettyString->toString(), "\""));
+  }
+  prettyString->advance(longestPrefixLen);
+  return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val : 
+                                      value;
+}
+
+double prettyToDouble(folly::StringPiece prettyString, const PrettyType type){
+  double result = prettyToDouble(&prettyString, type);
+  detail::enforceWhitespace(prettyString.data(), 
+                            prettyString.data() + prettyString.size());
+  return result;
+}
+
 std::string hexDump(const void* ptr, size_t size) {
   std::ostringstream os;
   hexDump(ptr, size, std::ostream_iterator<StringPiece>(os, "\n"));
@@ -221,13 +306,15 @@ fbstring errnoStr(int err) {
 
   fbstring result;
 
+  // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/strerror_r.3.html
   // http://www.kernel.org/doc/man-pages/online/pages/man3/strerror.3.html
-#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || \
-     !FOLLY_HAVE_FEATURES_H) && !_GNU_SOURCE
+#if defined(__APPLE__) || defined(__FreeBSD__) || \
+    ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
   // Using XSI-compatible strerror_r
   int r = strerror_r(err, buf, sizeof(buf));
 
-  if (r == -1) {
+  // OSX/FreeBSD use EINVAL and Linux uses -1 so just check for non-zero
+  if (r != 0) {
     result = to<fbstring>(
       "Unknown error ", err,
       " (strerror_r failed with error ", errno, ")");
@@ -242,30 +329,6 @@ fbstring errnoStr(int err) {
   return result;
 }
 
-#ifdef FOLLY_DEMANGLE
-
-fbstring demangle(const char* name) {
-  int status;
-  size_t len = 0;
-  // malloc() memory for the demangled type name
-  char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status);
-  if (status != 0) {
-    return name;
-  }
-  // len is the length of the buffer (including NUL terminator and maybe
-  // other junk)
-  return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString());
-}
-
-#else
-
-fbstring demangle(const char* name) {
-  return name;
-}
-
-#endif
-#undef FOLLY_DEMANGLE
-
 namespace detail {
 
 size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
@@ -311,3 +374,15 @@ size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
 } // namespace detail
 
 }   // namespace folly
+
+#ifdef FOLLY_DEFINED_DMGL
+# undef FOLLY_DEFINED_DMGL
+# undef DMGL_NO_OPTS
+# undef DMGL_PARAMS
+# undef DMGL_ANSI
+# undef DMGL_JAVA
+# undef DMGL_VERBOSE
+# undef DMGL_TYPES
+# undef DMGL_RET_POSTFIX
+#endif
+