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

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

View File

@ -216,10 +216,6 @@ struct Sexp {
/// Adding list elements
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
Sexp& front() { return list().front(); }
const Sexp& front() const { return list().front(); }
@ -234,11 +230,6 @@ struct Sexp {
bool head_symbolp(const Symbol& sym) const {
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)

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 "mu-utils.hh"
#include "mu-utils-format.hh"
#include "mu-error.hh"
#include "mu-option.hh"
@ -306,34 +305,6 @@ Mu::quote(const std::string& str)
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>
delta_ymwdhMs(const std::string& expr)
{

View File

@ -34,7 +34,6 @@
#include <algorithm>
#include <numeric>
#include "mu-utils-format.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);
/**
* 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
*

View File

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