Skip to content

Commit

Permalink
Merge branch 'fmtlib:master' into zig-pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Oct 16, 2023
2 parents 79ad694 + 2ac6c5c commit cf47456
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 45 deletions.
3 changes: 2 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,9 @@ template <typename Period> FMT_CONSTEXPR inline const char* get_units() {
if (std::is_same<Period, std::tera>::value) return "Ts";
if (std::is_same<Period, std::peta>::value) return "Ps";
if (std::is_same<Period, std::exa>::value) return "Es";
if (std::is_same<Period, std::ratio<60>>::value) return "m";
if (std::is_same<Period, std::ratio<60>>::value) return "min";
if (std::is_same<Period, std::ratio<3600>>::value) return "h";
if (std::is_same<Period, std::ratio<86400>>::value) return "d";
return nullptr;
}

Expand Down
12 changes: 9 additions & 3 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ template <typename T>
using remove_const_t = typename std::remove_const<T>::type;
template <typename T>
using remove_cvref_t = typename std::remove_cv<remove_reference_t<T>>::type;
template <typename T> struct type_identity { using type = T; };
template <typename T> struct type_identity {
using type = T;
};
template <typename T> using type_identity_t = typename type_identity<T>::type;
template <typename T>
using underlying_t = typename std::underlying_type<T>::type;
Expand Down Expand Up @@ -1506,7 +1508,9 @@ FMT_CONSTEXPR auto copy_str(R&& rng, OutputIt out) -> OutputIt {

#if FMT_GCC_VERSION && FMT_GCC_VERSION < 500
// A workaround for gcc 4.8 to make void_t work in a SFINAE context.
template <typename...> struct void_t_impl { using type = void; };
template <typename...> struct void_t_impl {
using type = void;
};
template <typename... T> using void_t = typename void_t_impl<T...>::type;
#else
template <typename...> using void_t = void;
Expand Down Expand Up @@ -2678,7 +2682,9 @@ template <typename Char = char> struct vformat_args {
using type = basic_format_args<
basic_format_context<std::back_insert_iterator<buffer<Char>>, Char>>;
};
template <> struct vformat_args<char> { using type = format_args; };
template <> struct vformat_args<char> {
using type = format_args;
};

// Use vformat_args and avoid type_identity to keep symbols short.
template <typename Char>
Expand Down
10 changes: 5 additions & 5 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,7 @@ template <> struct cache_accessor<double> {
{0xe0accfa875af45a7, 0x93eb1b80a33b8606},
{0x8c6c01c9498d8b88, 0xbc72f130660533c4},
{0xaf87023b9bf0ee6a, 0xeb8fad7c7f8680b5},
{ 0xdb68c2ca82ed2a05,
0xa67398db9f6820e2 }
{0xdb68c2ca82ed2a05, 0xa67398db9f6820e2}
#else
{0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b},
{0xce5d73ff402d98e3, 0xfb0a3d212dc81290},
Expand Down Expand Up @@ -1132,7 +1131,7 @@ FMT_INLINE int remove_trailing_zeros(uint32_t& n, int s = 0) noexcept {
FMT_ASSERT(n != 0, "");
// Modular inverse of 5 (mod 2^32): (mod_inv_5 * 5) mod 2^32 = 1.
constexpr uint32_t mod_inv_5 = 0xcccccccd;
constexpr uint32_t mod_inv_25 = 0xc28f5c29; // = mod_inv_5 * mod_inv_5
constexpr uint32_t mod_inv_25 = 0xc28f5c29; // = mod_inv_5 * mod_inv_5

while (true) {
auto q = rotr(n * mod_inv_25, 2);
Expand Down Expand Up @@ -1168,7 +1167,7 @@ FMT_INLINE int remove_trailing_zeros(uint64_t& n) noexcept {

// If n is not divisible by 10^8, work with n itself.
constexpr uint64_t mod_inv_5 = 0xcccccccccccccccd;
constexpr uint64_t mod_inv_25 = 0x8f5c28f5c28f5c29; // = mod_inv_5 * mod_inv_5
constexpr uint64_t mod_inv_25 = 0x8f5c28f5c28f5c29; // mod_inv_5 * mod_inv_5

int s = 0;
while (true) {
Expand Down Expand Up @@ -1439,7 +1438,8 @@ FMT_FUNC bool write_console(std::FILE* f, string_view text) {
auto u16 = utf8_to_utf16(text);
auto written = dword();
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
static_cast<uint32_t>(u16.size()), &written, nullptr) != 0;
static_cast<uint32_t>(u16.size()), &written,
nullptr) != 0;
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class FMT_API ostream {
output to the file.
*/
template <typename... T> void print(format_string<T...> fmt, T&&... args) {
vformat_to(detail::buffer_appender<char>(buffer_), fmt,
vformat_to(std::back_inserter(buffer_), fmt,
fmt::make_format_args(args...));
}
};
Expand Down
4 changes: 3 additions & 1 deletion include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ void format_value(buffer<Char>& buf, const T& value,
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
}

template <typename T> struct streamed_view { const T& value; };
template <typename T> struct streamed_view {
const T& value;
};

} // namespace detail

Expand Down
14 changes: 9 additions & 5 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
FMT_BEGIN_NAMESPACE
FMT_BEGIN_EXPORT

template <typename T> struct printf_formatter { printf_formatter() = delete; };
template <typename T> struct printf_formatter {
printf_formatter() = delete;
};

template <typename Char> class basic_printf_context {
private:
detail::buffer_appender<Char> out_;
basic_format_args<basic_printf_context> args_;

static_assert(
std::is_same<Char, char>::value || std::is_same<Char, wchar_t>::value,
"Unsupported code unit type.");
static_assert(std::is_same<Char, char>::value ||
std::is_same<Char, wchar_t>::value,
"Unsupported code unit type.");

public:
using char_type = Char;
Expand Down Expand Up @@ -106,7 +108,9 @@ struct is_zero_int {

template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {};

template <> struct make_unsigned_or_bool<bool> { using type = bool; };
template <> struct make_unsigned_or_bool<bool> {
using type = bool;
};

template <typename T, typename Context> class arg_converter {
private:
Expand Down
8 changes: 3 additions & 5 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ void write_escaped_path(basic_memory_buffer<Char>& quoted,
}

# ifdef _WIN32
template <>
inline auto get_path_string<char>(const std::filesystem::path& p) {
template <> inline auto get_path_string<char>(const std::filesystem::path& p) {
return to_utf8<wchar_t>(p.native(), to_utf8_error_policy::replace);
}

Expand Down Expand Up @@ -477,15 +476,14 @@ struct formatter<std::atomic<T>, Char,
#ifdef __cpp_lib_atomic_flag_test
FMT_EXPORT
template <typename Char>
struct formatter<std::atomic_flag, Char>
: formatter<bool, Char> {
struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
template <typename FormatContext>
auto format(const std::atomic_flag& v, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<bool, Char>::format(v.test(), ctx);
}
};
#endif // __cpp_lib_atomic_flag_test
#endif // __cpp_lib_atomic_flag_test

FMT_END_NAMESPACE
#endif // FMT_STD_H_
22 changes: 12 additions & 10 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,11 @@ TEST(chrono_test, format_default) {
fmt::format("{}", std::chrono::duration<int, std::peta>(42)));
EXPECT_EQ("42Es",
fmt::format("{}", std::chrono::duration<int, std::exa>(42)));
EXPECT_EQ("42m", fmt::format("{}", std::chrono::minutes(42)));
EXPECT_EQ("42min", fmt::format("{}", std::chrono::minutes(42)));
EXPECT_EQ("42h", fmt::format("{}", std::chrono::hours(42)));
# if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
EXPECT_EQ("42d", fmt::format("{}", std::chrono::days(42)));
# endif
EXPECT_EQ(
"42[15]s",
fmt::format("{}", std::chrono::duration<int, std::ratio<15, 1>>(42)));
Expand Down Expand Up @@ -740,21 +743,20 @@ TEST(chrono_test, unsigned_duration) {
}

TEST(chrono_test, weekday) {
auto loc = get_locale("ru_RU.UTF-8");
auto loc = get_locale("es_ES.UTF-8");
std::locale::global(loc);
auto mon = fmt::weekday(1);
auto sat = fmt::weekday(6);

auto tm = std::tm();
tm.tm_wday = static_cast<int>(mon.c_encoding());
tm.tm_wday = static_cast<int>(sat.c_encoding());

EXPECT_EQ(fmt::format("{}", mon), "Mon");
EXPECT_EQ(fmt::format("{:%a}", tm), "Mon");
EXPECT_EQ(fmt::format("{}", sat), "Sat");
EXPECT_EQ(fmt::format("{:%a}", tm), "Sat");

if (loc != std::locale::classic()) {
EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
Contains(fmt::format(loc, "{:L}", mon)));
EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
Contains(fmt::format(loc, "{:%a}", tm)));
auto saturdays = std::vector<std::string>{"sáb", "sá."};
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L}", sat)));
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", tm)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ FMT_BEGIN_NAMESPACE
template <> struct formatter<point> : nested_formatter<double> {
auto format(point p, format_context& ctx) const -> decltype(ctx.out()) {
return write_padded(ctx, [this, p](auto out) -> decltype(out) {
return format_to(out, "({}, {})", nested(p.x), nested(p.y));
return fmt::format_to(out, "({}, {})", nested(p.x), nested(p.y));
});
}
};
Expand Down
3 changes: 1 addition & 2 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,8 @@ TEST(std_test, format_const_bit_reference) {
}

TEST(std_test, format_bitset) {
const std::bitset<6> bs(42);
auto bs = std::bitset<6>(42);
EXPECT_EQ(fmt::format("{}", bs), "101010");
EXPECT_EQ(fmt::format("{:.4}", bs), "101010");
EXPECT_EQ(fmt::format("{:0>8}", bs), "00101010");
EXPECT_EQ(fmt::format("{:-^12}", bs), "---101010---");
}
Expand Down
3 changes: 1 addition & 2 deletions test/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ std::locale do_get_locale(const char* name) {

std::locale get_locale(const char* name, const char* alt_name) {
auto loc = do_get_locale(name);
if (loc == std::locale::classic() && alt_name) {
if (loc == std::locale::classic() && alt_name)
loc = do_get_locale(alt_name);
}
if (loc == std::locale::classic())
fmt::print(stderr, "{} locale is missing.\n", name);
return loc;
Expand Down
15 changes: 6 additions & 9 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -614,17 +614,14 @@ TEST(locale_test, complex) {
}

TEST(locale_test, chrono_weekday) {
auto loc = get_locale("ru_RU.UTF-8", "Russian_Russia.1251");
auto loc = get_locale("es_ES.UTF-8", "Spanish_Spain.1252");
auto loc_old = std::locale::global(loc);
auto mon = fmt::weekday(1);
EXPECT_EQ(fmt::format(L"{}", mon), L"Mon");
auto sat = fmt::weekday(6);
EXPECT_EQ(fmt::format(L"{}", sat), L"Sat");
if (loc != std::locale::classic()) {
// {L"\x43F\x43D", L"\x41F\x43D", L"\x43F\x43D\x434", L"\x41F\x43D\x434"}
// {L"пн", L"Пн", L"пнд", L"Пнд"}
EXPECT_THAT(
(std::vector<std::wstring>{L"\x43F\x43D", L"\x41F\x43D",
L"\x43F\x43D\x434", L"\x41F\x43D\x434"}),
Contains(fmt::format(loc, L"{:L}", mon)));
// L'\xE1' is 'á'.
auto saturdays = std::vector<std::wstring>{L"s\xE1""b", L"s\xE1."};
EXPECT_THAT(saturdays, Contains(fmt::format(loc, L"{:L}", sat)));
}
std::locale::global(loc_old);
}
Expand Down

0 comments on commit cf47456

Please sign in to comment.