reworked format exceptions
new exception which contain the pointer to the format as a class member
This commit is contained in:
parent
74d363d2f8
commit
264d006dd2
60
format.cc
60
format.cc
@ -110,15 +110,14 @@ const char fmt::internal::DIGITS[] =
|
|||||||
"6061626364656667686970717273747576777879"
|
"6061626364656667686970717273747576777879"
|
||||||
"8081828384858687888990919293949596979899";
|
"8081828384858687888990919293949596979899";
|
||||||
|
|
||||||
template <typename Char>
|
void fmt::internal::ReportUnknownType(char code, const char *type) {
|
||||||
void fmt::internal::ReportUnknownType(const Char *format, char code, const char *type) {
|
|
||||||
if (std::isprint(static_cast<unsigned char>(code))) {
|
if (std::isprint(static_cast<unsigned char>(code))) {
|
||||||
throw fmt::FormatError(fmt::str(
|
throw fmt::FormatError(fmt::str(
|
||||||
fmt::Format("unknown format code '{}' for {} while parsing {}") << code << type << format));
|
fmt::Format("unknown format code '{}' for {}") << code << type));
|
||||||
}
|
}
|
||||||
throw fmt::FormatError(
|
throw fmt::FormatError(
|
||||||
fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {} while parsing {}")
|
fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {}")
|
||||||
<< static_cast<unsigned>(code) << type << format));
|
<< static_cast<unsigned>(code) << type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -201,7 +200,7 @@ typename fmt::BasicWriter<Char>::CharPtr
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void fmt::BasicWriter<Char>::FormatDouble(
|
void fmt::BasicWriter<Char>::FormatDouble(
|
||||||
T value, const FormatSpec<Char> &spec, int precision) {
|
T value, const FormatSpec &spec, int precision) {
|
||||||
// Check type.
|
// Check type.
|
||||||
char type = spec.type();
|
char type = spec.type();
|
||||||
bool upper = false;
|
bool upper = false;
|
||||||
@ -221,7 +220,7 @@ void fmt::BasicWriter<Char>::FormatDouble(
|
|||||||
upper = true;
|
upper = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
internal::ReportUnknownType<Char>(spec.format(), type, "double");
|
internal::ReportUnknownType(type, "double");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -411,11 +410,13 @@ void fmt::BasicFormatter<Char>::CheckSign(const Char *&s, const Arg &arg) {
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
void fmt::BasicFormatter<Char>::DoFormat() {
|
void fmt::BasicFormatter<Char>::DoFormat() {
|
||||||
const Char *start = format_;
|
const Char *start = format_;
|
||||||
|
const Char *original = format_; // capture the format string before it is reset
|
||||||
format_ = 0;
|
format_ = 0;
|
||||||
next_arg_index_ = 0;
|
next_arg_index_ = 0;
|
||||||
const Char *s = start;
|
const Char *s = start;
|
||||||
typedef internal::Array<Char, BasicWriter<Char>::INLINE_BUFFER_SIZE> Buffer;
|
typedef internal::Array<Char, BasicWriter<Char>::INLINE_BUFFER_SIZE> Buffer;
|
||||||
BasicWriter<Char> &writer = *writer_;
|
BasicWriter<Char> &writer = *writer_;
|
||||||
|
try {
|
||||||
while (*s) {
|
while (*s) {
|
||||||
Char c = *s++;
|
Char c = *s++;
|
||||||
if (c != '{' && c != '}') continue;
|
if (c != '{' && c != '}') continue;
|
||||||
@ -431,7 +432,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
|||||||
|
|
||||||
const Arg &arg = ParseArgIndex(s);
|
const Arg &arg = ParseArgIndex(s);
|
||||||
|
|
||||||
FormatSpec<Char> spec(format_);
|
FormatSpec spec;
|
||||||
int precision = -1;
|
int precision = -1;
|
||||||
if (*s == ':') {
|
if (*s == ':') {
|
||||||
++s;
|
++s;
|
||||||
@ -544,7 +545,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
|||||||
}
|
}
|
||||||
if (value > INT_MAX)
|
if (value > INT_MAX)
|
||||||
ReportError(s, "number is too big in format");
|
ReportError(s, "number is too big in format");
|
||||||
precision = value;
|
precision = static_cast<int>(value);
|
||||||
if (*s++ != '}')
|
if (*s++ != '}')
|
||||||
throw FormatError("unmatched '{' in format");
|
throw FormatError("unmatched '{' in format");
|
||||||
--num_open_braces_;
|
--num_open_braces_;
|
||||||
@ -588,7 +589,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
|||||||
break;
|
break;
|
||||||
case CHAR: {
|
case CHAR: {
|
||||||
if (spec.type_ && spec.type_ != 'c')
|
if (spec.type_ && spec.type_ != 'c')
|
||||||
internal::ReportUnknownType<Char>(spec.format_, spec.type_, "char");
|
internal::ReportUnknownType(spec.type_, "char");
|
||||||
typedef typename BasicWriter<Char>::CharPtr CharPtr;
|
typedef typename BasicWriter<Char>::CharPtr CharPtr;
|
||||||
CharPtr out = CharPtr();
|
CharPtr out = CharPtr();
|
||||||
if (spec.width_ > 1) {
|
if (spec.width_ > 1) {
|
||||||
@ -610,7 +611,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
|||||||
}
|
}
|
||||||
case STRING: {
|
case STRING: {
|
||||||
if (spec.type_ && spec.type_ != 's')
|
if (spec.type_ && spec.type_ != 's')
|
||||||
internal::ReportUnknownType<Char>(spec.format_, spec.type_, "string");
|
internal::ReportUnknownType(spec.type_, "string");
|
||||||
const Char *str = arg.string.value;
|
const Char *str = arg.string.value;
|
||||||
std::size_t size = arg.string.size;
|
std::size_t size = arg.string.size;
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
@ -624,14 +625,14 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
|||||||
}
|
}
|
||||||
case POINTER:
|
case POINTER:
|
||||||
if (spec.type_ && spec.type_ != 'p')
|
if (spec.type_ && spec.type_ != 'p')
|
||||||
internal::ReportUnknownType<Char>(spec.format_, spec.type_, "pointer");
|
internal::ReportUnknownType(spec.type_, "pointer");
|
||||||
spec.flags_= HASH_FLAG;
|
spec.flags_= HASH_FLAG;
|
||||||
spec.type_ = 'x';
|
spec.type_ = 'x';
|
||||||
writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
|
writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
|
||||||
break;
|
break;
|
||||||
case CUSTOM:
|
case CUSTOM:
|
||||||
if (spec.type_)
|
if (spec.type_)
|
||||||
internal::ReportUnknownType<Char>(spec.format_, spec.type_, "object");
|
internal::ReportUnknownType(spec.type_, "object");
|
||||||
arg.custom.format(writer, arg.custom.value, spec);
|
arg.custom.format(writer, arg.custom.value, spec);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -639,19 +640,21 @@ void fmt::BasicFormatter<Char>::DoFormat() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (const FormatError &e) {
|
||||||
|
// rethrow FormatError with the format string pointed to by start
|
||||||
|
throw BasicFormatError<Char>(e.what(), original);
|
||||||
|
}
|
||||||
|
|
||||||
writer.buffer_.append(start, s);
|
writer.buffer_.append(start, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicit instantiations for char.
|
// Explicit instantiations for char.
|
||||||
|
|
||||||
template void fmt::internal::ReportUnknownType<char>(
|
|
||||||
const char *format, char code, const char *type);
|
|
||||||
|
|
||||||
template void fmt::BasicWriter<char>::FormatDouble<double>(
|
template void fmt::BasicWriter<char>::FormatDouble<double>(
|
||||||
double value, const FormatSpec<char> &spec, int precision);
|
double value, const FormatSpec &spec, int precision);
|
||||||
|
|
||||||
template void fmt::BasicWriter<char>::FormatDouble<long double>(
|
template void fmt::BasicWriter<char>::FormatDouble<long double>(
|
||||||
long double value, const FormatSpec<char> &spec, int precision);
|
long double value, const FormatSpec &spec, int precision);
|
||||||
|
|
||||||
template fmt::BasicWriter<char>::CharPtr
|
template fmt::BasicWriter<char>::CharPtr
|
||||||
fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
|
fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
|
||||||
@ -677,16 +680,20 @@ template void fmt::BasicFormatter<char>::CheckSign(
|
|||||||
|
|
||||||
template void fmt::BasicFormatter<char>::DoFormat();
|
template void fmt::BasicFormatter<char>::DoFormat();
|
||||||
|
|
||||||
|
template<> fmt::BasicFormatError<char>::BasicFormatError(const std::string &message, const char *format)
|
||||||
|
: std::runtime_error(message), format_(format) {}
|
||||||
|
|
||||||
|
template<> fmt::BasicFormatError<char>::~BasicFormatError() {
|
||||||
|
std::runtime_error::~runtime_error();
|
||||||
|
}
|
||||||
|
|
||||||
// Explicit instantiations for wchar_t.
|
// Explicit instantiations for wchar_t.
|
||||||
|
|
||||||
template void fmt::internal::ReportUnknownType<wchar_t>(
|
|
||||||
const wchar_t *format, char code, const char *type);
|
|
||||||
|
|
||||||
template void fmt::BasicWriter<wchar_t>::FormatDouble<double>(
|
template void fmt::BasicWriter<wchar_t>::FormatDouble<double>(
|
||||||
double value, const FormatSpec<wchar_t> &spec, int precision);
|
double value, const FormatSpec &spec, int precision);
|
||||||
|
|
||||||
template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>(
|
template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>(
|
||||||
long double value, const FormatSpec<wchar_t> &spec, int precision);
|
long double value, const FormatSpec &spec, int precision);
|
||||||
|
|
||||||
template fmt::BasicWriter<wchar_t>::CharPtr
|
template fmt::BasicWriter<wchar_t>::CharPtr
|
||||||
fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
|
fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
|
||||||
@ -713,4 +720,9 @@ template void fmt::BasicFormatter<wchar_t>::CheckSign(
|
|||||||
|
|
||||||
template void fmt::BasicFormatter<wchar_t>::DoFormat();
|
template void fmt::BasicFormatter<wchar_t>::DoFormat();
|
||||||
|
|
||||||
//template fmt::BasicFormatter<char>::Arg::Arg(wchar_t const*);
|
template<> fmt::BasicFormatError<wchar_t>::BasicFormatError(const std::string &message, const wchar_t *format)
|
||||||
|
: std::runtime_error(message), format_(format){}
|
||||||
|
|
||||||
|
template<> fmt::BasicFormatError<wchar_t>::~BasicFormatError() {
|
||||||
|
std::runtime_error::~runtime_error();
|
||||||
|
}
|
||||||
|
|||||||
47
format.h
47
format.h
@ -189,6 +189,9 @@ struct SignedIntTraits {
|
|||||||
template <>
|
template <>
|
||||||
struct IntTraits<int> : SignedIntTraits<int, unsigned> {};
|
struct IntTraits<int> : SignedIntTraits<int, unsigned> {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct IntTraits<uint32_t> : SignedIntTraits<uint32_t, unsigned> {};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct IntTraits<long> : SignedIntTraits<long, unsigned long> {};
|
struct IntTraits<long> : SignedIntTraits<long, unsigned long> {};
|
||||||
|
|
||||||
@ -201,8 +204,7 @@ struct IsLongDouble { enum {VALUE = 0}; };
|
|||||||
template <>
|
template <>
|
||||||
struct IsLongDouble<long double> { enum {VALUE = 1}; };
|
struct IsLongDouble<long double> { enum {VALUE = 1}; };
|
||||||
|
|
||||||
template <typename Char>
|
void ReportUnknownType(char code, const char *type);
|
||||||
void ReportUnknownType(const Char *format, char code, const char *type);
|
|
||||||
|
|
||||||
// Returns the number of decimal digits in n. Leading zeros are not counted
|
// Returns the number of decimal digits in n. Leading zeros are not counted
|
||||||
// except for n == 0 in which case CountDigits returns 1.
|
// except for n == 0 in which case CountDigits returns 1.
|
||||||
@ -290,6 +292,16 @@ class FormatError : public std::runtime_error {
|
|||||||
: std::runtime_error(message) {}
|
: std::runtime_error(message) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Char>
|
||||||
|
class BasicFormatError : public std::runtime_error {
|
||||||
|
private:
|
||||||
|
std::basic_string<Char> format_;
|
||||||
|
public:
|
||||||
|
explicit BasicFormatError(const std::string &message, const Char *format);
|
||||||
|
virtual ~BasicFormatError() throw();
|
||||||
|
const Char *format() const { return format_.c_str(); }
|
||||||
|
};
|
||||||
|
|
||||||
enum Alignment {
|
enum Alignment {
|
||||||
ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
|
ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
|
||||||
};
|
};
|
||||||
@ -344,14 +356,12 @@ struct AlignTypeSpec : AlignSpec {
|
|||||||
char type() const { return TYPE; }
|
char type() const { return TYPE; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Char>
|
|
||||||
struct FormatSpec : AlignSpec {
|
struct FormatSpec : AlignSpec {
|
||||||
unsigned flags_;
|
unsigned flags_;
|
||||||
char type_;
|
char type_;
|
||||||
const Char *format_;
|
|
||||||
|
|
||||||
FormatSpec(const Char *format, unsigned width = 0, char type = 0, wchar_t fill = ' ')
|
FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ')
|
||||||
: AlignSpec(width, fill), flags_(0), type_(type), format_(format) {}
|
: AlignSpec(width, fill), flags_(0), type_(type) {}
|
||||||
|
|
||||||
Alignment align() const { return align_; }
|
Alignment align() const { return align_; }
|
||||||
|
|
||||||
@ -360,7 +370,6 @@ struct FormatSpec : AlignSpec {
|
|||||||
bool hash_flag() const { return (flags_ & HASH_FLAG) != 0; }
|
bool hash_flag() const { return (flags_ & HASH_FLAG) != 0; }
|
||||||
|
|
||||||
char type() const { return type_; }
|
char type() const { return type_; }
|
||||||
const Char* format() const { return format_; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename SpecT>
|
template <typename T, typename SpecT>
|
||||||
@ -532,17 +541,17 @@ class BasicWriter {
|
|||||||
|
|
||||||
// Formats an integer.
|
// Formats an integer.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void FormatInt(T value, const FormatSpec<Char> &spec) {
|
void FormatInt(T value, const FormatSpec &spec) {
|
||||||
*this << IntFormatter<T, FormatSpec<Char> >(value, spec);
|
*this << IntFormatter<T, FormatSpec>(value, spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formats a floating-point number (double or long double).
|
// Formats a floating-point number (double or long double).
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void FormatDouble(T value, const FormatSpec<Char> &spec, int precision);
|
void FormatDouble(T value, const FormatSpec &spec, int precision);
|
||||||
|
|
||||||
template <typename StringChar>
|
template <typename StringChar>
|
||||||
CharPtr FormatString(const StringChar *s,
|
CharPtr FormatString(const StringChar *s,
|
||||||
std::size_t size, const FormatSpec<Char> &spec);
|
std::size_t size, const FormatSpec &spec);
|
||||||
|
|
||||||
// This method is private to disallow writing a wide string to a
|
// This method is private to disallow writing a wide string to a
|
||||||
// char stream and vice versa. If you want to print a wide string
|
// char stream and vice versa. If you want to print a wide string
|
||||||
@ -632,7 +641,7 @@ class BasicWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BasicWriter &operator<<(double value) {
|
BasicWriter &operator<<(double value) {
|
||||||
FormatDouble(value, FormatSpec<Char>(""), -1);
|
FormatDouble(value, FormatSpec(), -1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -641,7 +650,7 @@ class BasicWriter {
|
|||||||
(``'g'``) and writes it to the stream.
|
(``'g'``) and writes it to the stream.
|
||||||
*/
|
*/
|
||||||
BasicWriter &operator<<(long double value) {
|
BasicWriter &operator<<(long double value) {
|
||||||
FormatDouble(value, FormatSpec<Char>(""), -1);
|
FormatDouble(value, FormatSpec(), -1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -663,7 +672,7 @@ class BasicWriter {
|
|||||||
template <typename T, typename Spec>
|
template <typename T, typename Spec>
|
||||||
BasicWriter &operator<<(const IntFormatter<T, Spec> &f);
|
BasicWriter &operator<<(const IntFormatter<T, Spec> &f);
|
||||||
|
|
||||||
void Write(const std::basic_string<char> &s, const FormatSpec<char> &spec) {
|
void Write(const std::basic_string<char> &s, const FormatSpec &spec) {
|
||||||
FormatString(s.data(), s.size(), spec);
|
FormatString(s.data(), s.size(), spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -675,7 +684,7 @@ class BasicWriter {
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
template <typename StringChar>
|
template <typename StringChar>
|
||||||
typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString(
|
typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString(
|
||||||
const StringChar *s, std::size_t size, const FormatSpec<Char> &spec) {
|
const StringChar *s, std::size_t size, const FormatSpec &spec) {
|
||||||
CharPtr out = CharPtr();
|
CharPtr out = CharPtr();
|
||||||
if (spec.width() > size) {
|
if (spec.width() > size) {
|
||||||
out = GrowBuffer(spec.width());
|
out = GrowBuffer(spec.width());
|
||||||
@ -776,7 +785,7 @@ BasicWriter<Char> &BasicWriter<Char>::operator<<(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
internal::ReportUnknownType<Char>(f.format(), f.type(), "integer");
|
internal::ReportUnknownType(f.type(), "integer");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
@ -792,7 +801,7 @@ typedef BasicWriter<wchar_t> WWriter;
|
|||||||
|
|
||||||
// The default formatting function.
|
// The default formatting function.
|
||||||
template <typename Char, typename T>
|
template <typename Char, typename T>
|
||||||
void Format(BasicWriter<Char> &w, const FormatSpec<Char> &spec, const T &value) {
|
void Format(BasicWriter<Char> &w, const FormatSpec &spec, const T &value) {
|
||||||
std::basic_ostringstream<Char> os;
|
std::basic_ostringstream<Char> os;
|
||||||
os << value;
|
os << value;
|
||||||
w.Write(os.str(), spec);
|
w.Write(os.str(), spec);
|
||||||
@ -802,7 +811,7 @@ namespace internal {
|
|||||||
// Formats an argument of a custom type, such as a user-defined class.
|
// Formats an argument of a custom type, such as a user-defined class.
|
||||||
template <typename Char, typename T>
|
template <typename Char, typename T>
|
||||||
void FormatCustomArg(
|
void FormatCustomArg(
|
||||||
BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec) {
|
BasicWriter<Char> &w, const void *arg, const FormatSpec &spec) {
|
||||||
Format(w, spec, *static_cast<const T*>(arg));
|
Format(w, spec, *static_cast<const T*>(arg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -829,7 +838,7 @@ class BasicFormatter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef void (*FormatFunc)(
|
typedef void (*FormatFunc)(
|
||||||
BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec);
|
BasicWriter<Char> &w, const void *arg, const FormatSpec &spec);
|
||||||
|
|
||||||
// A format argument.
|
// A format argument.
|
||||||
class Arg {
|
class Arg {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user