proxygen
folly::FormatValue< double > Class Template Reference

#include <Format-inl.h>

Public Member Functions

 FormatValue (double val)
 
template<class FormatCallback >
void format (FormatArg &arg, FormatCallback &cb) const
 

Private Member Functions

void formatHelper (fbstring &piece, int &prefixLen, FormatArg &arg) const
 

Private Attributes

double val_
 

Detailed Description

template<>
class folly::FormatValue< double >

Definition at line 655 of file Format-inl.h.

Constructor & Destructor Documentation

folly::FormatValue< double >::FormatValue ( double  val)
inlineexplicit

Definition at line 657 of file Format-inl.h.

657 : val_(val) {}
double val
Definition: String.cpp:273

Member Function Documentation

template<class FormatCallback >
void folly::FormatValue< double >::format ( FormatArg arg,
FormatCallback &  cb 
) const
inline

Definition at line 660 of file Format-inl.h.

References folly::format_value::formatNumber().

660  {
661  fbstring piece;
662  int prefixLen;
663  formatHelper(piece, prefixLen, arg);
664  format_value::formatNumber(piece, prefixLen, arg, cb);
665  }
void formatHelper(fbstring &piece, int &prefixLen, FormatArg &arg) const
Definition: Format.cpp:95
basic_fbstring< char > fbstring
Definition: FBString.h:2904
void formatNumber(StringPiece val, int prefixLen, FormatArg &arg, FormatCallback &cb)
Definition: Format-inl.h:357
void folly::FormatValue< double >::formatHelper ( fbstring piece,
int &  prefixLen,
FormatArg arg 
) const
private

Definition at line 95 of file Format.cpp.

References shell_builder::builder, folly::constexpr_max(), folly::FormatArg::enforce(), folly::FormatArg::error(), folly::FormatArg::FLOAT, FOLLY_FALLTHROUGH, folly::FormatArg::kDefaultPrecision, folly::FormatArg::kDefaultPresentation, folly::FormatArg::PLUS_OR_MINUS, folly::FormatArg::precision, folly::FormatArg::presentation, folly::FormatArg::sign, folly::FormatArg::SPACE_OR_MINUS, folly::FormatArg::trailingDot, val, and folly::FormatArg::validate().

98  {
99  using ::double_conversion::DoubleToStringConverter;
100  using ::double_conversion::StringBuilder;
101 
102  arg.validate(FormatArg::Type::FLOAT);
103 
104  if (arg.presentation == FormatArg::kDefaultPresentation) {
105  arg.presentation = 'g';
106  }
107 
108  const char* infinitySymbol = isupper(arg.presentation) ? "INF" : "inf";
109  const char* nanSymbol = isupper(arg.presentation) ? "NAN" : "nan";
110  char exponentSymbol = isupper(arg.presentation) ? 'E' : 'e';
111 
112  if (arg.precision == FormatArg::kDefaultPrecision) {
113  arg.precision = 6;
114  }
115 
116  // 2+: for null terminator and optional sign shenanigans.
117  constexpr int bufLen = 2 +
118  constexpr_max(2 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
119  DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
121  8 + DoubleToStringConverter::kMaxExponentialDigits,
122  7 + DoubleToStringConverter::kMaxPrecisionDigits));
123  char buf[bufLen];
124  StringBuilder builder(buf + 1, bufLen - 1);
125 
126  char plusSign;
127  switch (arg.sign) {
129  plusSign = '+';
130  break;
132  plusSign = ' ';
133  break;
134  default:
135  plusSign = '\0';
136  break;
137  };
138 
139  auto flags = DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN |
140  (arg.trailingDot ? DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT
141  : 0);
142 
143  double val = val_;
144  switch (arg.presentation) {
145  case '%':
146  val *= 100;
148  case 'f':
149  case 'F': {
150  if (arg.precision > DoubleToStringConverter::kMaxFixedDigitsAfterPoint) {
151  arg.precision = DoubleToStringConverter::kMaxFixedDigitsAfterPoint;
152  }
153  DoubleToStringConverter conv(
154  flags,
155  infinitySymbol,
156  nanSymbol,
157  exponentSymbol,
158  -4,
159  arg.precision,
160  0,
161  0);
162  arg.enforce(
163  conv.ToFixed(val, arg.precision, &builder),
164  "fixed double conversion failed");
165  break;
166  }
167  case 'e':
168  case 'E': {
169  if (arg.precision > DoubleToStringConverter::kMaxExponentialDigits) {
170  arg.precision = DoubleToStringConverter::kMaxExponentialDigits;
171  }
172 
173  DoubleToStringConverter conv(
174  flags,
175  infinitySymbol,
176  nanSymbol,
177  exponentSymbol,
178  -4,
179  arg.precision,
180  0,
181  0);
182  arg.enforce(conv.ToExponential(val, arg.precision, &builder));
183  break;
184  }
185  case 'n': // should be locale-aware, but isn't
186  case 'g':
187  case 'G': {
188  if (arg.precision < DoubleToStringConverter::kMinPrecisionDigits) {
189  arg.precision = DoubleToStringConverter::kMinPrecisionDigits;
190  } else if (arg.precision > DoubleToStringConverter::kMaxPrecisionDigits) {
191  arg.precision = DoubleToStringConverter::kMaxPrecisionDigits;
192  }
193  DoubleToStringConverter conv(
194  flags,
195  infinitySymbol,
196  nanSymbol,
197  exponentSymbol,
198  -4,
199  arg.precision,
200  0,
201  0);
202  arg.enforce(conv.ToShortest(val, &builder));
203  break;
204  }
205  default:
206  arg.error("invalid specifier '", arg.presentation, "'");
207  }
208 
209  int len = builder.position();
210  builder.Finalize();
211  DCHECK_GT(len, 0);
212 
213  // Add '+' or ' ' sign if needed
214  char* p = buf + 1;
215  // anything that's neither negative nor nan
216  prefixLen = 0;
217  if (plusSign && (*p != '-' && *p != 'n' && *p != 'N')) {
218  *--p = plusSign;
219  ++len;
220  prefixLen = 1;
221  } else if (*p == '-') {
222  prefixLen = 1;
223  }
224 
225  piece = fbstring(p, size_t(len));
226 }
flags
Definition: http_parser.h:127
double val
Definition: String.cpp:273
static constexpr int kDefaultPrecision
Definition: FormatArg.h:150
constexpr T constexpr_max(T a)
Definition: ConstexprMath.h:68
static constexpr char kDefaultPresentation
Definition: FormatArg.h:156
basic_fbstring< char > fbstring
Definition: FBString.h:2904
#define FOLLY_FALLTHROUGH
Definition: CppAttributes.h:63

Member Data Documentation

double folly::FormatValue< double >::val_
private

Definition at line 670 of file Format-inl.h.


The documentation for this class was generated from the following files: