out(StringPiece(p, q));
p = q;
- if (p == end || *p != '}') {
- throw std::invalid_argument(
- "folly::format: single '}' in format string");
- }
+ CHECK(p != end && *p == '}') << "single '}' in format string";
++p;
}
};
outputString(StringPiece(p, q));
p = q + 1;
- if (p == end) {
- throw std::invalid_argument(
- "folly::format: '}' at end of format string");
- }
+ CHECK(p != end) << "'{' at end of format string";
// "{{" -> "{"
if (*p == '{') {
// Format string
q = static_cast<const char*>(memchr(p, '}', end - p));
- if (q == end) {
- throw std::invalid_argument("folly::format: missing ending '}'");
- }
+ CHECK(q != end) << "missing ending '}'";
FormatArg arg(StringPiece(p, q));
p = q + 1;
try {
argIndex = to<int>(piece);
} catch (const std::out_of_range& e) {
- arg.error("argument index must be integer");
+ LOG(FATAL) << "argument index must be integer";
}
- arg.enforce(argIndex >= 0, "argument index must be non-negative");
+ CHECK(argIndex >= 0)
+ << arg.errorStr("argument index must be non-negative");
hasExplicitArgIndex = true;
}
}
- if (hasDefaultArgIndex && hasExplicitArgIndex) {
- throw std::invalid_argument(
- "folly::format: may not have both default and explicit arg indexes");
- }
+ CHECK(!hasDefaultArgIndex || !hasExplicitArgIndex)
+ << "may not have both default and explicit arg indexes";
doFormat(argIndex, arg, out);
}
uval = val_;
sign = '\0';
- arg.enforce(arg.sign == FormatArg::Sign::DEFAULT,
- "sign specifications not allowed for unsigned values");
+ CHECK(arg.sign == FormatArg::Sign::DEFAULT)
+ << arg.errorStr("sign specifications not allowed for unsigned values");
}
// max of:
switch (presentation) {
case 'n': // TODO(tudorb): locale awareness?
case 'd':
- arg.enforce(!arg.basePrefix,
- "base prefix not allowed with '", presentation,
- "' specifier");
+ CHECK(!arg.basePrefix)
+ << arg.errorStr("base prefix not allowed with '", presentation,
+ "' specifier");
if (arg.thousandsSeparator) {
useSprintf("%'ju");
} else {
}
break;
case 'c':
- arg.enforce(!arg.basePrefix,
- "base prefix not allowed with '", presentation,
- "' specifier");
- arg.enforce(!arg.thousandsSeparator,
- "thousands separator (',') not allowed with '",
- presentation, "' specifier");
+ CHECK(!arg.basePrefix)
+ << arg.errorStr("base prefix not allowed with '", presentation,
+ "' specifier");
+ CHECK(!arg.thousandsSeparator)
+ << arg.errorStr("thousands separator (',') not allowed with '",
+ presentation, "' specifier");
valBufBegin = valBuf + 3;
*valBufBegin = static_cast<char>(uval);
valBufEnd = valBufBegin + 1;
break;
case 'o':
case 'O':
- arg.enforce(!arg.thousandsSeparator,
- "thousands separator (',') not allowed with '",
- presentation, "' specifier");
+ CHECK(!arg.thousandsSeparator)
+ << arg.errorStr("thousands separator (',') not allowed with '",
+ presentation, "' specifier");
valBufEnd = valBuf + valBufSize - 1;
valBufBegin = valBuf + detail::uintToOctal(valBuf, valBufSize - 1, uval);
if (arg.basePrefix) {
}
break;
case 'x':
- arg.enforce(!arg.thousandsSeparator,
- "thousands separator (',') not allowed with '",
- presentation, "' specifier");
+ CHECK(!arg.thousandsSeparator)
+ << arg.errorStr("thousands separator (',') not allowed with '",
+ presentation, "' specifier");
valBufEnd = valBuf + valBufSize - 1;
valBufBegin = valBuf + detail::uintToHexLower(valBuf, valBufSize - 1,
uval);
}
break;
case 'X':
- arg.enforce(!arg.thousandsSeparator,
- "thousands separator (',') not allowed with '",
- presentation, "' specifier");
+ CHECK(!arg.thousandsSeparator)
+ << arg.errorStr("thousands separator (',') not allowed with '",
+ presentation, "' specifier");
valBufEnd = valBuf + valBufSize - 1;
valBufBegin = valBuf + detail::uintToHexUpper(valBuf, valBufSize - 1,
uval);
break;
case 'b':
case 'B':
- arg.enforce(!arg.thousandsSeparator,
- "thousands separator (',') not allowed with '",
- presentation, "' specifier");
+ CHECK(!arg.thousandsSeparator)
+ << arg.errorStr("thousands separator (',') not allowed with '",
+ presentation, "' specifier");
valBufEnd = valBuf + valBufSize - 1;
valBufBegin = valBuf + detail::uintToBinary(valBuf, valBufSize - 1,
uval);
}
break;
default:
- arg.error("invalid specifier '", presentation, "'");
+ LOG(FATAL) << arg.errorStr("invalid specifier '", presentation, "'");
}
if (sign) {
}
break;
default:
- arg.error("invalid specifier '", arg.presentation, "'");
+ LOG(FATAL) << arg.errorStr("invalid specifier '", arg.presentation, "'");
}
int len = builder.position();
void format(FormatArg& arg, FormatCallback& cb) const {
if (arg.keyEmpty()) {
arg.validate(FormatArg::Type::OTHER);
- arg.enforce(arg.presentation == FormatArg::kDefaultPresentation ||
- arg.presentation == 's',
- "invalid specifier '", arg.presentation, "'");
+ CHECK(arg.presentation == FormatArg::kDefaultPresentation ||
+ arg.presentation == 's')
+ << arg.errorStr("invalid specifier '", arg.presentation, "'");
format_value::formatString(val_, arg, cb);
} else {
FormatValue<char>(val_.at(arg.splitIntKey())).format(arg, cb);
template <class FormatCallback>
void format(FormatArg& arg, FormatCallback& cb) const {
arg.validate(FormatArg::Type::OTHER);
- arg.enforce(arg.presentation == FormatArg::kDefaultPresentation,
- "invalid specifier '", arg.presentation, "'");
+ CHECK(arg.presentation == FormatArg::kDefaultPresentation)
+ << arg.errorStr("invalid specifier '", arg.presentation, "'");
format_value::formatString("(null)", arg, cb);
}
};
} else {
// Print as a pointer, in hex.
arg.validate(FormatArg::Type::OTHER);
- arg.enforce(arg.presentation == FormatArg::kDefaultPresentation,
- "invalid specifier '", arg.presentation, "'");
+ CHECK(arg.presentation == FormatArg::kDefaultPresentation)
+ << arg.errorStr("invalid specifier '", arg.presentation, "'");
arg.basePrefix = true;
arg.presentation = 'x';
if (arg.align == FormatArg::Align::DEFAULT) {
public:
template <class FormatCallback>
static void formatOrFail(T& value, FormatArg& arg, FormatCallback& cb) {
- arg.error("No formatter available for this type");
+ LOG(FATAL) << arg.errorStr("No formatter available for this type");
}
};
FormatValue<typename std::decay<B>::type>(val_.second).format(arg, cb);
break;
default:
- arg.error("invalid index for pair");
+ LOG(FATAL) << arg.errorStr("invalid index for pair");
}
}
template <class FormatCallback>
void format(FormatArg& arg, FormatCallback& cb) const {
int key = arg.splitIntKey();
- arg.enforce(key >= 0, "tuple index must be non-negative");
+ CHECK(key >= 0) << arg.errorStr("tuple index must be non-negative");
doFormat(key, arg, cb);
}
template <size_t K, class Callback>
typename std::enable_if<K == valueCount>::type
doFormatFrom(size_t i, FormatArg& arg, Callback& cb) const {
- arg.enforce("tuple index out of range, max=", i);
+ LOG(FATAL) << arg.errorStr("tuple index out of range, max=", i);
}
template <size_t K, class Callback>
}
if (*p == '0') {
- enforce(align == Align::DEFAULT, "alignment specified twice");
+ CHECK(align == Align::DEFAULT) << errorStr("alignment specified twice");
fill = '0';
align = Align::PAD_AFTER_SIGN;
if (++p == end) return;
if (++p == end) return;
}
- error("extra characters in format string");
+ LOG(FATAL) << "extra characters in format string";
}
void FormatArg::validate(Type type) const {
- enforce(keyEmpty(), "index not allowed");
+ CHECK(keyEmpty()) << "index not allowed";
switch (type) {
case Type::INTEGER:
- enforce(precision == kDefaultPrecision,
- "precision not allowed on integers");
+ CHECK(precision == kDefaultPrecision)
+ << errorStr("precision not allowed on integers");
break;
case Type::FLOAT:
- enforce(!basePrefix,
- "base prefix ('#') specifier only allowed on integers");
- enforce(!thousandsSeparator,
- "thousands separator (',') only allowed on integers");
+ CHECK(!basePrefix)
+ << errorStr("base prefix ('#') specifier only allowed on integers");
+ CHECK(!thousandsSeparator)
+ << errorStr("thousands separator (',') only allowed on integers");
break;
case Type::OTHER:
- enforce(align != Align::PAD_AFTER_SIGN,
- "'='alignment only allowed on numbers");
- enforce(sign == Sign::DEFAULT,
- "sign specifier only allowed on numbers");
- enforce(!basePrefix,
- "base prefix ('#') specifier only allowed on integers");
- enforce(!thousandsSeparator,
- "thousands separator (',') only allowed on integers");
+ CHECK(align != Align::PAD_AFTER_SIGN)
+ << errorStr("'='alignment only allowed on numbers");
+ CHECK(sign == Sign::DEFAULT)
+ << errorStr("sign specifier only allowed on numbers");
+ CHECK(!basePrefix)
+ << errorStr("base prefix ('#') specifier only allowed on integers");
+ CHECK(!thousandsSeparator)
+ << errorStr("thousands separator (',') only allowed on integers");
break;
}
}
}
}
+ template <typename... Args>
+ std::string errorStr(Args&&... args) const;
template <typename... Args>
void error(Args&&... args) const FOLLY_NORETURN;
+
/**
* Full argument string, as passed in to the constructor.
*/
NextKeyMode nextKeyMode_;
};
+template <typename... Args>
+inline std::string FormatArg::errorStr(Args&&... args) const {
+ return to<std::string>(
+ "invalid format argument {", fullArgString, "}: ",
+ std::forward<Args>(args)...);
+}
+
template <typename... Args>
inline void FormatArg::error(Args&&... args) const {
- throw std::invalid_argument(to<std::string>(
- "folly::format: invalid format argument {", fullArgString, "}: ",
- std::forward<Args>(args)...));
+ throw std::invalid_argument(errorStr(std::forward<Args>(args)...));
}
template <bool emptyOk>
inline StringPiece FormatArg::splitKey() {
- enforce(nextKeyMode_ != NextKeyMode::INT, "integer key expected");
+ CHECK(nextKeyMode_ != NextKeyMode::INT) << errorStr("integer key expected");
return doSplitKey<emptyOk>();
}
inline StringPiece FormatArg::doSplitKey() {
if (nextKeyMode_ == NextKeyMode::STRING) {
nextKeyMode_ = NextKeyMode::NONE;
- if (!emptyOk) { // static
- enforce(!nextKey_.empty(), "non-empty key required");
- }
+ CHECK(emptyOk || !nextKey_.empty()) << errorStr("non-empty key required");
return nextKey_;
}
if (key_.empty()) {
- if (!emptyOk) { // static
- error("non-empty key required");
- }
+ CHECK(emptyOk) << errorStr("non-empty key required");
return StringPiece();
}
if (e[-1] == ']') {
--e;
p = static_cast<const char*>(memchr(b, '[', e - b));
- enforce(p, "unmatched ']'");
+ CHECK(p) << errorStr("unmatched ']'");
} else {
p = static_cast<const char*>(memchr(b, '.', e - b));
}
p = e;
key_.clear();
}
- if (!emptyOk) { // static
- enforce(b != p, "non-empty key required");
- }
+ CHECK(emptyOk || b != p) << errorStr("non-empty key required");
+
return StringPiece(b, p);
}
try {
return to<int>(doSplitKey<true>());
} catch (const std::out_of_range& e) {
- error("integer key required");
+ LOG(FATAL) << errorStr("integer key required");
return 0; // unreached
}
}