proxygen
folly::FormatArg Struct Reference

#include <FormatArg.h>

Public Types

enum  Type { Type::INTEGER, Type::FLOAT, Type::OTHER }
 
enum  Align : uint8_t {
  Align::DEFAULT, Align::LEFT, Align::RIGHT, Align::PAD_AFTER_SIGN,
  Align::CENTER, Align::INVALID
}
 
enum  Sign : uint8_t {
  Sign::DEFAULT, Sign::PLUS_OR_MINUS, Sign::MINUS, Sign::SPACE_OR_MINUS,
  Sign::INVALID
}
 

Public Member Functions

 FormatArg (StringPiece sp)
 
void validate (Type type) const
 
template<typename... Args>
void enforce (bool v, Args &&...args) const
 
template<typename... Args>
std::string errorStr (Args &&...args) const
 
template<typename... Args>
void error (Args &&...args) const
 
template<bool emptyOk = false>
StringPiece splitKey ()
 
bool keyEmpty () const
 
int splitIntKey ()
 
void setNextIntKey (int val)
 
void setNextKey (StringPiece val)
 

Public Attributes

StringPiece fullArgString
 
char fill
 
Align align
 
Sign sign
 
bool basePrefix
 
bool thousandsSeparator
 
bool trailingDot
 
int width
 
int widthIndex
 
int precision
 
char presentation
 

Static Public Attributes

static constexpr char kDefaultFill = '\0'
 
static constexpr int kDefaultWidth = -1
 
static constexpr int kDynamicWidth = -2
 
static constexpr int kNoIndex = -1
 
static constexpr int kDefaultPrecision = -1
 
static constexpr char kDefaultPresentation = '\0'
 

Private Types

enum  NextKeyMode { NextKeyMode::NONE, NextKeyMode::INT, NextKeyMode::STRING }
 

Private Member Functions

void initSlow ()
 
template<bool emptyOk>
StringPiece doSplitKey ()
 

Private Attributes

StringPiece key_
 
int nextIntKey_
 
StringPiece nextKey_
 
NextKeyMode nextKeyMode_
 

Detailed Description

Parsed format argument.

Definition at line 37 of file FormatArg.h.

Member Enumeration Documentation

Alignment

Enumerator
DEFAULT 
LEFT 
RIGHT 
PAD_AFTER_SIGN 
CENTER 
INVALID 

Definition at line 101 of file FormatArg.h.

101  : uint8_t {
102  DEFAULT,
103  LEFT,
104  RIGHT,
105  PAD_AFTER_SIGN,
106  CENTER,
107  INVALID,
108  };
enum folly::FormatArg::NextKeyMode
strongprivate
Enumerator
NONE 
INT 
STRING 

Definition at line 199 of file FormatArg.h.

199  {
200  NONE,
201  INT,
202  STRING,
203  };
#define STRING

Sign

Enumerator
DEFAULT 
PLUS_OR_MINUS 
MINUS 
SPACE_OR_MINUS 
INVALID 

Definition at line 114 of file FormatArg.h.

114  : uint8_t {
115  DEFAULT,
116  PLUS_OR_MINUS,
117  MINUS,
118  SPACE_OR_MINUS,
119  INVALID,
120  };
Enumerator
INTEGER 
FLOAT 
OTHER 

Definition at line 60 of file FormatArg.h.

60  {
61  INTEGER,
62  FLOAT,
63  OTHER,
64  };

Constructor & Destructor Documentation

folly::FormatArg::FormatArg ( StringPiece  sp)
inlineexplicit

Parse a format argument from a string. Keeps a reference to the passed-in string – does not copy the given characters.

Definition at line 42 of file FormatArg.h.

References folly::Range< Iter >::empty().

43  : fullArgString(sp),
47  basePrefix(false),
48  thousandsSeparator(false),
49  trailingDot(false),
55  if (!sp.empty()) {
56  initSlow();
57  }
58  }
void initSlow()
Definition: Format.cpp:228
static constexpr int kDefaultPrecision
Definition: FormatArg.h:150
bool thousandsSeparator
Definition: FormatArg.h:131
static constexpr char kDefaultPresentation
Definition: FormatArg.h:156
static constexpr int kDefaultWidth
Definition: FormatArg.h:141
static constexpr char kDefaultFill
Definition: FormatArg.h:95
static constexpr int kNoIndex
Definition: FormatArg.h:143
StringPiece fullArgString
Definition: FormatArg.h:90
NextKeyMode nextKeyMode_
Definition: FormatArg.h:204

