/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2012-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/String.h>
-#include <folly/Format.h>
-#include <folly/ScopeGuard.h>
-
+#include <cctype>
#include <cerrno>
#include <cstdarg>
#include <cstring>
-#include <stdexcept>
#include <iterator>
-#include <cctype>
-#include <string.h>
+#include <stdexcept>
+
#include <glog/logging.h>
+#include <folly/ScopeGuard.h>
+
namespace folly {
+static_assert(IsConvertible<float>::value, "");
+static_assert(IsConvertible<int>::value, "");
+static_assert(IsConvertible<bool>::value, "");
+static_assert(IsConvertible<int>::value, "");
+static_assert(!IsConvertible<std::vector<int>>::value, "");
+
+static inline bool is_oddspace(char c) {
+ return c == '\n' || c == '\t' || c == '\r';
+}
+
+StringPiece ltrimWhitespace(StringPiece sp) {
+ // Spaces other than ' ' characters are less common but should be
+ // checked. This configuration where we loop on the ' '
+ // separately from oddspaces was empirically fastest.
+
+loop:
+ for (; !sp.empty() && sp.front() == ' '; sp.pop_front()) {
+ }
+ if (!sp.empty() && is_oddspace(sp.front())) {
+ sp.pop_front();
+ goto loop;
+ }
+
+ return sp;
+}
+
+StringPiece rtrimWhitespace(StringPiece sp) {
+ // Spaces other than ' ' characters are less common but should be
+ // checked. This configuration where we loop on the ' '
+ // separately from oddspaces was empirically fastest.
+
+loop:
+ for (; !sp.empty() && sp.back() == ' '; sp.pop_back()) {
+ }
+ if (!sp.empty() && is_oddspace(sp.back())) {
+ sp.pop_back();
+ goto loop;
+ }
+
+ return sp;
+}
+
namespace {
int stringAppendfImplHelper(char* buf,
output.append(heap_buffer.get(), size_t(final_bytes_used));
}
-} // anon namespace
+} // namespace
std::string stringPrintf(const char* format, ...) {
va_list ap;
{ "ns", 1e-9L },
{ "ps", 1e-12L },
{ "s ", 0 },
- { 0, 0 },
+ { nullptr, 0 },
};
const PrettySuffix kPrettyBytesMetricSuffixes[] = {
{ "MB", 1e6L },
{ "kB", 1e3L },
{ "B ", 0L },
- { 0, 0 },
+ { nullptr, 0 },
};
const PrettySuffix kPrettyBytesBinarySuffixes[] = {
{ "MB", int64_t(1) << 20 },
{ "kB", int64_t(1) << 10 },
{ "B ", 0L },
- { 0, 0 },
+ { nullptr, 0 },
};
const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = {
{ "MiB", int64_t(1) << 20 },
{ "KiB", int64_t(1) << 10 },
{ "B ", 0L },
- { 0, 0 },
+ { nullptr, 0 },
};
const PrettySuffix kPrettyUnitsMetricSuffixes[] = {
{ "M", 1e6L },
{ "k", 1e3L },
{ " ", 0 },
- { 0, 0 },
+ { nullptr, 0 },
};
const PrettySuffix kPrettyUnitsBinarySuffixes[] = {
{ "M", int64_t(1) << 20 },
{ "k", int64_t(1) << 10 },
{ " ", 0 },
- { 0, 0 },
+ { nullptr, 0 },
};
const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = {
{ "Mi", int64_t(1) << 20 },
{ "Ki", int64_t(1) << 10 },
{ " ", 0 },
- { 0, 0 },
+ { nullptr, 0 },
};
const PrettySuffix kPrettySISuffixes[] = {
{ "z", 1e-21L },
{ "y", 1e-24L },
{ " ", 0 },
- { 0, 0}
+ { nullptr, 0}
};
const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
kPrettySISuffixes,
};
-} // namespace
+} // namespace
std::string prettyPrint(double val, PrettyType type, bool addSpace) {
char buf[100];
c += rotated;
}
-} // anon namespace
+} // namespace
void toLowerAscii(char* str, size_t length) {
static const size_t kAlignMask64 = 7;
size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
std::string& line) {
+ static char hexValues[] = "0123456789abcdef";
// Line layout:
// 8: address
// 1: space
line.reserve(78);
const uint8_t* p = reinterpret_cast<const uint8_t*>(ptr) + offset;
size_t n = std::min(size - offset, size_t(16));
- format("{:08x} ", offset).appendTo(line);
+ line.push_back(hexValues[(offset >> 28) & 0xf]);
+ line.push_back(hexValues[(offset >> 24) & 0xf]);
+ line.push_back(hexValues[(offset >> 20) & 0xf]);
+ line.push_back(hexValues[(offset >> 16) & 0xf]);
+ line.push_back(hexValues[(offset >> 12) & 0xf]);
+ line.push_back(hexValues[(offset >> 8) & 0xf]);
+ line.push_back(hexValues[(offset >> 4) & 0xf]);
+ line.push_back(hexValues[offset & 0xf]);
+ line.push_back(' ');
for (size_t i = 0; i < n; i++) {
if (i == 8) {
line.push_back(' ');
}
- format(" {:02x}", p[i]).appendTo(line);
+
+ line.push_back(' ');
+ line.push_back(hexValues[(p[i] >> 4) & 0xf]);
+ line.push_back(hexValues[p[i] & 0xf]);
}
// 3 spaces for each byte we're not printing, one separating the halves
return join("\n", piecer);
}
-} // namespace folly
+} // namespace folly
#ifdef FOLLY_DEFINED_DMGL
# undef FOLLY_DEFINED_DMGL