mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Pass errors to handler instead of throwing (#566)
This commit is contained in:
15
.travis.yml
15
.travis.yml
@ -14,13 +14,24 @@ env:
|
|||||||
6pxmyzLHSn1ZR7OX5rfPvwM3tOyZ3H0=
|
6pxmyzLHSn1ZR7OX5rfPvwM3tOyZ3H0=
|
||||||
matrix:
|
matrix:
|
||||||
- BUILD=Doc
|
- BUILD=Doc
|
||||||
- BUILD=Debug STANDARD=0x
|
- BUILD=Debug STANDARD=14
|
||||||
- BUILD=Release STANDARD=0x
|
- BUILD=Release STANDARD=14
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
exclude:
|
exclude:
|
||||||
- os: osx
|
- os: osx
|
||||||
env: BUILD=Doc
|
env: BUILD=Doc
|
||||||
|
|
||||||
|
# Install gcc-6 for extended constexpr support.
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
sources:
|
||||||
|
- ubuntu-toolchain-r-test
|
||||||
|
packages:
|
||||||
|
- g++-6
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- export CXX=g++-6
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- support/travis-build.py
|
- support/travis-build.py
|
||||||
|
49
fmt/format.h
49
fmt/format.h
@ -3100,9 +3100,15 @@ struct precision_handler {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct error_handler {
|
||||||
|
void on_error(const char *message) {
|
||||||
|
FMT_THROW(format_error(message));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// A format specifier handler that sets fields in basic_format_specs.
|
// A format specifier handler that sets fields in basic_format_specs.
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
class specs_setter {
|
class specs_setter : public error_handler {
|
||||||
public:
|
public:
|
||||||
explicit specs_setter(basic_format_specs<Char> &specs): specs_(specs) {}
|
explicit specs_setter(basic_format_specs<Char> &specs): specs_(specs) {}
|
||||||
|
|
||||||
@ -3316,12 +3322,6 @@ class dynamic_specs_handler :
|
|||||||
ParseContext &context_;
|
ParseContext &context_;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct error_handler {
|
|
||||||
void on_error(const char *message) {
|
|
||||||
FMT_THROW(format_error(message));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Iterator, typename Handler>
|
template <typename Iterator, typename Handler>
|
||||||
constexpr Iterator parse_arg_id(Iterator it, Handler& handler) {
|
constexpr Iterator parse_arg_id(Iterator it, Handler& handler) {
|
||||||
using char_type = typename std::iterator_traits<Iterator>::value_type;
|
using char_type = typename std::iterator_traits<Iterator>::value_type;
|
||||||
@ -3358,7 +3358,7 @@ constexpr Iterator parse_arg_id(Iterator it, Handler& handler) {
|
|||||||
// format specifiers.
|
// format specifiers.
|
||||||
template <typename Iterator, typename Handler>
|
template <typename Iterator, typename Handler>
|
||||||
Iterator parse_format_specs(Iterator it, Handler &handler) {
|
Iterator parse_format_specs(Iterator it, Handler &handler) {
|
||||||
using char_type = typename Iterator::value_type;
|
using char_type = typename std::iterator_traits<Iterator>::value_type;
|
||||||
// Parse fill and alignment.
|
// Parse fill and alignment.
|
||||||
if (char_type c = *it) {
|
if (char_type c = *it) {
|
||||||
auto p = it + 1;
|
auto p = it + 1;
|
||||||
@ -3382,8 +3382,10 @@ Iterator parse_format_specs(Iterator it, Handler &handler) {
|
|||||||
handler.on_align(align);
|
handler.on_align(align);
|
||||||
if (p != it) {
|
if (p != it) {
|
||||||
if (c == '}') break;
|
if (c == '}') break;
|
||||||
if (c == '{')
|
if (c == '{') {
|
||||||
FMT_THROW(format_error("invalid fill character '{'"));
|
handler.on_error("invalid fill character '{'");
|
||||||
|
return it;
|
||||||
|
}
|
||||||
it += 2;
|
it += 2;
|
||||||
handler.on_fill(c);
|
handler.on_fill(c);
|
||||||
} else ++it;
|
} else ++it;
|
||||||
@ -3423,7 +3425,7 @@ Iterator parse_format_specs(Iterator it, Handler &handler) {
|
|||||||
if ('0' <= *it && *it <= '9') {
|
if ('0' <= *it && *it <= '9') {
|
||||||
handler.on_width(parse_nonnegative_int(it));
|
handler.on_width(parse_nonnegative_int(it));
|
||||||
} else if (*it == '{') {
|
} else if (*it == '{') {
|
||||||
struct width_handler : error_handler {
|
struct width_handler {
|
||||||
explicit width_handler(Handler &h) : handler(h) {}
|
explicit width_handler(Handler &h) : handler(h) {}
|
||||||
|
|
||||||
void operator()() { handler.on_dynamic_width(auto_id()); }
|
void operator()() { handler.on_dynamic_width(auto_id()); }
|
||||||
@ -3432,11 +3434,17 @@ Iterator parse_format_specs(Iterator it, Handler &handler) {
|
|||||||
handler.on_dynamic_width(id);
|
handler.on_dynamic_width(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void on_error(const char *message) {
|
||||||
|
handler.on_error(message);
|
||||||
|
}
|
||||||
|
|
||||||
Handler &handler;
|
Handler &handler;
|
||||||
} wh(handler);
|
} wh(handler);
|
||||||
it = parse_arg_id(it + 1, wh);
|
it = parse_arg_id(it + 1, wh);
|
||||||
if (*it++ != '}')
|
if (*it++ != '}') {
|
||||||
FMT_THROW(format_error("invalid format string"));
|
handler.on_error("invalid format string");
|
||||||
|
return it;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse precision.
|
// Parse precision.
|
||||||
@ -3445,7 +3453,7 @@ Iterator parse_format_specs(Iterator it, Handler &handler) {
|
|||||||
if ('0' <= *it && *it <= '9') {
|
if ('0' <= *it && *it <= '9') {
|
||||||
handler.on_precision(parse_nonnegative_int(it));
|
handler.on_precision(parse_nonnegative_int(it));
|
||||||
} else if (*it == '{') {
|
} else if (*it == '{') {
|
||||||
struct precision_handler : error_handler {
|
struct precision_handler {
|
||||||
explicit precision_handler(Handler &h) : handler(h) {}
|
explicit precision_handler(Handler &h) : handler(h) {}
|
||||||
|
|
||||||
void operator()() { handler.on_dynamic_precision(auto_id()); }
|
void operator()() { handler.on_dynamic_precision(auto_id()); }
|
||||||
@ -3454,13 +3462,20 @@ Iterator parse_format_specs(Iterator it, Handler &handler) {
|
|||||||
handler.on_dynamic_precision(id);
|
handler.on_dynamic_precision(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void on_error(const char *message) {
|
||||||
|
handler.on_error(message);
|
||||||
|
}
|
||||||
|
|
||||||
Handler &handler;
|
Handler &handler;
|
||||||
} ph(handler);
|
} ph(handler);
|
||||||
it = parse_arg_id(it + 1, ph);
|
it = parse_arg_id(it + 1, ph);
|
||||||
if (*it++ != '}')
|
if (*it++ != '}') {
|
||||||
FMT_THROW(format_error("invalid format string"));
|
handler.on_error("invalid format string");
|
||||||
|
return it;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
FMT_THROW(format_error("missing precision specifier"));
|
handler.on_error("missing precision specifier");
|
||||||
|
return it;
|
||||||
}
|
}
|
||||||
handler.end_precision();
|
handler.end_precision();
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,7 @@ if (CMAKE_CXX_STANDARD)
|
|||||||
set(CPP14_FLAG )
|
set(CPP14_FLAG )
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
|
message(STATUS "CPP14_FLAG: ${CPP14_FLAG}")
|
||||||
set(CMAKE_REQUIRED_FLAGS ${CPP14_FLAG})
|
set(CMAKE_REQUIRED_FLAGS ${CPP14_FLAG})
|
||||||
|
|
||||||
# Check if variadic templates are working and not affected by GCC bug 39653:
|
# Check if variadic templates are working and not affected by GCC bug 39653:
|
||||||
|
@ -89,10 +89,8 @@ common_cmake_flags = [
|
|||||||
'-DCMAKE_INSTALL_PREFIX=' + install_dir, '-DCMAKE_BUILD_TYPE=' + build
|
'-DCMAKE_INSTALL_PREFIX=' + install_dir, '-DCMAKE_BUILD_TYPE=' + build
|
||||||
]
|
]
|
||||||
extra_cmake_flags = []
|
extra_cmake_flags = []
|
||||||
if standard != '0x':
|
if standard != '14':
|
||||||
extra_cmake_flags = [
|
extra_cmake_flags = ['-DCMAKE_CXX_FLAGS=-std=c++' + standard]
|
||||||
'-DCMAKE_CXX_FLAGS=-std=c++' + standard, '-DFMT_USE_CPP11=OFF'
|
|
||||||
]
|
|
||||||
check_call(['cmake', '-DFMT_DOC=OFF', '-DFMT_PEDANTIC=ON', fmt_dir] +
|
check_call(['cmake', '-DFMT_DOC=OFF', '-DFMT_PEDANTIC=ON', fmt_dir] +
|
||||||
common_cmake_flags + extra_cmake_flags, cwd=build_dir)
|
common_cmake_flags + extra_cmake_flags, cwd=build_dir)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user