Member Function Documentation

template<bool emptyOk>
StringPiece folly::FormatArg::doSplitKey ( )
inlineprivate

Definition at line 228 of file FormatArg.h.

References b, folly::pushmi::operators::error(), folly::NONE, and STRING.

228  {
231  if (!emptyOk) { // static
232  enforce(!nextKey_.empty(), "non-empty key required");
233  }
234  return nextKey_;
235  }
236 
237  if (key_.empty()) {
238  if (!emptyOk) { // static
239  error("non-empty key required");
240  }
241  return StringPiece();
242  }
243 
244  const char* b = key_.begin();
245  const char* e = key_.end();
246  const char* p;
247  if (e[-1] == ']') {
248  --e;
249  p = static_cast<const char*>(memchr(b, '[', size_t(e - b)));
250  enforce(p != nullptr, "unmatched ']'");
251  } else {
252  p = static_cast<const char*>(memchr(b, '.', size_t(e - b)));
253  }
254  if (p) {
255  key_.assign(p + 1, e);
256  } else {
257  p = e;
258  key_.clear();
259  }
260  if (!emptyOk) { // static
261  enforce(b != p, "non-empty key required");
262  }
263  return StringPiece(b, p);
264 }
char b
StringPiece key_
Definition: FormatArg.h:196
void enforce(bool v, Args &&...args) const
Definition: FormatArg.h:76
StringPiece nextKey_
Definition: FormatArg.h:198
void clear()
Definition: Range.h:411
constexpr bool empty() const
Definition: Range.h:443
void assign(Iter start, Iter end)
Definition: Range.h:416
constexpr Iter end() const
Definition: Range.h:455
void error(Args &&...args) const
Definition: FormatArg.h:217
constexpr Iter begin() const
Definition: Range.h:452
Range< const char * > StringPiece
NextKeyMode nextKeyMode_
Definition: FormatArg.h:204
template<typename... Args>
void folly::FormatArg::enforce ( bool  v,
Args &&...  args 
) const
inline

Throw an exception if the first argument is false. The exception message will contain the argument string as well as any passed-in arguments to enforce, formatted using folly::to<std::string>.

Definition at line 76 of file FormatArg.h.

References testing::Args(), folly::pushmi::operators::error(), string, and UNLIKELY.

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

76  {
77  if (UNLIKELY(!v)) {
78  error(std::forward<Args>(args)...);
79  }
80  }
void error(Args &&...args) const
Definition: FormatArg.h:217
#define UNLIKELY(x)
Definition: Likely.h:48
template<typename... Args>
std::string folly::FormatArg::errorStr ( Args &&...  args) const
inline

Definition at line 208 of file FormatArg.h.

References testing::Args().

208  {
209  return to<std::string>(
210  "invalid format argument {",
212  "}: ",
213  std::forward<Args>(args)...);
214 }
StringPiece fullArgString
Definition: FormatArg.h:90
void folly::FormatArg::initSlow ( )
private

Definition at line 228 of file Format.cpp.

References a, b, c, folly::test::end(), folly::pushmi::operators::error(), folly::detail::formatAlignTable, folly::detail::formatSignTable, and s.

