fmt: more update to use new fmt-based APIs

This commit is contained in:
Dirk-Jan C. Binnema
2023-07-06 12:22:50 +03:00
parent 91c5a9bac5
commit 82235b9d49
8 changed files with 141 additions and 182 deletions

View File

@ -686,32 +686,3 @@ Mu::fputs_encoded (const std::string& str, FILE *stream)
return (rv == EOF) ? false: true;
}
__attribute__((format(printf, 2, 0)))
static bool
print_args (FILE *stream, const char *frm, va_list args)
{
char *str;
gboolean rv;
str = g_strdup_vprintf (frm, args);
rv = fputs_encoded (str, stream);
g_free (str);
return rv;
}
bool
Mu::print_encoded (const char *frm, ...)
{
va_list args;
gboolean rv;
g_return_val_if_fail (frm, false);
va_start (args, frm);
rv = print_args (stdout, frm, args);
va_end (args);
return rv;
}

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2020-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2020-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
@ -107,6 +107,7 @@ inline void mu_printerrln(fmt::format_string<T...> frm, T&&... args) noexcept {
fmt::println(stderr, frm, std::forward<T>(args)...);
}
/*
* Fprmatting
*/
@ -197,22 +198,27 @@ static inline std::string join(const std::vector<std::string>& svec, char sepa)
bool fputs_encoded (const std::string& str, FILE *stream);
/**
* print a formatted string (assumed to be in utf8-format) to stdout,
* print a fmt-style formatted string (assumed to be in utf8-format) to stdout,
* converted to the current locale
*
* @param a standard printf() format string, followed by a parameter list
* @param a standard fmt-style format string, followed by a parameter list
*
* @return true if printing worked, false otherwise
*/
bool print_encoded (const char *frm, ...) G_GNUC_PRINTF(1,2);
template<typename...T>
static inline bool mu_print_encoded(fmt::format_string<T...> frm, T&&... args) noexcept {
return fputs_encoded(fmt::format(frm, std::forward<T>(args)...),
::stdout);
}
/**
* Parse a date string to the corresponding time_t
* *
* @param date the date expressed a YYYYMMDDHHMMSS or any n... of the first
* characters, using the local timezone.
* @param first whether to fill out incomplete dates to the start or the end;
* ie. either 1972 -> 197201010000 or 1972 -> 197212312359
* characters, using the local timezone. Non-digits are ignored,
* so 2018-05-05 is equivalent to 20180505.
* @param first whether to fill out incomplete dates to the start (@true) or the
* end (@false); ie. either 1972 -> 197201010000 or 1972 -> 197212312359
*
* @return the corresponding time_t or Nothing if parsing failed.
*/
@ -302,17 +308,15 @@ to_us(Duration d)
struct StopWatch {
using Clock = std::chrono::steady_clock;
StopWatch(const std::string name) : start_{Clock::now()}, name_{name} {}
~StopWatch()
{
~StopWatch() {
const auto us{static_cast<double>(to_us(Clock::now() - start_))};
if (us > 2000000)
g_debug("sw: %s: finished after %0.1f s", name_.c_str(), us / 1000000);
mu_debug("sw: {}: finished after {:.1f} s", name_, us / 1000000);
else if (us > 2000)
g_debug("sw: %s: finished after %0.1f ms", name_.c_str(), us / 1000);
mu_debug("sw: {}: finished after {:.1f} ms", name_, us / 1000);
else
g_debug("sw: %s: finished after %g us", name_.c_str(), us);
mu_debug("sw: {}: finished after {} us", name_, us);
}
private:
Clock::time_point start_;
std::string name_;
@ -383,9 +387,9 @@ to_string_gchar(gchar*&& str)
/*
* Lexicals Number are lexicographically sortable string representations
* of numbers. Start with 'g' + length of number in hex, followed by
* the ascii for the hex represntation. So,
* Lexnums are lexicographically sortable string representations of non-negative
* integers. Start with 'f' + length of hex-representation number, followed by
* the hex representation itself. So,
*
* 0 -> 'g0'
* 1 -> 'g1'
@ -476,54 +480,6 @@ void seq_for_each(const Sequence& seq, UnaryOp op) {
std::for_each(seq.cbegin(), seq.cend(), op);
}
/**
* array of associated pair elements -- like an alist
* but based on std::array and thus can be constexpr
*/
template<typename T1, typename T2, std::size_t N>
using AssocPairs = std::array<std::pair<T1, T2>, N>;
/**
* Get the first value of the pair where the second element is @param s.
*
* @param p AssocPairs
* @param s some second pair value
*
* @return the matching first pair value, or Nothing if not found.
*/
template<typename P>
constexpr Option<typename P::value_type::first_type>
to_first(const P& p, typename P::value_type::second_type s)
{
for (const auto& item: p)
if (item.second == s)
return item.first;
return Nothing;
}
/**
* Get the second value of the pair where the first element is @param f.
*
* @param p AssocPairs
* @param f some first pair value
*
* @return the matching second pair value, or Nothing if not found.
*/
template<typename P>
constexpr Option<typename P::value_type::second_type>
to_second(const P& p, typename P::value_type::first_type f)
{
for (const auto& item: p)
if (item.first == f)
return item.second;
return Nothing;
}
/**
* Convert string view in something printable with %.*s
*/
#define STR_V(sv__) static_cast<int>((sv__).size()), (sv__).data()
struct MaybeAnsi {
explicit MaybeAnsi(bool use_color) : color_{use_color} {}
@ -555,7 +511,8 @@ struct MaybeAnsi {
private:
std::string ansi(Color c, bool fg = true) const
{
return color_ ? format("\x1b[%dm", static_cast<int>(c) + (fg ? 0 : 10)) : "";
return color_ ? mu_format("\x1b[{}m",
static_cast<int>(c) + (fg ? 0 : 10)) : "";
}
const bool color_;
@ -574,12 +531,10 @@ private:
#define MU_TO_NUM(ET, ELM) std::underlying_type_t<ET>(ELM)
#define MU_TO_ENUM(ET, NUM) static_cast<ET>(NUM)
#define MU_ENABLE_BITOPS(ET) \
constexpr ET operator&(ET e1, ET e2) \
{ \
constexpr ET operator&(ET e1, ET e2) { \
return MU_TO_ENUM(ET, MU_TO_NUM(ET, e1) & MU_TO_NUM(ET, e2)); \
} \
constexpr ET operator|(ET e1, ET e2) \
{ \
constexpr ET operator|(ET e1, ET e2) { \
return MU_TO_ENUM(ET, MU_TO_NUM(ET, e1) | MU_TO_NUM(ET, e2)); \
} \
constexpr ET operator~(ET e) { return MU_TO_ENUM(ET, ~(MU_TO_NUM(ET, e))); } \

View File

@ -282,35 +282,6 @@ test_locale_workaround()
g_assert_true(locale_workaround());
}
enum struct TestEnum { A, B, C };
constexpr AssocPairs<TestEnum, std::string_view, 3>
test_epairs = {{
{TestEnum::A, "a"},
{TestEnum::B, "b"},
{TestEnum::C, "c"},
}};
static constexpr Option<std::string_view>
to_name(TestEnum te)
{
return to_second(test_epairs, te);
}
static constexpr Option<TestEnum>
to_type(std::string_view name)
{
return to_first(test_epairs, name);
}
static void
test_enum_pairs(void)
{
assert_equal(to_name(TestEnum::A).value(), "a");
g_assert_true(to_type("c").value() == TestEnum::C);
}
static void
test_summarize(void)
@ -353,7 +324,6 @@ main(int argc, char* argv[])
g_test_add_func("/utils/define-bitmap", test_define_bitmap);
g_test_add_func("/utils/to-from-lexnum", test_to_from_lexnum);
g_test_add_func("/utils/locale-workaround", test_locale_workaround);
g_test_add_func("/utils/enum-pairs", test_enum_pairs);
return g_test_run();
}