Skip to content

Commit

Permalink
Remove fmt tasks switch from fmt to std::format internally.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed May 25, 2024
1 parent b0d26f3 commit 4db6ff4
Show file tree
Hide file tree
Showing 30 changed files with 96 additions and 11,242 deletions.
17 changes: 7 additions & 10 deletions src/cmd/pr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ namespace mob {
if (!task)
return 1;

u8cout << "checking out pr " << pr.number << " "
<< "in " << task->name() << "\n";
u8cout << "checking out pr " << pr.number << " " << "in "
<< task->name() << "\n";

git_wrap g(task->source_path());

g.fetch(task->git_url().string(),
fmt::format("pull/{}/head", pr.number));
std::format("pull/{}/head", pr.number));

g.checkout("FETCH_HEAD");
}
Expand Down Expand Up @@ -208,13 +208,10 @@ namespace mob {
{
nlohmann::json json;

constexpr auto* pattern =
"https://api.github.com/search/issues?per_page=100&q="
"is:pr+org:{org:}+author:{author:}+is:open+head:{branch:}";
constexpr auto* pattern = "https://api.github.com/search/issues?per_page=100&q="
"is:pr+org:{0:}+author:{1:}+is:open+head:{2:}";

const auto search_url =
fmt::format(pattern, fmt::arg("org", org), fmt::arg("author", author),
fmt::arg("branch", branch));
const auto search_url = std::format(pattern, org, author, branch);

u8cout << "search url is " << search_url << "\n";

Expand Down Expand Up @@ -275,7 +272,7 @@ namespace mob {
return {};
}

const url u(fmt::format("https://api.github.com/repos/{}/{}/pulls/{}",
const url u(std::format("https://api.github.com/repos/{}/{}/pulls/{}",
task->org(), task->repo(), pr));

curl_downloader dl;
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/release.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ namespace mob {
}

const auto q =
fmt::format("prefix {} already exists, delete?", path_to_utf8(prefix));
std::format("prefix {} already exists, delete?", path_to_utf8(prefix));

if (ask_yes_no(q, yn::no) != yn::yes)
return false;
Expand Down Expand Up @@ -489,7 +489,7 @@ namespace mob {
const auto* lcp = static_cast<LANGANDCODEPAGE*>(value_pointer);

const auto sub_block =
fmt::format(L"\\StringFileInfo\\{:04x}{:04x}\\FileVersion", lcp->wLanguage,
std::format(L"\\StringFileInfo\\{:04x}{:04x}\\FileVersion", lcp->wLanguage,
lcp->wCodePage);

ret = VerQueryValueW(buffer.get(), sub_block.c_str(), &value_pointer,
Expand Down
19 changes: 0 additions & 19 deletions src/core/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@
#include "../utility.h"
#include "conf.h"

namespace mob::details {

std::string converter<std::wstring>::convert(const std::wstring& s)
{
return utf16_to_utf8(s);
}

std::string converter<fs::path>::convert(const fs::path& s)
{
return utf16_to_utf8(s.native());
}

std::string converter<url>::convert(const url& u)
{
return u.string();
}

} // namespace mob::details

namespace mob {

// timestamps are relative to this
Expand Down
66 changes: 14 additions & 52 deletions src/core/context.h
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
#pragma once

#include "../utility.h"

// T to std::string converters
//
// those are kept in this namespace so they don't leak all over the place;
// they're used directly by context::do_log() below

namespace mob::details {

class mob::url;

template <class T, class = void>
struct converter {
static const T& convert(const T& t) { return t; }
};

template <>
struct converter<std::wstring> {
static std::string convert(const std::wstring& s);
};

template <>
struct converter<fs::path> {
static std::string convert(const fs::path& s);
};

template <>
struct converter<url> {
static std::string convert(const url& u);
};

template <class T>
struct converter<T, std::enable_if_t<std::is_enum_v<T>>> {
static std::string convert(T e)
{
return std::to_string(static_cast<std::underlying_type_t<T>>(e));
}
};

} // namespace mob::details
#include "./formatters.h"

namespace mob {

Expand All @@ -57,7 +19,7 @@ namespace mob {
// in places where there is no context available, there's a global one can that
// be retrieved with gcx() for logging
//
// all log functions will use fmt::format() internally, so they can be used
// all log functions will use std::format() internally, so they can be used
// like:
//
// cx.log(context::generic, "eat more {}", "potatoes");
Expand Down Expand Up @@ -161,47 +123,47 @@ namespace mob {
// logs a formatted string with the dump level
//
template <class... Args>
void dump(reason r, const char* f, Args&&... args) const
void dump(reason r, std::format_string<Args...> f, Args&&... args) const
{
do_log(false, r, level::dump, f, std::forward<Args>(args)...);
}

// logs a formatted string with the trace level
//
template <class... Args>
void trace(reason r, const char* f, Args&&... args) const
void trace(reason r, std::format_string<Args...> f, Args&&... args) const
{
do_log(false, r, level::trace, f, std::forward<Args>(args)...);
}

// logs a formatted string with the debug level
//
template <class... Args>
void debug(reason r, const char* f, Args&&... args) const
void debug(reason r, std::format_string<Args...> f, Args&&... args) const
{
do_log(false, r, level::debug, f, std::forward<Args>(args)...);
}

// logs a formatted string with the info level
//
template <class... Args>
void info(reason r, const char* f, Args&&... args) const
void info(reason r, std::format_string<Args...> f, Args&&... args) const
{
do_log(false, r, level::info, f, std::forward<Args>(args)...);
}

// logs a formatted string with the warning level
//
template <class... Args>
void warning(reason r, const char* f, Args&&... args) const
void warning(reason r, std::format_string<Args...> f, Args&&... args) const
{
do_log(false, r, level::warning, f, std::forward<Args>(args)...);
}

// logs a formatted string with the error level
//
template <class... Args>
void error(reason r, const char* f, Args&&... args) const
void error(reason r, std::format_string<Args...> f, Args&&... args) const
{
do_log(false, r, level::error, f, std::forward<Args>(args)...);
}
Expand All @@ -211,7 +173,8 @@ namespace mob {
// tasks
//
template <class... Args>
[[noreturn]] void bail_out(reason r, const char* f, Args&&... args) const
[[noreturn]] void bail_out(reason r, std::format_string<Args...> f,
Args&&... args) const
{
do_log(true, r, level::error, f, std::forward<Args>(args)...);
}
Expand All @@ -227,17 +190,16 @@ namespace mob {
// bailed exception after logging
//
template <class... Args>
void do_log(bool bail, reason r, level lv, const char* f, Args&&... args) const
void do_log(bool bail, reason r, level lv, std::format_string<Args...> f,
Args&&... args) const
{
// discard log if it's not enabled and it's not bailing out
if (!bail && !enabled(lv))
return;

try {
// formatting string
const std::string s =
fmt::format(f, details::converter<std::decay_t<Args>>::convert(
std::forward<Args>(args))...);
const std::string s = std::format(f, std::forward<Args>(args)...);

do_log_impl(bail, r, lv, s);
}
Expand All @@ -253,7 +215,7 @@ namespace mob {
// a ghetto conversion and hope it gives enough info
std::wstring s;

const char* p = f;
const char* p = f.get().data();
while (*p) {
s += (wchar_t)*p;
++p;
Expand Down
35 changes: 35 additions & 0 deletions src/core/formatters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "../utility/string.h"

template <>
struct std::formatter<std::wstring, char> : std::formatter<std::string, char> {
template <class FmtContext>
FmtContext::iterator format(std::wstring const& s, FmtContext& ctx) const
{
return std::formatter<std::string, char>::format(mob::utf16_to_utf8(s), ctx);
}
};

template <>
struct std::formatter<std::filesystem::path, char>
: std::formatter<std::basic_string<std::filesystem::path::value_type>, char> {
template <class FmtContext>
FmtContext::iterator format(std::filesystem::path const& s, FmtContext& ctx) const
{
return std::formatter<std::basic_string<std::filesystem::path::value_type>,
char>::format(s.native(), ctx);
}
};

template <class Enum, class CharT>
requires std::is_enum_v<Enum>
struct std::formatter<Enum, CharT>
: std::formatter<std::underlying_type_t<Enum>, CharT> {
template <class FmtContext>
FmtContext::iterator format(Enum v, FmtContext& ctx) const
{
return std::formatter<std::underlying_type_t<Enum>, CharT>::format(
static_cast<std::underlying_type_t<Enum>>(v), ctx);
}
};
4 changes: 2 additions & 2 deletions src/core/ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
namespace mob {

template <class... Args>
void ini_error(const ini_data& ini, std::size_t line, std::string_view f,
void ini_error(const ini_data& ini, std::size_t line, std::format_string<Args...> f,
Args&&... args)
{
gcx().bail_out(context::conf, "{}:{}: {}", path_to_utf8(ini.path), (line + 1),
fmt::format(f, std::forward<Args>(args)...));
std::format(f, std::forward<Args>(args)...));
}

ini_data::kv_map& ini_data::get_section(std::string_view name)
Expand Down
2 changes: 1 addition & 1 deletion src/core/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ namespace mob {

// check known installation paths for a bunch of versions
for (int v : {5, 6, 7, 8}) {
const fs::path inno_dir = fmt::format("Inno Setup {}", v);
const fs::path inno_dir = std::format("Inno Setup {}", v);

// check for both architectures
for (fs::path pf : {conf().path().pf_x86(), conf().path().pf_x64()}) {
Expand Down
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace mob {
add_task<parallel_tasks>()
.add_task<sevenz>()
.add_task<zlib>()
.add_task<fmt>()
.add_task<gtest>()
.add_task<libbsarch>()
.add_task<libloot>()
Expand Down
9 changes: 9 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ namespace mob {
};

} // namespace mob

template <>
struct std::formatter<mob::url, char> : std::formatter<std::string, char> {
template <class FmtContext>
FmtContext::iterator format(mob::url const& u, FmtContext& ctx) const
{
return std::formatter<std::string, char>::format(u.string(), ctx);
}
};
1 change: 0 additions & 1 deletion src/pch.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#include "pch.h"
#include <fmt/format.cc>
2 changes: 1 addition & 1 deletion src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <atomic>
#include <charconv>
#include <filesystem>
#include <format>
#include <fstream>
#include <functional>
#include <iostream>
Expand All @@ -69,7 +70,6 @@

#include <clipp.h>
#include <curl/curl.h>
#include <fmt/format.h>
#include <nlohmann/json.hpp>

#pragma warning(pop)
Expand Down
Loading

0 comments on commit 4db6ff4

Please sign in to comment.