refactor: add fmt::formatters (#2829)

* refactor: add fmt::formatters
This commit is contained in:
Charles Kerr
2022-03-27 21:55:30 -05:00
committed by GitHub
parent 71ad42b1ef
commit 9d141ee2b1
10 changed files with 190 additions and 113 deletions

View File

@@ -59,6 +59,7 @@
62F644738FE3D8788EBF73A9 /* block-info.cc in Sources */ = {isa = PBXBuildFile; fileRef = A54D44C6A7AAF131D9AE29F5 /* block-info.cc */; };
66F977825E65AD498C028BB0 /* announce-list.cc in Sources */ = {isa = PBXBuildFile; fileRef = 66F977825E65AD498C028BB1 /* announce-list.cc */; };
66F977825E65AD498C028BB2 /* announce-list.h in Headers */ = {isa = PBXBuildFile; fileRef = 66F977825E65AD498C028BB3 /* announce-list.h */; };
888A256631B3DE536FEB8B00 /* tr-strbuf.h in Headers */ = {isa = PBXBuildFile; fileRef = 888A256631B3DE536FEB8B01 /* tr-strbuf.h */; };
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
8D11072D0486CEB800E47090 /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.mm */; settings = {ATTRIBUTES = (); }; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
@@ -344,7 +345,6 @@
C1305EBE186A13B100F03351 /* file.cc in Sources */ = {isa = PBXBuildFile; fileRef = C1305EB8186A134000F03351 /* file.cc */; };
C1425B361EE9C605001DB85F /* tr-assert.h in Headers */ = {isa = PBXBuildFile; fileRef = C1425B331EE9C5EA001DB85F /* tr-assert.h */; };
C1425B371EE9C705001DB85F /* tr-macros.h in Headers */ = {isa = PBXBuildFile; fileRef = C1425B341EE9C5EA001DB85F /* tr-macros.h */; };
888A256631B3DE536FEB8B00 /* tr-strbuf.h in Headers */ = {isa = PBXBuildFile; fileRef = 888A256631B3DE536FEB8B01 /* tr-strbuf.h */; };
C1425B381EE9C805001DB85F /* peer-socket.h in Headers */ = {isa = PBXBuildFile; fileRef = C1425B351EE9C5EA001DB85F /* peer-socket.h */; };
C1639A741A55F4E000E42033 /* libb64.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C1639A6F1A55F4D600E42033 /* libb64.a */; };
C1639A781A55F56600E42033 /* cdecode.c in Sources */ = {isa = PBXBuildFile; fileRef = C1639A761A55F56600E42033 /* cdecode.c */; };
@@ -617,6 +617,7 @@
66F977825E65AD498C028BB1 /* announce-list.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "announce-list.cc"; sourceTree = "<group>"; };
66F977825E65AD498C028BB3 /* announce-list.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "announce-list.h"; sourceTree = "<group>"; };
6A044CBD8C049AFCBD4DB411 /* block-info.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "block-info.h"; sourceTree = SOURCE_ROOT; };
888A256631B3DE536FEB8B01 /* tr-strbuf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "tr-strbuf.h"; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* Transmission.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Transmission.app; sourceTree = BUILT_PRODUCTS_DIR; };
A200B8390A2263BA007BBB1E /* InfoWindowController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InfoWindowController.h; sourceTree = "<group>"; };
@@ -1071,7 +1072,6 @@
C1425B321EE9C5EA001DB85F /* tr-assert.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "tr-assert.cc"; sourceTree = "<group>"; };
C1425B331EE9C5EA001DB85F /* tr-assert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "tr-assert.h"; sourceTree = "<group>"; };
C1425B341EE9C5EA001DB85F /* tr-macros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "tr-macros.h"; sourceTree = "<group>"; };
888A256631B3DE536FEB8B01 /* tr-strbuf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "tr-macros.h"; sourceTree = "<group>"; };
C1425B351EE9C5EA001DB85F /* peer-socket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "peer-socket.h"; sourceTree = "<group>"; };
C1639A6F1A55F4D600E42033 /* libb64.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libb64.a; sourceTree = BUILT_PRODUCTS_DIR; };
C1639A761A55F56600E42033 /* cdecode.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = cdecode.c; path = src/cdecode.c; sourceTree = "<group>"; };
@@ -3393,17 +3393,14 @@
"third-party/arc4/src",
"third-party/dht",
"third-party/libb64/include",
"third-party/libevent/include",
"third-party/libutp",
"third-party/utfcpp/source",
"third-party/fmt/include",
"third-party/libdeflate",
"third-party/libpsl/include",
);
OTHER_CFLAGS = (
"$(inherited)",
"-DWITH_UTP",
"-DFMT_HEADER_ONLY",
"-D__TRANSMISSION__",
"-DHAVE_FLOCK",
"-DHAVE_STRLCPY",
@@ -3412,7 +3409,9 @@
"-DHAVE_ZLIB",
"-DHAVE_ICONV",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
PRODUCT_NAME = transmission;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
USER_HEADER_SEARCH_PATHS = "$(inherited) third-party/libnatpmp third-party/miniupnpc";
};
name = Debug;
@@ -3432,8 +3431,11 @@
.,
);
LD_RUNPATH_SEARCH_PATHS = "@executable_path/../Frameworks";
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = Transmission;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include";
WRAPPER_EXTENSION = app;
};
name = Debug;
@@ -3447,10 +3449,12 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = transmissioncli;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = Debug;
};
@@ -3463,16 +3467,15 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
"third-party/fmt/include",
);
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
"-DHAVE_DAEMON",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = "transmission-daemon";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = Debug;
};
@@ -3485,15 +3488,12 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
"third-party/fmt/include",
);
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = "transmission-remote";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = Debug;
};
@@ -3553,6 +3553,11 @@
INFOPLIST_PREPROCESS = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
SDKROOT = macosx;
STRIP_INSTALLED_PRODUCT = NO;
};
@@ -3596,17 +3601,14 @@
"third-party/arc4/src",
"third-party/dht",
"third-party/libb64/include",
"third-party/libevent/include",
"third-party/libutp",
"third-party/utfcpp/source",
"third-party/fmt/include",
"third-party/libdeflate",
"third-party/libpsl/include",
);
OTHER_CFLAGS = (
"$(inherited)",
"-DWITH_UTP",
"-DFMT_HEADER_ONLY",
"-D__TRANSMISSION__",
"-DHAVE_FLOCK",
"-DHAVE_STRLCPY",
@@ -3615,7 +3617,9 @@
"-DHAVE_ZLIB",
"-DHAVE_ICONV",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
PRODUCT_NAME = transmission;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
USER_HEADER_SEARCH_PATHS = "$(inherited) third-party/libnatpmp third-party/miniupnpc";
};
name = Release;
@@ -3629,10 +3633,12 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = transmissioncli;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = Release;
};
@@ -3651,8 +3657,11 @@
.,
);
LD_RUNPATH_SEARCH_PATHS = "@executable_path/../Frameworks";
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = Transmission;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include";
WRAPPER_EXTENSION = app;
};
name = Release;
@@ -3711,7 +3720,12 @@
INFOPLIST_PREFIX_HEADER = libtransmission/version.h;
INFOPLIST_PREPROCESS = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
OTHER_CFLAGS = "-DNDEBUG";
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
"-DNDEBUG",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
SDKROOT = macosx;
};
name = Release;
@@ -3720,6 +3734,8 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = NO;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
PRODUCT_NAME = dht;
};
name = Debug;
@@ -3728,6 +3744,8 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = NO;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
PRODUCT_NAME = dht;
};
name = "Release - Debug";
@@ -3736,6 +3754,8 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = NO;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
PRODUCT_NAME = dht;
};
name = Release;
@@ -3796,6 +3816,11 @@
INFOPLIST_PREPROCESS = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
SDKROOT = macosx;
STRIP_INSTALLED_PRODUCT = NO;
};
@@ -3816,8 +3841,11 @@
.,
);
LD_RUNPATH_SEARCH_PATHS = "@executable_path/../Frameworks";
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = Transmission;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include";
WRAPPER_EXTENSION = app;
};
name = "Release - Debug";
@@ -3831,10 +3859,12 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = transmissioncli;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = "Release - Debug";
};
@@ -3849,17 +3879,14 @@
"third-party/arc4/src",
"third-party/dht",
"third-party/libb64/include",
"third-party/libevent/include",
"third-party/libutp",
"third-party/utfcpp/source",
"third-party/fmt/include",
"third-party/libdeflate",
"third-party/libpsl/include",
);
OTHER_CFLAGS = (
"$(inherited)",
"-DWITH_UTP",
"-DFMT_HEADER_ONLY",
"-D__TRANSMISSION__",
"-DHAVE_FLOCK",
"-DHAVE_STRLCPY",
@@ -3868,7 +3895,9 @@
"-DHAVE_ZLIB",
"-DHAVE_ICONV",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
PRODUCT_NAME = transmission;
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
USER_HEADER_SEARCH_PATHS = "$(inherited) third-party/libnatpmp third-party/miniupnpc";
};
name = "Release - Debug";
@@ -3882,16 +3911,15 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
"third-party/fmt/include",
);
OTHER_CFLAGS = (
"$(inherited)",
"-DHAVE_DAEMON",
"-DFMT_HEADER_ONLY",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = "transmission-daemon";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = "Release - Debug";
};
@@ -3904,15 +3932,12 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
"third-party/fmt/include",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = "-lc++";
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
);
PRODUCT_NAME = "transmission-remote";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = "Release - Debug";
};
@@ -3969,8 +3994,11 @@
);
INFOPLIST_FILE = "macosx/QuickLookPlugin/QuickLookPlugin-Info.plist";
INSTALL_PATH = /Library/QuickLook;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = "$(TARGET_NAME)";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include";
WRAPPER_EXTENSION = qlgenerator;
};
name = Debug;
@@ -3988,8 +4016,11 @@
);
INFOPLIST_FILE = "macosx/QuickLookPlugin/QuickLookPlugin-Info.plist";
INSTALL_PATH = /Library/QuickLook;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = "$(TARGET_NAME)";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include";
WRAPPER_EXTENSION = qlgenerator;
};
name = "Release - Debug";
@@ -4007,8 +4038,11 @@
);
INFOPLIST_FILE = "macosx/QuickLookPlugin/QuickLookPlugin-Info.plist";
INSTALL_PATH = /Library/QuickLook;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = "$(TARGET_NAME)";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include";
WRAPPER_EXTENSION = qlgenerator;
};
name = Release;
@@ -4056,16 +4090,15 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
"third-party/fmt/include",
);
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
"-DHAVE_DAEMON",
);
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "-lc++";
PRODUCT_NAME = "transmission-daemon";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = Release;
};
@@ -4078,15 +4111,12 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
.,
"third-party/libevent/include",
"third-party/fmt/include",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = "-lc++";
OTHER_CFLAGS = (
"$(inherited)",
"-DFMT_HEADER_ONLY",
);
PRODUCT_NAME = "transmission-remote";
SYSTEM_HEADER_SEARCH_PATHS = "$(inherited) third-party/fmt/include third-party/libevent/include";
};
name = Release;
};

View File

@@ -468,7 +468,7 @@ static void tau_tracker_on_dns(int errcode, struct evutil_addrinfo* addr, void*
{
auto const errmsg = fmt::format(
_("Couldn't find address of tracker '{host}': {error} ({error_code})"),
fmt::arg("host", tracker->host.sv()),
fmt::arg("host", tracker->host),
fmt::arg("error", evutil_gai_strerror(errcode)),
fmt::arg("error_code", errcode));
logwarn(tracker->key, errmsg);
@@ -729,8 +729,7 @@ static struct tr_announcer_udp* announcer_udp_get(tr_session* session)
static tau_tracker* tau_session_get_tracker(tr_announcer_udp* tau, tr_interned_string announce_url)
{
// build a lookup key for this tracker
auto const announce_sv = announce_url.sv();
auto parsed = tr_urlParseTracker(announce_sv);
auto const parsed = tr_urlParseTracker(announce_url);
TR_ASSERT(parsed);
if (!parsed)
{

View File

@@ -7,6 +7,8 @@
#include <string_view>
#include <fmt/format.h>
#include "quark.h"
/**
@@ -44,7 +46,7 @@ public:
return *this = std::string_view{ c_str != nullptr ? c_str : "" };
}
[[nodiscard]] tr_quark quark() const
[[nodiscard]] constexpr tr_quark quark() const noexcept
{
return quark_;
}
@@ -61,7 +63,7 @@ public:
{
return std::data(this->sv());
}
[[nodiscard]] auto empty() const
[[nodiscard]] constexpr auto empty() const noexcept
{
return quark_ == TR_KEY_NONE;
}
@@ -92,7 +94,7 @@ public:
return std::rend(this->sv());
}
[[nodiscard]] auto compare(tr_interned_string const& that) const // <=>
[[nodiscard]] constexpr auto compare(tr_interned_string const& that) const noexcept // <=>
{
if (this->quark() < that.quark())
{
@@ -107,21 +109,21 @@ public:
return 0;
}
[[nodiscard]] bool operator<(tr_interned_string const& that) const
[[nodiscard]] constexpr bool operator<(tr_interned_string const& that) const noexcept
{
return this->compare(that) < 0;
}
[[nodiscard]] bool operator>(tr_interned_string const& that) const
[[nodiscard]] constexpr bool operator>(tr_interned_string const& that) const noexcept
{
return this->compare(that) > 0;
}
[[nodiscard]] bool operator==(tr_interned_string const& that) const
[[nodiscard]] constexpr bool operator==(tr_interned_string const& that) const noexcept
{
return this->compare(that) == 0;
}
[[nodiscard]] bool operator!=(tr_interned_string const& that) const
[[nodiscard]] constexpr bool operator!=(tr_interned_string const& that) const noexcept
{
return this->compare(that) != 0;
}
@@ -143,6 +145,21 @@ public:
return *this != std::string_view{ that != nullptr ? that : "" };
}
operator std::string_view() const
{
return sv();
}
private:
tr_quark quark_ = TR_KEY_NONE;
};
template<>
struct fmt::formatter<tr_interned_string> : formatter<std::string_view>
{
template<typename FormatContext>
constexpr auto format(tr_interned_string const& str, FormatContext& ctx) const
{
return formatter<std::string_view>::format(str.sv(), ctx);
}
};

View File

@@ -1442,13 +1442,13 @@ static void onBlocklistFetched(tr_web::FetchResponse const& web_response)
// so save content into a tmpfile
auto const filename = tr_pathbuf{ session->config_dir, "/blocklist.tmp"sv };
tr_error* error = nullptr;
if (!tr_saveFile(filename.sv(), content, &error))
if (!tr_saveFile(filename, content, &error))
{
fmt::format_to_n(
result,
sizeof(result),
_("Couldn't save '{path}': {error} ({error_code})"),
fmt::arg("path", filename.sv()),
fmt::arg("path", filename),
fmt::arg("error", error->message),
fmt::arg("error_code", error->code));
tr_error_clear(&error);
@@ -1740,7 +1740,7 @@ static char const* groupGet(tr_session* s, tr_variant* args_in, tr_variant* args
{
tr_variant* dict = tr_variantListAddDict(list, 5);
auto limits = group->getLimits();
tr_variantDictAddStrView(dict, TR_KEY_name, name.sv());
tr_variantDictAddStr(dict, TR_KEY_name, name);
tr_variantDictAddBool(dict, TR_KEY_uploadLimited, limits.up_limited);
tr_variantDictAddInt(dict, TR_KEY_uploadLimit, limits.up_limit_KBps);
tr_variantDictAddBool(dict, TR_KEY_downloadLimited, limits.down_limited);

View File

@@ -1863,7 +1863,7 @@ void tr_torrent::recheckCompleteness()
if (this->currentDir() == this->incompleteDir())
{
this->setLocation(this->downloadDir().sv(), true, nullptr, nullptr);
this->setLocation(this->downloadDir(), true, nullptr, nullptr);
}
}
@@ -2414,7 +2414,7 @@ static void setLocationImpl(struct LocationData* const data)
tr_logAddTraceTor(
tor,
fmt::format("Moving '{}' location from currentDir '{}' to '{}'", tor->name(), tor->currentDir().sv(), location));
fmt::format("Moving '{}' location from currentDir '{}' to '{}'", tor->name(), tor->currentDir(), location));
tr_sys_dir_create(location.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);

View File

@@ -10,26 +10,32 @@
#include <fmt/format.h>
/**
* A memory buffer which uses a builtin array of N bytes,
* but falls back to heap allocation when necessary.
* Useful for building temp strings without heap allocation.
* A memory buffer which uses a builtin array of N bytes, using heap
* memory only if its string gets too big. Its main use case is building
* temporary strings in stack memory.
*
* `fmt::basic_memory_buffer` is final, so aggregate instead
* of subclassing ¯\_(ツ)_/¯
* It is a convenience wrapper arouund `fmt::basic_memory_buffer`.
*/
template<typename T, size_t N>
template<typename Char, size_t N>
class tr_strbuf
{
private:
fmt::basic_memory_buffer<T, N> buffer_;
fmt::basic_memory_buffer<Char, N> buffer_;
public:
using value_type = T;
using const_reference = const T&;
using value_type = Char;
using const_reference = const Char&;
tr_strbuf() = default;
tr_strbuf(tr_strbuf const& other) = delete;
tr_strbuf& operator=(tr_strbuf const& other) = delete;
auto& operator=(tr_strbuf&& other)
tr_strbuf(tr_strbuf&& other) noexcept
{
buffer_ = std::move(other.buffer_);
}
auto& operator=(tr_strbuf&& other) noexcept
{
buffer_ = std::move(other.buffer_);
return *this;
@@ -41,104 +47,104 @@ public:
append(args...);
}
[[nodiscard]] constexpr auto begin()
[[nodiscard]] constexpr auto begin() noexcept
{
return buffer_.begin();
}
[[nodiscard]] constexpr auto end()
[[nodiscard]] constexpr auto end() noexcept
{
return buffer_.end();
}
[[nodiscard]] constexpr auto begin() const
[[nodiscard]] constexpr auto begin() const noexcept
{
return buffer_.begin();
}
[[nodiscard]] constexpr auto end() const
[[nodiscard]] constexpr auto end() const noexcept
{
return buffer_.end();
}
[[nodiscard]] constexpr auto& operator[](size_t pos)
[[nodiscard]] constexpr Char& at(size_t pos) noexcept
{
return buffer_[pos];
}
[[nodiscard]] constexpr auto const& operator[](size_t pos) const
[[nodiscard]] constexpr Char at(size_t pos) const noexcept
{
return buffer_[pos];
}
[[nodiscard]] constexpr auto size() const
[[nodiscard]] constexpr auto size() const noexcept
{
return buffer_.size();
}
[[nodiscard]] constexpr bool empty() const
[[nodiscard]] constexpr bool empty() const noexcept
{
return size() == 0;
}
[[nodiscard]] constexpr auto* data()
[[nodiscard]] constexpr auto* data() noexcept
{
return buffer_.data();
}
[[nodiscard]] constexpr auto const* data() const
[[nodiscard]] constexpr auto const* data() const noexcept
{
return buffer_.data();
}
[[nodiscard]] constexpr auto const* c_str() const
[[nodiscard]] constexpr auto const* c_str() const noexcept
{
return data();
}
[[nodiscard]] constexpr auto sv() const
[[nodiscard]] constexpr auto sv() const noexcept
{
return std::basic_string_view<T>{ data(), size() };
return std::basic_string_view<Char>{ data(), size() };
}
///
[[nodiscard]] constexpr bool ends_with(T const& x) const
[[nodiscard]] constexpr bool ends_with(Char const& x) const noexcept
{
auto const n = size();
return n != 0 && data()[n - 1] == x;
}
template<typename ContiguousRange>
[[nodiscard]] constexpr bool ends_with(ContiguousRange const& x) const
[[nodiscard]] constexpr bool ends_with(ContiguousRange const& x) const noexcept
{
auto const x_len = std::size(x);
auto const len = size();
return len >= x_len && this->sv().substr(len - x_len) == x;
}
[[nodiscard]] constexpr bool ends_with(T const* x) const
[[nodiscard]] constexpr bool ends_with(Char const* x) const noexcept
{
return x != nullptr && ends_with(std::basic_string_view<T>(x));
return x != nullptr && ends_with(std::basic_string_view<Char>(x));
}
///
[[nodiscard]] constexpr bool starts_with(T const& x) const
[[nodiscard]] constexpr bool starts_with(Char const& x) const noexcept
{
return !empty() && *data() == x;
}
template<typename ContiguousRange>
[[nodiscard]] constexpr bool starts_with(ContiguousRange const& x) const
[[nodiscard]] constexpr bool starts_with(ContiguousRange const& x) const noexcept
{
auto const x_len = std::size(x);
return size() >= x_len && this->sv().substr(0, x_len) == x;
}
[[nodiscard]] constexpr bool starts_with(T const* x) const
[[nodiscard]] constexpr bool starts_with(Char const* x) const noexcept
{
return x != nullptr && starts_with(std::basic_string_view<T>(x));
return x != nullptr && starts_with(std::basic_string_view<Char>(x));
}
///
@@ -157,7 +163,7 @@ public:
///
void append(T const& value)
void append(Char const& value)
{
buffer_.push_back(value);
ensure_sz();
@@ -170,11 +176,11 @@ public:
ensure_sz();
}
void append(T const* sz_value)
void append(Char const* sz_value)
{
if (sz_value != nullptr)
{
append(std::basic_string_view<T>{ sz_value });
append(std::basic_string_view<Char>{ sz_value });
}
}
@@ -207,10 +213,15 @@ public:
return *this;
}
void push_back(Char const& value)
{
append(value);
}
///
template<typename... Args>
void join(T delim, Args const&... args)
void join(Char delim, Args const&... args)
{
((append(args), append(delim)), ...);
resize(size() - 1);
@@ -224,9 +235,19 @@ public:
}
template<typename... Args>
void join(T const* sz_delim, Args const&... args)
void join(Char const* sz_delim, Args const&... args)
{
join(std::basic_string_view<T>{ sz_delim }, args...);
join(std::basic_string_view<Char>{ sz_delim }, args...);
}
[[nodiscard]] constexpr operator std::basic_string_view<Char>() const noexcept
{
return sv();
}
[[nodiscard]] constexpr operator auto() const noexcept
{
return c_str();
}
private:
@@ -245,6 +266,16 @@ private:
}
};
template<typename Char, size_t N>
struct fmt::formatter<tr_strbuf<Char, N>> : formatter<std::basic_string_view<Char>, Char>
{
template<typename FormatContext>
constexpr auto format(tr_strbuf<Char, N> const& strbuf, FormatContext& ctx) const
{
return formatter<std::basic_string_view<Char>, Char>::format(strbuf.sv(), ctx);
}
};
/**
* Good for building short-term URLs.
* The initial size is big enough to avoid heap allocs in most cases,

View File

@@ -238,7 +238,7 @@ uint8_t* tr_loadFile(std::string_view path_in, size_t* size, tr_error** error)
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path.sv()),
fmt::arg("path", path),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -247,7 +247,7 @@ uint8_t* tr_loadFile(std::string_view path_in, size_t* size, tr_error** error)
if (info.type != TR_SYS_PATH_IS_FILE)
{
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path.sv())));
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path)));
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
return nullptr;
}
@@ -264,7 +264,7 @@ uint8_t* tr_loadFile(std::string_view path_in, size_t* size, tr_error** error)
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path.sv()),
fmt::arg("path", path),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -276,7 +276,7 @@ uint8_t* tr_loadFile(std::string_view path_in, size_t* size, tr_error** error)
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path.sv()),
fmt::arg("path", path),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_sys_file_close(fd);
@@ -302,7 +302,7 @@ bool tr_loadFile(std::string_view path_in, std::vector<char>& setme, tr_error**
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path.sv()),
fmt::arg("path", path),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -311,7 +311,7 @@ bool tr_loadFile(std::string_view path_in, std::vector<char>& setme, tr_error**
if (info.type != TR_SYS_PATH_IS_FILE)
{
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path.sv())));
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path)));
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
return false;
}
@@ -322,7 +322,7 @@ bool tr_loadFile(std::string_view path_in, std::vector<char>& setme, tr_error**
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path.sv()),
fmt::arg("path", path),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -334,7 +334,7 @@ bool tr_loadFile(std::string_view path_in, std::vector<char>& setme, tr_error**
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path.sv()),
fmt::arg("path", path),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_sys_file_close(fd);
@@ -391,7 +391,7 @@ bool tr_saveFile(std::string_view filename_in, std::string_view contents, tr_err
return false;
}
tr_logAddTrace(fmt::format("Saved '{}'", filename.sv()));
tr_logAddTrace(fmt::format("Saved '{}'", filename));
return true;
}

View File

@@ -43,17 +43,17 @@ TEST_F(PeerMgrActiveRequestsTest, requestsMadeAreCounted)
auto const block = tr_block_index_t{ 100 };
auto const peer = static_cast<tr_peer*>(nullptr);
auto const when = time_t(0);
auto const when = time_t{};
EXPECT_EQ(0, requests.count(block));
EXPECT_EQ(0, requests.count(peer));
EXPECT_EQ(0, requests.size());
EXPECT_EQ(0U, requests.count(block));
EXPECT_EQ(0U, requests.count(peer));
EXPECT_EQ(0U, requests.size());
EXPECT_TRUE(requests.add(block, peer, when));
EXPECT_EQ(1, requests.count(block));
EXPECT_EQ(1, requests.count(peer));
EXPECT_EQ(1, requests.size());
EXPECT_EQ(1U, requests.count(block));
EXPECT_EQ(1U, requests.count(peer));
EXPECT_EQ(1U, requests.size());
}
TEST_F(PeerMgrActiveRequestsTest, requestsAreRemoved)

View File

@@ -27,7 +27,7 @@ protected:
TEST_F(QuarkTest, allPredefinedKeysCanBeLookedUp)
{
for (int i = 0; i < TR_N_KEYS; i++)
for (size_t i = 0; i < TR_N_KEYS; ++i)
{
auto const str = quarkGetString(i);
auto const q = tr_quark_lookup(str);

View File

@@ -102,14 +102,14 @@ TEST_F(StrbufTest, indexOperator)
// mutable
{
auto buf = tr_pathbuf{ Value1 };
buf[0] = 'W';
buf.at(0) = 'W';
EXPECT_EQ(Value2, buf.sv());
}
// const
{
auto const buf = tr_pathbuf{ Value1 };
EXPECT_EQ(Value1.front(), buf[0]);
EXPECT_EQ(Value1.front(), buf.at(0));
}
}