This commit is contained in:
Gregory Czajkowski 2013-11-20 13:24:53 -08:00
commit 6efa9ba41c
2 changed files with 240 additions and 206 deletions

View File

@ -410,10 +410,12 @@ 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;
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;
@ -542,7 +544,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_;
@ -637,6 +639,11 @@ 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);
} }
@ -672,6 +679,13 @@ 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::BasicWriter<wchar_t>::FormatDouble<double>( template void fmt::BasicWriter<wchar_t>::FormatDouble<double>(
@ -704,3 +718,10 @@ template void fmt::BasicFormatter<wchar_t>::CheckSign(
const wchar_t *&s, const Arg &arg); const wchar_t *&s, const Arg &arg);
template void fmt::BasicFormatter<wchar_t>::DoFormat(); template void fmt::BasicFormatter<wchar_t>::DoFormat();
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();
}

View File

@ -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> {};
@ -289,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
}; };