X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FString.cpp;h=f4a923963f3ecd05fabb76790fc949b51361ac7c;hb=7acba7e1a0a75d22087647b3e9e830c9b0e8d41e;hp=37bc231fc4cea9def5dfc181ad04fcb09a6a9483;hpb=d7d91eb119b9f0758cd7c9e952e9a58209ca5a67;p=folly.git diff --git a/folly/String.cpp b/folly/String.cpp index 37bc231f..f4a92396 100644 --- a/folly/String.cpp +++ b/folly/String.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,20 +16,61 @@ #include -#include -#include - +#include #include #include #include -#include #include -#include -#include +#include + #include +#include + namespace folly { +static_assert(IsConvertible::value, ""); +static_assert(IsConvertible::value, ""); +static_assert(IsConvertible::value, ""); +static_assert(IsConvertible::value, ""); +static_assert(!IsConvertible>::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, @@ -66,24 +107,24 @@ void stringAppendfImpl(std::string& output, const char* format, va_list args) { } if (static_cast(bytes_used) < inline_buffer.size()) { - output.append(inline_buffer.data(), bytes_used); + output.append(inline_buffer.data(), size_t(bytes_used)); return; } // Couldn't fit. Heap allocate a buffer, oh well. - std::unique_ptr heap_buffer(new char[bytes_used + 1]); - int final_bytes_used = - stringAppendfImplHelper(heap_buffer.get(), bytes_used + 1, format, args); + std::unique_ptr heap_buffer(new char[size_t(bytes_used + 1)]); + int final_bytes_used = stringAppendfImplHelper( + heap_buffer.get(), size_t(bytes_used + 1), format, args); // The second call can take fewer bytes if, for example, we were printing a // string buffer with null-terminating char using a width specifier - // vsnprintf("%.*s", buf.size(), buf) CHECK(bytes_used >= final_bytes_used); // We don't keep the trailing '\0' in our output string - output.append(heap_buffer.get(), final_bytes_used); + output.append(heap_buffer.get(), size_t(final_bytes_used)); } -} // anon namespace +} // namespace std::string stringPrintf(const char* format, ...) { va_list ap; @@ -146,7 +187,7 @@ const PrettySuffix kPrettyTimeSuffixes[] = { { "ns", 1e-9L }, { "ps", 1e-12L }, { "s ", 0 }, - { 0, 0 }, + { nullptr, 0 }, }; const PrettySuffix kPrettyBytesMetricSuffixes[] = { @@ -155,7 +196,7 @@ const PrettySuffix kPrettyBytesMetricSuffixes[] = { { "MB", 1e6L }, { "kB", 1e3L }, { "B ", 0L }, - { 0, 0 }, + { nullptr, 0 }, }; const PrettySuffix kPrettyBytesBinarySuffixes[] = { @@ -164,7 +205,7 @@ const PrettySuffix kPrettyBytesBinarySuffixes[] = { { "MB", int64_t(1) << 20 }, { "kB", int64_t(1) << 10 }, { "B ", 0L }, - { 0, 0 }, + { nullptr, 0 }, }; const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = { @@ -173,7 +214,7 @@ const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = { { "MiB", int64_t(1) << 20 }, { "KiB", int64_t(1) << 10 }, { "B ", 0L }, - { 0, 0 }, + { nullptr, 0 }, }; const PrettySuffix kPrettyUnitsMetricSuffixes[] = { @@ -182,7 +223,7 @@ const PrettySuffix kPrettyUnitsMetricSuffixes[] = { { "M", 1e6L }, { "k", 1e3L }, { " ", 0 }, - { 0, 0 }, + { nullptr, 0 }, }; const PrettySuffix kPrettyUnitsBinarySuffixes[] = { @@ -191,7 +232,7 @@ const PrettySuffix kPrettyUnitsBinarySuffixes[] = { { "M", int64_t(1) << 20 }, { "k", int64_t(1) << 10 }, { " ", 0 }, - { 0, 0 }, + { nullptr, 0 }, }; const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = { @@ -200,7 +241,7 @@ const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = { { "Mi", int64_t(1) << 20 }, { "Ki", int64_t(1) << 10 }, { " ", 0 }, - { 0, 0 }, + { nullptr, 0 }, }; const PrettySuffix kPrettySISuffixes[] = { @@ -225,7 +266,7 @@ const PrettySuffix kPrettySISuffixes[] = { { "z", 1e-21L }, { "y", 1e-24L }, { " ", 0 }, - { 0, 0} + { nullptr, 0} }; const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = { @@ -239,7 +280,7 @@ const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = { kPrettySISuffixes, }; -} // namespace +} // namespace std::string prettyPrint(double val, PrettyType type, bool addSpace) { char buf[100]; @@ -299,7 +340,7 @@ double prettyToDouble(folly::StringPiece *const prettyString, "Unable to parse suffix \"", prettyString->toString(), "\"")); } - prettyString->advance(longestPrefixLen); + prettyString->advance(size_t(longestPrefixLen)); return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val : value; } @@ -373,7 +414,7 @@ void toLowerAscii8(char& c) { // by adding 0x20. // Step 1: Clear the high order bit. We'll deal with it in Step 5. - unsigned char rotated = c & 0x7f; + uint8_t rotated = uint8_t(c & 0x7f); // Currently, the value of rotated, as a function of the original c is: // below 'A': 0- 64 // 'A'-'Z': 65- 90 @@ -419,7 +460,7 @@ void toLowerAscii8(char& c) { // At this point, rotated is 0x20 if c is 'A'-'Z' and 0x00 otherwise // Step 7: Add rotated to c - c += rotated; + c += char(rotated); } void toLowerAscii32(uint32_t& c) { @@ -454,7 +495,7 @@ void toLowerAscii64(uint64_t& c) { c += rotated; } -} // anon namespace +} // namespace void toLowerAscii(char* str, size_t length) { static const size_t kAlignMask64 = 7; @@ -507,6 +548,7 @@ namespace detail { 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 @@ -520,13 +562,24 @@ size_t hexDumpLine(const void* ptr, size_t offset, size_t size, line.reserve(78); const uint8_t* p = reinterpret_cast(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 @@ -591,7 +644,7 @@ std::string stripLeftMargin(std::string s) { return join("\n", piecer); } -} // namespace folly +} // namespace folly #ifdef FOLLY_DEFINED_DMGL # undef FOLLY_DEFINED_DMGL