228  {
229  auto b = fullArgString.begin();
230  auto end = fullArgString.end();
231 
232  // Parse key
233  auto p = static_cast<const char*>(memchr(b, ':', size_t(end - b)));
234  if (!p) {
235  key_ = StringPiece(b, end);
236  return;
237  }
238  key_ = StringPiece(b, p);
239 
240  if (*p == ':') {
241  // parse format spec
242  if (++p == end) {
243  return;
244  }
245 
246  // fill/align, or just align
247  Align a;
248  if (p + 1 != end &&
249  (a = formatAlignTable[static_cast<unsigned char>(p[1])]) !=
250  Align::INVALID) {
251  fill = *p;
252  align = a;
253  p += 2;
254  if (p == end) {
255  return;
256  }
257  } else if (
258  (a = formatAlignTable[static_cast<unsigned char>(*p)]) !=
259  Align::INVALID) {
260  align = a;
261  if (++p == end) {
262  return;
263  }
264  }
265 
266  Sign s;
267  unsigned char uSign = static_cast<unsigned char>(*p);
268  if ((s = formatSignTable[uSign]) != Sign::INVALID) {
269  sign = s;
270  if (++p == end) {
271  return;
272  }
273  }
274 
275  if (*p == '#') {
276  basePrefix = true;
277  if (++p == end) {
278  return;
279  }
280  }
281 
282  if (*p == '0') {
283  enforce(align == Align::DEFAULT, "alignment specified twice");
284  fill = '0';
286  if (++p == end) {
287  return;
288  }
289  }
290 
291  auto readInt = [&] {
292  auto const c = p;
293  do {
294  ++p;
295  } while (p != end && *p >= '0' && *p <= '9');
296  return to<int>(StringPiece(c, p));
297  };
298 
299  if (*p == '*') {
301  ++p;
302 
303  if (p == end) {
304  return;
305  }
306 
307  if (*p >= '0' && *p <= '9') {
308  widthIndex = readInt();
309  }
310 
311  if (p == end) {
312  return;
313  }
314  } else if (*p >= '0' && *p <= '9') {
315  width = readInt();
316 
317  if (p == end) {
318  return;
319  }
320  }
321 
322  if (*p == ',') {
323  thousandsSeparator = true;
324  if (++p == end) {
325  return;
326  }
327  }
328 
329  if (*p == '.') {
330  auto d = ++p;
331  while (p != end && *p >= '0' && *p <= '9') {
332  ++p;
333  }
334  if (p != d) {
335  precision = to<int>(StringPiece(d, p));
336  if (p != end && *p == '.') {
337  trailingDot = true;
338  ++p;
339  }
340  } else {
341  trailingDot = true;
342  }
343 
344  if (p == end) {
345  return;
346  }
347  }
348 
349  presentation = *p;
350  if (++p == end) {
351  return;
352  }
353  }
354 
355  error("extra characters in format string");
356 }
FOLLY_STORAGE_CONSTEXPR auto formatAlignTable
Definition: Format.cpp:78
char b
FOLLY_STORAGE_CONSTEXPR auto formatSignTable
Definition: Format.cpp:80
StringPiece key_
Definition: FormatArg.h:196
void enforce(bool v, Args &&...args) const
Definition: FormatArg.h:76
bool thousandsSeparator
Definition: FormatArg.h:131
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
static constexpr int kDynamicWidth
Definition: FormatArg.h:142
char a
constexpr Iter end() const
Definition: Range.h:455
void error(Args &&...args) const
Definition: FormatArg.h:217
constexpr Iter begin() const
Definition: Range.h:452
static set< string > s
Range< const char * > StringPiece
StringPiece fullArgString
Definition: FormatArg.h:90
char c
bool folly::FormatArg::keyEmpty ( ) const
inline

Is the entire key empty?

Definition at line 169 of file FormatArg.h.

References folly::NONE.

169  {
170  return nextKeyMode_ == NextKeyMode::NONE && key_.empty();
171  }
StringPiece key_
Definition: FormatArg.h:196
constexpr bool empty() const
Definition: Range.h:443
NextKeyMode nextKeyMode_
Definition: FormatArg.h:204
void folly::FormatArg::setNextIntKey ( int  val)
inline

Definition at line 179 of file FormatArg.h.

References folly::NONE, and val.

void folly::FormatArg::setNextKey ( StringPiece  val)
inline

Definition at line 185 of file FormatArg.h.

References folly::NONE, STRING, and val.

int folly::FormatArg::splitIntKey ( )
inline

Split an key component from "key", which must be non-empty and a valid integer (an exception is thrown otherwise).

Definition at line 266 of file FormatArg.h.

References folly::pushmi::operators::error(), and folly::NONE.

Referenced by folly::FormatValue< dynamic >::format(), and folly::FormatValue< detail::DefaultValueWrapper< dynamic, V > >::format().

