...
 
Commits (24)
......@@ -83,8 +83,8 @@ matrix:
- g++-4.4
sources:
- ubuntu-toolchain-r-test
# Android
- language: android
- name: Android NDK (Gradle)
language: android
addons:
apt:
update: true
......@@ -92,23 +92,24 @@ matrix:
- ubuntu-toolchain-r-test
packages:
- ninja-build
- wget
- unzip
- curl
- tree
android:
components:
- tools
- platform-tools
- android-21
env:
- ANDROID=true
- android-25 # 7.0
- android-27 # 8.1
- android-28 # 9.0
- build-tools-28.0.3
before_install:
# Download/Install Gradle
- wget https://services.gradle.org/distributions/gradle-4.10.2-bin.zip
- mkdir -p gradle
- unzip -q -d ./gradle gradle-4.10.2-bin.zip
- export GRADLE=gradle/gradle-4.10.2/bin/gradle
- bash $GRADLE --version
# Install Gradle from https://sdkman.io/
- curl -s "https://get.sdkman.io" | bash > /dev/null
- source "$HOME/.sdkman/bin/sdkman-init.sh"
- sdk version
- sdk install gradle
- sdk use gradle
- gradle --version
install:
# Accept SDK Licenses + Install NDK
- yes | sdkmanager --update > /dev/null 2>&1
......@@ -116,7 +117,8 @@ matrix:
before_script:
- pushd ./support
script:
- bash ../$GRADLE clean assemble
- gradle clean
- gradle assemble
after_success:
- popd;
- tree ./libs
......
......@@ -352,12 +352,12 @@ further details see the `source
================= ============= ===========
Library Method Run Time, s
================= ============= ===========
libc printf 1.35
libc++ std::ostream 3.42
fmt 534bff7 fmt::print 1.56
tinyformat 2.0.1 tfm::printf 3.73
Boost Format 1.54 boost::format 8.44
Folly Format folly::format 2.54
libc printf 1.01
libc++ std::ostream 2.98
fmt 31510cb fmt::print 1.13
tinyformat 2.0.1 tfm::printf 3.23
Boost Format 1.54 boost::format 7.34
Folly Format folly::format 2.23
================= ============= ===========
As you can see ``boost::format`` is much slower than the alternative methods; this
......
......@@ -249,10 +249,11 @@ template <typename Int> inline int to_int(Int value) {
template <typename Rep, typename OutputIt>
OutputIt static format_chrono_duration_value(OutputIt out, Rep val,
int precision) {
if (precision < 0)
return format_to(out, "{}", val);
else
return format_to(out, "{:.{}f}", val, precision);
if (precision < 0) {
return format_to(out, std::is_floating_point<Rep>::value ? "{:g}" : "{}",
val);
}
return format_to(out, "{:.{}f}", val, precision);
}
template <typename Period, typename OutputIt>
......@@ -480,10 +481,10 @@ struct formatter<std::chrono::duration<Rep, Period>, Char> {
auto out = std::back_inserter(buf);
typedef output_range<decltype(ctx.out()), Char> range;
basic_writer<range> w(range(ctx.out()));
internal::handle_dynamic_spec<internal::width_checker>(spec.width_,
width_ref, ctx);
internal::handle_dynamic_spec<internal::width_checker>(
spec.width_, width_ref, ctx, format_str.begin());
internal::handle_dynamic_spec<internal::precision_checker>(
precision, precision_ref, ctx);
precision, precision_ref, ctx, format_str.begin());
if (begin == end || *begin == '}') {
out = internal::format_chrono_duration_value(out, d.count(), precision);
internal::format_chrono_duration_unit<Period>(out);
......
......@@ -15,21 +15,33 @@ FMT_BEGIN_NAMESPACE
#ifdef FMT_DEPRECATED_COLORS
// color and (v)print_colored are deprecated.
enum color { black, red, green, yellow, blue, magenta, cyan, white };
FMT_API void vprint_colored(color c, string_view format, format_args args);
FMT_API void vprint_colored(color c, wstring_view format, wformat_args args);
enum FMT_DEPRECATED color {
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white
};
FMT_DEPRECATED FMT_API void vprint_colored(color c, string_view format,
format_args args);
FMT_DEPRECATED FMT_API void vprint_colored(color c, wstring_view format,
wformat_args args);
template <typename... Args>
inline void print_colored(color c, string_view format_str,
const Args&... args) {
FMT_DEPRECATED inline void print_colored(color c, string_view format_str,
const Args&... args) {
vprint_colored(c, format_str, make_format_args(args...));
}
template <typename... Args>
inline void print_colored(color c, wstring_view format_str,
const Args&... args) {
FMT_DEPRECATED inline void print_colored(color c, wstring_view format_str,
const Args&... args) {
vprint_colored(c, format_str, make_format_args<wformat_context>(args...));
}
inline void vprint_colored(color c, string_view format, format_args args) {
FMT_DEPRECATED inline void vprint_colored(color c, string_view format,
format_args args) {
char escape[] = "\x1b[30m";
escape[3] = static_cast<char>('0' + c);
std::fputs(escape, stdout);
......@@ -37,7 +49,8 @@ inline void vprint_colored(color c, string_view format, format_args args) {
std::fputs(internal::data::RESET_COLOR, stdout);
}
inline void vprint_colored(color c, wstring_view format, wformat_args args) {
FMT_DEPRECATED inline void vprint_colored(color c, wstring_view format,
wformat_args args) {
wchar_t escape[] = L"\x1b[30m";
escape[3] = static_cast<wchar_t>('0' + c);
std::fputws(escape, stdout);
......@@ -621,9 +634,8 @@ template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(const text_style& ts,
const S& format_str,
const Args&... args) {
return internal::vformat(
ts, to_string_view(format_str),
*internal::checked_args<S, Args...>(format_str, args...));
return internal::vformat(ts, to_string_view(format_str),
{internal::make_args_checked(format_str, args...)});
}
#endif
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -45,9 +45,8 @@ template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(const std::locale& loc,
const S& format_str,
const Args&... args) {
return internal::vformat(
loc, to_string_view(format_str),
*internal::checked_args<S, Args...>(format_str, args...));
return internal::vformat(loc, to_string_view(format_str),
{internal::make_args_checked(format_str, args...)});
}
template <typename String, typename OutputIt, typename... Args>
......
......@@ -56,8 +56,8 @@ template <typename Char> struct test_stream : std::basic_ostream<Char> {
template <typename T, typename Char> class is_streamable {
private:
template <typename U>
static decltype(internal::declval<test_stream<Char>&>()
<< internal::declval<U>(),
static decltype((void)(internal::declval<test_stream<Char>&>()
<< internal::declval<U>()),
std::true_type())
test(int);
......@@ -138,8 +138,8 @@ template <typename S, typename... Args>
inline typename std::enable_if<internal::is_string<S>::value>::type print(
std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
const Args&... args) {
internal::checked_args<S, Args...> ca(format_str, args...);
vprint(os, to_string_view(format_str), *ca);
vprint(os, to_string_view(format_str),
{internal::make_args_checked(format_str, args...)});
}
FMT_END_NAMESPACE
......
......@@ -187,7 +187,6 @@ class prepared_format {
public:
typedef FMT_CHAR(Format) char_type;
typedef format_part<char_type> format_part_t;
typedef internal::checked_args<Format, Args...> checked_args;
prepared_format(Format f)
: format_(std::move(f)), parts_provider_(to_string_view(format_)) {}
......@@ -218,7 +217,7 @@ class prepared_format {
std::basic_string<char_type> format(const Args&... args) const {
basic_memory_buffer<char_type> buffer;
typedef back_insert_range<internal::basic_buffer<char_type>> range;
this->vformat_to(range(buffer), *checked_args(format_, args...));
this->vformat_to(range(buffer), make_args_checked(format_, args...));
return to_string(buffer);
}
......@@ -229,7 +228,7 @@ class prepared_format {
const Args&... args) const {
internal::container_buffer<Container> buffer(internal::get_container(out));
typedef back_insert_range<internal::basic_buffer<char_type>> range;
this->vformat_to(range(buffer), *checked_args(format_, args...));
this->vformat_to(range(buffer), make_args_checked(format_, args...));
return out;
}
......@@ -245,15 +244,18 @@ class prepared_format {
inline typename buffer_context<char_type>::type::iterator format_to(
basic_memory_buffer<char_type, SIZE>& buf, const Args&... args) const {
typedef back_insert_range<internal::basic_buffer<char_type>> range;
return this->vformat_to(range(buf), *checked_args(format_, args...));
return this->vformat_to(range(buf), make_args_checked(format_, args...));
}
private:
template <typename Range, typename Context>
typename Context::iterator vformat_to(Range out,
basic_format_args<Context> args) const {
typedef typename buffer_context<char_type>::type context;
template <typename Range>
typename context::iterator vformat_to(Range out,
basic_format_args<context> args) const {
const auto format_view = internal::to_string_view(format_);
Context ctx(out.begin(), format_view, args);
basic_parse_context<char_type> parse_ctx(format_view);
context ctx(out.begin(), args);
const auto& parts = parts_provider_.parts();
for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) {
......@@ -270,34 +272,34 @@ class prepared_format {
} break;
case format_part_t::which_value::argument_id: {
advance_parse_context_to_specification(ctx, part);
format_arg<Range>(ctx, value.arg_id);
advance_parse_context_to_specification(parse_ctx, part);
format_arg<Range>(parse_ctx, ctx, value.arg_id);
} break;
case format_part_t::which_value::named_argument_id: {
advance_parse_context_to_specification(ctx, part);
advance_parse_context_to_specification(parse_ctx, part);
const auto named_arg_id = value.named_arg_id.to_view(format_view);
format_arg<Range>(ctx, named_arg_id);
format_arg<Range>(parse_ctx, ctx, named_arg_id);
} break;
case format_part_t::which_value::specification: {
const auto& arg_id_value = value.spec.arg_id.val;
const auto arg =
value.spec.arg_id.which ==
format_part_t::argument_id::which_arg_id::index
? ctx.get_arg(arg_id_value.index)
: ctx.get_arg(arg_id_value.named_index.to_view(format_));
? ctx.arg(arg_id_value.index)
: ctx.arg(arg_id_value.named_index.to_view(format_));
auto specs = value.spec.parsed_specs;
handle_dynamic_spec<internal::width_checker>(specs.width_,
specs.width_ref, ctx);
handle_dynamic_spec<internal::width_checker>(
specs.width_, specs.width_ref, ctx, format_view.begin());
handle_dynamic_spec<internal::precision_checker>(
specs.precision, specs.precision_ref, ctx);
specs.precision, specs.precision_ref, ctx, format_view.begin());
check_prepared_specs(specs, arg.type());
advance_parse_context_to_specification(ctx, part);
advance_parse_context_to_specification(parse_ctx, part);
ctx.advance_to(
visit_format_arg(arg_formatter<Range>(ctx, &specs), arg));
visit_format_arg(arg_formatter<Range>(ctx, FMT_NULL, &specs), arg));
} break;
}
}
......@@ -305,19 +307,20 @@ class prepared_format {
return ctx.out();
}
template <typename Context>
void advance_parse_context_to_specification(Context& ctx,
const format_part_t& part) const {
void advance_parse_context_to_specification(
basic_parse_context<char_type>& parse_ctx,
const format_part_t& part) const {
const auto view = to_string_view(format_);
const auto specification_begin = view.data() + part.end_of_argument_id;
ctx.parse_context().advance_to(specification_begin);
parse_ctx.advance_to(specification_begin);
}
template <typename Range, typename Context, typename Id>
void format_arg(Context& ctx, Id arg_id) const {
ctx.parse_context().check_arg_id(arg_id);
void format_arg(basic_parse_context<char_type>& parse_ctx, Context& ctx,
Id arg_id) const {
parse_ctx.check_arg_id(arg_id);
const auto stopped_at =
visit_format_arg(arg_formatter<Range>(ctx), ctx.get_arg(arg_id));
visit_format_arg(arg_formatter<Range>(ctx), ctx.arg(arg_id));
ctx.advance_to(stopped_at);
}
......@@ -383,7 +386,6 @@ template <typename Format> class compiletime_prepared_parts_type_provider {
private:
FMT_CONSTEXPR const char_type* find_matching_brace(const char_type* begin,
const char_type* end) {
FMT_CONSTEXPR_DECL const basic_string_view<char_type> text = Format{};
unsigned braces_counter{0u};
for (; begin != end; ++begin) {
if (*begin == '{') {
......@@ -538,14 +540,15 @@ struct parts_container_concept_check : std::true_type {
template <typename T> static std::false_type has_add_check(check_second);
template <typename T>
static decltype(declval<T>().add(declval<typename T::format_part_type>()),
std::true_type()) has_add_check(check_first);
static decltype(
(void)declval<T>().add(declval<typename T::format_part_type>()),
std::true_type()) has_add_check(check_first);
typedef decltype(has_add_check<PartsContainer>(check_first())) has_add;
static_assert(has_add::value, "PartsContainer doesn't provide add() method");
template <typename T> static std::false_type has_last_check(check_second);
template <typename T>
static decltype(declval<T>().last(),
static decltype((void)declval<T>().last(),
std::true_type()) has_last_check(check_first);
typedef decltype(has_last_check<PartsContainer>(check_first())) has_last;
static_assert(has_last::value,
......@@ -554,9 +557,9 @@ struct parts_container_concept_check : std::true_type {
template <typename T>
static std::false_type has_substitute_last_check(check_second);
template <typename T>
static decltype(
declval<T>().substitute_last(declval<typename T::format_part_type>()),
std::true_type()) has_substitute_last_check(check_first);
static decltype((void)declval<T>().substitute_last(
declval<typename T::format_part_type>()),
std::true_type()) has_substitute_last_check(check_first);
typedef decltype(has_substitute_last_check<PartsContainer>(
check_first())) has_substitute_last;
static_assert(has_substitute_last::value,
......@@ -564,7 +567,7 @@ struct parts_container_concept_check : std::true_type {
template <typename T> static std::false_type has_begin_check(check_second);
template <typename T>
static decltype(declval<T>().begin(),
static decltype((void)declval<T>().begin(),
std::true_type()) has_begin_check(check_first);
typedef decltype(has_begin_check<PartsContainer>(check_first())) has_begin;
static_assert(has_begin::value,
......@@ -572,7 +575,7 @@ struct parts_container_concept_check : std::true_type {
template <typename T> static std::false_type has_end_check(check_second);
template <typename T>
static decltype(declval<T>().end(),
static decltype((void)declval<T>().end(),
std::true_type()) has_end_check(check_first);
typedef decltype(has_end_check<PartsContainer>(check_first())) has_end;
static_assert(has_end::value, "PartsContainer doesn't provide end() method");
......
......@@ -250,7 +250,7 @@ class printf_arg_formatter
\endrst
*/
printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
: base(Range(iter), &spec, ctx.locale()), context_(ctx) {}
: base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
template <typename T>
typename std::enable_if<std::is_integral<T>::value, iterator>::type
......@@ -319,7 +319,7 @@ class printf_arg_formatter
/** Formats an argument of a custom (user-defined) type. */
iterator operator()(typename basic_format_arg<context_type>::handle handle) {
handle.format(context_);
handle.format(context_.parse_context(), context_);
return this->out();
}
};
......@@ -339,24 +339,23 @@ template <typename T> struct printf_formatter {
/** This template formats data and writes the output to a writer. */
template <typename OutputIt, typename Char, typename ArgFormatter>
class basic_printf_context :
// Inherit publicly as a workaround for the icc bug
// https://software.intel.com/en-us/forums/intel-c-compiler/topic/783476.
public internal::context_base<
OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
class basic_printf_context {
public:
/** The character type for the output. */
typedef Char char_type;
typedef basic_format_arg<basic_printf_context> format_arg;
template <typename T> struct formatter_type {
typedef printf_formatter<T> type;
};
private:
typedef internal::context_base<OutputIt, basic_printf_context, Char> base;
typedef typename base::format_arg format_arg;
typedef basic_format_specs<char_type> format_specs;
OutputIt out_;
basic_format_args<basic_printf_context> args_;
basic_parse_context<Char> parse_ctx_;
static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
// Returns the argument with specified index or, if arg_index is equal
......@@ -376,11 +375,18 @@ class basic_printf_context :
*/
basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
basic_format_args<basic_printf_context> args)
: base(out, format_str, args) {}
: out_(out), args_(args), parse_ctx_(format_str) {}
OutputIt out() { return out_; }
void advance_to(OutputIt it) { out_ = it; }
format_arg arg(unsigned id) const { return args_.get(id); }
using base::advance_to;
using base::out;
using base::parse_context;
basic_parse_context<Char>& parse_context() { return parse_ctx_; }
FMT_CONSTEXPR void on_error(const char* message) {
parse_ctx_.on_error(message);
}
/** Formats stored arguments and writes the output to the range. */
OutputIt format();
......@@ -417,8 +423,10 @@ template <typename OutputIt, typename Char, typename AF>
typename basic_printf_context<OutputIt, Char, AF>::format_arg
basic_printf_context<OutputIt, Char, AF>::get_arg(unsigned arg_index) {
if (arg_index == std::numeric_limits<unsigned>::max())
return this->do_get_arg(this->parse_context().next_arg_id());
return base::arg(arg_index - 1);
arg_index = parse_ctx_.next_arg_id();
else
parse_ctx_.check_arg_id(--arg_index);
return internal::get_arg(*this, arg_index);
}
template <typename OutputIt, typename Char, typename AF>
......@@ -462,9 +470,8 @@ unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
template <typename OutputIt, typename Char, typename AF>
OutputIt basic_printf_context<OutputIt, Char, AF>::format() {
auto out = this->out();
const auto range = this->parse_context();
const Char* const end = range.end();
const Char* start = range.begin();
const Char* start = parse_ctx_.begin();
const Char* end = parse_ctx_.end();
auto it = start;
while (it != end) {
char_type c = *it++;
......
......@@ -76,7 +76,7 @@ template <typename OutputIterator> void copy(char ch, OutputIterator out) {
template <typename T> class is_like_std_string {
template <typename U>
static auto check(U* p)
-> decltype(p->find('a'), p->length(), p->data(), int());
-> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
template <typename> static void check(...);
public:
......@@ -104,10 +104,10 @@ struct is_range_<
/// tuple_size and tuple_element check.
template <typename T> class is_tuple_like_ {
template <typename U>
static auto check(U* p)
-> decltype(std::tuple_size<U>::value,
internal::declval<typename std::tuple_element<0, U>::type>(),
int());
static auto check(U* p) -> decltype(
std::tuple_size<U>::value,
(void)internal::declval<typename std::tuple_element<0, U>::type>(),
int());
template <typename> static void check(...);
public:
......
......@@ -191,9 +191,8 @@ constexpr void basic_format_parse_context<charT>::check_arg_id(size_t) {
namespace std {
template<class O, class charT> FMT_REQUIRES(OutputIterator<O, const charT&>)
class basic_format_context {
basic_format_parse_context<charT> parse_context_; // exposition only
basic_format_args<basic_format_context> args_; // exposition only
O out_; // exposition only
basic_format_args<basic_format_context> args_; // exposition only
O out_; // exposition only
public:
using iterator = O;
......@@ -202,7 +201,6 @@ namespace std {
template<class T>
using formatter_type = formatter<T>;
basic_format_parse_context<charT>& parse_context() noexcept;
basic_format_arg<basic_format_context> arg(size_t id) const;
iterator out();
......@@ -210,11 +208,8 @@ namespace std {
// Implementation details:
using format_arg = basic_format_arg<basic_format_context>;
basic_format_context(O out, fmt::string_view fmt, basic_format_args<basic_format_context> args, fmt::internal::locale_ref)
: out_(out), parse_context_({fmt.data(), fmt.size()}), args_(args) {}
basic_format_arg<basic_format_context> next_arg() {
return arg(parse_context_.next_arg_id());
}
basic_format_context(O out, basic_format_args<basic_format_context> args, fmt::internal::locale_ref)
: out_(out), args_(args) {}
fmt::internal::error_handler error_handler() const { return {}; }
basic_format_arg<basic_format_context> arg(fmt::basic_string_view<charT>) const {
return {}; // unused: named arguments are not supported yet
......@@ -224,9 +219,6 @@ namespace std {
}
namespace std {
template<class O, class charT>
basic_format_parse_context<charT>& basic_format_context<O, charT>::parse_context() noexcept { return parse_context_; }
template<class O, class charT>
basic_format_arg<basic_format_context<O, charT>> basic_format_context<O, charT>::arg(size_t id) const { return args_.get(id); }
......@@ -273,7 +265,7 @@ namespace std {
explicit basic_format_arg(const T* p) noexcept; // exposition only
template<class T, typename = std::enable_if_t<
!Integral<T> && is_default_constructible_v<formatter<T, char_type>>>>
!Integral<T> && is_default_constructible_v<typename Context::template formatter_type<T>>>>
explicit basic_format_arg(const T& v) noexcept; // exposition only
explicit operator bool() const noexcept;
......@@ -360,33 +352,39 @@ template<class Context>
template<class T, typename>
/* explicit */ basic_format_arg<Context>::basic_format_arg(const T& v) noexcept
: value(handle(v)) {}
template<class Context>
/* explicit */ basic_format_arg<Context>::operator bool() const noexcept {
return !holds_alternative<monostate>(value);
}
}
namespace std {
template<class Context>
class basic_format_arg<Context>::handle {
const void* ptr_; // exposition only
void (*format_)(Context&, const void*); // exposition only
void (*format_)(basic_format_parse_context<char_type>&,
Context&, const void*); // exposition only
public:
template<class T> explicit handle(const T& val) noexcept; // exposition only
public:
template<class T> explicit handle(const T& val) noexcept; // exposition only
void format(Context& ctx) const;
void format(basic_format_parse_context<char_type>&, Context& ctx) const;
};
}
namespace std {
template<class Context>
template<class T> /* explicit */ basic_format_arg<Context>::handle::handle(const T& val) noexcept
: ptr_(&val), format_([](Context& ctx, const void* ptr) {
typename Context::template formatter_type<T> f;
ctx.parse_context().advance_to(f.parse(ctx.parse_context()));
ctx.advance_to(f.format(*static_cast<const T*>(ptr), ctx));
: ptr_(&val), format_([](basic_format_parse_context<char_type>& parse_ctx, Context& format_ctx, const void* ptr) {
typename Context::template formatter_type<T> f;
parse_ctx.advance_to(f.parse(parse_ctx));
format_ctx.advance_to(f.format(*static_cast<const T*>(ptr), format_ctx));
}) {}
template<class Context>
void basic_format_arg<Context>::handle::format(Context& ctx) const {
format_(ctx, ptr_);
void basic_format_arg<Context>::handle::format(basic_format_parse_context<char_type>& parse_ctx, Context& format_ctx) const {
format_(parse_ctx, format_ctx, ptr_);
}
// http://fmtlib.net/Text%20Formatting.html#format.visit
......@@ -460,11 +458,13 @@ class arg_formatter
typename fmt::internal::arg_formatter_base<Range>::iterator>,
public fmt::internal::arg_formatter_base<Range> {
private:
typedef typename Range::value_type char_type;
typedef fmt::internal::arg_formatter_base<Range> base;
typedef std::basic_format_context<typename base::iterator, char_type> context_type;
using char_type = typename Range::value_type;
using base = fmt::internal::arg_formatter_base<Range>;
using format_context = std::basic_format_context<typename base::iterator, char_type>;
using parse_context = basic_format_parse_context<char_type>;
context_type& ctx_;
parse_context& parse_ctx_;
format_context& ctx_;
public:
typedef Range range;
......@@ -478,14 +478,14 @@ class arg_formatter
*spec* contains format specifier information for standard argument types.
\endrst
*/
explicit arg_formatter(context_type& ctx, fmt::format_specs* spec = FMT_NULL)
: base(Range(ctx.out()), spec, {}), ctx_(ctx) {}
arg_formatter(parse_context& parse_ctx, format_context& ctx, fmt::format_specs* spec = FMT_NULL)
: base(Range(ctx.out()), spec, {}), parse_ctx_(parse_ctx), ctx_(ctx) {}
using base::operator();
/** Formats an argument of a user-defined type. */
iterator operator()(typename std::basic_format_arg<context_type>::handle handle) {
handle.format(ctx_);
iterator operator()(typename std::basic_format_arg<format_context>::handle handle) {
handle.format(parse_ctx_, ctx_);
return this->out();
}
......@@ -528,16 +528,18 @@ inline fmt::internal::type get_type(basic_format_arg<Context> arg) {
}, arg);
}
template <typename Char, typename Context>
template <typename Context>
class custom_formatter {
private:
Context& ctx_;
using parse_context = basic_format_parse_context<typename Context::char_type>;
parse_context& parse_ctx_;
Context& format_ctx_;
public:
explicit custom_formatter(Context& ctx) : ctx_(ctx) {}
custom_formatter(parse_context& parse_ctx, Context& ctx) : parse_ctx_(parse_ctx), format_ctx_(ctx) {}
bool operator()(typename basic_format_arg<Context>::handle h) const {
h.format(ctx_);
h.format(parse_ctx_, format_ctx_);
return true;
}
......@@ -551,7 +553,7 @@ struct format_handler : fmt::internal::error_handler {
format_handler(range r, basic_string_view<Char> str,
basic_format_args<Context> format_args,
fmt::internal::locale_ref loc)
: context(r.begin(), str, format_args, loc) {}
: parse_ctx(str), context(r.begin(), format_args, loc) {}
void on_text(const Char* begin, const Char* end) {
auto size = fmt::internal::to_unsigned(end - begin);
......@@ -561,36 +563,39 @@ struct format_handler : fmt::internal::error_handler {
context.advance_to(out);
}
void on_arg_id() { arg = context.next_arg(); }
void on_arg_id() {
arg = context.arg(parse_ctx.next_arg_id());
}
void on_arg_id(unsigned id) {
context.parse_context().check_arg_id(id);
parse_ctx.check_arg_id(id);
arg = context.arg(id);
}
void on_arg_id(fmt::basic_string_view<Char> id) {}
void on_replacement_field(const Char* p) {
context.parse_context().advance_to(p);
custom_formatter<Char, Context> f(context);
parse_ctx.advance_to(p);
custom_formatter<Context> f(parse_ctx, context);
if (!visit_format_arg(f, arg))
context.advance_to(visit_format_arg(ArgFormatter(context), arg));
context.advance_to(visit_format_arg(ArgFormatter(parse_ctx, context), arg));
}
const Char* on_format_specs(const Char* begin, const Char* end) {
auto& parse_ctx = context.parse_context();
parse_ctx.advance_to(begin);
custom_formatter<Char, Context> f(context);
custom_formatter<Context> f(parse_ctx, context);
if (visit_format_arg(f, arg)) return parse_ctx.begin();
fmt::basic_format_specs<Char> specs;
using fmt::internal::specs_handler;
fmt::internal::specs_checker<specs_handler<Context>> handler(
specs_handler<Context>(specs, context), get_type(arg));
using parse_context = basic_format_parse_context<Char>;
fmt::internal::specs_checker<specs_handler<parse_context, Context>> handler(
specs_handler<parse_context, Context>(specs, parse_ctx, context), get_type(arg));
begin = parse_format_specs(begin, end, handler);
if (begin == end || *begin != '}') on_error("missing '}' in format string");
parse_ctx.advance_to(begin);
context.advance_to(visit_format_arg(ArgFormatter(context, &specs), arg));
context.advance_to(visit_format_arg(ArgFormatter(parse_ctx, context, &specs), arg));
return begin;
}
basic_format_parse_context<Char> parse_ctx;
Context context;
basic_format_arg<Context> arg;
};
......@@ -704,7 +709,7 @@ template<FMT_CONCEPT(OutputIterator<const wchar_t&>) O, class... Args>
template<FMT_CONCEPT(OutputIterator<const char&>) O>
O vformat_to(O out, string_view fmt, format_args_t<O, char> args) {
typedef fmt::output_range<O, char> range;
detail::format_handler<detail::arg_formatter<range>, char, format_context>
detail::format_handler<detail::arg_formatter<range>, char, basic_format_context<O, char>>
h(range(out), fmt, args, {});
fmt::internal::parse_format_string<false>(fmt::to_string_view(fmt), h);
return h.context.out();
......
......@@ -11,8 +11,8 @@ FMT_BEGIN_NAMESPACE
template struct internal::basic_data<void>;
// Workaround a bug in MSVC2013 that prevents instantiation of grisu2_format.
bool (*instantiate_grisu2_format)(double, internal::buffer&,
core_format_specs) = internal::grisu2_format;
bool (*instantiate_grisu2_format)(double, internal::buffer&, core_format_specs,
int&) = internal::grisu2_format;
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
template FMT_API internal::locale_ref::locale_ref(const std::locale& loc);
......
......@@ -9,10 +9,12 @@ buildscript {
//
// https://developer.android.com/studio/releases/gradle-plugin
//
// Notice that 3.1.3 here is the version of [Android Gradle Plugin]
// Accroding to URL above you will need Gradle 4.4 or higher
// Notice that 3.3.0 here is the version of [Android Gradle Plugin]
// Accroding to URL above you will need Gradle 5.0 or higher
//
classpath 'com.android.tools.build:gradle:3.1.3'
// If you are using Android Studio, and it is using Gradle's lower
// version, Use the plugin version 3.1.3 ~ 3.2.0 for Gradle 4.4 ~ 4.10
classpath 'com.android.tools.build:gradle:3.3.0'
}
}
repositories {
......@@ -43,8 +45,8 @@ android {
defaultConfig {
minSdkVersion 21 // Android 5.0+
targetSdkVersion 25 // Follow Compile SDK
versionCode 20 // Follow release count
versionName "5.2.1" // Follow Official version
versionCode 21 // Follow release count
versionName "5.3.0" // Follow Official version
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
......
......@@ -88,6 +88,7 @@ add_fmt_test(assert-test)
add_fmt_test(chrono-test)
add_fmt_test(core-test)
add_fmt_test(grisu-test)
target_compile_definitions(grisu-test PRIVATE FMT_USE_GRISU=1)
add_fmt_test(gtest-extra-test)
add_fmt_test(format-test mock-allocator.h)
if (NOT (MSVC AND BUILD_SHARED_LIBS))
......
......@@ -230,8 +230,8 @@ TEST(ChronoTest, FormatSimpleQq) {
TEST(ChronoTest, FormatPrecisionQq) {
EXPECT_THROW_MSG(fmt::format("{:.2%Q %q}", std::chrono::seconds(42)),
fmt::format_error,
"precision not allowed for this argument type");
fmt::format_error,
"precision not allowed for this argument type");
EXPECT_EQ("1.2 ms", fmt::format("{:.1%Q %q}", dms(1.234)));
EXPECT_EQ("1.23 ms", fmt::format("{:.{}%Q %q}", dms(1.234), 2));
}
......
......@@ -223,10 +223,9 @@ struct custom_context {
};
bool called;
fmt::format_parse_context ctx;
fmt::format_parse_context parse_context() {
return fmt::format_parse_context("");
}
fmt::format_parse_context& parse_context() { return ctx; }
void advance_to(const char*) {}
};
......@@ -234,8 +233,8 @@ TEST(ArgTest, MakeValueWithCustomContext) {
test_struct t;
fmt::internal::value<custom_context> arg =
fmt::internal::make_value<custom_context>(t);
custom_context ctx = {false};
arg.custom.format(&t, ctx);
custom_context ctx = {false, fmt::format_parse_context("")};
arg.custom.format(&t, ctx.parse_context(), ctx);
EXPECT_TRUE(ctx.called);
}
......@@ -290,13 +289,13 @@ VISIT_TYPE(unsigned long, unsigned long long);
VISIT_TYPE(float, double);
#define CHECK_ARG_(Char, expected, value) \
{ \
testing::StrictMock<mock_visitor<decltype(expected)>> visitor; \
EXPECT_CALL(visitor, visit(expected)); \
typedef std::back_insert_iterator<basic_buffer<Char>> iterator; \
fmt::visit(visitor, \
make_arg<fmt::basic_format_context<iterator, Char>>(value)); \
#define CHECK_ARG_(Char, expected, value) \
{ \
testing::StrictMock<mock_visitor<decltype(expected)>> visitor; \
EXPECT_CALL(visitor, visit(expected)); \
typedef std::back_insert_iterator<basic_buffer<Char>> iterator; \
fmt::visit_format_arg( \
visitor, make_arg<fmt::basic_format_context<iterator, Char>>(value)); \
}
#define CHECK_ARG(value, typename_) \
......@@ -378,8 +377,9 @@ struct check_custom {
void grow(std::size_t) {}
} buffer;
fmt::internal::basic_buffer<char>& base = buffer;
fmt::format_context ctx(std::back_inserter(base), "", fmt::format_args());
h.format(ctx);
fmt::format_parse_context parse_ctx("");
fmt::format_context ctx(std::back_inserter(base), fmt::format_args());
h.format(parse_ctx, ctx);
EXPECT_EQ("test", std::string(buffer.data, buffer.size()));
return test_result();
}
......@@ -391,14 +391,14 @@ TEST(ArgTest, CustomArg) {
visitor;
testing::StrictMock<visitor> v;
EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke(check_custom()));
fmt::visit(v, make_arg<fmt::format_context>(test));
fmt::visit_format_arg(v, make_arg<fmt::format_context>(test));
}
TEST(ArgTest, VisitInvalidArg) {
testing::StrictMock<mock_visitor<fmt::monostate>> visitor;
EXPECT_CALL(visitor, visit(_));
fmt::basic_format_arg<fmt::format_context> arg;
visit(visitor, arg);
fmt::visit_format_arg(visitor, arg);
}
TEST(StringViewTest, Length) {
......
......@@ -20,8 +20,9 @@ class custom_arg_formatter
typedef fmt::arg_formatter<range> base;
custom_arg_formatter(fmt::format_context& ctx,
fmt::format_parse_context* parse_ctx,
fmt::format_specs* s = FMT_NULL)
: base(ctx, s) {}
: base(ctx, parse_ctx, s) {}
using base::operator();
......
......@@ -102,7 +102,8 @@ TEST(FPTest, GetCachedPower) {
TEST(FPTest, Grisu2FormatCompilesWithNonIEEEDouble) {
fmt::memory_buffer buf;
grisu2_format(4.2f, buf, fmt::core_format_specs());
int exp = 0;
grisu2_format(4.2f, buf, fmt::core_format_specs(), exp);
}
template <typename T> struct ValueExtractor : fmt::internal::function<T> {
......@@ -116,9 +117,10 @@ template <typename T> struct ValueExtractor : fmt::internal::function<T> {
TEST(FormatTest, ArgConverter) {
long long value = std::numeric_limits<long long>::max();
auto arg = fmt::internal::make_arg<fmt::format_context>(value);
visit(fmt::internal::arg_converter<long long, fmt::format_context>(arg, 'd'),
arg);
EXPECT_EQ(value, visit(ValueExtractor<long long>(), arg));
fmt::visit_format_arg(
fmt::internal::arg_converter<long long, fmt::format_context>(arg, 'd'),
arg);
EXPECT_EQ(value, fmt::visit_format_arg(ValueExtractor<long long>(), arg));
}
TEST(FormatTest, FormatNegativeNaN) {
......
This diff is collapsed.
......@@ -5,11 +5,10 @@
//
// For the license information refer to format.h.
#define FMT_USE_GRISU std::numeric_limits<double>::is_iec559
#include "fmt/format.h"
#include "gtest.h"
bool reported_skipped;
static bool reported_skipped;
#undef TEST
#define TEST(test_fixture, test_name) \
......@@ -36,6 +35,20 @@ TEST(GrisuTest, Inf) {
EXPECT_EQ("-inf", fmt::format("{}", -inf));
}
TEST(GrisuTest, Zero) {
EXPECT_EQ("0", fmt::format("{}", 0.0));
TEST(GrisuTest, Zero) { EXPECT_EQ("0.0", fmt::format("{}", 0.0)); }
TEST(GrisuTest, Round) {
EXPECT_EQ("1.9156918820264798e-56",
fmt::format("{}", 1.9156918820264798e-56));
}
TEST(GrisuTest, Prettify) {
EXPECT_EQ("0.0001", fmt::format("{}", 1e-4));
EXPECT_EQ("1e-5", fmt::format("{}", 1e-5));
EXPECT_EQ("9.999e-5", fmt::format("{}", 9.999e-5));
EXPECT_EQ("10000000000.0", fmt::format("{}", 1e10));
EXPECT_EQ("1e+11", fmt::format("{}", 1e11));
EXPECT_EQ("12340000000.0", fmt::format("{}", 1234e7));
EXPECT_EQ("12.34", fmt::format("{}", 1234e-2));
EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6));
}
......@@ -26,6 +26,11 @@ static std::wostream& operator<<(std::wostream& os, const Date& d) {
return os;
}
// Make sure that overloaded comma operators do no harm to is_streamable.
struct type_with_comma_op {};
template <typename T> void operator,(type_with_comma_op, const T&);
template <typename T> type_with_comma_op operator<<(T&, const Date&);
enum TestEnum {};
static std::ostream& operator<<(std::ostream& os, TestEnum) {
return os << "TestEnum";
......@@ -49,17 +54,19 @@ TEST(OStreamTest, Enum) {
typedef fmt::back_insert_range<fmt::internal::buffer> range;
struct test_arg_formatter : fmt::arg_formatter<range> {
fmt::format_parse_context parse_ctx;
test_arg_formatter(fmt::format_context& ctx, fmt::format_specs& s)
: fmt::arg_formatter<range>(ctx, &s) {}
: fmt::arg_formatter<range>(ctx, &parse_ctx, &s), parse_ctx("") {}
};
TEST(OStreamTest, CustomArg) {
fmt::memory_buffer buffer;
fmt::internal::buffer& base = buffer;
fmt::format_context ctx(std::back_inserter(base), "", fmt::format_args());
fmt::format_context ctx(std::back_inserter(base), fmt::format_args());
fmt::format_specs spec;
test_arg_formatter af(ctx, spec);
visit(af, fmt::internal::make_arg<fmt::format_context>(TestEnum()));
fmt::visit_format_arg(
af, fmt::internal::make_arg<fmt::format_context>(TestEnum()));
EXPECT_EQ("TestEnum", std::string(buffer.data(), buffer.size()));
}
......@@ -153,7 +160,7 @@ TEST(OStreamTest, WriteToOStreamMaxSize) {
EXPECT_CALL(streambuf, xsputn(data, static_cast<std::streamsize>(n)))
.WillOnce(testing::Return(max_streamsize));
data += n;
size -= static_cast<std::size_t>(n);
size -= n;
} while (size != 0);
fmt::internal::write(os, buffer);
}
......@@ -178,8 +185,7 @@ template <typename Output> Output& operator<<(Output& out, ABC) {
}
} // namespace fmt_test
template <typename T>
struct TestTemplate {};
template <typename T> struct TestTemplate {};
template <typename T>
std::ostream& operator<<(std::ostream& os, TestTemplate<T>) {
......@@ -187,14 +193,13 @@ std::ostream& operator<<(std::ostream& os, TestTemplate<T>) {
}
namespace fmt {
template <typename T>
struct formatter<TestTemplate<T>> : formatter<int> {
template <typename T> struct formatter<TestTemplate<T>> : formatter<int> {
template <typename FormatContext>
typename FormatContext::iterator format(TestTemplate<T>, FormatContext& ctx) {
return formatter<int>::format(2, ctx);
}
};
}
} // namespace fmt
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 407
TEST(OStreamTest, Template) {
......
......@@ -169,7 +169,6 @@ TEST(PrepareTest, FormatPart_ComparisonOperators) {
auto rhs = format_part(specs);
EXPECT_EQ(lhs, rhs);
const auto specs_text = fmt::string_view("<10");
specs.parsed_specs = prepared_specs();
lhs = format_part(specs);
rhs = format_part(specs);
......@@ -191,8 +190,6 @@ TEST(PrepareTest, FormatPart_ComparisonOperators) {
rhs = format_part(rhs_spec);
EXPECT_NE(lhs, rhs);
const auto lhs_specs_text = fmt::string_view("<10");
const auto rhs_specs_text = fmt::string_view("<42");
lhs_spec = format_part::specification(specs_argument_id);
rhs_spec = format_part::specification(specs_argument_id);
lhs_spec.parsed_specs.precision = 1;
......@@ -202,7 +199,6 @@ TEST(PrepareTest, FormatPart_ComparisonOperators) {
EXPECT_NE(lhs, rhs);
}
{
const auto specs_text = fmt::string_view{"<10"};
const auto specs_argument_id = 0u;
const auto specs_named_argument_id =
fmt::internal::string_view_metadata(0, 42);
......@@ -622,8 +618,9 @@ template <typename T> struct user_allocator {
~user_allocator() = default;
template <typename U> user_allocator(const user_allocator<U>&) {}
pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0) {
pointer allocate(
size_type cnt,
typename std::allocator<fmt::monostate>::const_pointer = FMT_NULL) {
return new value_type[cnt];
}
......@@ -633,8 +630,8 @@ template <typename T> struct user_allocator {
void destroy(pointer p) { (*p).~value_type(); }
bool operator==(const user_allocator& other) const { return true; }
bool operator!=(const user_allocator& other) const { return false; }
bool operator==(const user_allocator&) const { return true; }
bool operator!=(const user_allocator&) const { return false; }
};
TEST(PrepareTest, PassUserTypeFormat) {
......
......@@ -368,8 +368,8 @@ TEST(PrintfTest, Length) {
TestLength<std::size_t>("z");
TestLength<std::ptrdiff_t>("t");
long double max = std::numeric_limits<long double>::max();
EXPECT_PRINTF(fmt::format("{}", max), "%g", max);
EXPECT_PRINTF(fmt::format("{}", max), "%Lg", max);
EXPECT_PRINTF(fmt::format("{:.6}", max), "%g", max);
EXPECT_PRINTF(fmt::format("{:.6}", max), "%Lg", max);
}
TEST(PrintfTest, Bool) {
......