X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFormat-inl.h;h=945aa59ee296281777e3a51efc4361bf2ff4a194;hb=cc0ca971057edb5e4c8f78946b0c5508395ecc48;hp=64591d6af702e8dfadafffeb2737b87def6a5132;hpb=bae1869791b9fad1d53a45e18124e636b4ced923;p=folly.git diff --git a/folly/Format-inl.h b/folly/Format-inl.h index 64591d6a..945aa59e 100644 --- a/folly/Format-inl.h +++ b/folly/Format-inl.h @@ -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. @@ -19,6 +19,7 @@ #endif #include +#include #include #include #include @@ -27,6 +28,7 @@ #include #include #include +#include // Ignore -Wformat-nonliteral warnings within this file #pragma GCC diagnostic push @@ -172,7 +174,7 @@ void BaseFormatter::operator()(Output& out) auto p = s.begin(); auto end = s.end(); while (p != end) { - auto q = static_cast(memchr(p, '}', end - p)); + auto q = static_cast(memchr(p, '}', size_t(end - p))); if (!q) { out(StringPiece(p, end)); break; @@ -195,7 +197,7 @@ void BaseFormatter::operator()(Output& out) bool hasDefaultArgIndex = false; bool hasExplicitArgIndex = false; while (p != end) { - auto q = static_cast(memchr(p, '{', end - p)); + auto q = static_cast(memchr(p, '{', size_t(end - p))); if (!q) { outputString(StringPiece(p, end)); break; @@ -215,7 +217,7 @@ void BaseFormatter::operator()(Output& out) } // Format string - q = static_cast(memchr(p, '}', end - p)); + q = static_cast(memchr(p, '}', size_t(end - p))); if (q == nullptr) { throw BadFormatArg("folly::format: missing ending '}'"); } @@ -240,7 +242,7 @@ void BaseFormatter::operator()(Output& out) arg.enforce(arg.widthIndex == FormatArg::kNoIndex, "cannot provide width arg index without value arg index"); int sizeArg = nextArg++; - arg.width = getSizeArg(sizeArg, arg); + arg.width = getSizeArg(size_t(sizeArg), arg); } argIndex = nextArg++; @@ -249,12 +251,12 @@ void BaseFormatter::operator()(Output& out) if (arg.width == FormatArg::kDynamicWidth) { arg.enforce(arg.widthIndex != FormatArg::kNoIndex, "cannot provide value arg index without width arg index"); - arg.width = getSizeArg(arg.widthIndex, arg); + arg.width = getSizeArg(size_t(arg.widthIndex), arg); } try { argIndex = to(piece); - } catch (const std::out_of_range& e) { + } catch (const std::out_of_range&) { arg.error("argument index must be integer"); } arg.enforce(argIndex >= 0, "argument index must be non-negative"); @@ -267,7 +269,7 @@ void BaseFormatter::operator()(Output& out) "folly::format: may not have both default and explicit arg indexes"); } - doFormat(argIndex, arg, out); + doFormat(size_t(argIndex), arg, out); } } @@ -300,7 +302,7 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) { if (arg.precision != FormatArg::kDefaultPrecision && val.size() > static_cast(arg.precision)) { - val.reset(val.data(), arg.precision); + val.reset(val.data(), size_t(arg.precision)); } constexpr int padBufSize = 128; @@ -310,7 +312,7 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) { auto pad = [&padBuf, &cb, padBufSize] (int chars) { while (chars) { int n = std::min(chars, padBufSize); - cb(StringPiece(padBuf, n)); + cb(StringPiece(padBuf, size_t(n))); chars -= n; } }; @@ -320,7 +322,7 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) { val.size() < static_cast(arg.width)) { char fill = arg.fill == FormatArg::kDefaultFill ? ' ' : arg.fill; int padChars = static_cast (arg.width - val.size()); - memset(padBuf, fill, std::min(padBufSize, padChars)); + memset(padBuf, fill, size_t(std::min(padBufSize, padChars))); switch (arg.align) { case FormatArg::Align::DEFAULT: @@ -357,8 +359,8 @@ void formatNumber(StringPiece val, int prefixLen, FormatArg& arg, arg.align = FormatArg::Align::RIGHT; } else if (prefixLen && arg.align == FormatArg::Align::PAD_AFTER_SIGN) { // Split off the prefix, then do any padding if necessary - cb(val.subpiece(0, prefixLen)); - val.advance(prefixLen); + cb(val.subpiece(0, size_t(prefixLen))); + val.advance(size_t(prefixLen)); arg.width = std::max(arg.width - prefixLen, 0); } format_value::formatString(val, arg, cb); @@ -442,7 +444,7 @@ class FormatValue< char sign; if (std::is_signed::value) { if (folly::is_negative(val_)) { - uval = static_cast(-val_); + uval = UT(-static_cast(val_)); sign = '-'; } else { uval = static_cast(val_); @@ -459,7 +461,7 @@ class FormatValue< } } } else { - uval = val_; + uval = static_cast(val_); sign = '\0'; arg.enforce(arg.sign == FormatArg::Sign::DEFAULT, @@ -490,20 +492,15 @@ class FormatValue< "' specifier"); valBufBegin = valBuf + 3; // room for sign and base prefix -#ifdef _MSC_VER - char valBuf2[valBufSize]; - snprintf(valBuf2, valBufSize, "%ju", static_cast(uval)); - int len = GetNumberFormat( - LOCALE_USER_DEFAULT, - 0, - valBuf2, - nullptr, - valBufBegin, - (int)((valBuf + valBufSize) - valBufBegin) - ); -#else +#if defined(__ANDROID__) int len = snprintf(valBufBegin, (valBuf + valBufSize) - valBufBegin, - "%'ju", static_cast(uval)); + "%" PRIuMAX, static_cast(uval)); +#else + int len = snprintf( + valBufBegin, + size_t((valBuf + valBufSize) - valBufBegin), + "%ju", + static_cast(uval)); #endif // valBufSize should always be big enough, so this should never // happen. @@ -679,7 +676,7 @@ class FormatValue< "invalid specifier '", arg.presentation, "'"); format_value::formatString(val_, arg, cb); } else { - FormatValue(val_.at(arg.splitIntKey())).format(arg, cb); + FormatValue(val_.at(size_t(arg.splitIntKey()))).format(arg, cb); } } @@ -767,7 +764,9 @@ template class TryFormatValue { public: template - static void formatOrFail(T& value, FormatArg& arg, FormatCallback& cb) { + static void formatOrFail(T& /* value */, + FormatArg& arg, + FormatCallback& /* cb */) { arg.error("No formatter available for this type"); } }; @@ -1043,8 +1042,8 @@ class FormatValue> { static constexpr size_t valueCount = std::tuple_size::value; template - typename std::enable_if::type - doFormatFrom(size_t i, FormatArg& arg, Callback& cb) const { + typename std::enable_if::type doFormatFrom( + size_t i, FormatArg& arg, Callback& /* cb */) const { arg.enforce("tuple index out of range, max=", i); }