X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFormat-inl.h;h=5331bbba199038caed2cae4e9104ebcb40d5fe1e;hb=8b05be27769e2e3d5d1681251953506c22f76b45;hp=77248b79dad43f62d208cb17d7fe19524bc766c1;hpb=bd1cd83b305dceb9b5c5b6da1732f22ec9d2deef;p=folly.git diff --git a/folly/Format-inl.h b/folly/Format-inl.h index 77248b79..5331bbba 100644 --- a/folly/Format-inl.h +++ b/folly/Format-inl.h @@ -306,8 +306,19 @@ namespace format_value { template void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) { + if (arg.width != FormatArg::kDefaultWidth && arg.width < 0) { + throw BadFormatArg("folly::format: invalid width"); + } + if (arg.precision != FormatArg::kDefaultPrecision && arg.precision < 0) { + throw BadFormatArg("folly::format: invalid precision"); + } + + // XXX: clang should be smart enough to not need the two static_cast + // uses below given the above checks. If clang ever becomes that smart, we + // should remove the otherwise unnecessary warts. + if (arg.precision != FormatArg::kDefaultPrecision && - val.size() > arg.precision) { + val.size() > static_cast(arg.precision)) { val.reset(val.data(), arg.precision); } @@ -324,9 +335,10 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) { }; int padRemaining = 0; - if (arg.width != FormatArg::kDefaultWidth && val.size() < arg.width) { + if (arg.width != FormatArg::kDefaultWidth && + val.size() < static_cast(arg.width)) { char fill = arg.fill == FormatArg::kDefaultFill ? ' ' : arg.fill; - int padChars = arg.width - val.size(); + int padChars = static_cast (arg.width - val.size()); memset(padBuf, fill, std::min(padBufSize, padChars)); switch (arg.align) { @@ -634,7 +646,7 @@ class FormatValue { DoubleToStringConverter::kMaxFixedDigitsAfterPoint), (8 + DoubleToStringConverter::kMaxExponentialDigits), (7 + DoubleToStringConverter::kMaxPrecisionDigits)})]; - StringBuilder builder(buf + 1, sizeof(buf) - 1); + StringBuilder builder(buf + 1, static_cast (sizeof(buf) - 1)); char plusSign; switch (arg.sign) { @@ -649,6 +661,11 @@ class FormatValue { break; }; + auto flags = + DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN | + (arg.trailingDot ? DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT + : 0); + double val = val_; switch (arg.presentation) { case '%': @@ -660,13 +677,14 @@ class FormatValue { DoubleToStringConverter::kMaxFixedDigitsAfterPoint) { arg.precision = DoubleToStringConverter::kMaxFixedDigitsAfterPoint; } - DoubleToStringConverter conv( - DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN, - infinitySymbol, - nanSymbol, - exponentSymbol, - -4, arg.precision, - 0, 0); + DoubleToStringConverter conv(flags, + infinitySymbol, + nanSymbol, + exponentSymbol, + -4, + arg.precision, + 0, + 0); arg.enforce(conv.ToFixed(val, arg.precision, &builder), "fixed double conversion failed"); } @@ -678,13 +696,14 @@ class FormatValue { arg.precision = DoubleToStringConverter::kMaxExponentialDigits; } - DoubleToStringConverter conv( - DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN, - infinitySymbol, - nanSymbol, - exponentSymbol, - -4, arg.precision, - 0, 0); + DoubleToStringConverter conv(flags, + infinitySymbol, + nanSymbol, + exponentSymbol, + -4, + arg.precision, + 0, + 0); arg.enforce(conv.ToExponential(val, arg.precision, &builder)); } break; @@ -698,13 +717,14 @@ class FormatValue { DoubleToStringConverter::kMaxPrecisionDigits) { arg.precision = DoubleToStringConverter::kMaxPrecisionDigits; } - DoubleToStringConverter conv( - DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN, - infinitySymbol, - nanSymbol, - exponentSymbol, - -4, arg.precision, - 0, 0); + DoubleToStringConverter conv(flags, + infinitySymbol, + nanSymbol, + exponentSymbol, + -4, + arg.precision, + 0, + 0); arg.enforce(conv.ToShortest(val, &builder)); } break;