Skip to content

Commit

Permalink
Add support for exotic string_view iterators (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed May 12, 2019
1 parent b488df6 commit 98b3775
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ template <int GRISU_VERSION> struct grisu_shortest_handler {

template <typename Double, FMT_ENABLE_IF_T(sizeof(Double) == sizeof(uint64_t))>
FMT_API bool grisu_format(Double value, buffer<char>& buf, int precision,
unsigned options, int& exp) {
unsigned options, int& exp) {
FMT_ASSERT(value >= 0, "value is negative");
bool fixed = (options & grisu_options::fixed) != 0;
if (value <= 0) { // <= instead of == to silence a warning.
Expand Down
14 changes: 10 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ class format_string_checker {
FMT_CONSTEXPR void on_replacement_field(const Char*) {}

FMT_CONSTEXPR const Char* on_format_specs(const Char* begin, const Char*) {
context_.advance_to(begin);
advance_to(context_, begin);
return arg_id_ < NUM_ARGS ? parse_funcs_[arg_id_](context_) : begin;
}

Expand Down Expand Up @@ -3215,6 +3215,12 @@ basic_format_context<Range, Char>::arg(basic_string_view<char_type> name) {
return arg;
}

template <typename Char, typename ErrorHandler>
FMT_CONSTEXPR void advance_to(basic_parse_context<Char, ErrorHandler>& ctx,
const Char* p) {
ctx.advance_to(ctx.begin() + (p - &*ctx.begin()));
}

template <typename ArgFormatter, typename Char, typename Context>
struct format_handler : internal::error_handler {
typedef typename ArgFormatter::range range;
Expand Down Expand Up @@ -3242,15 +3248,15 @@ struct format_handler : internal::error_handler {
void on_arg_id(basic_string_view<Char> id) { arg = context.arg(id); }

void on_replacement_field(const Char* p) {
parse_context.advance_to(p);
advance_to(parse_context, p);
internal::custom_formatter<Context> f(parse_context, context);
if (!visit_format_arg(f, arg))
context.advance_to(
visit_format_arg(ArgFormatter(context, &parse_context), arg));
}

const Char* on_format_specs(const Char* begin, const Char* end) {
parse_context.advance_to(begin);
advance_to(parse_context, begin);
internal::custom_formatter<Context> f(parse_context, context);
if (visit_format_arg(f, arg)) return parse_context.begin();
basic_format_specs<Char> specs;
Expand All @@ -3261,7 +3267,7 @@ struct format_handler : internal::error_handler {
arg.type());
begin = parse_format_specs(begin, end, handler);
if (begin == end || *begin != '}') on_error("missing '}' in format string");
parse_context.advance_to(begin);
advance_to(parse_context, begin);
context.advance_to(
visit_format_arg(ArgFormatter(context, &parse_context, &specs), arg));
return begin;
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/prepare.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class prepared_format {
const format_part_t& part) const {
const auto view = to_string_view(format_);
const auto specification_begin = view.data() + part.end_of_argument_id;
parse_ctx.advance_to(specification_begin);
advance_to(parse_ctx, specification_begin);
}

template <typename Range, typename Context, typename Id>
Expand Down

0 comments on commit 98b3775

Please sign in to comment.