From c904108f4d76643a60fa654e90a93fc6318fd6d9 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 28 Oct 2020 10:52:16 +0100 Subject: [PATCH] Use fmt::format for string formatting where it improves clarity. --- src/files.cpp | 18 +++++++++++------- src/generators/rss.cpp | 29 +++++++++++++++-------------- src/git.cpp | 10 ++++++---- src/gitea.cpp | 24 +++++++++++++----------- src/time.cpp | 3 +-- 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/files.cpp b/src/files.cpp index 80b0151..21f9512 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -19,6 +19,8 @@ #include "json.hpp" #include "types.hpp" +#include + #include #include #include @@ -32,6 +34,7 @@ namespace FediBlock::files { +using fmt::format; using std::getenv; using std::ifstream; using std::ofstream; @@ -70,7 +73,7 @@ void remove_tmpdir() fs::path get_xdg_dir(const string &identifier) { - const string xdg_variable{("XDG_" + identifier) += "_HOME"}; + const string xdg_variable{format("XDG_{:s}_HOME", identifier)}; char *env{getenv(xdg_variable.c_str())}; if (env != nullptr) { @@ -133,11 +136,12 @@ fs::path get_cachedir() string get_access_token() { - ifstream file(get_datadir() / "gitea_access_token"); + const fs::path access_token_path{get_datadir() / "gitea_access_token"}; + ifstream file(access_token_path); if (!file.good()) { - throw runtime_error{"Could not read access token: " - + (get_datadir() / "gitea_access_token").string()}; + throw runtime_error{format("Could not read access token: {:s}", + access_token_path.string())}; } stringstream ss; ss << file.rdbuf(); @@ -199,7 +203,7 @@ vector read_json_files(const bool cache) fs::path get_lockfile() { #if defined(__linux__) - fs::path path{"/run/user/" + to_string(getuid())}; // pam_systemd + fs::path path{format("/run/user/{:d}", getuid())}; // pam_systemd if (!fs::exists(path)) { path = "/run/lock"; // Linux @@ -236,8 +240,8 @@ bool create_lockfile() } catch (const std::ofstream::failure &e) { - const auto msg{string("Could not write lockfile: ") += e.what()}; - throw std::runtime_error{msg.c_str()}; + throw std::runtime_error{ + format("Could not write lockfile: {:s}", e.what())}; } return true; diff --git a/src/generators/rss.cpp b/src/generators/rss.cpp index 1968245..3e20a7e 100644 --- a/src/generators/rss.cpp +++ b/src/generators/rss.cpp @@ -20,6 +20,8 @@ #include "git.hpp" #include "time.hpp" +#include + #include #include #include @@ -35,6 +37,7 @@ namespace FediBlock::rss { +using fmt::format; using std::find; using std::getenv; using std::none_of; @@ -149,11 +152,11 @@ void write_rss(ostream &out, const vector &entries, "FediBlock: " + entry.report_time + " " + entry.instance); write_line(out, 6, "pubDate", time::to_string(entry.report_time, rss_time_format)); - write_line(out, 6, "link", baseurl + '#' += entry.instance); + write_line(out, 6, "link", + format("{:s}#{:s}", baseurl, entry.instance)); - string item_description{"

" + cgi::text2html(entry.description) - + "

"}; - item_description += "

Tags: "; + string item_description{format("

{:s}

Tags: ", + cgi::text2html(entry.description))}; for (const auto &tag : entry.tags) { if (tag != *entry.tags.begin()) @@ -162,25 +165,23 @@ void write_rss(ostream &out, const vector &entries, } item_description += tag; } - item_description += "

