mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-26 20:20:04 +00:00
Refactor error reporting to reduce duplication.
This commit is contained in:
parent
605d2600f8
commit
5debb2aa86
23
format.cc
23
format.cc
@ -175,13 +175,17 @@ int parse_nonnegative_int(const Char *&s) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void require_numeric_argument(const Arg &arg, char spec) {
|
||||||
|
if (arg.type > Arg::LAST_NUMERIC_TYPE) {
|
||||||
|
throw fmt::FormatError(
|
||||||
|
fmt::format("format specifier '{}' requires numeric argument", spec));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void check_sign(const Char *&s, const Arg &arg) {
|
void check_sign(const Char *&s, const Arg &arg) {
|
||||||
char sign = static_cast<char>(*s);
|
char sign = static_cast<char>(*s);
|
||||||
if (arg.type > Arg::LAST_NUMERIC_TYPE) {
|
require_numeric_argument(arg, sign);
|
||||||
throw fmt::FormatError(fmt::format(
|
|
||||||
"format specifier '{}' requires numeric argument", sign));
|
|
||||||
}
|
|
||||||
if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
|
if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
|
||||||
throw fmt::FormatError(fmt::format(
|
throw fmt::FormatError(fmt::format(
|
||||||
"format specifier '{}' requires signed argument", sign));
|
"format specifier '{}' requires signed argument", sign));
|
||||||
@ -1059,8 +1063,8 @@ const Char *fmt::BasicFormatter<Char>::format(
|
|||||||
s += 2;
|
s += 2;
|
||||||
spec.fill_ = c;
|
spec.fill_ = c;
|
||||||
} else ++s;
|
} else ++s;
|
||||||
if (spec.align_ == ALIGN_NUMERIC && arg.type > Arg::LAST_NUMERIC_TYPE)
|
if (spec.align_ == ALIGN_NUMERIC)
|
||||||
throw FormatError("format specifier '=' requires numeric argument");
|
require_numeric_argument(arg, '=');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (--p >= s);
|
} while (--p >= s);
|
||||||
@ -1083,9 +1087,7 @@ const Char *fmt::BasicFormatter<Char>::format(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (*s == '#') {
|
if (*s == '#') {
|
||||||
if (arg.type > Arg::LAST_NUMERIC_TYPE)
|
require_numeric_argument(arg, '#');
|
||||||
// TODO: make FormatError accept arguments
|
|
||||||
throw FormatError("format specifier '#' requires numeric argument");
|
|
||||||
spec.flags_ |= HASH_FLAG;
|
spec.flags_ |= HASH_FLAG;
|
||||||
++s;
|
++s;
|
||||||
}
|
}
|
||||||
@ -1093,8 +1095,7 @@ const Char *fmt::BasicFormatter<Char>::format(
|
|||||||
// Parse width and zero flag.
|
// Parse width and zero flag.
|
||||||
if ('0' <= *s && *s <= '9') {
|
if ('0' <= *s && *s <= '9') {
|
||||||
if (*s == '0') {
|
if (*s == '0') {
|
||||||
if (arg.type > Arg::LAST_NUMERIC_TYPE)
|
require_numeric_argument(arg, '0');
|
||||||
throw FormatError("format specifier '0' requires numeric argument");
|
|
||||||
spec.align_ = ALIGN_NUMERIC;
|
spec.align_ = ALIGN_NUMERIC;
|
||||||
spec.fill_ = '0';
|
spec.fill_ = '0';
|
||||||
}
|
}
|
||||||
|
4
format.h
4
format.h
@ -215,8 +215,8 @@ typedef BasicStringRef<wchar_t> WStringRef;
|
|||||||
*/
|
*/
|
||||||
class FormatError : public std::runtime_error {
|
class FormatError : public std::runtime_error {
|
||||||
public:
|
public:
|
||||||
explicit FormatError(const std::string &message)
|
explicit FormatError(StringRef message)
|
||||||
: std::runtime_error(message) {}
|
: std::runtime_error(message.c_str()) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
Loading…
Reference in New Issue
Block a user