proxygen
folly::format_value Namespace Reference

Functions

template<class FormatCallback >
void formatString (StringPiece val, FormatArg &arg, FormatCallback &cb)
 
template<class FormatCallback >
void formatNumber (StringPiece val, int prefixLen, FormatArg &arg, FormatCallback &cb)
 
template<class FormatCallback , class Derived , bool containerMode, class... Args>
void formatFormatter (const BaseFormatter< Derived, containerMode, Args... > &formatter, FormatArg &arg, FormatCallback &cb)
 

Detailed Description

Utilities for all format value specializations.

Function Documentation

template<class FormatCallback , class Derived , bool containerMode, class... Args>
void folly::format_value::formatFormatter ( const BaseFormatter< Derived, containerMode, Args... > &  formatter,
FormatArg arg,
FormatCallback &  cb 
)

Format a Formatter object recursively. Behaves just like formatString(fmt.str(), arg, cb); but avoids creating a temporary string if possible.

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

References folly::FormatArg::align, folly::BaseFormatter< Derived, containerMode, Args >::fbstr(), formatString(), max, min, folly::FormatArg::precision, and folly::FormatArg::width.

Referenced by folly::FormatValue< KeyValue >::format(), and folly::vformat().

383  {
384  if (arg.width == FormatArg::kDefaultWidth &&
385  arg.precision == FormatArg::kDefaultPrecision) {
386  // nothing to do
387  formatter(cb);
388  } else if (
389  arg.align != FormatArg::Align::LEFT &&
390  arg.align != FormatArg::Align::DEFAULT) {
391  // We can only avoid creating a temporary string if we align left,
392  // as we'd need to know the size beforehand otherwise
393  format_value::formatString(formatter.fbstr(), arg, cb);
394  } else {
395  auto fn = [&arg, &cb](StringPiece sp) mutable {
396  int sz = static_cast<int>(sp.size());
397  if (arg.precision != FormatArg::kDefaultPrecision) {
398  sz = std::min(arg.precision, sz);
399  sp.reset(sp.data(), size_t(sz));
400  arg.precision -= sz;
401  }
402  if (!sp.empty()) {
403  cb(sp);
404  if (arg.width != FormatArg::kDefaultWidth) {
405  arg.width = std::max(arg.width - sz, 0);
406  }
407  }
408  };
409  formatter(fn);
410  if (arg.width != FormatArg::kDefaultWidth && arg.width != 0) {
411  // Rely on formatString to do appropriate padding
413  }
414  }
415 }
LogLevel max
Definition: LogLevel.cpp:31
void formatString(StringPiece val, FormatArg &arg, FormatCallback &cb)
Definition: Format-inl.h:298
LogLevel min
Definition: LogLevel.cpp:30
Range< const char * > StringPiece
template<class FormatCallback >
void folly::format_value::formatNumber ( StringPiece  val,
int  prefixLen,
FormatArg arg,
FormatCallback &  cb 
)

Format a number in "val"; the first prefixLen characters form the prefix (sign, "0x" base prefix, etc) which must be left-aligned if the alignment is Align::PAD_AFTER_SIGN. Treats Align::DEFAULT as Align::LEFT. Ignores arg.precision, as that has a different meaning for numbers (not "maximum field width")

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

References folly::Range< Iter >::advance(), folly::FormatArg::align, testing::Args(), formatString(), max, folly::FormatArg::precision, folly::Range< Iter >::subpiece(), and folly::FormatArg::width.

Referenced by folly::FormatValue< T, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value >::type >::doFormat(), folly::FormatValue< double >::format(), and folly::vformat().

361  {
362  // precision means something different for numbers
363  arg.precision = FormatArg::kDefaultPrecision;
364  if (arg.align == FormatArg::Align::DEFAULT) {
365  arg.align = FormatArg::Align::RIGHT;
366  } else if (prefixLen && arg.align == FormatArg::Align::PAD_AFTER_SIGN) {
367  // Split off the prefix, then do any padding if necessary
368  cb(val.subpiece(0, size_t(prefixLen)));
369  val.advance(size_t(prefixLen));
370  arg.width = std::max(arg.width - prefixLen, 0);
371  }
373 }
LogLevel max
Definition: LogLevel.cpp:31
double val
Definition: String.cpp:273
void formatString(StringPiece val, FormatArg &arg, FormatCallback &cb)
Definition: Format-inl.h:298
template<class FormatCallback >
void folly::format_value::formatString ( StringPiece  val,
FormatArg arg,
FormatCallback &  cb 
)

Format a string in "val", obeying appropriate alignment, padding, width, and precision. Treats Align::DEFAULT as Align::LEFT, and Align::PAD_AFTER_SIGN as Align::RIGHT; use formatNumber for number-specific formatting.

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

References folly::FormatArg::align, folly::Range< Iter >::data(), folly::FormatArg::fill, min, folly::FormatArg::precision, folly::Range< Iter >::reset(), folly::Range< Iter >::size(), and folly::FormatArg::width.

Referenced by folly::FormatValue< bool >::format(), formatFormatter(), formatNumber(), and folly::vformat().

298  {
299  if (arg.width != FormatArg::kDefaultWidth && arg.width < 0) {
300  throw_exception<BadFormatArg>("folly::format: invalid width");
301  }
302  if (arg.precision != FormatArg::kDefaultPrecision && arg.precision < 0) {
303  throw_exception<BadFormatArg>("folly::format: invalid precision");
304  }
305 
306  if (arg.precision != FormatArg::kDefaultPrecision &&
307  val.size() > static_cast<size_t>(arg.precision)) {
308  val.reset(val.data(), static_cast<size_t>(arg.precision));
309  }
310 
311  constexpr int padBufSize = 128;
312  char padBuf[padBufSize];
313 
314  // Output padding, no more than padBufSize at once
315  auto pad = [&padBuf, &cb, padBufSize](int chars) {
316  while (chars) {
317  int n = std::min(chars, padBufSize);
318  cb(StringPiece(padBuf, size_t(n)));
319  chars -= n;
320  }
321  };
322 
323  int padRemaining = 0;
324  if (arg.width != FormatArg::kDefaultWidth &&
325  val.size() < static_cast<size_t>(arg.width)) {
326  char fill = arg.fill == FormatArg::kDefaultFill ? ' ' : arg.fill;
327  int padChars = static_cast<int>(arg.width - val.size());
328  memset(padBuf, fill, size_t(std::min(padBufSize, padChars)));
329 
330  switch (arg.align) {
331  case FormatArg::Align::DEFAULT:
332  case FormatArg::Align::LEFT:
333  padRemaining = padChars;
334  break;
335  case FormatArg::Align::CENTER:
336  pad(padChars / 2);
337  padRemaining = padChars - padChars / 2;
338  break;
339  case FormatArg::Align::RIGHT:
340  case FormatArg::Align::PAD_AFTER_SIGN:
341  pad(padChars);
342  break;
343  default:
344  abort();
345  break;
346  }
347  }
348 
349  cb(val);
350 
351  if (padRemaining) {
352  pad(padRemaining);
353  }
354 }
double val
Definition: String.cpp:273
LogLevel min
Definition: LogLevel.cpp:30
Range< const char * > StringPiece