X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFormat-inl.h;h=ec5eb3569c245e22eba2fab1041d121ace459537;hb=a5562cb52be583e617374b7ef66c5560946dc11b;hp=70f5564d1a9faccf804a5e9213c8ebd8d0e9eb0a;hpb=115059030653fe6f73e351073097ba1e06507f21;p=folly.git diff --git a/folly/Format-inl.h b/folly/Format-inl.h index 70f5564d..ec5eb356 100644 --- a/folly/Format-inl.h +++ b/folly/Format-inl.h @@ -1,5 +1,5 @@ /* - * 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. @@ -27,12 +27,13 @@ #include #include +#include #include #include // Ignore -Wformat-nonliteral warnings within this file -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-nonliteral" +FOLLY_PUSH_WARNING +FOLLY_GCC_DISABLE_WARNING("-Wformat-nonliteral") namespace folly { @@ -157,9 +158,7 @@ template BaseFormatter::BaseFormatter( StringPiece str, Args&&... args) - : str_(str), - values_(FormatValue::type>( - std::forward(args))...) {} + : str_(str), values_(std::forward(args)...) {} template template @@ -182,7 +181,7 @@ void BaseFormatter::operator()( p = q; if (p == end || *p != '}') { - throw BadFormatArg("folly::format: single '}' in format string"); + throwBadFormatArg("folly::format: single '}' in format string"); } ++p; } @@ -204,7 +203,7 @@ void BaseFormatter::operator()( p = q + 1; if (p == end) { - throw BadFormatArg("folly::format: '}' at end of format string"); + throwBadFormatArg("folly::format: '}' at end of format string"); } // "{{" -> "{" @@ -217,7 +216,7 @@ void BaseFormatter::operator()( // Format string q = static_cast(memchr(p, '}', size_t(end - p))); if (q == nullptr) { - throw BadFormatArg("folly::format: missing ending '}'"); + throwBadFormatArg("folly::format: missing ending '}'"); } FormatArg arg(StringPiece(p, q)); p = q + 1; @@ -266,11 +265,11 @@ void BaseFormatter::operator()( } if (hasDefaultArgIndex && hasExplicitArgIndex) { - throw BadFormatArg( + throwBadFormatArg( "folly::format: may not have both default and explicit arg indexes"); } - asDerived().template doFormat(size_t(argIndex), arg, out); + asDerived().doFormat(size_t(argIndex), arg, out); } } @@ -292,19 +291,15 @@ 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"); + throwBadFormatArg("folly::format: invalid width"); } if (arg.precision != FormatArg::kDefaultPrecision && arg.precision < 0) { - throw BadFormatArg("folly::format: invalid precision"); + throwBadFormatArg("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() > static_cast(arg.precision)) { - val.reset(val.data(), size_t(arg.precision)); + val.reset(val.data(), static_cast(arg.precision)); } constexpr int padBufSize = 128; @@ -684,7 +679,7 @@ class FormatValue { float val_; }; -// Sring-y types (implicitly convertible to StringPiece, except char*) +// String-y types (implicitly convertible to StringPiece, except char*) template class FormatValue< T, @@ -937,7 +932,7 @@ template <> struct KeyFromStringPiece : public FormatTraitsBase { typedef fbstring key_type; static fbstring convert(StringPiece s) { - return s.toFbstring(); + return s.to(); } }; @@ -956,7 +951,10 @@ struct KeyableTraitsAssoc : public FormatTraitsBase { typedef typename T::key_type key_type; typedef typename T::value_type::second_type value_type; static const value_type& at(const T& map, StringPiece key) { - return map.at(KeyFromStringPiece::convert(key)); + if (auto ptr = get_ptr(map, KeyFromStringPiece::convert(key))) { + return *ptr; + } + detail::throwFormatKeyNotFoundException(key); } static const value_type& at(const T& map, StringPiece key, const value_type& dflt) { @@ -1127,4 +1125,4 @@ typename std::enable_if::value>::type toAppend( } // namespace folly -#pragma GCC diagnostic pop +FOLLY_POP_WARNING