/*
- * Copyright 2013 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 {
{ 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,
kPrettyBytesMetricSuffixes,
kPrettyUnitsMetricSuffixes,
kPrettyUnitsBinarySuffixes,
kPrettyUnitsBinaryIECSuffixes,
+ kPrettySISuffixes,
};
} // namespace
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"));
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,
} // 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
+