"; - item_description += "Receipts:
    "; + item_description += "

    Receipts:
      "; for (const auto &receipt : entry.receipts) { - ((((item_description += "
    • ") += - receipt) += "
    • "; + item_description += + format("
    • {0:s}
    • ", receipt); } item_description += "
    "; if (!entry.screenshot_filepath.empty()) { item_description += "

    Screenshot:
    "; - ((item_description += "

    )") += '\n'; + item_description += + format("

    \n", + baseurl, entry.screenshot_filepath); } write_line(out, 6, "description", - ""); + format("", item_description)); out << " \n"; } diff --git a/src/git.cpp b/src/git.cpp index cb6e168..c1e0c5a 100644 --- a/src/git.cpp +++ b/src/git.cpp @@ -21,6 +21,7 @@ #include "gitea.hpp" #include "json.hpp" +#include #include #include @@ -46,6 +47,7 @@ namespace FediBlock::git { +using fmt::format; using std::ofstream; using std::runtime_error; using std::string; @@ -57,9 +59,8 @@ using std::this_thread::sleep_for; using namespace std::chrono_literals; git_repository *_repo{nullptr}; -const string _clone_url{ - (((((string{"git@"} += config::forge_domain) += ":") += - config::forge_org) += "/") += config::forge_repo_data) += ".git"}; +const string _clone_url{format("git@{:s}:{:s}/{:s}.git", config::forge_domain, + config::forge_org, config::forge_repo_data)}; fs::path _repo_dir{}; void init(const bool cache) @@ -145,7 +146,8 @@ void commit(const entry_type &entry) ofstream file(basename + ".json"); if (!file.good()) { - throw runtime_error{"Could not create file: " + basename + ".json"}; + throw runtime_error{ + format("Could not create file: {:s}.json", basename)}; } file << json::to_json(entry); file.close(); diff --git a/src/gitea.cpp b/src/gitea.cpp index 800e079..8962925 100644 --- a/src/gitea.cpp +++ b/src/gitea.cpp @@ -22,6 +22,7 @@ #include "types.hpp" #include +#include #include #include @@ -31,6 +32,7 @@ namespace FediBlock::gitea { +using fmt::format; using std::runtime_error; using std::string; using std::string_view; @@ -91,7 +93,7 @@ string api_request(const http_method method, const string_view path, const string_view body) { CURLcode code{}; - string url{(string("https://") += config::forge_domain) += path}; + string url{format("https://{:s}{:s}", config::forge_domain, path)}; _curl_buffer_body.clear(); switch (method) @@ -122,13 +124,13 @@ string api_request(const http_method method, const string_view path, code = curl_easy_setopt(_connection, CURLOPT_URL, url.c_str()); if (code != CURLE_OK) { - throw runtime_error{"Couldn't set URL: " + to_string(code)}; + throw runtime_error{format("Couldn't set URL: {:d}", code)}; } code = curl_easy_perform(_connection); if (code != CURLE_OK) { - throw runtime_error{"libcurl error: " + to_string(code)}; + throw runtime_error{format("libcurl error: {:d}", code)}; } long http_status{0}; // NOLINT(google-runtime-int) @@ -136,7 +138,7 @@ string api_request(const http_method method, const string_view path, curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status); if (http_status < 200 || http_status >= 300) { - throw runtime_error{"HTTP error: " + to_string(http_status)}; + throw runtime_error{format("HTTP error: {:d}", http_status)}; } return _curl_buffer_body; @@ -145,8 +147,8 @@ string api_request(const http_method method, const string_view path, void pull_request(const string_view branch, const entry_type &entry) { api_request(http_method::POST, - (((string("/api/v1/repos/") += config::forge_org) += "/") += - config::forge_repo_data) += "/pulls", + format("/api/v1/repos/{:s}/{:s}/pulls", config::forge_org, + config::forge_repo_data), json::pull_request_body(branch, entry)); ++_last_pr_number; } @@ -155,11 +157,11 @@ uint64_t get_last_pr_number() { if (_last_pr_number == 0) { - const auto answer{api_request( - http_method::GET, - (((string("/api/v1/repos/") += config::forge_org) += "/") += - config::forge_repo_data) += "/pulls", - "?sort=newest&limit=1")}; + const auto answer{ + api_request(http_method::GET, + format("/api/v1/repos/{:s}/{:s}/pulls", + config::forge_org, config::forge_repo_data), + "?sort=newest&limit=1")}; if (answer == "[]") { return 0; diff --git a/src/time.cpp b/src/time.cpp index 6da00a0..7d3b06b 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -54,8 +54,7 @@ string to_string(const string_view timestring, const string_view format) std::tm tm{}; tm.tm_isdst = -1; // Detect daylight saving time. stringstream ss(timestring.data()); - ss >> get_time(&tm, - "%Y-%m-%dT%T"); // Assume time is UTC (%F doesn't work). + ss >> get_time(&tm, "%Y-%m-%dT%T"); // Assume time is UTC (%F doesn't work). return to_string(system_clock::from_time_t(timegm(&tm)), format); }