remove Mu::format, use mu_format

Use the new fmt-based formatting.
This commit is contained in:
Dirk-Jan C. Binnema 2023-08-19 17:13:01 +03:00
parent ce397f3c25
commit 15f08488d3
10 changed files with 52 additions and 139 deletions

View File

@ -327,7 +327,7 @@ Parser::Private::data(Mu::Tokens& tokens, WarningVec& warnings) const
auto fields = process_field(field, flags_); auto fields = process_field(field, flags_);
if (fields.empty()) { // not valid field... if (fields.empty()) { // not valid field...
warnings.push_back({token.pos, format("invalid field '%s'", field.c_str())}); warnings.push_back({token.pos, mu_format("invalid field '{}'", field)});
fields = process_field("", flags_); fields = process_field("", flags_);
// fallback, treat the whole of foo:bar as a value // fallback, treat the whole of foo:bar as a value
return value(fields, field + ":" + val, token.pos, warnings); return value(fields, field + ":" + val, token.pos, warnings);

View File

@ -418,16 +418,15 @@ setup(const TestData& tdata)
/* create maildirs */ /* create maildirs */
for (size_t i = 0; i != tdata.num_maildirs; ++i) { for (size_t i = 0; i != tdata.num_maildirs; ++i) {
const auto mdir = format("%s/maildir-%zu", top_maildir.c_str(), i); const auto mdir = mu_format("{}/maildir-{}", top_maildir, i);
auto res = maildir_mkdir(mdir); auto res = maildir_mkdir(mdir);
g_assert(!!res); g_assert(!!res);
} }
const auto rx = Regex::make("@ID@"); const auto rx = Regex::make("@ID@");
/* create messages */ /* create messages */
for (size_t n = 0; n != tdata.num_messages; ++n) { for (size_t n = 0; n != tdata.num_messages; ++n) {
auto mpath = format("%s/maildir-%zu/cur/msg-%zu:2,S", auto mpath = mu_format("{}/maildir-{}/cur/msg-{}:2,S",
top_maildir.c_str(), top_maildir, n % tdata.num_maildirs,
n % tdata.num_maildirs,
n); n);
std::ofstream stream(mpath); std::ofstream stream(mpath);
auto msg = message(*rx, n); auto msg = message(*rx, n);
@ -441,7 +440,7 @@ tear_down()
{ {
/* ugly */ /* ugly */
GError *err{}; GError *err{};
const auto cmd{format("/bin/rm -rf '%s' '%s'", BENCH_MAILDIRS, BENCH_STORE)}; const auto cmd{mu_format("/bin/rm -rf '{}' '{}'", BENCH_MAILDIRS, BENCH_STORE)};
if (!g_spawn_command_line_sync(cmd.c_str(), NULL, NULL, NULL, &err)) { if (!g_spawn_command_line_sync(cmd.c_str(), NULL, NULL, NULL, &err)) {
mu_warning("error: {}", err ? err->message : "?"); mu_warning("error: {}", err ? err->message : "?");
g_clear_error(&err); g_clear_error(&err);

View File

@ -25,7 +25,7 @@
#include <errno.h> #include <errno.h>
#include <cstdint> #include <cstdint>
#include "mu-utils-format.hh" #include "mu-utils.hh"
#include <glib.h> #include <glib.h>
#ifndef FMT_HEADER_ONLY #ifndef FMT_HEADER_ONLY
@ -166,6 +166,12 @@ private:
const std::string what_; const std::string what_;
}; };
static inline auto
format_as(const Error& err) {
return mu_format("<{} ({})>", err.what(), Error::error_number(err.code()));
}
} // namespace Mu } // namespace Mu
#endif /* MU_ERROR_HH__ */ #endif /* MU_ERROR_HH__ */

View File

@ -216,10 +216,6 @@ struct Sexp {
/// Adding list elements /// Adding list elements
Sexp& add_list(Sexp&& l) { for (auto&& e: l) add(std::move(e)); return *this;}; Sexp& add_list(Sexp&& l) { for (auto&& e: l) add(std::move(e)); return *this;};
/// Use list as stack.
Sexp& prepend(Sexp&& e) { list().insert(list().begin(), std::move(e)); return *this;};
Sexp& prepend(const Sexp& e) { list().insert(list().begin(), e); return *this;};
/// Some convenience for the query parser /// Some convenience for the query parser
Sexp& front() { return list().front(); } Sexp& front() { return list().front(); }
const Sexp& front() const { return list().front(); } const Sexp& front() const { return list().front(); }
@ -234,11 +230,6 @@ struct Sexp {
bool head_symbolp(const Symbol& sym) const { bool head_symbolp(const Symbol& sym) const {
if (head_symbolp()) return head()->symbolp(sym); else return false; if (head_symbolp()) return head()->symbolp(sym); else return false;
} }
Option<Sexp&> tail() {
if (listp()&&!empty()&&cbegin()+1!=cend()) return *(begin()+1); else return Nothing; }
Option<const Sexp&> tail() const {
if (listp()&&!empty()&&cbegin()+1!=cend()) return *(cbegin()+1); else return Nothing; }
/** /**
* Property lists (aka plists) * Property lists (aka plists)

View File

@ -1,61 +0,0 @@
/*
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#ifndef MU_UTILS_FORMAT_HH__
#define MU_UTILS_FORMAT_HH__
#include <string>
#include <cstdarg>
namespace Mu {
/**
* Quote & escape a string for " and \
*
* @param str a string
*
* @return quoted string
*/
std::string quote(const std::string& str);
/**
* Format a string, printf style
*
* @param frm format string
* @param ... parameters
*
* @return a formatted string
*/
std::string format(const char* frm, ...) __attribute__((format(printf, 1, 2)));
/**
* Format a string, printf style
*
* @param frm format string
* @param ... parameters
*
* @return a formatted string
*/
std::string vformat(const char* frm, va_list args) __attribute__((format(printf, 1, 0)));
} // namepace Mu
#endif /* MU_UTILS_FORMAT_HH__ */

View File

@ -44,7 +44,6 @@
#include <glib/gprintf.h> #include <glib/gprintf.h>
#include "mu-utils.hh" #include "mu-utils.hh"
#include "mu-utils-format.hh"
#include "mu-error.hh" #include "mu-error.hh"
#include "mu-option.hh" #include "mu-option.hh"
@ -306,34 +305,6 @@ Mu::quote(const std::string& str)
return res + "\""; return res + "\"";
} }
std::string
Mu::format(const char* frm, ...)
{
va_list args;
va_start(args, frm);
auto str = vformat(frm, args);
va_end(args);
return str;
}
std::string
Mu::vformat(const char* frm, va_list args)
{
char* s{};
const auto res = g_vasprintf(&s, frm, args);
if (res == -1) {
std::cerr << "string format failed" << std::endl;
return {};
}
std::string str{s};
g_free(s);
return str;
}
static Option<::time_t> static Option<::time_t>
delta_ymwdhMs(const std::string& expr) delta_ymwdhMs(const std::string& expr)
{ {

View File

@ -34,7 +34,6 @@
#include <algorithm> #include <algorithm>
#include <numeric> #include <numeric>
#include "mu-utils-format.hh"
#include "mu-option.hh" #include "mu-option.hh"
@ -375,6 +374,17 @@ std::string size_to_string(int64_t size);
*/ */
std::string summarize(const std::string& str, size_t max_lines); std::string summarize(const std::string& str, size_t max_lines);
/**
* Quote & escape a string for " and \
*
* @param str a string
*
* @return quoted string
*/
std::string quote(const std::string& str);
/** /**
* Convert any ostreamable<< value to a string * Convert any ostreamable<< value to a string
* *

View File

@ -195,8 +195,8 @@ test_clean()
static void static void
test_format() test_format()
{ {
g_assert_true(format("hello %s", "world") == "hello world"); g_assert_true(mu_format("hello {}", "world") == "hello world");
g_assert_true(format("hello %s, %u", "world", 123) == "hello world, 123"); g_assert_true(mu_format("hello {}, {}", "world", 123) == "hello world, 123");
} }
static void static void

View File

@ -39,21 +39,22 @@ using namespace Mu;
static std::string static std::string
fill_contacts_cache(const std::string& path) fill_contacts_cache(const std::string& path)
{ {
auto cmdline = format("/bin/sh -c '" auto cmdline = mu_format("/bin/sh -c '"
"%s --quiet init --muhome=%s --maildir=%s ; " "{} --quiet init --muhome={} --maildir={} ; "
"%s --quiet index --muhome=%s '", "{} --quiet index --muhome={} '",
MU_PROGRAM, MU_PROGRAM,
path.c_str(), path,
MU_TESTMAILDIR, MU_TESTMAILDIR,
MU_PROGRAM, MU_PROGRAM,
path.c_str()); path);
if (g_test_verbose()) if (g_test_verbose())
g_print("%s\n", cmdline.c_str()); mu_println("{}", cmdline);
GError *err{}; GError *err{};
if (!g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, &err)) { if (!g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, &err)) {
g_printerr("Error: %s\n", err ? err->message : "?"); mu_printerrln("Error: {}", err ? err->message : "?");
g_clear_error(&err);
g_assert(0); g_assert(0);
} }

View File

@ -45,27 +45,24 @@ static std::string DBPATH; /* global */
static void static void
fill_database(void) fill_database(void)
{ {
gchar * cmdline; GError* err{};
GError* err; auto&& cmdline = mu_format("/bin/sh -c '"
"{} --quiet init --muhome={} --maildir={} ; "
cmdline = g_strdup_printf("/bin/sh -c '" "{} --quiet index --muhome={} '",
"%s --quiet init --muhome=%s --maildir=%s ; "
"%s --quiet index --muhome=%s '",
MU_PROGRAM, MU_PROGRAM,
DBPATH.c_str(), DBPATH,
MU_TESTMAILDIR2, MU_TESTMAILDIR2,
MU_PROGRAM, MU_PROGRAM,
DBPATH.c_str()); DBPATH);
if (g_test_verbose()) if (g_test_verbose())
g_print("%s\n", cmdline); mu_println("{}", cmdline);
err = NULL; err = NULL;
if (!g_spawn_command_line_sync(cmdline, NULL, NULL, NULL, &err)) { if (!g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, &err)) {
g_printerr("Error: %s\n", err ? err->message : "?"); mu_printerrln("Error: {}", err ? err->message : "?");
g_clear_error(&err);
g_assert(0); g_assert(0);
} }
g_free(cmdline);
} }
static unsigned static unsigned
@ -222,10 +219,10 @@ test_mu_find_links(void)
{ {
char *output, *erroutput; char *output, *erroutput;
const auto tmpdir{test_random_tmpdir()}; const auto tmpdir{test_random_tmpdir()};
auto cmdline = format("%s find --muhome=%s --format=links --linksdir=%s " auto cmdline = mu_format("{} find --muhome={} --format=links --linksdir={} "
"mime:message/rfc822", MU_PROGRAM, DBPATH.c_str(), tmpdir.c_str()); "mime:message/rfc822", MU_PROGRAM, DBPATH, tmpdir);
if (g_test_verbose()) if (g_test_verbose())
g_print("cmdline: %s\n", cmdline.c_str()); mu_println("cmdline: %s", cmdline);
g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL)); g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL));
/* there should be no errors */ /* there should be no errors */
@ -236,9 +233,8 @@ test_mu_find_links(void)
output = erroutput = NULL; output = erroutput = NULL;
/* furthermore, two symlinks should be there */ /* furthermore, two symlinks should be there */
const auto f1{format("%s/cur/rfc822.1", tmpdir.c_str())}; const auto f1{mu_format("{}/cur/rfc822.1", tmpdir)};
const auto f2{format("%s/cur/rfc822.2", tmpdir.c_str())}; const auto f2{mu_format("{}/cur/rfc822.2", tmpdir)};
g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK); g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK);
g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK); g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK);
@ -247,7 +243,7 @@ test_mu_find_links(void)
* when we find the first target file already exists */ * when we find the first target file already exists */
if (g_test_verbose()) if (g_test_verbose())
g_print("cmdline: %s\n", cmdline.c_str()); mu_println("cmdline: {}", cmdline);
g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL)); g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL));
g_assert_cmpuint(newlines_in_output(output), ==, 0); g_assert_cmpuint(newlines_in_output(output), ==, 0);
@ -258,12 +254,12 @@ test_mu_find_links(void)
/* now we try again with --clearlinks, and the we should be /* now we try again with --clearlinks, and the we should be
* back to 0 errors */ * back to 0 errors */
cmdline = format("%s find --muhome=%s --format=links --linksdir=%s --clearlinks " cmdline = mu_format("{} find --muhome={} --format=links --linksdir={} --clearlinks "
"mime:message/rfc822", MU_PROGRAM, DBPATH.c_str(), tmpdir.c_str()); "mime:message/rfc822", MU_PROGRAM, DBPATH, tmpdir);
g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL)); g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL));
if (g_test_verbose()) if (g_test_verbose())
g_print("cmdline: %s\n", cmdline.c_str()); mu_println("cmdline: {}", cmdline);
g_assert_cmpuint(newlines_in_output(output), ==, 0); g_assert_cmpuint(newlines_in_output(output), ==, 0);
g_assert_cmpuint(newlines_in_output(erroutput), ==, 0); g_assert_cmpuint(newlines_in_output(erroutput), ==, 0);
g_free(output); g_free(output);