Skip to content

reimplement formatter<tuple_join_view> #2457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2021
Merged

Conversation

sunmy2019
Copy link
Contributor

@sunmy2019 sunmy2019 commented Aug 18, 2021

PR for #2451. I reimplemented that formatter.

It supports the following code: (Tested on clang12 and gcc11, for std=c++11/17/20)

    std::tuple<int, int, int, int> a{1, 1, -1, -1};
    fmt::print("{:+03}\n", fmt::join(a, ", "));
    fmt::print(FMT_STRING("{:-010}\n"), fmt::join(a, ", "));
    fmt::print(FMT_COMPILE("{:-010}\n"), fmt::join(a, ", "));

    std::tuple<int, long, unsigned, unsigned long> b{1, 1, -1, -1};
    fmt::print("{:010}\n", fmt::join(b, ", "));
    fmt::print(FMT_STRING("{:010}\n"), fmt::join(b, ", "));
    fmt::print(FMT_COMPILE("{:010}\n"), fmt::join(b, ", "));

    std::tuple<float, double, long double> c{1.1, 1.11, -1.111};
    fmt::print("{:3.6f}\n", fmt::join(c, ", "));

    std::tuple<> d;
    fmt::print("{}\n", fmt::join(d, ", "));

I add two functions parse_subroutine and format_subroutine as subroutines of parse and format, respectively.

I have no idea the best appropriate function name. It may need a change. Also the index_helper can be replaced by std::integral_constant.

This code is only a prototype to demostrate my ideas, and not in its best shape. Comments are welcome.

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I have no idea the best appropriate function name. It may need a change.

I suggest renaming parse_subroutine and format_subroutine to do_parse and do_format respectively which is a common pattern for helper functions like this.

Also the index_helper can be replaced by std::integral_constant.

Please do, replace index_helper with std::integral_constant.

1. completely reimplement `formatter<tuple_join_view<Char, T...>, Char>`
2. Add some tests
@vitaut vitaut merged commit a44c8f6 into fmtlib:master Aug 23, 2021
@vitaut
Copy link
Contributor

vitaut commented Aug 23, 2021

Thank you!

@cgmb
Copy link

cgmb commented Aug 23, 2021

Very cool.

@vitaut
Copy link
Contributor

vitaut commented Aug 23, 2021

One potential optimization is to create one formatter per type instead of one formatter per tuple element. Then std::tuple<int, int, int, int> will only need one formatter and call to parse.

@sunmy2019
Copy link
Contributor Author

sunmy2019 commented Aug 23, 2021

One potential optimization is to create one formatter per type instead of one formatter per tuple element. Then std::tuple<int, int, int, int> will only need one formatter and call to parse.

This can be done with a lot of extra work, though. We basically need to

  1. deduplicate the same types T.
  2. find the correct formatter for each args.

I think this is possible with these technologies demonstrated below. https://godbolt.org/z/h41q9Wav5

// check whether T is in Ts...
template <typename T, typename... Ts>
struct appear : public std::integral_constant<bool, false> {};

template <typename T, typename T1, typename... Ts>
struct appear<T, T1, Ts...>
    : public std::integral_constant<
          bool, std::is_same<T, T1>::value ? true : appear<T, Ts...>::value> {};

// we need this helper function to unpack std::tuple
template <typename T, typename... Ts>
typename std::conditional<appear<T, Ts...>::value, std::tuple<Ts...>,
                          std::tuple<T, Ts...>>::type
    get_type(std::tuple<Ts...>);


// deduplicate to get std::tuple<...>
// for example:
// deduplicate<int, int, double, int>::type is std::tuple<double, int>
template <typename... Ts>
struct deduplicate {
  using type = std::tuple<>;
};

template <typename T, typename... Ts>
struct deduplicate<T, Ts...> {
  using type = decltype(get_type<T>(std::declval<typename deduplicate<Ts...>::type>()));
};

and for the second part

// get the index of a type from the tuple
template <typename T, typename... Ts>
struct get_index : public std::integral_constant<size_t, 0> {};

template <typename T, typename T1, typename... Ts>
struct get_index<T, T1, Ts...>
    : public std::integral_constant<
          size_t,
          std::is_same<T, T1>::value ? 0 : get_index<T, Ts...>::value + 1> {};

template <typename T, typename T1>
struct get_tuple_index;

template <typename T, typename... Ts>
struct get_tuple_index<T, std::tuple<Ts...>> {
  size_t value = get_index<T, Ts...>::value;
};

As for this, we also need to handle std::decay_t. I think it's technically possible. It remains only a design choice.

PoetaKodu pushed a commit to pacc-repo/fmt that referenced this pull request Nov 11, 2021
* reimplement `formatter<tuple_join_view>`

1. completely reimplement `formatter<tuple_join_view<Char, T...>, Char>`
2. Add some tests

* use FMT_THROW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants