Changes code style to Google Style.

Renames namespace meta to internal and put copy functions int it.
Replaced constexpr by FMT_CONSTEXPR.
Replaced std::ptrdiff_t by std::size_t.
This commit is contained in:
Remotion 2018-05-12 22:55:51 +02:00
parent 50ee89b223
commit a442c77c9a
2 changed files with 187 additions and 153 deletions

View File

@ -24,201 +24,196 @@ namespace fmt {
template <typename Char> template <typename Char>
struct formatting_base { struct formatting_base {
template <typename ParseContext> template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return ctx.begin(); } FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
}; };
template <typename Char, typename Enable = void> template <typename Char, typename Enable = void>
struct formatting_range : formatting_base<Char> struct formatting_range : formatting_base<Char> {
{ Char prefix = '{';
Char prefix = '{'; Char delimiter = ',';
Char delimiter = ','; Char postfix = '}';
Char postfix = '}'; static FMT_CONSTEXPR bool add_spaces = false;
bool add_spaces = true;
}; };
template <typename Char, typename Enable = void> template <typename Char, typename Enable = void>
struct formatting_tuple : formatting_base<Char> struct formatting_tuple : formatting_base<Char> {
{ Char prefix = '[';
Char prefix = '['; Char delimiter = ',';
Char delimiter = ','; Char postfix = ']';
Char postfix = ']'; static FMT_CONSTEXPR bool add_spaces = false;
bool add_spaces = true;
}; };
template<typename RangeT, typename OutputIterator> namespace internal {
template <typename RangeT, typename OutputIterator>
void copy(const RangeT &range, OutputIterator out) { void copy(const RangeT &range, OutputIterator out) {
for (const auto& it : range) { for (const auto &it : range) {
*out++ = it; *out++ = it;
} }
} }
template<typename OutputIterator> template <typename OutputIterator>
void copy(const char *str, OutputIterator out) { void copy(const char *str, OutputIterator out) {
const char* p_curr = str; const char *p_curr = str;
while (*p_curr) { while (*p_curr) {
*out++ = *p_curr++; *out++ = *p_curr++;
} }
} }
template<typename OutputIterator> template <typename OutputIterator>
void copy(const char ch, OutputIterator out) { void copy(char ch, OutputIterator out) {
*out++ = ch; *out++ = ch;
} }
} // namespace fmt } // namespace internal
namespace internal {
namespace fmt {
namespace meta {
/// Return true value if T has std::string interface, like std::string_view. /// Return true value if T has std::string interface, like std::string_view.
template<typename T> template <typename T>
class is_like_std_string { class is_like_std_string {
template<typename U> static auto check(U* p) -> decltype( template <typename U>
p->find('a') static auto check(U *p) -> decltype(p->find('a'), p->length(), p->data(), int());
, p->length() template <typename>
, p->data() static void check(...);
, int());
template<typename > static void check(...); public:
public: static FMT_CONSTEXPR bool value = !std::is_void<decltype(check<T>(nullptr))>::value;
static const bool value = !std::is_void< decltype(check<T>(nullptr)) >::value;
}; };
template<typename T> template <typename T>
constexpr bool is_like_std_string_v = is_like_std_string<T>::value; FMT_CONSTEXPR bool is_like_std_string_v = is_like_std_string<T>::value;
template<typename... Ts> template <typename... Ts>
struct conditional_helper {}; struct conditional_helper {};
template<typename T, typename _ = void> template <typename T, typename _ = void>
struct is_range_ : std::false_type {}; struct is_range_ : std::false_type {};
template<typename T> template <typename T>
struct is_range_<T, struct is_range_<T, std::conditional_t<false,
std::conditional_t< false, conditional_helper<decltype(std::declval<T>().begin()),
conditional_helper< decltype(std::declval<T>().end())>,
decltype(std::declval<T>().begin()), void>> : std::true_type {};
decltype(std::declval<T>().end())
>, void>
> : std::true_type {};
template<typename T>
constexpr bool is_range_v = is_range_<T>::value && !is_like_std_string<T>::value;
template <typename T>
FMT_CONSTEXPR bool is_range_v = is_range_<T>::value && !is_like_std_string<T>::value;
/// tuple_size and tuple_element check. /// tuple_size and tuple_element check.
template<typename T> template <typename T>
class is_tuple_like_ { class is_tuple_like_ {
template<typename U> static auto check(U* p) -> decltype( template <typename U>
std::tuple_size< U >::value, static auto check(U *p)
std::declval<typename std::tuple_element<0, U>::type>(), -> decltype(std::tuple_size<U>::value,
int()); std::declval<typename std::tuple_element<0, U>::type>(), int());
template<typename > static void check(...); template <typename>
public: static void check(...);
static constexpr bool value = !std::is_void< decltype(check<T>(nullptr)) >::value;
public:
static FMT_CONSTEXPR bool value = !std::is_void<decltype(check<T>(nullptr))>::value;
}; };
template<typename T> template <typename T>
constexpr bool is_tuple_like_v = is_tuple_like_<T>::value && !is_range_<T>::value; FMT_CONSTEXPR bool is_tuple_like_v = is_tuple_like_<T>::value && !is_range_<T>::value;
//=-------------------------------------------------------------------------------------------------------------------- //=--------------------------------------------------------------------------------------------------------------------
template<size_t... Is, class Tuple, class F> template <size_t... Is, class Tuple, class F>
void for_each(std::index_sequence<Is...>, Tuple&& tup, F&& f) noexcept { void for_each(std::index_sequence<Is...>, Tuple &&tup, F &&f) noexcept {
using std::get; using std::get;
// using free function get<I>(T) now. // using free function get<I>(T) now.
const int _[] = { 0, const int _[] = {0, ((void)f(get<Is>(tup)), 0)...};
((void)f(get<Is>(tup)), (void)_; // blocks warnings
0)... };
(void)_; // blocks warnings
} }
//=-------------------------------------------------------------------------------------------------------------------- //=--------------------------------------------------------------------------------------------------------------------
template<class T> template <class T>
constexpr std::make_index_sequence<std::tuple_size<T>::value> FMT_CONSTEXPR std::make_index_sequence<std::tuple_size<T>::value>
get_indexes(T const&) { return {}; } get_indexes(T const &) { return {}; }
//=-------------------------------------------------------------------------------------------------------------------- //=--------------------------------------------------------------------------------------------------------------------
template<class Tuple, class F> template <class Tuple, class F>
void for_each(Tuple&& tup, F&& f) { void for_each(Tuple &&tup, F &&f) {
const auto indexes = get_indexes(tup); const auto indexes = get_indexes(tup);
for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f)); for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
} }
} // namespace meta } // namespace internal
} // namespace fmt
namespace fmt {
// ===================================================================================================================== // =====================================================================================================================
template<typename TupleT, typename Char> template <typename TupleT, typename Char>
struct formatter< TupleT, Char struct formatter<TupleT, Char, std::enable_if_t<fmt::internal::is_tuple_like_v<TupleT>>> {
, std::enable_if_t<fmt::meta::is_tuple_like_v<TupleT>> > fmt::formatting_tuple<Char> formating;
{
fmt::formatting_tuple<Char> formating;
template <typename ParseContext> template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
return formating.parse(ctx); return formating.parse(ctx);
} }
template <typename FormatContext = format_context> template <typename FormatContext = format_context>
auto format(const TupleT &values, FormatContext &ctx) -> decltype(ctx.out()) { auto format(const TupleT &values, FormatContext &ctx) -> decltype(ctx.out()) {
auto out = ctx.out(); auto out = ctx.out();
std::ptrdiff_t i = 0; std::size_t i = 0;
fmt::copy(formating.prefix, out); internal::copy(formating.prefix, out);
fmt::meta::for_each(values, [&](const auto &v) { internal::for_each(values, [&](const auto &v) {
if (i++ > 0) { fmt::copy(formating.delimiter, out); } if (i++ > 0) {
if (formating.add_spaces) { format_to(out, " {}", v); } internal::copy(formating.delimiter, out);
else { format_to(out, "{}", v); } }
}); if (formating.add_spaces) {
if (formating.add_spaces) { *out++ = ' '; } format_to(out, " {}", v);
fmt::copy(formating.postfix, out); } else {
format_to(out, "{}", v);
}
});
if (formating.add_spaces) {
*out++ = ' ';
}
internal::copy(formating.postfix, out);
return ctx.out(); return ctx.out();
} }
}; };
} // namespace fmt template <typename RangeT, typename Char>
struct formatter<RangeT, Char, std::enable_if_t<fmt::internal::is_range_v<RangeT>>> {
static FMT_CONSTEXPR std::size_t range_length_limit =
FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the range.
fmt::formatting_range<Char> formating;
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
return formating.parse(ctx);
}
namespace fmt { template <typename FormatContext>
auto format(const RangeT &values, FormatContext &ctx) -> decltype(ctx.out()) {
auto out = ctx.out();
template<typename RangeT, typename Char> internal::copy(formating.prefix, out);
struct formatter <RangeT, Char, std::enable_if_t<fmt::meta::is_range_v<RangeT>> > std::size_t i = 0;
{ for (const auto &it : values) {
static constexpr std::ptrdiff_t range_length_limit = FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the range. if (i > 0) {
internal::copy(formating.delimiter, out);
fmt::formatting_range<Char> formating; }
if (formating.add_spaces) {
template <typename ParseContext> format_to(out, " {}", it);
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { } else {
return formating.parse(ctx); format_to(out, "{}", it);
} }
if (++i > range_length_limit) {
template <typename FormatContext> format_to(out, " ... <other elements>");
auto format(const RangeT &values, FormatContext &ctx) -> decltype(ctx.out()) { break;
auto out = ctx.out(); }
fmt::copy(formating.prefix, out); }
std::ptrdiff_t i = 0; if (formating.add_spaces) {
for (const auto& it : values) { *out++ = ' ';
if (i > 0) { fmt::copy(formating.delimiter, out); } }
if (formating.add_spaces) { format_to(out, " {}", it); } internal::copy(formating.postfix, out);
else { format_to(out, "{}", it); } return ctx.out();
if (++i > range_length_limit) { }
format_to(out, " ... <other elements>");
break;
}
}
if (formating.add_spaces) { *out++ = ' '; }
fmt::copy(formating.postfix, out);
return ctx.out();
}
}; };
} // namespace fmt } // namespace fmt
#endif // FMT_RANGES_H_ #endif // FMT_RANGES_H_

View File

@ -20,32 +20,71 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include <map> #include <map>
#include <string>
TEST(RangesTest, FormatVector) { TEST(RangesTest, FormatVector) {
std::vector<int32_t> iv{ 1,2,3,5,7,11 }; std::vector<int32_t> iv{1, 2, 3, 5, 7, 11};
auto ivf = fmt::format("{}", iv); auto ivf = fmt::format("{}", iv);
EXPECT_EQ("{ 1, 2, 3, 5, 7, 11 }", ivf); EXPECT_EQ("{1,2,3,5,7,11}", ivf);
} }
TEST(RangesTest, FormatVector2) { TEST(RangesTest, FormatVector2) {
std::vector<std::vector<int32_t>> ivv{ {1,2},{3,5},{7,11} }; std::vector<std::vector<int32_t>> ivv{{1, 2}, {3, 5}, {7, 11}};
auto ivf = fmt::format("{}", ivv); auto ivf = fmt::format("{}", ivv);
EXPECT_EQ("{ { 1, 2 }, { 3, 5 }, { 7, 11 } }", ivf); EXPECT_EQ("{{1,2},{3,5},{7,11}}", ivf);
} }
TEST(RangesTest, FormatMap) { TEST(RangesTest, FormatMap) {
std::map<std::string, int32_t> simap{ {"one",1}, {"two",2} }; std::map<std::string, int32_t> simap{{"one", 1}, {"two", 2}};
EXPECT_EQ("{ [ one, 1 ], [ two, 2 ] }", fmt::format("{}", simap)); EXPECT_EQ("{[one,1],[two,2]}", fmt::format("{}", simap));
} }
TEST(RangesTest, FormatPair) { TEST(RangesTest, FormatPair) {
std::pair<int64_t, float> pa1{42, 3.14159265358979f}; std::pair<int64_t, float> pa1{42, 3.14159265358979f};
EXPECT_EQ("[ 42, 3.14159 ]", fmt::format("{}", pa1)); EXPECT_EQ("[42,3.14159]", fmt::format("{}", pa1));
} }
TEST(RangesTest, FormatTuple) { TEST(RangesTest, FormatTuple) {
std::tuple<int64_t, float, std::string> tu1{42, 3.14159265358979f, "this is tuple"}; std::tuple<int64_t, float, std::string> tu1{42, 3.14159265358979f,
EXPECT_EQ("[ 42, 3.14159, this is tuple ]", fmt::format("{}", tu1)); "this is tuple"};
EXPECT_EQ("[42,3.14159,this is tuple]", fmt::format("{}", tu1));
} }
#if (__cplusplus > 201402L) || \
(defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
struct my_struct {
int32_t i;
std::string str; // can throw
template <std::size_t N>
decltype(auto) get() const noexcept {
if constexpr (N == 0)
return i;
else if constexpr (N == 1)
return fmt::string_view{str};
}
};
template <std::size_t N>
decltype(auto) get(const my_struct& s) noexcept {
return s.get<N>();
}
namespace std {
template <>
struct tuple_size<my_struct> : std::integral_constant<std::size_t, 2> {};
template <std::size_t N>
struct tuple_element<N, my_struct> {
using type = decltype(std::declval<my_struct>().get<N>());
};
} // namespace std
TEST(RangesTest, FormatStruct) {
my_struct mst{13, "my struct"};
EXPECT_EQ("[13,my struct]", fmt::format("{}", mst));
}
#endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)