X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FString.cpp;h=e8de48dac9c10dfd72460f98b466a7e6e9ab6556;hp=337b7e37dac94371203e3a3894861dcdb14bfa4c;hb=b3ef1edce60571289bd205b71a7eacaedf0e6598;hpb=c947d451ab60011477ee6e08f7ee2ff2024d437d diff --git a/folly/String.cpp b/folly/String.cpp index 337b7e37..e8de48da 100644 --- a/folly/String.cpp +++ b/folly/String.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2015 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. @@ -25,6 +25,7 @@ #include #include #include +#include #include namespace folly { @@ -65,21 +66,21 @@ 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 @@ -284,7 +285,7 @@ double prettyToDouble(folly::StringPiece *const prettyString, bestPrefixId = j; } } else if (prettyString->startsWith(suffixes[j].suffix)) { - int suffixLen = strlen(suffixes[j].suffix); + int suffixLen = int(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) { @@ -298,15 +299,14 @@ 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; } double prettyToDouble(folly::StringPiece prettyString, const PrettyType type){ double result = prettyToDouble(&prettyString, type); - detail::enforceWhitespace(prettyString.data(), - prettyString.data() + prettyString.size()); + detail::enforceWhitespace(prettyString); return result; } @@ -329,9 +329,20 @@ fbstring errnoStr(int err) { // 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 defined(__APPLE__) || defined(__FreeBSD__) ||\ - defined(__CYGWIN__) || defined(__ANDROID__) ||\ - ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) +#if defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_VER)) + // mingw64 has no strerror_r, but Windows has strerror_s, which C11 added + // as well. So maybe we should use this across all platforms (together + // with strerrorlen_s). Note strerror_r and _s have swapped args. + int r = strerror_s(buf, sizeof(buf), err); + if (r != 0) { + result = to( + "Unknown error ", err, + " (strerror_r failed with error ", errno, ")"); + } else { + result.assign(buf); + } +#elif defined(FOLLY_HAVE_XSI_STRERROR_R) || \ + defined(__APPLE__) || defined(__ANDROID__) // Using XSI-compatible strerror_r int r = strerror_r(err, buf, sizeof(buf)); @@ -362,7 +373,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 @@ -408,7 +419,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) { @@ -529,13 +540,57 @@ size_t hexDumpLine(const void* ptr, size_t offset, size_t size, } line.append(16 - n, ' '); line.push_back('|'); - DCHECK_EQ(line.size(), 78); + DCHECK_EQ(line.size(), 78u); return n; } } // namespace detail +std::string stripLeftMargin(std::string s) { + std::vector pieces; + split("\n", s, pieces); + auto piecer = range(pieces); + + auto piece = (piecer.end() - 1); + auto needle = std::find_if(piece->begin(), + piece->end(), + [](char c) { return c != ' ' && c != '\t'; }); + if (needle == piece->end()) { + (piecer.end() - 1)->clear(); + } + piece = piecer.begin(); + needle = std::find_if(piece->begin(), + piece->end(), + [](char c) { return c != ' ' && c != '\t'; }); + if (needle == piece->end()) { + piecer.erase(piecer.begin(), piecer.begin() + 1); + } + + const auto sentinel = std::numeric_limits::max(); + auto indent = sentinel; + size_t max_length = 0; + for (piece = piecer.begin(); piece != piecer.end(); piece++) { + needle = std::find_if(piece->begin(), + piece->end(), + [](char c) { return c != ' ' && c != '\t'; }); + if (needle != piece->end()) { + indent = std::min(indent, size_t(needle - piece->begin())); + } else { + max_length = std::max(piece->size(), max_length); + } + } + indent = indent == sentinel ? max_length : indent; + for (piece = piecer.begin(); piece != piecer.end(); piece++) { + if (piece->size() < indent) { + piece->clear(); + } else { + piece->erase(piece->begin(), piece->begin() + indent); + } + } + return join("\n", piecer); +} + } // namespace folly #ifdef FOLLY_DEFINED_DMGL