266  {
269  return nextIntKey_;
270  }
271  try {
272  return to<int>(doSplitKey<true>());
273  } catch (const std::out_of_range&) {
274  error("integer key required");
275  return 0; // unreached
276  }
277 }
void error(Args &&...args) const
Definition: FormatArg.h:217
NextKeyMode nextKeyMode_
Definition: FormatArg.h:204
template<bool emptyOk>
StringPiece folly::FormatArg::splitKey ( )
inline

Split a key component from "key", which must be non-empty (an exception is thrown otherwise).

Definition at line 222 of file FormatArg.h.

Referenced by folly::FormatValue< dynamic >::format(), and folly::FormatValue< detail::DefaultValueWrapper< dynamic, V > >::format().

222  {
223  enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
224  return doSplitKey<emptyOk>();
225 }
void enforce(bool v, Args &&...args) const
Definition: FormatArg.h:76
NextKeyMode nextKeyMode_
Definition: FormatArg.h:204
void folly::FormatArg::validate ( Type  type) const

Validate the argument for the given type; throws on error.

Definition at line 358 of file Format.cpp.

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

358  {
359  enforce(keyEmpty(), "index not allowed");
360  switch (type) {
361  case Type::INTEGER:
362  enforce(
363  precision == kDefaultPrecision, "precision not allowed on integers");
364  break;
365  case Type::FLOAT:
366  enforce(
367  !basePrefix, "base prefix ('#') specifier only allowed on integers");
368  enforce(
370  "thousands separator (',') only allowed on integers");
371  break;
372  case Type::OTHER:
373  enforce(
375  "'='alignment only allowed on numbers");
376  enforce(sign == Sign::DEFAULT, "sign specifier only allowed on numbers");
377  enforce(
378  !basePrefix, "base prefix ('#') specifier only allowed on integers");
379  enforce(
381  "thousands separator (',') only allowed on integers");
382  break;
383  }
384 }
void enforce(bool v, Args &&...args) const
Definition: FormatArg.h:76
static constexpr int kDefaultPrecision
Definition: FormatArg.h:150
bool thousandsSeparator
Definition: FormatArg.h:131
bool keyEmpty() const
Definition: FormatArg.h:169

Member Data Documentation

bool folly::FormatArg::basePrefix
char folly::FormatArg::fill

Definition at line 96 of file FormatArg.h.

Referenced by folly::format_value::formatString().

StringPiece folly::FormatArg::fullArgString

Full argument string, as passed in to the constructor.

Definition at line 90 of file FormatArg.h.

constexpr char folly::FormatArg::kDefaultFill = '\0'
static

Fill

Definition at line 95 of file FormatArg.h.

constexpr int folly::FormatArg::kDefaultPrecision = -1
static

Precision

Definition at line 150 of file FormatArg.h.

Referenced by folly::FormatValue< double >::formatHelper().

constexpr char folly::FormatArg::kDefaultPresentation = '\0'
static

Presentation

Definition at line 156 of file FormatArg.h.

Referenced by folly::FormatValue< double >::formatHelper().

constexpr int folly::FormatArg::kDefaultWidth = -1
static

Field width and optional argument index

Definition at line 141 of file FormatArg.h.

constexpr int folly::FormatArg::kDynamicWidth = -2
static

Definition at line 142 of file FormatArg.h.

StringPiece folly::FormatArg::key_
private

Definition at line 196 of file FormatArg.h.

constexpr int folly::FormatArg::kNoIndex = -1
static

Definition at line 143 of file FormatArg.h.

int folly::FormatArg::nextIntKey_
private

Definition at line 197 of file FormatArg.h.

StringPiece folly::FormatArg::nextKey_
private

Definition at line 198 of file FormatArg.h.

NextKeyMode folly::FormatArg::nextKeyMode_
private

Definition at line 204 of file FormatArg.h.

bool folly::FormatArg::thousandsSeparator
bool folly::FormatArg::trailingDot

Force a trailing decimal on doubles which could be rendered as ints

Definition at line 136 of file FormatArg.h.

Referenced by folly::FormatValue< double >::formatHelper().

int folly::FormatArg::width
int folly::FormatArg::widthIndex

Definition at line 145 of file FormatArg.h.


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